Skip to content

Commit

Permalink
Hide the directory column on narrow terminal
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
afbjorklund committed Jan 5, 2023
1 parent ca201a7 commit 4b74383
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 14 additions & 1 deletion cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"errors"
"fmt"
"os"
"reflect"
"sort"
"strings"

"github.com/cheggaaa/pb/v3/termutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -161,7 +164,17 @@ func listAction(cmd *cobra.Command, args []string) error {
return err
}

return store.PrintInstances(cmd.OutOrStdout(), instances, format, &store.PrintOptions{AllFields: allFields})
options := store.PrintOptions{AllFields: allFields}
out := cmd.OutOrStdout()
if out == os.Stdout {
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
options.IsTerminal = true
if w, err := termutil.TerminalWidth(); err == nil {
options.TerminalWidth = w
}
}
}
return store.PrintInstances(out, instances, format, &options)
}

func listBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
24 changes: 23 additions & 1 deletion pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ func AddGlobalFields(inst *Instance) (FormatData, error) {

type PrintOptions struct {
AllFields bool
IsTerminal bool
TerminalWidth int
}

// PrintInstances prints instances in a requested format to a given io.Writer.
Expand Down Expand Up @@ -263,7 +265,27 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
all := options != nil && options.AllFields
hideType := len(types) == 1 && !all
hideArch := len(archs) == 1 && !all
hideDir := false /* TODO */ && !all

columns := 1 // NAME
columns += 2 // STATUS
columns += 2 // SSH
if !hideType {
columns++ // VMTYPE
}
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
}
}

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

0 comments on commit 4b74383

Please sign in to comment.