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 Dec 27, 2022
1 parent 192caff commit 8c2fd9d
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/user"
"reflect"
"sort"
"strings"
"text/tabwriter"
"text/template"

"github.com/cheggaaa/pb/v3/termutil"
"github.com/docker/go-units"
"github.com/lima-vm/lima/pkg/store"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -63,6 +67,18 @@ func instanceMatches(arg string, instances []string) []string {
return matches
}

func terminalWidth(out io.Writer) int {
width := 120
if out == os.Stdout {
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
if w, err := termutil.TerminalWidth(); err == nil {
return w
}
}
}
return width
}

func listAction(cmd *cobra.Command, args []string) error {
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
Expand Down Expand Up @@ -182,21 +198,27 @@ func listAction(cmd *cobra.Command, args []string) error {
return err
}

w := tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0)
out := cmd.OutOrStdout()
w := tabwriter.NewWriter(out, 4, 8, 4, ' ', 0)

fmt.Fprint(w, "NAME\tSTATUS\tSSH")
cols := 5 // two for status, ssh
hideType := len(types) == 1 && !show
if !hideType {
fmt.Fprint(w, "\tVMTYPE")
cols++
}
hideArch := len(archs) == 1 && !show
if !hideArch {
fmt.Fprint(w, "\tARCH")
cols++
}
fmt.Fprint(w, "\tCPUS\tMEMORY\tDISK")
hideDir := false && !show
cols += 3
hideDir := (cols+2)*8 > terminalWidth(out) && !show
if !hideDir {
fmt.Fprint(w, "\tDIR")
cols += 2 // two for dir
}
fmt.Fprintln(w)

Expand Down

0 comments on commit 8c2fd9d

Please sign in to comment.