1
0
mirror of https://github.com/docker/cli.git synced 2026-10-31 11:35:26 +03:00

cli/command/image: getPossibleChips: simplify

Extract the check to a closure that filters in-place, and
run the check in a single loop.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-03-20 12:28:34 +01:00
parent 7922984193
commit 64c8d68045

View File

@@ -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)
var possible []imageChip
for _, img := range view.images {
details := []imageDetails{img.Details}
for _, c := range img.Children {
details = append(details, c.Details)
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
}
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 _, img := range view.images {
if check(img.Details) {
return chips
}
for _, c := range img.Children {
if check(c.Details) {
return chips
}
}
}
return possible
return chips
}
func printImageTree(outs command.Streams, view treeView) {