Skip to content

Commit 6378f62

Browse files
authored
Merge pull request #171 from pellared/no-color-empty-val
NO_COLOR requires a non-empty string
2 parents 98e6a4a + 5d7e63e commit 6378f62

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ suits you.
77

88
![Color](https://user-images.githubusercontent.com/438920/96832689-03b3e000-13f4-11eb-9803-46f4c4de3406.jpg)
99

10-
1110
## Install
1211

1312
```bash
@@ -124,17 +123,17 @@ fmt.Println("All text will now be bold magenta.")
124123
```
125124

126125
### Disable/Enable color
127-
126+
128127
There might be a case where you want to explicitly disable/enable color output. the
129128
`go-isatty` package will automatically disable color output for non-tty output streams
130129
(for example if the output were piped directly to `less`).
131130

132131
The `color` package also disables color output if the [`NO_COLOR`](https://no-color.org) environment
133-
variable is set (regardless of its value).
132+
variable is set to a non-empty string.
134133

135134
`Color` has support to disable/enable colors programmatically both globally and
136135
for single color definitions. For example suppose you have a CLI app and a
137-
`--no-color` bool flag. You can easily disable the color output with:
136+
`-no-color` bool flag. You can easily disable the color output with:
138137

139138
```go
140139
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
@@ -167,11 +166,10 @@ To output color in GitHub Actions (or other CI systems that support ANSI colors)
167166
* Save/Return previous values
168167
* Evaluate fmt.Formatter interface
169168

170-
171169
## Credits
172170

173-
* [Fatih Arslan](https://github.com/fatih)
174-
* Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)
171+
* [Fatih Arslan](https://github.com/fatih)
172+
* Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)
175173

176174
## License
177175

color.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
// set (regardless of its value). This is a global option and affects all
2020
// colors. For more control over each color block use the methods
2121
// DisableColor() individually.
22-
NoColor = noColorExists() || os.Getenv("TERM") == "dumb" ||
22+
NoColor = noColorIsSet() || os.Getenv("TERM") == "dumb" ||
2323
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
2424

2525
// Output defines the standard output of the print functions. By default,
@@ -35,10 +35,9 @@ var (
3535
colorsCacheMu sync.Mutex // protects colorsCache
3636
)
3737

38-
// noColorExists returns true if the environment variable NO_COLOR exists.
39-
func noColorExists() bool {
40-
_, exists := os.LookupEnv("NO_COLOR")
41-
return exists
38+
// noColorIsSet returns true if the environment variable NO_COLOR is set to a non-empty string.
39+
func noColorIsSet() bool {
40+
return os.Getenv("NO_COLOR") != ""
4241
}
4342

4443
// Color defines a custom color object which is defined by SGR parameters.
@@ -120,7 +119,7 @@ func New(value ...Attribute) *Color {
120119
params: make([]Attribute, 0),
121120
}
122121

123-
if noColorExists() {
122+
if noColorIsSet() {
124123
c.noColor = boolPtr(true)
125124
}
126125

color_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestNoColor_Env(t *testing.T) {
183183
{text: "hwhite", code: FgHiWhite},
184184
}
185185

186-
os.Setenv("NO_COLOR", "")
186+
os.Setenv("NO_COLOR", "1")
187187
t.Cleanup(func() {
188188
os.Unsetenv("NO_COLOR")
189189
})
@@ -197,7 +197,41 @@ func TestNoColor_Env(t *testing.T) {
197197
t.Errorf("Expecting %s, got '%s'\n", c.text, line)
198198
}
199199
}
200+
}
200201

202+
func Test_noColorIsSet(t *testing.T) {
203+
tests := []struct {
204+
name string
205+
act func()
206+
want bool
207+
}{
208+
{
209+
name: "default",
210+
act: func() {},
211+
want: false,
212+
},
213+
{
214+
name: "NO_COLOR=1",
215+
act: func() { os.Setenv("NO_COLOR", "1") },
216+
want: true,
217+
},
218+
{
219+
name: "NO_COLOR=",
220+
act: func() { os.Setenv("NO_COLOR", "") },
221+
want: false,
222+
},
223+
}
224+
for _, tt := range tests {
225+
t.Run(tt.name, func(t *testing.T) {
226+
t.Cleanup(func() {
227+
os.Unsetenv("NO_COLOR")
228+
})
229+
tt.act()
230+
if got := noColorIsSet(); got != tt.want {
231+
t.Errorf("noColorIsSet() = %v, want %v", got, tt.want)
232+
}
233+
})
234+
}
201235
}
202236

203237
func TestColorVisual(t *testing.T) {

0 commit comments

Comments
 (0)