Skip to content

Commit 8d1525a

Browse files
authored
Merge pull request #6614 from vvoland/list-tree-header-ansi
image/tree: Fix table header having escape codes when not tty
2 parents 30d597d + e5f4649 commit 8d1525a

File tree

6 files changed

+143
-9
lines changed

6 files changed

+143
-9
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Info -> U In Use
2-
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
2+
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Info -> U In Use
2-
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
2+
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Info -> U In Use
2-
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
2+
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Info -> U In Use
2-
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
2+
IMAGE ID DISK USAGE CONTENT SIZE EXTRA

cli/command/image/tree.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ func getPossibleChips(view treeView) (chips []imageChip) {
224224
return possible
225225
}
226226

227-
func printImageTree(dockerCLI command.Cli, view treeView) {
228-
if streamRedirected(dockerCLI.Out()) {
229-
_, _ = fmt.Fprintln(dockerCLI.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
227+
func printImageTree(outs command.Streams, view treeView) {
228+
if streamRedirected(outs.Out()) {
229+
_, _ = fmt.Fprintln(outs.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
230230
}
231231

232-
out := tui.NewOutput(dockerCLI.Out())
232+
out := tui.NewOutput(outs.Out())
233233
_, width := out.GetTtySize()
234234
if width == 0 {
235235
width = 80
@@ -241,6 +241,8 @@ func printImageTree(dockerCLI command.Cli, view treeView) {
241241
topNameColor := out.Color(aec.NewBuilder(aec.BlueF, aec.Bold).ANSI)
242242
normalColor := out.Color(tui.ColorSecondary)
243243
untaggedColor := out.Color(tui.ColorTertiary)
244+
titleColor := out.Color(tui.ColorTitle)
245+
244246
isTerm := out.IsTerminal()
245247

246248
out.Println(generateLegend(out, width))
@@ -316,7 +318,7 @@ func printImageTree(dockerCLI command.Cli, view treeView) {
316318
_, _ = fmt.Fprint(out, strings.Repeat(" ", columnSpacing))
317319
}
318320

319-
_, _ = fmt.Fprint(out, h.Print(tui.ColorTitle, strings.ToUpper(h.Title)))
321+
_, _ = fmt.Fprint(out, h.Print(titleColor, strings.ToUpper(h.Title)))
320322
}
321323
_, _ = fmt.Fprintln(out)
322324

cli/command/image/tree_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package image
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/docker/cli/internal/test"
8+
"gotest.tools/v3/assert"
9+
)
10+
11+
func TestPrintImageTreeAnsiTty(t *testing.T) {
12+
testCases := []struct {
13+
name string
14+
stdinTty bool
15+
stdoutTty bool
16+
stderrTty bool
17+
expectedAnsi bool
18+
}{
19+
{
20+
name: "non-terminal",
21+
stdinTty: false,
22+
stdoutTty: false,
23+
stderrTty: false,
24+
25+
expectedAnsi: false,
26+
},
27+
{
28+
name: "terminal",
29+
stdinTty: true,
30+
stdoutTty: true,
31+
stderrTty: true,
32+
33+
expectedAnsi: true,
34+
},
35+
{
36+
name: "stdout-tty-only",
37+
stdinTty: false,
38+
stdoutTty: true,
39+
stderrTty: false,
40+
41+
expectedAnsi: true,
42+
},
43+
{
44+
name: "stdin-stderr-tty-only",
45+
stdinTty: true,
46+
stdoutTty: false,
47+
stderrTty: true,
48+
49+
expectedAnsi: false,
50+
},
51+
{
52+
name: "stdout-stdin-tty",
53+
stdinTty: true,
54+
stdoutTty: true,
55+
stderrTty: false,
56+
57+
expectedAnsi: true,
58+
},
59+
{
60+
name: "stdout-stderr-tty",
61+
stdinTty: false,
62+
stdoutTty: true,
63+
stderrTty: true,
64+
65+
expectedAnsi: true,
66+
},
67+
{
68+
name: "stdin-tty-only",
69+
stdinTty: true,
70+
stdoutTty: false,
71+
stderrTty: false,
72+
73+
expectedAnsi: false,
74+
},
75+
{
76+
name: "stderr-tty-only",
77+
stdinTty: false,
78+
stdoutTty: false,
79+
stderrTty: true,
80+
81+
expectedAnsi: false,
82+
},
83+
}
84+
85+
mockView := treeView{
86+
images: []topImage{
87+
{
88+
Names: []string{"test-image:latest"},
89+
Details: imageDetails{
90+
ID: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
91+
DiskUsage: "10.5 MB",
92+
InUse: true,
93+
ContentSize: "5.2 MB",
94+
},
95+
Children: []subImage{
96+
{
97+
Platform: "linux/amd64",
98+
Available: true,
99+
Details: imageDetails{
100+
ID: "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
101+
DiskUsage: "5.1 MB",
102+
InUse: false,
103+
ContentSize: "2.5 MB",
104+
},
105+
},
106+
},
107+
},
108+
},
109+
imageSpacing: false,
110+
}
111+
112+
for _, tc := range testCases {
113+
t.Run(tc.name, func(t *testing.T) {
114+
cli := test.NewFakeCli(nil)
115+
cli.In().SetIsTerminal(tc.stdinTty)
116+
cli.Out().SetIsTerminal(tc.stdoutTty)
117+
cli.Err().SetIsTerminal(tc.stderrTty)
118+
119+
printImageTree(cli, mockView)
120+
121+
out := cli.OutBuffer().String()
122+
assert.Check(t, len(out) > 0, "Output should not be empty")
123+
124+
hasAnsi := strings.Contains(out, "\x1b[")
125+
if tc.expectedAnsi {
126+
assert.Check(t, hasAnsi, "Output should contain ANSI escape codes")
127+
} else {
128+
assert.Check(t, !hasAnsi, "Output should not contain ANSI escape codes")
129+
}
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)