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

list/tree: No extra spacing for graphdriver #5356

Merged
merged 1 commit into from
Aug 19, 2024
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
31 changes: 24 additions & 7 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ type treeOptions struct {
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 {
images, err := dockerCLI.Client().ImageList(ctx, imagetypes.ListOptions{
All: opts.all,
Expand All @@ -32,7 +39,9 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
return err
}

view := make([]topImage, 0, len(images))
view := treeView{
images: make([]topImage, 0, len(images)),
}
for _, img := range images {
details := imageDetails{
ID: img.ID,
Expand Down Expand Up @@ -66,20 +75,23 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error

totalContent += im.Size.Content
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)

view = append(view, topImage{
view.images = append(view.images, topImage{
Names: img.RepoTags,
Details: details,
Children: children,
created: img.Created,
})
}

sort.Slice(view, func(i, j int) bool {
return view[i].created > view[j].created
sort.Slice(view.images, func(i, j int) bool {
return view.images[i].created > view.images[j].created
})

return printImageTree(dockerCLI, view)
Expand Down Expand Up @@ -108,7 +120,7 @@ type subImage struct {

const columnSpacing = 3

func printImageTree(dockerCLI command.Cli, images []topImage) error {
func printImageTree(dockerCLI command.Cli, view treeView) error {
out := dockerCLI.Out()
_, width := out.GetTtySize()
if width == 0 {
Expand Down Expand Up @@ -197,6 +209,7 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {
nameWidth -= d
}

images := view.images
// Try to make the first column as narrow as possible
widest := widestFirstColumnValue(columns, images)
if nameWidth > widest {
Expand All @@ -217,10 +230,14 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {

// Print images
for _, img := range images {
_, _ = fmt.Fprintln(out, "")
printNames(out, columns, img, topNameColor, untaggedColor)
printDetails(out, columns, normalColor, img.Details)

if len(img.Children) > 0 || view.imageSpacing {
_, _ = fmt.Fprintln(out)
}
printChildren(out, columns, img, normalColor)
_, _ = fmt.Fprintln(out)
}

return nil
Expand All @@ -240,7 +257,6 @@ func printDetails(out *streams.Out, headers []imgColumn, defaultColor aec.ANSI,
val := h.DetailsValue(&details)
_, _ = fmt.Fprint(out, h.Print(clr, val))
}
fmt.Printf("\n")
}

func printChildren(out *streams.Out, headers []imgColumn, img topImage, normalColor aec.ANSI) {
Expand All @@ -257,6 +273,7 @@ func printChildren(out *streams.Out, headers []imgColumn, img topImage, normalCo
}

printDetails(out, headers, clr, sub.Details)
_, _ = fmt.Fprintln(out, "")
}
}

Expand Down
Loading