Skip to content

Commit aa5d00a

Browse files
committed
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 <[email protected]>
1 parent c44e8a0 commit aa5d00a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

cli/command/image/tree.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,10 @@ func printImageTree(outs command.Streams, view treeView) {
230230
}
231231

232232
out := tui.NewOutput(outs.Out())
233+
isTerm := out.IsTerminal()
234+
233235
_, width := out.GetTtySize()
234-
if width == 0 {
235-
width = 80
236-
}
237-
if width < 20 {
236+
if isTerm && width < 20 {
238237
width = 20
239238
}
240239

@@ -243,8 +242,6 @@ func printImageTree(outs command.Streams, view treeView) {
243242
untaggedColor := out.Color(tui.ColorTertiary)
244243
titleColor := out.Color(tui.ColorTitle)
245244

246-
isTerm := out.IsTerminal()
247-
248245
out.Println(generateLegend(out, width))
249246

250247
possibleChips := getPossibleChips(view)
@@ -340,25 +337,27 @@ func printImageTree(outs command.Streams, view treeView) {
340337
// to display their content.
341338
func adjustColumns(width uint, columns []imgColumn, images []topImage) []imgColumn {
342339
nameWidth := int(width)
343-
for idx, h := range columns {
344-
if h.Width == 0 {
345-
continue
346-
}
347-
d := h.Width
348-
if idx > 0 {
349-
d += columnSpacing
350-
}
351-
// If the first column gets too short, remove remaining columns
352-
if nameWidth-d < 12 {
353-
columns = columns[:idx]
354-
break
340+
if nameWidth > 0 {
341+
for idx, h := range columns {
342+
if h.Width == 0 {
343+
continue
344+
}
345+
d := h.Width
346+
if idx > 0 {
347+
d += columnSpacing
348+
}
349+
// If the first column gets too short, remove remaining columns
350+
if nameWidth-d < 12 {
351+
columns = columns[:idx]
352+
break
353+
}
354+
nameWidth -= d
355355
}
356-
nameWidth -= d
357356
}
358357

359358
// Try to make the first column as narrow as possible
360359
widest := widestFirstColumnValue(columns, images)
361-
if nameWidth > widest {
360+
if width == 0 || nameWidth > widest {
362361
nameWidth = widest
363362
}
364363
columns[0].Width = nameWidth

0 commit comments

Comments
 (0)