Skip to content

Commit fbfb69f

Browse files
authored
Merge pull request #6873 from thaJeztah/simplify_chips
cli/command/image: getPossibleChips: simplify
2 parents 424955d + 64c8d68 commit fbfb69f

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

cli/command/image/tree.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,30 +206,38 @@ var allChips = []imageChip{
206206
chipInUse,
207207
}
208208

209+
// getPossibleChips returns the list of chips used by at least one image.
210+
// It is used to determine which columns to print (and how much width to
211+
// reserve).
209212
func getPossibleChips(view treeView) (chips []imageChip) {
210-
remaining := make([]imageChip, len(allChips))
211-
copy(remaining, allChips)
213+
remaining := slices.Clone(allChips)
214+
215+
check := func(d imageDetails) (done bool) {
216+
// filter without allocating
217+
out := remaining[:0]
218+
for _, chip := range remaining {
219+
if chip.check(&d) {
220+
chips = append(chips, chip)
221+
continue
222+
}
223+
out = append(out, chip)
224+
}
225+
remaining = out
226+
return len(remaining) == 0
227+
}
212228

213-
var possible []imageChip
214229
for _, img := range view.images {
215-
details := []imageDetails{img.Details}
216-
217-
for _, c := range img.Children {
218-
details = append(details, c.Details)
230+
if check(img.Details) {
231+
return chips
219232
}
220-
221-
for _, d := range details {
222-
for idx := len(remaining) - 1; idx >= 0; idx-- {
223-
chip := remaining[idx]
224-
if chip.check(&d) {
225-
possible = append(possible, chip)
226-
remaining = append(remaining[:idx], remaining[idx+1:]...)
227-
}
233+
for _, c := range img.Children {
234+
if check(c.Details) {
235+
return chips
228236
}
229237
}
230238
}
231239

232-
return possible
240+
return chips
233241
}
234242

235243
func printImageTree(outs command.Streams, view treeView) {

0 commit comments

Comments
 (0)