Skip to content

Commit facac65

Browse files
committed
Hide identical columns and add unit tests
Signed-off-by: Anders F Björklund <[email protected]>
1 parent a38aae7 commit facac65

File tree

3 files changed

+125
-14
lines changed

3 files changed

+125
-14
lines changed

cmd/limactl/list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ func listAction(cmd *cobra.Command, args []string) error {
168168
out := cmd.OutOrStdout()
169169
if out == os.Stdout {
170170
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
171-
options.IsTerminal = true
172171
if w, err := termutil.TerminalWidth(); err == nil {
173172
options.TerminalWidth = w
174173
}

pkg/store/instance.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ func AddGlobalFields(inst *Instance) (FormatData, error) {
234234

235235
type PrintOptions struct {
236236
AllFields bool
237-
IsTerminal bool
238237
TerminalWidth int
239238
}
240239

@@ -254,29 +253,39 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
254253
archs[instance.Arch]++
255254
}
256255
all := options != nil && options.AllFields
257-
hideType := len(types) == 1 && !all
258-
hideArch := len(archs) == 1 && !all
256+
width := 0
257+
if options != nil {
258+
width = options.TerminalWidth
259+
}
260+
hideType := false
261+
hideArch := false
262+
hideDir := false
259263

260264
columns := 1 // NAME
261265
columns += 2 // STATUS
262266
columns += 2 // SSH
267+
if width != 0 && (columns+7)*8 > width && !all {
268+
hideType = len(types) == 1
269+
}
263270
if !hideType {
264271
columns++ // VMTYPE
265272
}
273+
if width != 0 && (columns+6)*8 > width && !all {
274+
hideArch = len(archs) == 1
275+
}
266276
if !hideArch {
267277
columns++ // ARCH
268278
}
269-
columns++ // CPUS
270-
columns++ // MEMORY
271-
columns++ // DISK
272-
columns += 2 // DIR
273-
hideDir := false
274-
if options != nil && options.IsTerminal && !all {
275-
width := options.TerminalWidth
276-
if width != 0 && columns*8 > width {
277-
hideDir = true
278-
}
279+
columns++ // CPUS
280+
columns++ // MEMORY
281+
columns++ // DISK
282+
if width != 0 && (columns+2)*8 > width && !all {
283+
hideDir = true
284+
}
285+
if !hideDir {
286+
columns += 2 // DIR
279287
}
288+
_ = columns
280289

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

pkg/store/instance_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package store
2+
3+
import (
4+
"bytes"
5+
"os/user"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/lima-vm/lima/pkg/limayaml"
10+
"gotest.tools/v3/assert"
11+
)
12+
13+
var instance = Instance{
14+
Name: "foo",
15+
Status: StatusStopped,
16+
VMType: limayaml.QEMU,
17+
Arch: limayaml.X8664,
18+
Dir: "dir",
19+
}
20+
21+
var table = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
22+
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B dir\n"
23+
24+
// for width 60, everything is hidden
25+
var table60 = "NAME STATUS SSH CPUS MEMORY DISK\n" +
26+
"foo Stopped 127.0.0.1:0 0 0B 0B\n"
27+
28+
// for width 80, identical is hidden (type/arch)
29+
var table80 = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
30+
"foo Stopped 127.0.0.1:0 0 0B 0B dir\n"
31+
32+
// for width 80, directory is hidden (if not identical)
33+
var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" +
34+
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" +
35+
"bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n"
36+
37+
const separator = string(filepath.Separator)
38+
39+
var tableHome = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
40+
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B ~" + separator + "dir\n"
41+
42+
func TestPrintInstanceTable(t *testing.T) {
43+
var buf bytes.Buffer
44+
instances := []*Instance{&instance}
45+
PrintInstances(&buf, instances, "table", nil)
46+
assert.Equal(t, buf.String(), table)
47+
}
48+
49+
func TestPrintInstanceTable60(t *testing.T) {
50+
var buf bytes.Buffer
51+
instances := []*Instance{&instance}
52+
options := PrintOptions{TerminalWidth: 60}
53+
PrintInstances(&buf, instances, "table", &options)
54+
assert.Equal(t, buf.String(), table60)
55+
}
56+
57+
func TestPrintInstanceTable80(t *testing.T) {
58+
var buf bytes.Buffer
59+
instances := []*Instance{&instance}
60+
options := PrintOptions{TerminalWidth: 80}
61+
PrintInstances(&buf, instances, "table", &options)
62+
assert.Equal(t, buf.String(), table80)
63+
}
64+
65+
func TestPrintInstanceTable100(t *testing.T) {
66+
var buf bytes.Buffer
67+
instances := []*Instance{&instance}
68+
options := PrintOptions{TerminalWidth: 100}
69+
PrintInstances(&buf, instances, "table", &options)
70+
assert.Equal(t, buf.String(), table)
71+
}
72+
73+
func TestPrintInstanceTableAll(t *testing.T) {
74+
var buf bytes.Buffer
75+
instances := []*Instance{&instance}
76+
options := PrintOptions{TerminalWidth: 40, AllFields: true}
77+
PrintInstances(&buf, instances, "table", &options)
78+
assert.Equal(t, buf.String(), table)
79+
}
80+
81+
func TestPrintInstanceTableTwo(t *testing.T) {
82+
var buf bytes.Buffer
83+
instance1 := instance
84+
instance2 := instance
85+
instance2.Name = "bar"
86+
instance2.VMType = limayaml.VZ
87+
instance2.Arch = limayaml.AARCH64
88+
instances := []*Instance{&instance1, &instance2}
89+
options := PrintOptions{TerminalWidth: 80}
90+
PrintInstances(&buf, instances, "table", &options)
91+
assert.Equal(t, buf.String(), tableTwo)
92+
}
93+
94+
func TestPrintInstanceTableHome(t *testing.T) {
95+
var buf bytes.Buffer
96+
u, err := user.Current()
97+
assert.NilError(t, err)
98+
instance1 := instance
99+
instance1.Dir = filepath.Join(u.HomeDir, "dir")
100+
instances := []*Instance{&instance1}
101+
PrintInstances(&buf, instances, "table", nil)
102+
assert.Equal(t, buf.String(), tableHome)
103+
}

0 commit comments

Comments
 (0)