Skip to content

Commit

Permalink
Support NO_COLOR (https://no-color.org).
Browse files Browse the repository at this point in the history
Also, handle `IS_NO_COLOR=1` and `IS_NO_COLOR=FALSE`.
  • Loading branch information
AlekSi committed Dec 14, 2021
1 parent fb18f75 commit 1fd8679
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ func Test(t *testing.T) {

## Color

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

```
go test -nocolor
```

```
IS_NO_COLOR=true go test
NO_COLOR=1 go test
```
17 changes: 14 additions & 3 deletions is.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// the helper methods:
//
// func Test(t *testing.T) {
//
// // always start tests with this
// is := is.New(t)
//
Expand All @@ -32,7 +31,6 @@
//
// body := readBody(r)
// is.True(strings.Contains(body, "Hi there"))
//
// }
package is

Expand All @@ -45,6 +43,7 @@ import (
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)

Expand Down Expand Up @@ -74,7 +73,19 @@ type I struct {
var noColorFlag bool

func init() {
envNoColor := os.Getenv("IS_NO_COLOR") == "true"
var envNoColor bool

// prefer https://no-color.org (with any value)
if _, ok := os.LookupEnv("NO_COLOR"); ok {
envNoColor = true
}

if v, ok := os.LookupEnv("IS_NO_COLOR"); ok {
if b, err := strconv.ParseBool(v); err == nil {
envNoColor = b
}
}

flag.BoolVar(&noColorFlag, "nocolor", envNoColor, "turns off colors")
}

Expand Down

0 comments on commit 1fd8679

Please sign in to comment.