list/tree: No extra spacing for graphdriver

Don't output the extra spacing around the images when none of the
top-level image entries has any children.

This makes the list look better when ran against the graphdrivers image
store.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
(cherry picked from commit 7b91647943)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2024-08-17 01:18:19 +02:00
parent 60ce3fbc96
commit 0c4e7478e2
No known key found for this signature in database
GPG Key ID: B85EFCFE26DEF92A
1 changed files with 24 additions and 7 deletions

View File

@ -22,6 +22,13 @@ type treeOptions struct {
filters filters.Args filters filters.Args
} }
type treeView struct {
images []topImage
// imageSpacing indicates whether there should be extra spacing between images.
imageSpacing bool
}
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error { func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
images, err := dockerCLI.Client().ImageList(ctx, imagetypes.ListOptions{ images, err := dockerCLI.Client().ImageList(ctx, imagetypes.ListOptions{
All: opts.all, All: opts.all,
@ -32,7 +39,9 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
return err return err
} }
view := make([]topImage, 0, len(images)) view := treeView{
images: make([]topImage, 0, len(images)),
}
for _, img := range images { for _, img := range images {
details := imageDetails{ details := imageDetails{
ID: img.ID, ID: img.ID,
@ -66,11 +75,14 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
totalContent += im.Size.Content totalContent += im.Size.Content
children = append(children, sub) children = append(children, sub)
// Add extra spacing between images if there's at least one entry with children.
view.imageSpacing = true
} }
details.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3) details.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3)
view = append(view, topImage{ view.images = append(view.images, topImage{
Names: img.RepoTags, Names: img.RepoTags,
Details: details, Details: details,
Children: children, Children: children,
@ -78,8 +90,8 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
}) })
} }
sort.Slice(view, func(i, j int) bool { sort.Slice(view.images, func(i, j int) bool {
return view[i].created > view[j].created return view.images[i].created > view.images[j].created
}) })
return printImageTree(dockerCLI, view) return printImageTree(dockerCLI, view)
@ -108,7 +120,7 @@ type subImage struct {
const columnSpacing = 3 const columnSpacing = 3
func printImageTree(dockerCLI command.Cli, images []topImage) error { func printImageTree(dockerCLI command.Cli, view treeView) error {
out := dockerCLI.Out() out := dockerCLI.Out()
_, width := out.GetTtySize() _, width := out.GetTtySize()
if width == 0 { if width == 0 {
@ -197,6 +209,7 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {
nameWidth -= d nameWidth -= d
} }
images := view.images
// Try to make the first column as narrow as possible // Try to make the first column as narrow as possible
widest := widestFirstColumnValue(columns, images) widest := widestFirstColumnValue(columns, images)
if nameWidth > widest { if nameWidth > widest {
@ -217,10 +230,14 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {
// Print images // Print images
for _, img := range images { for _, img := range images {
_, _ = fmt.Fprintln(out, "")
printNames(out, columns, img, topNameColor, untaggedColor) printNames(out, columns, img, topNameColor, untaggedColor)
printDetails(out, columns, normalColor, img.Details) printDetails(out, columns, normalColor, img.Details)
if len(img.Children) > 0 || view.imageSpacing {
_, _ = fmt.Fprintln(out)
}
printChildren(out, columns, img, normalColor) printChildren(out, columns, img, normalColor)
_, _ = fmt.Fprintln(out)
} }
return nil return nil
@ -240,7 +257,6 @@ func printDetails(out *streams.Out, headers []imgColumn, defaultColor aec.ANSI,
val := h.DetailsValue(&details) val := h.DetailsValue(&details)
_, _ = fmt.Fprint(out, h.Print(clr, val)) _, _ = fmt.Fprint(out, h.Print(clr, val))
} }
fmt.Printf("\n")
} }
func printChildren(out *streams.Out, headers []imgColumn, img topImage, normalColor aec.ANSI) { func printChildren(out *streams.Out, headers []imgColumn, img topImage, normalColor aec.ANSI) {
@ -257,6 +273,7 @@ func printChildren(out *streams.Out, headers []imgColumn, img topImage, normalCo
} }
printDetails(out, headers, clr, sub.Details) printDetails(out, headers, clr, sub.Details)
_, _ = fmt.Fprintln(out, "")
} }
} }