diff --git a/cli/command/image/tree.go b/cli/command/image/tree.go index 6616e9e207a1..d56cf560e7f8 100644 --- a/cli/command/image/tree.go +++ b/cli/command/image/tree.go @@ -206,30 +206,38 @@ var allChips = []imageChip{ chipInUse, } +// getPossibleChips returns the list of chips used by at least one image. +// It is used to determine which columns to print (and how much width to +// reserve). func getPossibleChips(view treeView) (chips []imageChip) { - remaining := make([]imageChip, len(allChips)) - copy(remaining, allChips) + remaining := slices.Clone(allChips) + + check := func(d imageDetails) (done bool) { + // filter without allocating + out := remaining[:0] + for _, chip := range remaining { + if chip.check(&d) { + chips = append(chips, chip) + continue + } + out = append(out, chip) + } + remaining = out + return len(remaining) == 0 + } - var possible []imageChip for _, img := range view.images { - details := []imageDetails{img.Details} - - for _, c := range img.Children { - details = append(details, c.Details) + if check(img.Details) { + return chips } - - for _, d := range details { - for idx := len(remaining) - 1; idx >= 0; idx-- { - chip := remaining[idx] - if chip.check(&d) { - possible = append(possible, chip) - remaining = append(remaining[:idx], remaining[idx+1:]...) - } + for _, c := range img.Children { + if check(c.Details) { + return chips } } } - return possible + return chips } func printImageTree(outs command.Streams, view treeView) {