-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac65c2
commit 8dc517b
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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. | ||
Aurora = auroraLib.NewAurora(EnableColor) | ||
} |
File renamed without changes.