Skip to content

Commit 02e4121

Browse files
authored
Merge pull request #43 from AlekSi/no_color
Support NO_COLOR (https://no-color.org).
2 parents fb18f75 + 1fd8679 commit 02e4121

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ func Test(t *testing.T) {
3131

3232
## Color
3333

34-
To turn off the colors, run `go test` with the `-nocolor` flag, or with the env var `IS_NO_COLOR=true`.
34+
To turn off the colors, run `go test` with the `-nocolor` flag,
35+
or with the env var [`NO_COLOR` (with any value)](https://no-color.org).
3536

3637
```
3738
go test -nocolor
3839
```
3940

4041
```
41-
IS_NO_COLOR=true go test
42+
NO_COLOR=1 go test
4243
```

is.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
// the helper methods:
2323
//
2424
// func Test(t *testing.T) {
25-
//
2625
// // always start tests with this
2726
// is := is.New(t)
2827
//
@@ -32,7 +31,6 @@
3231
//
3332
// body := readBody(r)
3433
// is.True(strings.Contains(body, "Hi there"))
35-
//
3634
// }
3735
package is
3836

@@ -45,6 +43,7 @@ import (
4543
"os"
4644
"path/filepath"
4745
"reflect"
46+
"strconv"
4847
"strings"
4948
)
5049

@@ -74,7 +73,19 @@ type I struct {
7473
var noColorFlag bool
7574

7675
func init() {
77-
envNoColor := os.Getenv("IS_NO_COLOR") == "true"
76+
var envNoColor bool
77+
78+
// prefer https://no-color.org (with any value)
79+
if _, ok := os.LookupEnv("NO_COLOR"); ok {
80+
envNoColor = true
81+
}
82+
83+
if v, ok := os.LookupEnv("IS_NO_COLOR"); ok {
84+
if b, err := strconv.ParseBool(v); err == nil {
85+
envNoColor = b
86+
}
87+
}
88+
7889
flag.BoolVar(&noColorFlag, "nocolor", envNoColor, "turns off colors")
7990
}
8091

0 commit comments

Comments
 (0)