-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcons_color.go
213 lines (191 loc) · 4.71 KB
/
cons_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
207
208
209
210
211
212
213
package cons
import (
"fmt"
"regexp"
"strings"
)
const (
Start = "\033["
Reset = "\033[0m"
)
var (
plain = false
colors = map[string][2]string{
"k": {"30", "40"}, // black
"r": {"31", "41"}, // red
"g": {"32", "42"}, // green
"y": {"33", "43"}, // yellow
"b": {"34", "44"}, // blue
"m": {"35", "45"}, // magenta
"c": {"36", "46"}, // cyan
"w": {"37", "47"}, // white
}
styles = map[string]string{
"b": "1", // bold
"k": "5", // blink
"u": "4", // underline
"i": "3", // italic
"v": "7", // inverted
"f": "51", // framed
}
// Three diagnostics set after each call to Color()
// Tracers[0] is three indexes used in parsing the arg to Color():
// the first +, the :, and the second +
// Tracers[1] gives the parsed colors and styles as foreground color, style, background color, style
// Tracers[2] gives the final ansi control string
Tracers [3]string
)
func append_code(base string, code string) string {
if len(code) == 0 {
return base
}
appended := base
if len(appended) > 0 {
appended += ";"
}
appended += code
return appended
}
type style_spec struct {
fg_color string
fg_style string
bg_color string
bg_style string
}
func (spec style_spec) String() string {
var result string
result = append_code(result, spec.fg_color)
result = append_code(result, spec.fg_style)
result = append_code(result, spec.bg_color)
//result = append_code(result, spec.bg_style)
return result
}
func IsValid(s string) bool {
rx := []string{"(?:^[krgbymcw]$)", "(?:^[bkuivf]$)"}
rxIx := 0
for ix, roon := range s {
switch roon {
case '.', '-', '_':
continue
}
if ix == 1 {
rxIx = 1
} else {
rxIx = 0
}
match, err := regexp.MatchString(rx[rxIx], string(roon))
if err != nil || !match {
return false
}
}
return true
}
// ColorCode returns the ansi control chars for coloring and styling console text
// code should be in UTF-8
func ColorCode(code string) string {
code = strings.ToLower(code)
if plain || code == "" || code == "reset" {
return Reset
}
var spec style_spec
var ptrs = []*string{&spec.fg_color, &spec.fg_style, &spec.bg_color, &spec.bg_style}
ptrIx := 0
for _, roon := range code {
switch roon {
case ':':
ptrIx = 2
case 'v', 'i', 'b', 'u', 'k', 'f', 'y', 'w', 'r', 'g', 'm', 'c':
*ptrs[ptrIx] = string(roon)
ptrIx++
case '+':
// do nothing, for backwards compatibility
default: // i.e., +, _, -, .
ptrIx++
}
}
Tracers[1] = fmt.Sprintf("%s %s %s %s",
spec.fg_color, spec.fg_style, spec.bg_color, spec.bg_style)
// now map to the real styles
spec.fg_color = colors[spec.fg_color][0]
spec.fg_style = styles[spec.fg_style]
spec.bg_color = colors[spec.bg_color][1]
spec.bg_style = styles[spec.bg_style]
// get the ansi codes without the start and end escapes
// (useful for printing the codes without sending escapes)
Tracers[2] = fmt.Sprintf("%s", spec)
if len(spec.String()) == 0 {
return ""
}
return fmt.Sprintf("%s%sm", Start, spec)
}
func Code2Ansi(code string) string {
code = strings.ToLower(code)
if plain || code == "" {
return ""
}
return "" // TODO
}
func resetIfNeeded(code string) string {
if len(code) > 0 {
return Reset
}
return ""
}
// Color(s, style) Surrounds `s` with ANSI color and reset code.
// deprecated, use Style instead
func Color(s, style string) string {
code := ColorCode(style)
return code + s + resetIfNeeded(code)
}
func Style(style string, s string) string {
code := ColorCode(style)
return code + s + resetIfNeeded(code)
}
// if flag is false, returns s unchanged
func StyleIf(style string, s string, flag bool) string {
if flag {
return Style(style, s)
}
return s
}
// ColorFunc Creates a fast closure.
//
// Prefer ColorFunc over Style, Printf etc. if you are concerned about performance
// as it does not recompute ANSI codes.
func ColorFunc(style string) func(string) string {
if style == "" {
return func(s string) string {
return s
}
} else {
code := ColorCode(style)
return func(s string) string {
return code + s + resetIfNeeded(code)
}
}
}
// convenience function vars
var ShowRed = ColorFunc("r")
var ShowBlue = ColorFunc("b")
var ShowYellow = ColorFunc("y")
var ShowMagenta = ColorFunc("m")
var ShowGreen = ColorFunc("g")
var ShowCyan = ColorFunc("c")
// DisableColors disables ANSI color codes. On by default.
func DisableColors(disable bool) {
plain = disable
}
// convenience function
func Prints(style, text string) {
fmt.Print(Style(style, text))
}
// convenience function
func Printfs(style, format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
fmt.Printf("%s", Style(style, s))
}
// convenience function
func Sprintfs(style, format string, args ...interface{}) string {
s := fmt.Sprintf(format, args...)
return Style(style, s)
}