/
color.go
executable file
·206 lines (173 loc) · 4.08 KB
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package iostreams
import (
"fmt"
"strconv"
"strings"
"github.com/mgutz/ansi"
)
var (
magenta = ansi.ColorFunc("magenta")
cyan = ansi.ColorFunc("cyan")
red = ansi.ColorFunc("red")
yellow = ansi.ColorFunc("yellow")
blue = ansi.ColorFunc("blue")
green = ansi.ColorFunc("green")
gray = ansi.ColorFunc("black+h")
bold = ansi.ColorFunc("default+b")
cyanBold = ansi.ColorFunc("cyan+b")
gray256 = func(t string) string {
return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[m", 38, 242, t)
}
)
func NewColorScheme(enabled, is256enabled bool, trueColor bool) *ColorScheme {
return &ColorScheme{
enabled: enabled,
is256enabled: is256enabled,
hasTrueColor: trueColor,
}
}
type ColorScheme struct {
enabled bool
is256enabled bool
hasTrueColor bool
}
func (c *ColorScheme) Bold(t string) string {
if !c.enabled {
return t
}
return bold(t)
}
func (c *ColorScheme) Boldf(t string, args ...interface{}) string {
return c.Bold(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) Red(t string) string {
if !c.enabled {
return t
}
return red(t)
}
func (c *ColorScheme) Redf(t string, args ...interface{}) string {
return c.Red(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) Yellow(t string) string {
if !c.enabled {
return t
}
return yellow(t)
}
func (c *ColorScheme) Yellowf(t string, args ...interface{}) string {
return c.Yellow(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) Green(t string) string {
if !c.enabled {
return t
}
return green(t)
}
func (c *ColorScheme) Greenf(t string, args ...interface{}) string {
return c.Green(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) Gray(t string) string {
if !c.enabled {
return t
}
if c.is256enabled {
return gray256(t)
}
return gray(t)
}
func (c *ColorScheme) Grayf(t string, args ...interface{}) string {
return c.Gray(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) Magenta(t string) string {
if !c.enabled {
return t
}
return magenta(t)
}
func (c *ColorScheme) Magentaf(t string, args ...interface{}) string {
return c.Magenta(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) Cyan(t string) string {
if !c.enabled {
return t
}
return cyan(t)
}
func (c *ColorScheme) Cyanf(t string, args ...interface{}) string {
return c.Cyan(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) CyanBold(t string) string {
if !c.enabled {
return t
}
return cyanBold(t)
}
func (c *ColorScheme) Blue(t string) string {
if !c.enabled {
return t
}
return blue(t)
}
func (c *ColorScheme) Bluef(t string, args ...interface{}) string {
return c.Blue(fmt.Sprintf(t, args...))
}
func (c *ColorScheme) SuccessIcon() string {
return c.SuccessIconWithColor(c.Green)
}
func (c *ColorScheme) SuccessIconWithColor(colo func(string) string) string {
return colo("✓")
}
func (c *ColorScheme) WarningIcon() string {
return c.Yellow("!")
}
func (c *ColorScheme) FailureIcon() string {
return c.FailureIconWithColor(c.Red)
}
func (c *ColorScheme) FailureIconWithColor(colo func(string) string) string {
return colo("X")
}
func (c *ColorScheme) ColorFromString(s string) func(string) string {
s = strings.ToLower(s)
var fn func(string) string
switch s {
case "bold":
fn = c.Bold
case "red":
fn = c.Red
case "yellow":
fn = c.Yellow
case "green":
fn = c.Green
case "gray":
fn = c.Gray
case "magenta":
fn = c.Magenta
case "cyan":
fn = c.Cyan
case "blue":
fn = c.Blue
default:
fn = func(s string) string {
return s
}
}
return fn
}
// ColorFromRGB returns a function suitable for TablePrinter.AddField
// that calls HexToRGB, coloring text if supported by the terminal.
func (c *ColorScheme) ColorFromRGB(hex string) func(string) string {
return func(s string) string {
return c.HexToRGB(hex, s)
}
}
// HexToRGB uses the given hex to color x if supported by the terminal.
func (c *ColorScheme) HexToRGB(hex string, x string) string {
if !c.enabled || !c.hasTrueColor || len(hex) != 6 {
return x
}
r, _ := strconv.ParseInt(hex[0:2], 16, 64)
g, _ := strconv.ParseInt(hex[2:4], 16, 64)
b, _ := strconv.ParseInt(hex[4:6], 16, 64)
return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, x)
}