Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO_COLOR requires a non-empty string #171

Merged
merged 3 commits into from Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions README.md
Expand Up @@ -7,7 +7,6 @@ suits you.

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


## Install

```bash
Expand Down Expand Up @@ -124,17 +123,17 @@ fmt.Println("All text will now be bold magenta.")
```

### Disable/Enable color

There might be a case where you want to explicitly disable/enable color output. the
`go-isatty` package will automatically disable color output for non-tty output streams
(for example if the output were piped directly to `less`).

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

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

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


## Credits

* [Fatih Arslan](https://github.com/fatih)
* Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)
* [Fatih Arslan](https://github.com/fatih)
* Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)

## License

Expand Down
11 changes: 5 additions & 6 deletions color.go
Expand Up @@ -19,7 +19,7 @@ var (
// set (regardless of its value). This is a global option and affects all
// colors. For more control over each color block use the methods
// DisableColor() individually.
NoColor = noColorExists() || os.Getenv("TERM") == "dumb" ||
NoColor = noColorIsSet() || os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))

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

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

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

if noColorExists() {
if noColorIsSet() {
c.noColor = boolPtr(true)
}

Expand Down
36 changes: 35 additions & 1 deletion color_test.go
Expand Up @@ -183,7 +183,7 @@ func TestNoColor_Env(t *testing.T) {
{text: "hwhite", code: FgHiWhite},
}

os.Setenv("NO_COLOR", "")
os.Setenv("NO_COLOR", "1")
t.Cleanup(func() {
os.Unsetenv("NO_COLOR")
})
Expand All @@ -197,7 +197,41 @@ func TestNoColor_Env(t *testing.T) {
t.Errorf("Expecting %s, got '%s'\n", c.text, line)
}
}
}

func Test_noColorIsSet(t *testing.T) {
tests := []struct {
name string
act func()
want bool
}{
{
name: "default",
act: func() {},
want: false,
},
{
name: "NO_COLOR=1",
act: func() { os.Setenv("NO_COLOR", "1") },
want: true,
},
{
name: "NO_COLOR=",
act: func() { os.Setenv("NO_COLOR", "") },
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Cleanup(func() {
os.Unsetenv("NO_COLOR")
})
tt.act()
if got := noColorIsSet(); got != tt.want {
t.Errorf("noColorIsSet() = %v, want %v", got, tt.want)
}
})
}
}

func TestColorVisual(t *testing.T) {
Expand Down