Skip to content

Commit fa953cd

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

File tree

2 files changed

+131
-14
lines changed

2 files changed

+131
-14
lines changed

pkg/store/instance.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ func AddGlobalFields(inst *Instance) (FormatData, error) {
233233
return data, nil
234234
}
235235

236+
func HomeDir() (string, error) {
237+
u, err := user.Current()
238+
if err != nil {
239+
return "", err
240+
}
241+
return u.HomeDir, nil
242+
}
243+
236244
type PrintOptions struct {
237245
AllFields bool
238246
IsTerminal bool
@@ -263,29 +271,39 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
263271
archs[instance.Arch]++
264272
}
265273
all := options != nil && options.AllFields
266-
hideType := len(types) == 1 && !all
267-
hideArch := len(archs) == 1 && !all
274+
width := 0
275+
if options != nil && options.IsTerminal {
276+
width = options.TerminalWidth
277+
}
278+
hideType := false
279+
hideArch := false
280+
hideDir := false
268281

269282
columns := 1 // NAME
270283
columns += 2 // STATUS
271284
columns += 2 // SSH
285+
if width != 0 && (columns+7)*8 > width && !all {
286+
hideType = len(types) == 1
287+
}
272288
if !hideType {
273289
columns++ // VMTYPE
274290
}
291+
if width != 0 && (columns+6)*8 > width && !all {
292+
hideArch = len(archs) == 1
293+
}
275294
if !hideArch {
276295
columns++ // ARCH
277296
}
278-
columns++ // CPUS
279-
columns++ // MEMORY
280-
columns++ // DISK
281-
columns += 2 // DIR
282-
hideDir := false
283-
if options != nil && options.IsTerminal && !all {
284-
width := options.TerminalWidth
285-
if width != 0 && columns*8 > width {
286-
hideDir = true
287-
}
297+
columns++ // CPUS
298+
columns++ // MEMORY
299+
columns++ // DISK
300+
if width != 0 && (columns+2)*8 > width && !all {
301+
hideDir = true
288302
}
303+
if !hideDir {
304+
columns += 2 // DIR
305+
}
306+
_ = columns
289307

290308
w := tabwriter.NewWriter(w, 4, 8, 4, ' ', 0)
291309
fmt.Fprint(w, "NAME\tSTATUS\tSSH")
@@ -301,11 +319,10 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
301319
}
302320
fmt.Fprintln(w)
303321

304-
u, err := user.Current()
322+
homeDir, err := HomeDir()
305323
if err != nil {
306324
return err
307325
}
308-
homeDir := u.HomeDir
309326

310327
for _, instance := range instances {
311328
dir := instance.Dir

pkg/store/instance_test.go

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

0 commit comments

Comments
 (0)