Skip to content

Commit 0839624

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. Signed-off-by: Anders F Björklund <[email protected]>
1 parent ca201a7 commit 0839624

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

cmd/limactl/list.go

Lines changed: 14 additions & 1 deletion
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
)
@@ -161,7 +164,17 @@ func listAction(cmd *cobra.Command, args []string) error {
161164
return err
162165
}
163166

164-
return store.PrintInstances(cmd.OutOrStdout(), instances, format, &store.PrintOptions{AllFields: allFields})
167+
options := store.PrintOptions{AllFields: allFields}
168+
out := cmd.OutOrStdout()
169+
if out == os.Stdout {
170+
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
171+
options.IsTerminal = true
172+
if w, err := termutil.TerminalWidth(); err == nil {
173+
options.TerminalWidth = w
174+
}
175+
}
176+
}
177+
return store.PrintInstances(out, instances, format, &options)
165178
}
166179

167180
func listBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

pkg/store/instance.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ func AddGlobalFields(inst *Instance) (FormatData, error) {
234234
}
235235

236236
type PrintOptions struct {
237-
AllFields bool
237+
AllFields bool
238+
IsTerminal bool
239+
TerminalWidth int
238240
}
239241

240242
// PrintInstances prints instances in a requested format to a given io.Writer.
@@ -263,7 +265,27 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
263265
all := options != nil && options.AllFields
264266
hideType := len(types) == 1 && !all
265267
hideArch := len(archs) == 1 && !all
266-
hideDir := false /* TODO */ && !all
268+
269+
columns := 1 // NAME
270+
columns += 2 // STATUS
271+
columns += 2 // SSH
272+
if !hideType {
273+
columns++ // VMTYPE
274+
}
275+
if !hideArch {
276+
columns++ // ARCH
277+
}
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+
}
288+
}
267289

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

0 commit comments

Comments
 (0)