Skip to content

Commit 15b76ce

Browse files
authored
Merge pull request cli#125 from github/ansi-colorfunc
Make use of ansi.ColorFunc
2 parents 64dcdd6 + ebb7f39 commit 15b76ce

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

utils/color.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,20 @@ func NewColorable(f *os.File) io.Writer {
1515
}
1616

1717
func makeColorFunc(color string) func(string) string {
18+
cf := ansi.ColorFunc(color)
1819
return func(arg string) string {
19-
output := arg
2020
if isatty.IsTerminal(os.Stdout.Fd()) {
21-
output = ansi.Color(color+arg+ansi.Reset, "")
21+
return cf(arg)
2222
}
23-
24-
return output
23+
return arg
2524
}
2625
}
2726

28-
var Black = makeColorFunc(ansi.Black)
29-
var White = makeColorFunc(ansi.White)
30-
var Magenta = makeColorFunc(ansi.Magenta)
31-
var Cyan = makeColorFunc(ansi.Cyan)
32-
var Red = makeColorFunc(ansi.Red)
33-
var Yellow = makeColorFunc(ansi.Yellow)
34-
var Blue = makeColorFunc(ansi.Blue)
35-
var Green = makeColorFunc(ansi.Green)
36-
var Gray = makeColorFunc(ansi.LightBlack)
37-
38-
func Bold(arg string) string {
39-
output := arg
40-
if isatty.IsTerminal(os.Stdout.Fd()) {
41-
// This is really annoying. If you just define Bold as ColorFunc("+b") it will properly bold but
42-
// will not use the default color, resulting in black and probably unreadable text. This forces
43-
// the default color before bolding.
44-
output = ansi.Color(ansi.DefaultFG+arg+ansi.Reset, "+b")
45-
}
46-
return output
47-
}
27+
var Magenta = makeColorFunc("magenta")
28+
var Cyan = makeColorFunc("cyan")
29+
var Red = makeColorFunc("red")
30+
var Yellow = makeColorFunc("yellow")
31+
var Blue = makeColorFunc("blue")
32+
var Green = makeColorFunc("green")
33+
var Gray = makeColorFunc("black+h")
34+
var Bold = makeColorFunc("default+b")

0 commit comments

Comments
 (0)