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

added support for NO_COLOR #586

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ func needsQuote(s string) bool {

// colorize returns the string s wrapped in ANSI code c, unless disabled is true.
func colorize(s interface{}, c int, disabled bool) string {
e := os.Getenv("NO_COLOR")
if e == "true" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GNU libtextstyle doc says:

The environment variable NO_COLOR can be used to suppress styling in the textual output. When this environment variable is set (to any value), libtextstyle-enabled programs will not emit colors and other text styling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh you are right, fixed the code now.

disabled = true
}

if disabled {
return fmt.Sprintf("%s", s)
}
Expand Down
38 changes: 38 additions & 0 deletions console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ func TestConsoleWriter(t *testing.T) {
}
})

t.Run("NO_COLOR = true", func(t *testing.T) {
os.Setenv("NO_COLOR", "true")

buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf}

_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "<nil> WRN Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
os.Unsetenv("NO_COLOR")
})

t.Run("NO_COLOR = false", func(t *testing.T) {
os.Setenv("NO_COLOR", "false")

buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf}

_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
os.Unsetenv("NO_COLOR")
})

t.Run("Write fields", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
Expand Down
Loading