Skip to content

Commit 24b04b5

Browse files
committed
Make use of ansi.ColorFunc
Speed up repeated calls to color functions by using ansi.ColorFunc to create a closure per each color. https://godoc.org/github.com/mgutz/ansi#ColorFunc
1 parent 07df89f commit 24b04b5

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

utils/color.go

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
11
package utils
22

33
import (
4+
"os"
5+
46
"github.com/mattn/go-isatty"
57
"github.com/mgutz/ansi"
6-
"os"
78
)
89

910
func makeColorFunc(color string) func(string) string {
11+
cf := ansi.ColorFunc(color)
1012
return func(arg string) string {
11-
output := arg
1213
if isatty.IsTerminal(os.Stdout.Fd()) {
13-
output = ansi.Color(color+arg+ansi.Reset, "")
14+
return cf(arg)
1415
}
15-
16-
return output
16+
return arg
1717
}
1818
}
1919

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

0 commit comments

Comments
 (0)