-
Notifications
You must be signed in to change notification settings - Fork 84
/
tag.go
343 lines (296 loc) · 8.02 KB
/
tag.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package color
import (
"fmt"
"io"
"regexp"
"strings"
)
// output colored text like use html tag. (not support windows cmd)
const (
// Regex to match color tags
// golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+)
// MatchExpr = `<([a-z=;]+)>(.*?)<\/\1>`
// 所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>"
// 支持自定义颜色属性的tag "<fg=white;bg=blue;op=bold>content</>"
// (?s:...) s - 让 "." 匹配换行
MatchExpr = `<([a-zA-Z_=,;]+)>(?s:(.*?))<\/>`
// Regex to match color attributes
AttrExpr = `(fg|bg|op)=([a-z,]+);?`
// Regex used for removing color tags
// StripExpr = `<[\/]?[a-zA-Z=;]+>`
// 随着上面的做一些调整
StripExpr = `<[\/]?[a-zA-Z_=,;]*>`
)
var (
matchRegex = regexp.MustCompile(MatchExpr)
stripRegex = regexp.MustCompile(StripExpr)
)
// Some internal defined color tags
// usage: <tag>content text</>
// @notice 加 0 在前面是为了防止之前的影响到现在的设置
var colorTags = map[string]string{
// basic tags,
"red": "0;31",
"blue": "0;34",
"cyan": "0;36",
"black": "0;30",
"green": "0;32",
"white": "1;37",
"default": "39", // no color
"normal": "39", // no color
"brown": "0;33",
"yellow": "1;33",
"magenta": "0;35",
"magentaB": "1;35", // add bold
// alert tags, like bootstrap's alert
"suc": "1;32", // same "green" and "bold"
"success": "1;32",
"info": "0;32", // same "green",
"comment": "0;33", // same "brown"
"note": "36;1",
"notice": "36;4",
"warn": "0;1;33",
"warning": "0;30;43",
"primary": "0;34",
"danger": "1;31", // same "red" but add bold
"err": "97;41",
"error": "97;41", // fg light white; bg red
// more tags
"lightRed": "1;31",
"light_red": "1;31",
"lightGreen": "1;32",
"light_green": "1;32",
"lightBlue": "1;34",
"light_blue": "1;34",
"lightCyan": "1;36",
"light_cyan": "1;36",
"lightDray": "0;37",
"light_gray": "0;37",
"gray": "0;90",
"darkGray": "0;90",
"dark_gray": "0;90",
"lightYellow": "0;93",
"light_yellow": "0;93",
"lightMagenta": "0;95",
"light_magenta": "0;95",
// extra
"lightRedEx": "0;91",
"light_red_ex": "0;91",
"lightGreenEx": "0;92",
"light_green_ex": "0;92",
"lightBlueEx": "0;94",
"light_blue_ex": "0;94",
"lightCyanEx": "0;96",
"light_cyan_ex": "0;96",
"whiteEx": "0;97",
"white_ex": "0;97",
// option
"bold": "1",
"underscore": "4",
"reverse": "7",
}
// Foreground colors map
var FgColors = map[string]Color{
"black": FgBlack,
"red": FgRed,
"green": FgGreen,
"yellow": FgYellow,
"blue": FgBlue,
"magenta": FgMagenta,
"cyan": FgCyan,
"white": FgWhite,
"default": FgDefault,
}
// Background colors map
var BgColors = map[string]Color{
"black": BgBlack,
"red": BgRed,
"green": BgGreen,
"yellow": BgYellow,
"blue": BgBlue,
"magenta": BgMagenta,
"cyan": BgCyan,
"white": BgWhite,
"default": BgDefault,
}
// color options map
var Options = map[string]Color{
"reset": OpReset,
"bold": OpBold,
"fuzzy": OpFuzzy,
"italic": OpItalic,
"underscore": OpUnderscore,
"blink": OpBlink,
"reverse": OpReverse,
"concealed": OpConcealed,
}
// ApplyTag for messages
func ApplyTag(tag string, args ...interface{}) string {
return buildColoredText(GetStyleCode(tag), args...)
}
// Print messages
func Print(args ...interface{}) (int, error) {
return fmt.Print(Render(args...))
}
// Printf format and print messages
func Printf(format string, args ...interface{}) (int, error) {
return fmt.Print(Render(fmt.Sprintf(format, args...)))
}
// Println messages line
func Println(args ...interface{}) (int, error) {
return fmt.Println(Render(args...))
}
// Fprint print rendered messages to writer
func Fprint(w io.Writer, args ...interface{}) (int, error) {
return fmt.Fprint(w, Render(args...))
}
// Fprintf print format and rendered messages to writer
func Fprintf(w io.Writer, format string, args ...interface{}) (int, error) {
return fmt.Fprint(w, String(fmt.Sprintf(format, args...)))
}
// Fprintln print rendered messages line to writer
func Fprintln(w io.Writer, args ...interface{}) (int, error) {
return fmt.Fprintln(w, Render(args...))
}
// Render return rendered string
func Render(args ...interface{}) string {
return ReplaceTag(fmt.Sprint(args...))
}
// Sprint return rendered string
func Sprint(args ...interface{}) string {
return Render(args...)
}
// Sprintf format and return rendered string
func Sprintf(format string, args ...interface{}) string {
return String(fmt.Sprintf(format, args...))
}
// String alias of the ReplaceTag
func String(str string) string {
return ReplaceTag(str)
}
// RenderStr alias of the ReplaceTag
func RenderStr(str string) string {
return ReplaceTag(str)
}
// ReplaceTag parse string, replace tag and return rendered string
func ReplaceTag(str string, dumpIt ...bool) string {
// not contains color tag
if !strings.Contains(str, "<") {
return str
}
// disable color OR not support color render
if !Enable || isLikeInCmd {
return ClearTag(str)
}
// find color tags by regex
matched := matchRegex.FindAllStringSubmatch(str, -1)
// item: 0 full text 1 tag name 2 tag content
for _, item := range matched {
full, tag, content := item[0], item[1], item[2]
// fmt.Printf("full: %s tag: %s, tag content:%s old: %s \n", full, tag, content)
// custom color in tag: "<fg=white;bg=blue;op=bold>content</>"
if code := ParseCodeFromAttr(tag); len(code) > 0 {
now := buildColoredText(code, content)
str = strings.Replace(str, full, now, 1)
continue
}
// use defined tag: "<tag>content</>"
if code := GetStyleCode(tag); len(code) > 0 {
now := buildColoredText(code, content)
// old := WrapTag(content, tag) is equals to var 'full'
str = strings.Replace(str, full, now, 1)
}
}
return str
}
// ParseCodeFromAttr parse color attributes.
// attr like:
// "fg=VALUE;bg=VALUE;op=VALUE", VALUE please see var: FgColors, BgColors, Options
// eg:
// "fg=yellow"
// "bg=red"
// "op=bold,underscore" option is allow multi value
// "fg=white;bg=blue;op=bold"
// "fg=white;op=bold,underscore"
func ParseCodeFromAttr(attr string) (code string) {
if !strings.Contains(attr, "=") {
return
}
attr = strings.Trim(attr, ";=,")
if len(attr) == 0 {
return
}
var colors []Color
reg := regexp.MustCompile(`(fg|bg|op)=([a-z,]+);?`)
matched := reg.FindAllStringSubmatch(attr, -1)
// fmt.Printf("matched %+v\n", matched)
for _, item := range matched {
pos, val := item[1], item[2]
switch pos {
case "fg":
if c, ok := FgColors[val]; ok {
colors = append(colors, c)
}
case "bg":
if c, ok := BgColors[val]; ok {
colors = append(colors, c)
}
case "op": // options allow multi value
if !strings.Contains(val, ",") {
ns := strings.Split(val, ",")
for _, n := range ns {
if c, ok := Options[n]; ok {
colors = append(colors, c)
}
}
} else if c, ok := Options[val]; ok {
colors = append(colors, c)
}
}
// fmt.Printf("pos: %s, val: %s\n", pos, val)
}
return buildColorCode(colors...)
}
// GetStyleCode get color code by tag name
func GetStyleCode(name string) string {
if code, ok := colorTags[name]; ok {
return code
}
return ""
}
// WrapTag wrap a tag for a string "<tag>content</>"
func WrapTag(str string, tag string) string {
// return fmt.Sprintf("<%s>%s</%s>", tag, str, tag)
return fmt.Sprintf("<%s>%s</>", tag, str)
}
// ClearTag clear all tag for a string
func ClearTag(str string) string {
if !strings.Contains(str, "<") {
return str
}
return stripRegex.ReplaceAllString(str, "")
}
// GetColorTags
func GetColorTags() map[string]string {
return colorTags
}
// IsDefinedTag is defined tag name
func IsDefinedTag(name string) bool {
_, ok := colorTags[name]
return ok
}
// IsFgColor
func IsFgColor(name string) bool {
_, ok := FgColors[name]
return ok
}
// IsBgColor
func IsBgColor(name string) bool {
_, ok := BgColors[name]
return ok
}
// IsOption
func IsOption(name string) bool {
_, ok := Options[name]
return ok
}