From 8e3ccd4921aa66914a33b3118a5b2035ab8dc319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Sun, 13 Nov 2022 10:58:32 +0100 Subject: [PATCH 1/2] NO_COLOR requires a non-empty string --- README.md | 12 +++++------- color.go | 11 +++++------ color_test.go | 3 +-- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5152bf5..d69cd36 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ suits you. ![Color](https://user-images.githubusercontent.com/438920/96832689-03b3e000-13f4-11eb-9803-46f4c4de3406.jpg) - ## Install ```bash @@ -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 programatically 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") @@ -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 diff --git a/color.go b/color.go index 8738b37..8646409 100644 --- a/color.go +++ b/color.go @@ -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 @@ -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. @@ -120,7 +119,7 @@ func New(value ...Attribute) *Color { params: make([]Attribute, 0), } - if noColorExists() { + if noColorIsSet() { c.noColor = boolPtr(true) } diff --git a/color_test.go b/color_test.go index e045dbf..81255b6 100644 --- a/color_test.go +++ b/color_test.go @@ -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") }) @@ -197,7 +197,6 @@ func TestNoColor_Env(t *testing.T) { t.Errorf("Expecting %s, got '%s'\n", c.text, line) } } - } func TestColorVisual(t *testing.T) { From b06fcbf502c5690ec9345cc1e13169d25bcda87c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Sun, 13 Nov 2022 11:13:42 +0100 Subject: [PATCH 2/2] Add Test_noColorIsSet --- color_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/color_test.go b/color_test.go index 81255b6..e22cb78 100644 --- a/color_test.go +++ b/color_test.go @@ -199,6 +199,41 @@ func TestNoColor_Env(t *testing.T) { } } +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) { // First Visual Test Output = colorable.NewColorableStdout()