Skip to content

Commit 26010e4

Browse files
committed
image/tree: Print longest names first and use full width
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>
1 parent f4a68da commit 26010e4

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

cli/command/image/tree.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,23 @@ func printNames(out *streams.Out, headers []imgColumn, img topImage, color, unta
281281
_, _ = fmt.Fprint(out, headers[0].Print(untaggedColor, "<untagged>"))
282282
}
283283

284-
for nameIdx, name := range img.Names {
285-
if nameIdx != 0 {
286-
_, _ = fmt.Fprintln(out, "")
284+
// TODO: Replace with namesLongestToShortest := slices.SortedFunc(slices.Values(img.Names))
285+
// once we move to Go 1.23.
286+
namesLongestToShortest := make([]string, len(img.Names))
287+
copy(namesLongestToShortest, img.Names)
288+
sort.Slice(namesLongestToShortest, func(i, j int) bool {
289+
return len(namesLongestToShortest[i]) > len(namesLongestToShortest[j])
290+
})
291+
292+
for nameIdx, name := range namesLongestToShortest {
293+
// Don't limit first names to the column width because only the last
294+
// name will be printed alongside other columns.
295+
if nameIdx < len(img.Names)-1 {
296+
_, fullWidth := out.GetTtySize()
297+
_, _ = fmt.Fprintln(out, color.Apply(truncateRunes(name, int(fullWidth))))
298+
} else {
299+
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
287300
}
288-
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
289301
}
290302
}
291303

0 commit comments

Comments
 (0)