Support NO_COLOR environment variable to disable color.

This commit is contained in:
Brett Buddin 2020-04-02 20:02:23 -04:00
parent e066e98a9a
commit 21e445beab
No known key found for this signature in database
GPG key ID: C51265E441C4C5AC

View file

@ -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")