@@ -9,6 +9,18 @@ import (
99 "github.com/mgutz/ansi"
1010)
1111
12+ var _isStdoutTerminal = false
13+ var checkedTerminal = false
14+
15+ func isStdoutTerminal () bool {
16+ if ! checkedTerminal {
17+ fd := os .Stdout .Fd ()
18+ _isStdoutTerminal = isatty .IsTerminal (fd ) || isatty .IsCygwinTerminal (fd )
19+ checkedTerminal = true
20+ }
21+ return _isStdoutTerminal
22+ }
23+
1224// NewColorable returns an output stream that handles ANSI color sequences on Windows
1325func NewColorable (f * os.File ) io.Writer {
1426 return colorable .NewColorable (f )
@@ -17,18 +29,33 @@ func NewColorable(f *os.File) io.Writer {
1729func makeColorFunc (color string ) func (string ) string {
1830 cf := ansi .ColorFunc (color )
1931 return func (arg string ) string {
20- if isatty . IsTerminal ( os . Stdout . Fd () ) {
32+ if isStdoutTerminal ( ) {
2133 return cf (arg )
2234 }
2335 return arg
2436 }
2537}
2638
39+ // Magenta outputs ANSI color if stdout is a tty
2740var Magenta = makeColorFunc ("magenta" )
41+
42+ // Cyan outputs ANSI color if stdout is a tty
2843var Cyan = makeColorFunc ("cyan" )
44+
45+ // Red outputs ANSI color if stdout is a tty
2946var Red = makeColorFunc ("red" )
47+
48+ // Yellow outputs ANSI color if stdout is a tty
3049var Yellow = makeColorFunc ("yellow" )
50+
51+ // Blue outputs ANSI color if stdout is a tty
3152var Blue = makeColorFunc ("blue" )
53+
54+ // Green outputs ANSI color if stdout is a tty
3255var Green = makeColorFunc ("green" )
56+
57+ // Gray outputs ANSI color if stdout is a tty
3358var Gray = makeColorFunc ("black+h" )
59+
60+ // Bold outputs ANSI color if stdout is a tty
3461var Bold = makeColorFunc ("default+b" )
0 commit comments