@@ -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).
209212func 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
235243func printImageTree (outs command.Streams , view treeView ) {
0 commit comments