Skip to content

Commit b2249da

Browse files
authored
text: don't enable colors if TERM is set to dumb (#374)
1 parent f8bbd12 commit b2249da

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

text/color.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func areColorsOnInTheEnv() bool {
2727
if os.Getenv("FORCE_COLOR") == "1" {
2828
return true
2929
}
30-
return os.Getenv("NO_COLOR") == "" || os.Getenv("NO_COLOR") == "0"
30+
if os.Getenv("NO_COLOR") == "" || os.Getenv("NO_COLOR") == "0" {
31+
return os.Getenv("TERM") != "dumb"
32+
}
33+
34+
return false
3135
}
3236

3337
// The logic here is inspired from github.com/fatih/color; the following is

text/color_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,45 @@ func TestColor_EnableAndDisable(t *testing.T) {
2828
func TestColor_areColorsOnInTheEnv(t *testing.T) {
2929
_ = os.Setenv("FORCE_COLOR", "0")
3030
_ = os.Setenv("NO_COLOR", "0")
31+
_ = os.Setenv("TERM", "xterm")
3132
assert.True(t, areColorsOnInTheEnv())
3233

34+
_ = os.Setenv("FORCE_COLOR", "0")
35+
_ = os.Setenv("NO_COLOR", "0")
36+
_ = os.Setenv("TERM", "dumb")
37+
assert.False(t, areColorsOnInTheEnv())
38+
39+
// ---
40+
_ = os.Setenv("FORCE_COLOR", "0")
41+
_ = os.Setenv("NO_COLOR", "1")
42+
_ = os.Setenv("TERM", "xterm")
43+
assert.False(t, areColorsOnInTheEnv())
44+
3345
_ = os.Setenv("FORCE_COLOR", "0")
3446
_ = os.Setenv("NO_COLOR", "1")
47+
_ = os.Setenv("TERM", "dumb")
3548
assert.False(t, areColorsOnInTheEnv())
3649

50+
// ---
3751
_ = os.Setenv("FORCE_COLOR", "1")
3852
_ = os.Setenv("NO_COLOR", "0")
53+
_ = os.Setenv("TERM", "xterm")
54+
assert.True(t, areColorsOnInTheEnv())
55+
56+
_ = os.Setenv("FORCE_COLOR", "1")
57+
_ = os.Setenv("NO_COLOR", "0")
58+
_ = os.Setenv("TERM", "dumb")
59+
assert.True(t, areColorsOnInTheEnv())
60+
61+
// ---
62+
_ = os.Setenv("FORCE_COLOR", "1")
63+
_ = os.Setenv("NO_COLOR", "1")
64+
_ = os.Setenv("TERM", "xterm")
3965
assert.True(t, areColorsOnInTheEnv())
4066

4167
_ = os.Setenv("FORCE_COLOR", "1")
4268
_ = os.Setenv("NO_COLOR", "1")
69+
_ = os.Setenv("TERM", "dumb")
4370
assert.True(t, areColorsOnInTheEnv())
4471
}
4572

0 commit comments

Comments
 (0)