From 21e445beab5b0866417547db6ac3b3ebcbff31e7 Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Thu, 2 Apr 2020 20:02:23 -0400 Subject: [PATCH] Support NO_COLOR environment variable to disable color. --- utils/color.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/utils/color.go b/utils/color.go index 0a002588f..46629ef8d 100644 --- a/utils/color.go +++ b/utils/color.go @@ -9,8 +9,10 @@ import ( "github.com/mgutz/ansi" ) +var _isColorEnabled = true var _isStdoutTerminal = false var checkedTerminal = false +var checkedNoColor = false func isStdoutTerminal() bool { if !checkedTerminal { @@ -33,13 +35,21 @@ func NewColorable(f *os.File) io.Writer { func makeColorFunc(color string) func(string) string { cf := ansi.ColorFunc(color) return func(arg string) string { - if isStdoutTerminal() { + if isColorEnabled() && isStdoutTerminal() { return cf(arg) } return arg } } +func isColorEnabled() bool { + if !checkedNoColor { + _isColorEnabled = os.Getenv("NO_COLOR") != "1" + checkedNoColor = true + } + return _isColorEnabled +} + // Magenta outputs ANSI color if stdout is a tty var Magenta = makeColorFunc("magenta")