Skip to content

Commit

Permalink
Hide identical columns and add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Jan 8, 2023
1 parent 0839624 commit d9aab45
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 14 deletions.
45 changes: 31 additions & 14 deletions pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ func AddGlobalFields(inst *Instance) (FormatData, error) {
return data, nil
}

func HomeDir() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
return u.HomeDir, nil
}

type PrintOptions struct {
AllFields bool
IsTerminal bool
Expand Down Expand Up @@ -263,29 +271,39 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
archs[instance.Arch]++
}
all := options != nil && options.AllFields
hideType := len(types) == 1 && !all
hideArch := len(archs) == 1 && !all
width := 0
if options != nil && options.IsTerminal {
width = options.TerminalWidth
}
hideType := false
hideArch := false
hideDir := false

columns := 1 // NAME
columns += 2 // STATUS
columns += 2 // SSH
if width != 0 && (columns+7)*8 > width && !all {
hideType = len(types) == 1
}
if !hideType {
columns++ // VMTYPE
}
if width != 0 && (columns+6)*8 > width && !all {
hideArch = len(archs) == 1
}
if !hideArch {
columns++ // ARCH
}
columns++ // CPUS
columns++ // MEMORY
columns++ // DISK
columns += 2 // DIR
hideDir := false
if options != nil && options.IsTerminal && !all {
width := options.TerminalWidth
if width != 0 && columns*8 > width {
hideDir = true
}
columns++ // CPUS
columns++ // MEMORY
columns++ // DISK
if width != 0 && (columns+2)*8 > 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 All @@ -301,11 +319,10 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
}
fmt.Fprintln(w)

u, err := user.Current()
homeDir, err := HomeDir()
if err != nil {
return err
}
homeDir := u.HomeDir

for _, instance := range instances {
dir := instance.Dir
Expand Down
102 changes: 102 additions & 0 deletions pkg/store/instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package store

import (
"bytes"
"path/filepath"
"testing"

"github.com/lima-vm/lima/pkg/limayaml"
"gotest.tools/v3/assert"
)

var instance Instance = Instance{
Name: "foo",
Status: StatusStopped,
VMType: limayaml.QEMU,
Arch: limayaml.X8664,
Dir: "dir",
}

var table string = "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"

// for width 60, everything is hidden
var table60 string = "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 table80 string = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
"foo Stopped 127.0.0.1:0 0 0B 0B dir\n"

// for width 80, directory is hidden (if not identical)
var tableTwo string = "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"

const separator = string(filepath.Separator)

var tableHome string = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B ~" + separator + "dir\n"

func TestPrintInstanceTable(t *testing.T) {
var buf bytes.Buffer
instances := []*Instance{&instance}
PrintInstances(&buf, instances, "table", nil)
assert.Equal(t, buf.String(), table)
}

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

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

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

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

func TestPrintInstanceTableTwo(t *testing.T) {
var buf bytes.Buffer
instance1 := instance
instance2 := instance
instance2.Name = "bar"
instance2.VMType = limayaml.VZ
instance2.Arch = limayaml.AARCH64
instances := []*Instance{&instance1, &instance2}
options := PrintOptions{IsTerminal: true, TerminalWidth: 80}
PrintInstances(&buf, instances, "table", &options)
assert.Equal(t, buf.String(), tableTwo)
}

func TestPrintInstanceTableHome(t *testing.T) {
var buf bytes.Buffer
homeDir, err := HomeDir()
assert.NilError(t, err)
instance1 := instance
instance1.Dir = filepath.Join(homeDir, "dir")
instances := []*Instance{&instance1}
PrintInstances(&buf, instances, "table", nil)
assert.Equal(t, buf.String(), tableHome)
}

0 comments on commit d9aab45

Please sign in to comment.