Skip to content

Commit 36f7088

Browse files
committed
feat: Respect NO_COLOR flag.
fix jwalton/gchalk#2
1 parent 8354fad commit 36f7088

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ By default, supportscolor checks `os.Args` for the `--color` and `--no-color` CL
6767

6868
For situations where using `--color` is not possible, use the environment variable `FORCE_COLOR=1` (level 1 - 16 colors), `FORCE_COLOR=2` (level 2 - 256 colors), or `FORCE_COLOR=3` (level 3 - true color) to forcefully enable color, or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
6969

70+
If `NO_COLOR` is specified and `FORCE_COLOR` is not, then colors will be disabled.
71+
7072
Explicit 256/True color mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.

supportscolor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ func checkForceColorFlags(env environment) *ColorLevel {
8383

8484
func checkForceColorEnv(env environment) *ColorLevel {
8585
forceColor, present := env.LookupEnv("FORCE_COLOR")
86-
8786
if present {
8887
if forceColor == "true" || forceColor == "" {
8988
result := Basic
@@ -109,6 +108,11 @@ func checkForceColorEnv(env environment) *ColorLevel {
109108
}
110109
}
111110

111+
if _, isNoColor := env.LookupEnv("NO_COLOR"); isNoColor {
112+
result := None
113+
return &result
114+
}
115+
112116
return nil
113117
}
114118

supportscolor_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ func TestDoNotCacheForceColor(t *testing.T) {
112112
}
113113
}
114114

115+
func TestNoColor(t *testing.T) {
116+
result := SupportsColor(0, setEnvironment(&testEnvironment{
117+
env: map[string]string{"NO_COLOR": ""},
118+
isNotTerminal: true,
119+
}))
120+
121+
if result.Level != None {
122+
t.Errorf("Expected %v, got %v", None, result.Level)
123+
}
124+
}
125+
115126
func TestReturnNoneIfNotTty(t *testing.T) {
116127
result := SupportsColor(0, setEnvironment(&testEnvironment{isNotTerminal: true}))
117128
if result.Level != None {

0 commit comments

Comments
 (0)