Skip to content

Commit

Permalink
Hide the directory column on narrow terminal
Browse files Browse the repository at this point in the history
If the terminal width does not fit all the list
columns, then hide "dir" column - unless requested.

Without terminal (or unknown), hide the identical.
If the terminal is wide enough, then don't hide.

Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Jan 18, 2023
1 parent bc6508a commit 07456b9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 8 deletions.
10 changes: 10 additions & 0 deletions cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"errors"
"fmt"
"os"
"reflect"
"sort"
"strings"

"github.com/cheggaaa/pb/v3/termutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -163,6 +166,13 @@ func listAction(cmd *cobra.Command, args []string) error {

options := store.PrintOptions{AllFields: allFields}
out := cmd.OutOrStdout()
if out == os.Stdout {
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
if w, err := termutil.TerminalWidth(); err == nil {
options.TerminalWidth = w
}
}
}
return store.PrintInstances(out, instances, format, &options)
}

Expand Down
38 changes: 35 additions & 3 deletions pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ func AddGlobalFields(inst *Instance) (FormatData, error) {
}

type PrintOptions struct {
AllFields bool
AllFields bool
TerminalWidth int
}

// PrintInstances prints instances in a requested format to a given io.Writer.
Expand All @@ -252,14 +253,45 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
archs[instance.Arch]++
}
all := options != nil && options.AllFields
width := 0
if options != nil {
width = options.TerminalWidth
}
columnWidth := 8
hideType := false
hideArch := false
hideDir := false

hideType = len(types) == 1 && !all
columns := 1 // NAME
columns += 2 // STATUS
columns += 2 // SSH
// can we still fit the remaining columns (7)
if width == 0 || (columns+7)*columnWidth > width && !all {
hideType = len(types) == 1
}
if !hideType {
columns++ // VMTYPE
}
// only hide arch if it is the same as the host arch
goarch := limayaml.NewArch(runtime.GOARCH)
hideArch = len(archs) == 1 && instances[0].Arch == goarch && !all
// can we still fit the remaining columns (6)
if width == 0 || (columns+6)*columnWidth > width && !all {
hideArch = len(archs) == 1 && instances[0].Arch == goarch
}
if !hideArch {
columns++ // ARCH
}
columns++ // CPUS
columns++ // MEMORY
columns++ // DISK
// can we still fit the remaining columns (2)
if width != 0 && (columns+2)*columnWidth > width && !all {
hideDir = true
}
if !hideDir {
columns += 2 // DIR
}
_ = columns

w := tabwriter.NewWriter(w, 4, 8, 4, ' ', 0)
fmt.Fprint(w, "NAME\tSTATUS\tSSH")
Expand Down
61 changes: 56 additions & 5 deletions pkg/store/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,26 @@ var tableHome = "NAME STATUS SSH CPUS MEMORY DISK DIR
var tableAll = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
"foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"

var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B dir\n" +
"bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B dir\n"
// for width 60, everything is hidden
var table60 = "NAME STATUS SSH CPUS MEMORY DISK\n" +
"foo Stopped 127.0.0.1:0 0 0B 0B\n"

// for width 80, identical is hidden (type/arch)
var table80i = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
"foo Stopped 127.0.0.1:0 0 0B 0B dir\n"

// for width 80, different arch is still shown (not dir)
var table80d = "NAME STATUS SSH ARCH CPUS MEMORY DISK\n" +
"foo Stopped 127.0.0.1:0 unknown 0 0B 0B\n"

// for width 100, nothing is hidden
var table100 = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
"foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"

// for width 80, directory is hidden (if not identical)
var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" +
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" +
"bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n"

func TestPrintInstanceTable(t *testing.T) {
var buf bytes.Buffer
Expand Down Expand Up @@ -67,10 +84,44 @@ func TestPrintInstanceTableHome(t *testing.T) {
assert.Equal(t, tableHome, buf.String())
}

func TestPrintInstanceTable60(t *testing.T) {
var buf bytes.Buffer
instances := []*Instance{&instance}
options := PrintOptions{TerminalWidth: 60}
PrintInstances(&buf, instances, "table", &options)
assert.Equal(t, table60, buf.String())
}

func TestPrintInstanceTable80SameArch(t *testing.T) {
var buf bytes.Buffer
instances := []*Instance{&instance}
options := PrintOptions{TerminalWidth: 80}
PrintInstances(&buf, instances, "table", &options)
assert.Equal(t, table80i, buf.String())
}

func TestPrintInstanceTable80DiffArch(t *testing.T) {
var buf bytes.Buffer
instance1 := instance
instance1.Arch = limayaml.NewArch("unknown")
instances := []*Instance{&instance1}
options := PrintOptions{TerminalWidth: 80}
PrintInstances(&buf, instances, "table", &options)
assert.Equal(t, table80d, buf.String())
}

func TestPrintInstanceTable100(t *testing.T) {
var buf bytes.Buffer
instances := []*Instance{&instance}
options := PrintOptions{TerminalWidth: 100}
PrintInstances(&buf, instances, "table", &options)
assert.Equal(t, table100, buf.String())
}

func TestPrintInstanceTableAll(t *testing.T) {
var buf bytes.Buffer
instances := []*Instance{&instance}
options := PrintOptions{AllFields: true}
options := PrintOptions{TerminalWidth: 40, AllFields: true}
PrintInstances(&buf, instances, "table", &options)
assert.Equal(t, tableAll, buf.String())
}
Expand All @@ -86,7 +137,7 @@ func TestPrintInstanceTableTwo(t *testing.T) {
instance2.VMType = limayaml.VZ
instance2.Arch = limayaml.AARCH64
instances := []*Instance{&instance1, &instance2}
options := PrintOptions{}
options := PrintOptions{TerminalWidth: 80}
PrintInstances(&buf, instances, "table", &options)
assert.Equal(t, tableTwo, buf.String())
}

0 comments on commit 07456b9

Please sign in to comment.