Skip to content

Commit 9258315

Browse files
committed
image/tree: Allow image names to overflow instead of truncating
Users were experiencing poor UX when image names were truncated in the table output. Instead of cutting off long image names with ellipsis, the names now wrap to the next line to ensure full visibility. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 511dad6 commit 9258315

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

cli/command/image/tree.go

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,10 @@ func printImageTree(outs command.Streams, view treeView) {
258258
possibleChips := getPossibleChips(view)
259259
columns := []imgColumn{
260260
{
261-
Title: "Image",
262-
Align: alignLeft,
263-
Width: 0,
261+
Title: "Image",
262+
Align: alignLeft,
263+
Width: 0,
264+
NoEllipsis: true,
264265
},
265266
{
266267
Title: "ID",
@@ -436,13 +437,18 @@ func printNames(out tui.Output, headers []imgColumn, img topImage, color, untagg
436437
}
437438

438439
for nameIdx, name := range img.Names {
439-
// Don't limit first names to the column width because only the last
440-
// name will be printed alongside other columns.
441-
if nameIdx < len(img.Names)-1 {
442-
_, fullWidth := out.GetTtySize()
443-
_, _ = fmt.Fprintln(out, color.Apply(tui.Ellipsis(name, int(fullWidth))))
444-
} else {
445-
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
440+
nameWidth := tui.Width(name)
441+
lastName := nameIdx == len(img.Names)-1
442+
multiLine := nameWidth > headers[0].Width
443+
444+
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
445+
446+
// Put the last name in the same line as other columns ONLY if it doesn't exceed the column width.
447+
if !lastName || multiLine {
448+
_, _ = fmt.Fprintln(out)
449+
}
450+
if multiLine && lastName {
451+
_, _ = fmt.Fprint(out, strings.Repeat(" ", headers[0].Width))
446452
}
447453
}
448454
}
@@ -462,6 +468,7 @@ type imgColumn struct {
462468

463469
DetailsValue func(*imageDetails) string
464470
Color *aec.ANSI
471+
NoEllipsis bool
465472
}
466473

467474
func (h imgColumn) Print(clr aec.ANSI, s string) string {
@@ -478,12 +485,16 @@ func (h imgColumn) Print(clr aec.ANSI, s string) string {
478485
func (h imgColumn) PrintC(clr aec.ANSI, s string) string {
479486
ln := tui.Width(s)
480487

481-
if ln > h.Width {
482-
return clr.Apply(tui.Ellipsis(s, h.Width))
483-
}
484-
485488
fill := h.Width - ln
486489

490+
if fill < 0 {
491+
if h.NoEllipsis {
492+
fill = 0
493+
} else {
494+
return clr.Apply(tui.Ellipsis(s, h.Width))
495+
}
496+
}
497+
487498
l := fill / 2
488499
r := fill - l
489500

@@ -492,20 +503,33 @@ func (h imgColumn) PrintC(clr aec.ANSI, s string) string {
492503

493504
func (h imgColumn) PrintL(clr aec.ANSI, s string) string {
494505
ln := tui.Width(s)
495-
if ln > h.Width {
496-
return clr.Apply(tui.Ellipsis(s, h.Width))
506+
507+
fill := h.Width - ln
508+
509+
if fill < 0 {
510+
if h.NoEllipsis {
511+
fill = 0
512+
} else {
513+
return clr.Apply(tui.Ellipsis(s, h.Width))
514+
}
497515
}
498516

499-
return clr.Apply(s) + strings.Repeat(" ", h.Width-ln)
517+
return clr.Apply(s) + strings.Repeat(" ", fill)
500518
}
501519

502520
func (h imgColumn) PrintR(clr aec.ANSI, s string) string {
503521
ln := tui.Width(s)
504-
if ln > h.Width {
505-
return clr.Apply(tui.Ellipsis(s, h.Width))
522+
fill := h.Width - ln
523+
524+
if fill < 0 {
525+
if h.NoEllipsis {
526+
fill = 0
527+
} else {
528+
return clr.Apply(tui.Ellipsis(s, h.Width))
529+
}
506530
}
507531

508-
return strings.Repeat(" ", h.Width-ln) + clr.Apply(s)
532+
return strings.Repeat(" ", fill) + clr.Apply(s)
509533
}
510534

511535
// widestFirstColumnValue calculates the width needed to fully display the image names and platforms.

0 commit comments

Comments
 (0)