image/tree: Respect NO_COLOR env variable
Do not use the fancy colored output if NO_COLOR variable is set to 1 following the https://no-color.org/ convention. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
@ -292,7 +292,7 @@ func printImageTree(outs command.Streams, view treeView) {
|
||||
Width: func() int {
|
||||
maxChipsWidth := 0
|
||||
for _, chip := range possibleChips {
|
||||
s := chip.String(isTerm)
|
||||
s := out.Sprint(chip)
|
||||
l := tui.Width(s)
|
||||
maxChipsWidth += l
|
||||
}
|
||||
@ -308,9 +308,9 @@ func printImageTree(outs command.Streams, view treeView) {
|
||||
var b strings.Builder
|
||||
for _, chip := range possibleChips {
|
||||
if chip.check(d) {
|
||||
b.WriteString(chip.String(isTerm))
|
||||
b.WriteString(out.Sprint(chip))
|
||||
} else {
|
||||
b.WriteString(chipPlaceholder.String(isTerm))
|
||||
b.WriteString(out.Sprint(chipPlaceholder))
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
|
||||
@ -15,6 +15,7 @@ func TestPrintImageTreeAnsiTty(t *testing.T) {
|
||||
stdoutTty bool
|
||||
stderrTty bool
|
||||
expectedAnsi bool
|
||||
noColorEnv bool
|
||||
}{
|
||||
{
|
||||
name: "non-terminal",
|
||||
@ -80,6 +81,24 @@ func TestPrintImageTreeAnsiTty(t *testing.T) {
|
||||
|
||||
expectedAnsi: false,
|
||||
},
|
||||
{
|
||||
name: "no-color-env",
|
||||
stdinTty: false,
|
||||
stdoutTty: false,
|
||||
stderrTty: false,
|
||||
|
||||
noColorEnv: true,
|
||||
expectedAnsi: false,
|
||||
},
|
||||
{
|
||||
name: "no-color-env-terminal",
|
||||
stdinTty: true,
|
||||
stdoutTty: true,
|
||||
stderrTty: true,
|
||||
|
||||
noColorEnv: true,
|
||||
expectedAnsi: false,
|
||||
},
|
||||
}
|
||||
|
||||
mockView := treeView{
|
||||
@ -115,6 +134,11 @@ func TestPrintImageTreeAnsiTty(t *testing.T) {
|
||||
cli.In().SetIsTerminal(tc.stdinTty)
|
||||
cli.Out().SetIsTerminal(tc.stdoutTty)
|
||||
cli.Err().SetIsTerminal(tc.stderrTty)
|
||||
if tc.noColorEnv {
|
||||
t.Setenv("NO_COLOR", "1")
|
||||
} else {
|
||||
t.Setenv("NO_COLOR", "")
|
||||
}
|
||||
|
||||
printImageTree(cli, mockView)
|
||||
|
||||
@ -123,9 +147,9 @@ func TestPrintImageTreeAnsiTty(t *testing.T) {
|
||||
|
||||
hasAnsi := strings.Contains(out, "\x1b[")
|
||||
if tc.expectedAnsi {
|
||||
assert.Check(t, hasAnsi, "Output should contain ANSI escape codes")
|
||||
assert.Check(t, hasAnsi, "Output should contain ANSI escape codes, output: %s", out)
|
||||
} else {
|
||||
assert.Check(t, !hasAnsi, "Output should not contain ANSI escape codes")
|
||||
assert.Check(t, !hasAnsi, "Output should not contain ANSI escape codes, output: %s", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user