Support NO_COLOR environment variable to disable color.
This commit is contained in:
parent
e066e98a9a
commit
21e445beab
1 changed files with 11 additions and 1 deletions
|
|
@ -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")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue