Skip to content

Commit

Permalink
image/tree: Print longest names first and use full width
Browse files Browse the repository at this point in the history
When printing image names, sort them by length and print the longest
first. This also allows them to use a full terminal width because they
are not printed alongside other columns.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
  • Loading branch information
vvoland committed Jan 17, 2025
1 parent f4a68da commit 708b732
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package image
import (
"context"
"fmt"
"slices"
"sort"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -281,11 +282,21 @@ func printNames(out *streams.Out, headers []imgColumn, img topImage, color, unta
_, _ = fmt.Fprint(out, headers[0].Print(untaggedColor, "<untagged>"))
}

for nameIdx, name := range img.Names {
if nameIdx != 0 {
_, _ = fmt.Fprintln(out, "")
namesLongestToShortest := slices.SortedFunc(slices.Values(img.Names),
func(a, b string) int {
return len(b) - len(a)
},
)

for nameIdx, name := range namesLongestToShortest {
// Don't limit first names to the column width because only the last
// name will be printed alongside other columns.
if nameIdx < len(img.Names)-1 {
_, fullWidth := out.GetTtySize()
_, _ = fmt.Fprintln(out, color.Apply(truncateRunes(name, int(fullWidth))))
} else {
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
}
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
}
}

Expand Down

0 comments on commit 708b732

Please sign in to comment.