Skip to content

Commit 07456b9

Browse files
committed
Hide the directory column on narrow terminal
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]>
1 parent bc6508a commit 07456b9

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

cmd/limactl/list.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"reflect"
78
"sort"
89
"strings"
910

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

164167
options := store.PrintOptions{AllFields: allFields}
165168
out := cmd.OutOrStdout()
169+
if out == os.Stdout {
170+
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
171+
if w, err := termutil.TerminalWidth(); err == nil {
172+
options.TerminalWidth = w
173+
}
174+
}
175+
}
166176
return store.PrintInstances(out, instances, format, &options)
167177
}
168178

pkg/store/instance.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ func AddGlobalFields(inst *Instance) (FormatData, error) {
233233
}
234234

235235
type PrintOptions struct {
236-
AllFields bool
236+
AllFields bool
237+
TerminalWidth int
237238
}
238239

239240
// PrintInstances prints instances in a requested format to a given io.Writer.
@@ -252,14 +253,45 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
252253
archs[instance.Arch]++
253254
}
254255
all := options != nil && options.AllFields
256+
width := 0
257+
if options != nil {
258+
width = options.TerminalWidth
259+
}
260+
columnWidth := 8
255261
hideType := false
256262
hideArch := false
257263
hideDir := false
258264

259-
hideType = len(types) == 1 && !all
265+
columns := 1 // NAME
266+
columns += 2 // STATUS
267+
columns += 2 // SSH
268+
// can we still fit the remaining columns (7)
269+
if width == 0 || (columns+7)*columnWidth > width && !all {
270+
hideType = len(types) == 1
271+
}
272+
if !hideType {
273+
columns++ // VMTYPE
274+
}
260275
// only hide arch if it is the same as the host arch
261276
goarch := limayaml.NewArch(runtime.GOARCH)
262-
hideArch = len(archs) == 1 && instances[0].Arch == goarch && !all
277+
// can we still fit the remaining columns (6)
278+
if width == 0 || (columns+6)*columnWidth > width && !all {
279+
hideArch = len(archs) == 1 && instances[0].Arch == goarch
280+
}
281+
if !hideArch {
282+
columns++ // ARCH
283+
}
284+
columns++ // CPUS
285+
columns++ // MEMORY
286+
columns++ // DISK
287+
// can we still fit the remaining columns (2)
288+
if width != 0 && (columns+2)*columnWidth > width && !all {
289+
hideDir = true
290+
}
291+
if !hideDir {
292+
columns += 2 // DIR
293+
}
294+
_ = columns
263295

264296
w := tabwriter.NewWriter(w, 4, 8, 4, ' ', 0)
265297
fmt.Fprint(w, "NAME\tSTATUS\tSSH")

pkg/store/instance_test.go

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,26 @@ var tableHome = "NAME STATUS SSH CPUS MEMORY DISK DIR
3636
var tableAll = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
3737
"foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"
3838

39-
var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
40-
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B dir\n" +
41-
"bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B dir\n"
39+
// for width 60, everything is hidden
40+
var table60 = "NAME STATUS SSH CPUS MEMORY DISK\n" +
41+
"foo Stopped 127.0.0.1:0 0 0B 0B\n"
42+
43+
// for width 80, identical is hidden (type/arch)
44+
var table80i = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
45+
"foo Stopped 127.0.0.1:0 0 0B 0B dir\n"
46+
47+
// for width 80, different arch is still shown (not dir)
48+
var table80d = "NAME STATUS SSH ARCH CPUS MEMORY DISK\n" +
49+
"foo Stopped 127.0.0.1:0 unknown 0 0B 0B\n"
50+
51+
// for width 100, nothing is hidden
52+
var table100 = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
53+
"foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"
54+
55+
// for width 80, directory is hidden (if not identical)
56+
var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" +
57+
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" +
58+
"bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n"
4259

4360
func TestPrintInstanceTable(t *testing.T) {
4461
var buf bytes.Buffer
@@ -67,10 +84,44 @@ func TestPrintInstanceTableHome(t *testing.T) {
6784
assert.Equal(t, tableHome, buf.String())
6885
}
6986

87+
func TestPrintInstanceTable60(t *testing.T) {
88+
var buf bytes.Buffer
89+
instances := []*Instance{&instance}
90+
options := PrintOptions{TerminalWidth: 60}
91+
PrintInstances(&buf, instances, "table", &options)
92+
assert.Equal(t, table60, buf.String())
93+
}
94+
95+
func TestPrintInstanceTable80SameArch(t *testing.T) {
96+
var buf bytes.Buffer
97+
instances := []*Instance{&instance}
98+
options := PrintOptions{TerminalWidth: 80}
99+
PrintInstances(&buf, instances, "table", &options)
100+
assert.Equal(t, table80i, buf.String())
101+
}
102+
103+
func TestPrintInstanceTable80DiffArch(t *testing.T) {
104+
var buf bytes.Buffer
105+
instance1 := instance
106+
instance1.Arch = limayaml.NewArch("unknown")
107+
instances := []*Instance{&instance1}
108+
options := PrintOptions{TerminalWidth: 80}
109+
PrintInstances(&buf, instances, "table", &options)
110+
assert.Equal(t, table80d, buf.String())
111+
}
112+
113+
func TestPrintInstanceTable100(t *testing.T) {
114+
var buf bytes.Buffer
115+
instances := []*Instance{&instance}
116+
options := PrintOptions{TerminalWidth: 100}
117+
PrintInstances(&buf, instances, "table", &options)
118+
assert.Equal(t, table100, buf.String())
119+
}
120+
70121
func TestPrintInstanceTableAll(t *testing.T) {
71122
var buf bytes.Buffer
72123
instances := []*Instance{&instance}
73-
options := PrintOptions{AllFields: true}
124+
options := PrintOptions{TerminalWidth: 40, AllFields: true}
74125
PrintInstances(&buf, instances, "table", &options)
75126
assert.Equal(t, tableAll, buf.String())
76127
}
@@ -86,7 +137,7 @@ func TestPrintInstanceTableTwo(t *testing.T) {
86137
instance2.VMType = limayaml.VZ
87138
instance2.Arch = limayaml.AARCH64
88139
instances := []*Instance{&instance1, &instance2}
89-
options := PrintOptions{}
140+
options := PrintOptions{TerminalWidth: 80}
90141
PrintInstances(&buf, instances, "table", &options)
91142
assert.Equal(t, tableTwo, buf.String())
92143
}

0 commit comments

Comments
 (0)