|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + |
4 | 6 | "github.com/mattn/go-isatty" |
5 | 7 | "github.com/mgutz/ansi" |
6 | | - "os" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | func makeColorFunc(color string) func(string) string { |
| 11 | + cf := ansi.ColorFunc(color) |
10 | 12 | return func(arg string) string { |
11 | | - output := arg |
12 | 13 | if isatty.IsTerminal(os.Stdout.Fd()) { |
13 | | - output = ansi.Color(color+arg+ansi.Reset, "") |
| 14 | + return cf(arg) |
14 | 15 | } |
15 | | - |
16 | | - return output |
| 16 | + return arg |
17 | 17 | } |
18 | 18 | } |
19 | 19 |
|
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