Skip to content

Commit 9a3b032

Browse files
authored
Merge pull request cli#126 from github/cygwin-color
Fix color output to Git Bash
2 parents 15b76ce + a045895 commit 9a3b032

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

utils/color.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
1325
func NewColorable(f *os.File) io.Writer {
1426
return colorable.NewColorable(f)
@@ -17,18 +29,33 @@ func NewColorable(f *os.File) io.Writer {
1729
func 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
2740
var Magenta = makeColorFunc("magenta")
41+
42+
// Cyan outputs ANSI color if stdout is a tty
2843
var Cyan = makeColorFunc("cyan")
44+
45+
// Red outputs ANSI color if stdout is a tty
2946
var Red = makeColorFunc("red")
47+
48+
// Yellow outputs ANSI color if stdout is a tty
3049
var Yellow = makeColorFunc("yellow")
50+
51+
// Blue outputs ANSI color if stdout is a tty
3152
var Blue = makeColorFunc("blue")
53+
54+
// Green outputs ANSI color if stdout is a tty
3255
var Green = makeColorFunc("green")
56+
57+
// Gray outputs ANSI color if stdout is a tty
3358
var Gray = makeColorFunc("black+h")
59+
60+
// Bold outputs ANSI color if stdout is a tty
3461
var Bold = makeColorFunc("default+b")

0 commit comments

Comments
 (0)