Skip to content

Commit 7b91647

Browse files
committed
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>
1 parent 8675f4c commit 7b91647

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

cli/command/image/tree.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ type treeOptions struct {
2222
filters filters.Args
2323
}
2424

25+
type treeView struct {
26+
images []topImage
27+
28+
// imageSpacing indicates whether there should be extra spacing between images.
29+
imageSpacing bool
30+
}
31+
2532
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
2633
images, err := dockerCLI.Client().ImageList(ctx, imagetypes.ListOptions{
2734
All: opts.all,
@@ -32,7 +39,9 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
3239
return err
3340
}
3441

35-
view := make([]topImage, 0, len(images))
42+
view := treeView{
43+
images: make([]topImage, 0, len(images)),
44+
}
3645
for _, img := range images {
3746
details := imageDetails{
3847
ID: img.ID,
@@ -66,20 +75,23 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
6675

6776
totalContent += im.Size.Content
6877
children = append(children, sub)
78+
79+
// Add extra spacing between images if there's at least one entry with children.
80+
view.imageSpacing = true
6981
}
7082

7183
details.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3)
7284

73-
view = append(view, topImage{
85+
view.images = append(view.images, topImage{
7486
Names: img.RepoTags,
7587
Details: details,
7688
Children: children,
7789
created: img.Created,
7890
})
7991
}
8092

81-
sort.Slice(view, func(i, j int) bool {
82-
return view[i].created > view[j].created
93+
sort.Slice(view.images, func(i, j int) bool {
94+
return view.images[i].created > view.images[j].created
8395
})
8496

8597
return printImageTree(dockerCLI, view)
@@ -108,7 +120,7 @@ type subImage struct {
108120

109121
const columnSpacing = 3
110122

111-
func printImageTree(dockerCLI command.Cli, images []topImage) error {
123+
func printImageTree(dockerCLI command.Cli, view treeView) error {
112124
out := dockerCLI.Out()
113125
_, width := out.GetTtySize()
114126
if width == 0 {
@@ -197,6 +209,7 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {
197209
nameWidth -= d
198210
}
199211

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

218231
// Print images
219232
for _, img := range images {
220-
_, _ = fmt.Fprintln(out, "")
221233
printNames(out, columns, img, topNameColor, untaggedColor)
222234
printDetails(out, columns, normalColor, img.Details)
235+
236+
if len(img.Children) > 0 || view.imageSpacing {
237+
_, _ = fmt.Fprintln(out)
238+
}
223239
printChildren(out, columns, img, normalColor)
240+
_, _ = fmt.Fprintln(out)
224241
}
225242

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

246262
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
257273
}
258274

259275
printDetails(out, headers, clr, sub.Details)
276+
_, _ = fmt.Fprintln(out, "")
260277
}
261278
}
262279

0 commit comments

Comments
 (0)