Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image/tree: Print longest names first and use full width #5757

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,23 @@ 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, "")
// TODO: Replace with namesLongestToShortest := slices.SortedFunc(slices.Values(img.Names))
// once we move to Go 1.23.
namesLongestToShortest := make([]string, len(img.Names))
copy(namesLongestToShortest, img.Names)
Benehiko marked this conversation as resolved.
Show resolved Hide resolved
sort.Slice(namesLongestToShortest, func(i, j int) bool {
return len(namesLongestToShortest[i]) > len(namesLongestToShortest[j])
})

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
Loading