image/tree: Don't limit name width if non tty

Previously when no terminal was attached the width was assumed to be 80.
This is too short for most image names which truncated the names when
output was redirect (for example to `grep`).

This disabled the name truncation if the terminal width can't be
determined.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-11-13 11:35:45 +01:00
parent c44e8a0727
commit aa5d00a3a4

View File

@ -230,11 +230,10 @@ func printImageTree(outs command.Streams, view treeView) {
}
out := tui.NewOutput(outs.Out())
isTerm := out.IsTerminal()
_, width := out.GetTtySize()
if width == 0 {
width = 80
}
if width < 20 {
if isTerm && width < 20 {
width = 20
}
@ -243,8 +242,6 @@ func printImageTree(outs command.Streams, view treeView) {
untaggedColor := out.Color(tui.ColorTertiary)
titleColor := out.Color(tui.ColorTitle)
isTerm := out.IsTerminal()
out.Println(generateLegend(out, width))
possibleChips := getPossibleChips(view)
@ -340,25 +337,27 @@ func printImageTree(outs command.Streams, view treeView) {
// to display their content.
func adjustColumns(width uint, columns []imgColumn, images []topImage) []imgColumn {
nameWidth := int(width)
for idx, h := range columns {
if h.Width == 0 {
continue
if nameWidth > 0 {
for idx, h := range columns {
if h.Width == 0 {
continue
}
d := h.Width
if idx > 0 {
d += columnSpacing
}
// If the first column gets too short, remove remaining columns
if nameWidth-d < 12 {
columns = columns[:idx]
break
}
nameWidth -= d
}
d := h.Width
if idx > 0 {
d += columnSpacing
}
// If the first column gets too short, remove remaining columns
if nameWidth-d < 12 {
columns = columns[:idx]
break
}
nameWidth -= d
}
// Try to make the first column as narrow as possible
widest := widestFirstColumnValue(columns, images)
if nameWidth > widest {
if width == 0 || nameWidth > widest {
nameWidth = widest
}
columns[0].Width = nameWidth