-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
color.go
47 lines (41 loc) · 1.65 KB
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//go:build !windows
package color
import (
"os"
auroraLib "github.com/logrusorgru/aurora"
"github.com/mattn/go-isatty"
)
// We're using a couple of libraries that produce ANSI escape sequences to print
// colored text:
//
// * aurora
// * zerolog
//
// Ideally, both of these libraries would support the standard NO_COLOR
// environment variable, but they don't, so we need to handle it ourselves.
// Fortunately, both libraries include an option to disable colors, so we can
// check for NO_COLOR ourselves and disable color manually if needed.
//
// Another consideration is that not all terminal environments support ANSI
// escape codes (the Windows 7 CMD terminal doesn't, for example). So, we try to
// detect that scenario and disable colors. It would be good to do this in a
// more robust way, something like checking the terminfo/termcap capabilities of
// the terminal to see if it's capable of interpreting ANSI escape sequences,
// but that seems complex and error prone and I'm not really sure that it's
// worth it. So I'm taking a more basic approach here, where we just check to
// see if the terminal is a TTY and assume that if it is a TTY, then it can
// probably interpret ANSI escape sequences.
//
// Reference:
// https://github.com/logrusorgru/aurora/issues/2
// https://eklitzke.org/ansi-color-codes
var EnableColor = isatty.IsTerminal(os.Stdout.Fd()) &&
len(os.Getenv("NO_COLOR")) == 0
var Aurora auroraLib.Aurora
func init() {
// HACK: Ideally, aurora would support NO_COLOR, but at least they give us a
// config option so that we can disable color manually.
//
// See the longer comment above EnableColor.
Aurora = auroraLib.NewAurora(EnableColor)
}