@@ -281,11 +281,23 @@ func printNames(out *streams.Out, headers []imgColumn, img topImage, color, unta
281281 _ , _ = fmt .Fprint (out , headers [0 ].Print (untaggedColor , "<untagged>" ))
282282 }
283283
284- for nameIdx , name := range img .Names {
285- if nameIdx != 0 {
286- _ , _ = fmt .Fprintln (out , "" )
284+ // TODO: Replace with namesLongestToShortest := slices.SortedFunc(slices.Values(img.Names))
285+ // once we move to Go 1.23.
286+ namesLongestToShortest := make ([]string , len (img .Names ))
287+ copy (namesLongestToShortest , img .Names )
288+ sort .Slice (namesLongestToShortest , func (i , j int ) bool {
289+ return len (namesLongestToShortest [i ]) > len (namesLongestToShortest [j ])
290+ })
291+
292+ for nameIdx , name := range namesLongestToShortest {
293+ // Don't limit first names to the column width because only the last
294+ // name will be printed alongside other columns.
295+ if nameIdx < len (img .Names )- 1 {
296+ _ , fullWidth := out .GetTtySize ()
297+ _ , _ = fmt .Fprintln (out , color .Apply (truncateRunes (name , int (fullWidth ))))
298+ } else {
299+ _ , _ = fmt .Fprint (out , headers [0 ].Print (color , name ))
287300 }
288- _ , _ = fmt .Fprint (out , headers [0 ].Print (color , name ))
289301 }
290302}
291303
0 commit comments