38
38
* internal defined color tags
39
39
*************************************************************/
40
40
41
- // There are internal defined color tags
42
- // Usage: <tag>content text</>
41
+ // There are internal defined fg color tags
42
+ //
43
+ // Usage:
44
+ // <tag>content text</>
45
+ //
43
46
// @notice 加 0 在前面是为了防止之前的影响到现在的设置
44
47
var colorTags = map [string ]string {
45
48
// basic tags
@@ -72,7 +75,9 @@ var colorTags = map[string]string{
72
75
"magenta" : "0;35" ,
73
76
"mga" : "0;35" , // short name
74
77
"magentaB" : "1;35" , // with bold
78
+ "magenta1" : "1;35" ,
75
79
"mgb" : "1;35" ,
80
+ "mga1" : "1;35" ,
76
81
"mgaB" : "1;35" ,
77
82
78
83
// light/hi tags
@@ -90,7 +95,7 @@ var colorTags = map[string]string{
90
95
"light_magenta" : "0;95" ,
91
96
"hiMagenta" : "0;95" ,
92
97
"hi_magenta" : "0;95" ,
93
- "lightMagentaB " : "1;95" , // with bold
98
+ "lightMagenta1 " : "1;95" , // with bold
94
99
"hiMagentaB" : "1;95" , // with bold
95
100
"hi_magenta_b" : "1;95" ,
96
101
"lightRed" : "0;91" ,
@@ -127,9 +132,14 @@ var colorTags = map[string]string{
127
132
// option
128
133
"bold" : "1" ,
129
134
"b" : "1" ,
135
+ "italic" : "3" ,
136
+ "i" : "3" , // italic
130
137
"underscore" : "4" ,
131
138
"us" : "4" , // short name for 'underscore'
139
+ "blink" : "5" ,
140
+ "fb" : "6" , // fast blink
132
141
"reverse" : "7" ,
142
+ "st" : "9" , // strikethrough
133
143
134
144
// alert tags, like bootstrap's alert
135
145
"suc" : "1;32" , // same "green" and "bold"
@@ -146,12 +156,141 @@ var colorTags = map[string]string{
146
156
"error" : "97;41" , // fg light white; bg red
147
157
}
148
158
159
+ /*************************************************************
160
+ * internal defined tag attributes
161
+ *************************************************************/
162
+
163
+ // built-in attributes for fg,bg 16-colors and op codes.
164
+ var (
165
+ attrFgs = map [string ]string {
166
+ // basic colors
167
+
168
+ "black" : FgBlack .Code (),
169
+ "red" : "31" ,
170
+ "green" : "32" ,
171
+ "brown" : "33" , // #A52A2A
172
+ "yellow" : "33" ,
173
+ "ylw" : "33" ,
174
+ "blue" : "34" ,
175
+ "cyan" : "36" ,
176
+ "magenta" : "35" ,
177
+ "mga" : "35" ,
178
+ "white" : FgWhite .Code (),
179
+ "default" : "39" , // no color
180
+ "normal" : "39" , // no color
181
+
182
+ // light/hi colors
183
+
184
+ "darkGray" : FgDarkGray .Code (),
185
+ "dark_gray" : "90" ,
186
+ "gray" : "90" ,
187
+ "lightYellow" : "93" ,
188
+ "light_yellow" : "93" ,
189
+ "hiYellow" : "93" ,
190
+ "hi_yellow" : "93" ,
191
+ "lightMagenta" : "95" ,
192
+ "light_magenta" : "95" ,
193
+ "hiMagenta" : "95" ,
194
+ "hi_magenta" : "95" ,
195
+ "hi_mga" : "95" ,
196
+ "lightRed" : "91" ,
197
+ "light_red" : "91" ,
198
+ "hiRed" : "91" ,
199
+ "hi_red" : "91" ,
200
+ "lightGreen" : "92" ,
201
+ "light_green" : "92" ,
202
+ "hiGreen" : "92" ,
203
+ "hi_green" : "92" ,
204
+ "lightBlue" : "94" ,
205
+ "light_blue" : "94" ,
206
+ "hiBlue" : "94" ,
207
+ "hi_blue" : "94" ,
208
+ "lightCyan" : "96" ,
209
+ "light_cyan" : "96" ,
210
+ "hiCyan" : "96" ,
211
+ "hi_cyan" : "96" ,
212
+ "lightWhite" : "97" ,
213
+ "light_white" : "97" ,
214
+ }
215
+
216
+ attrBgs = map [string ]string {
217
+ // basic colors
218
+
219
+ "black" : BgBlack .Code (),
220
+ "red" : "41" ,
221
+ "green" : "42" ,
222
+ "brown" : "43" , // #A52A2A
223
+ "yellow" : "43" ,
224
+ "ylw" : "43" ,
225
+ "blue" : "44" ,
226
+ "cyan" : "46" ,
227
+ "magenta" : "45" ,
228
+ "mga" : "45" ,
229
+ "white" : FgWhite .Code (),
230
+ "default" : "49" , // no color
231
+ "normal" : "49" , // no color
232
+
233
+ // light/hi colors
234
+
235
+ "darkGray" : BgDarkGray .Code (),
236
+ "dark_gray" : "100" ,
237
+ "gray" : "100" ,
238
+ "lightYellow" : "103" ,
239
+ "light_yellow" : "103" ,
240
+ "hiYellow" : "103" ,
241
+ "hi_yellow" : "103" ,
242
+ "lightMagenta" : "105" ,
243
+ "light_magenta" : "105" ,
244
+ "hiMagenta" : "105" ,
245
+ "hi_magenta" : "105" ,
246
+ "hi_mga" : "105" ,
247
+ "lightRed" : "101" ,
248
+ "light_red" : "101" ,
249
+ "hiRed" : "101" ,
250
+ "hi_red" : "101" ,
251
+ "lightGreen" : "102" ,
252
+ "light_green" : "102" ,
253
+ "hiGreen" : "102" ,
254
+ "hi_green" : "102" ,
255
+ "lightBlue" : "104" ,
256
+ "light_blue" : "104" ,
257
+ "hiBlue" : "104" ,
258
+ "hi_blue" : "104" ,
259
+ "lightCyan" : "106" ,
260
+ "light_cyan" : "106" ,
261
+ "hiCyan" : "106" ,
262
+ "hi_cyan" : "106" ,
263
+ "lightWhite" : BgLightWhite .Code (),
264
+ "light_white" : "107" ,
265
+ }
266
+
267
+ attrOpts = map [string ]string {
268
+ "reset" : OpReset .Code (),
269
+ "bold" : OpBold .Code (),
270
+ "b" : OpBold .Code (),
271
+ "fuzzy" : OpFuzzy .Code (),
272
+ "italic" : OpItalic .Code (),
273
+ "i" : OpItalic .Code (),
274
+ "underscore" : OpUnderscore .Code (),
275
+ "us" : OpUnderscore .Code (),
276
+ "u" : OpUnderscore .Code (),
277
+ "blink" : OpBlink .Code (),
278
+ "fastblink" : OpFastBlink .Code (),
279
+ "fb" : OpFastBlink .Code (),
280
+ "reverse" : OpReverse .Code (),
281
+ "concealed" : OpConcealed .Code (),
282
+ "strikethrough" : OpStrikethrough .Code (),
283
+ "st" : OpStrikethrough .Code (),
284
+ }
285
+ )
286
+
149
287
/*************************************************************
150
288
* parse color tags
151
289
*************************************************************/
152
290
153
291
var (
154
292
tagParser = TagParser {}
293
+ // regex for match color 256 code
155
294
rxNumStr = regexp .MustCompile ("^[0-9]{1,3}$" )
156
295
rxHexCode = regexp .MustCompile ("^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$" )
157
296
)
@@ -182,11 +321,18 @@ func (tp *TagParser) ParseByEnv(str string) string {
182
321
if ! Enable || ! SupportColor () {
183
322
return ClearTag (str )
184
323
}
185
-
186
324
return tp .Parse (str )
187
325
}
188
326
189
327
// Parse parse given string, replace color tag and return rendered string
328
+ //
329
+ // Use built in tags:
330
+ // <TAG_NAME>CONTENT</>
331
+ // // e.g: `<info>message</>`
332
+ //
333
+ // Custom tag attributes:
334
+ // `<fg=VALUE;bg=VALUE;op=VALUES>CONTENT</>`
335
+ // // e.g: `<fg=167;bg=232>wel</>`
190
336
func (tp * TagParser ) Parse (str string ) string {
191
337
// not contains color tag
192
338
if ! strings .Contains (str , "</>" ) {
@@ -198,14 +344,15 @@ func (tp *TagParser) Parse(str string) string {
198
344
199
345
// item: 0 full text 1 tag name 2 tag content
200
346
for _ , item := range matched {
201
- full , tag , content := item [0 ], item [1 ], item [2 ]
347
+ full , tag , body := item [0 ], item [1 ], item [2 ]
202
348
203
- // use defined tag name: "<info>content</>" -> tag: "info"
349
+ // use defined color tag name: "<info>content</>" -> tag: "info"
204
350
if ! strings .ContainsRune (tag , '=' ) {
205
- code := colorTags [tag ]
206
- if len (code ) > 0 {
207
- now := RenderString (code , content )
208
- // old := WrapTag(content, tag) is equals to var 'full'
351
+ if code := colorTags [tag ]; len (code ) > 0 {
352
+ str = strings .Replace (str , full , RenderString (code , body ), 1 )
353
+ } else if code , ok := namedRgbMap [tag ]; ok {
354
+ code = strings .Replace (code , "," , ";" , - 1 )
355
+ now := RenderString (FgRGBPfx + code , body )
209
356
str = strings .Replace (str , full , now , 1 )
210
357
}
211
358
continue
@@ -214,18 +361,13 @@ func (tp *TagParser) Parse(str string) string {
214
361
// custom color in tag
215
362
// - basic: "fg=white;bg=blue;op=bold"
216
363
if code := ParseCodeFromAttr (tag ); len (code ) > 0 {
217
- now := RenderString (code , content )
218
- str = strings .Replace (str , full , now , 1 )
364
+ str = strings .Replace (str , full , RenderString (code , body ), 1 )
219
365
}
220
366
}
221
367
222
368
return str
223
369
}
224
370
225
- // func (tp *TagParser) ParseAttr(attr string) (code string) {
226
- // return
227
- // }
228
-
229
371
// ReplaceTag parse string, replace color tag and return rendered string
230
372
func ReplaceTag (str string ) string {
231
373
return tagParser .ParseByEnv (str )
@@ -236,17 +378,20 @@ func ReplaceTag(str string) string {
236
378
// attr format:
237
379
// // VALUE please see var: FgColors, BgColors, AllOptions
238
380
// "fg=VALUE;bg=VALUE;op=VALUE"
381
+ //
239
382
// 16 color:
240
383
// "fg=yellow"
241
384
// "bg=red"
242
- // "op=bold,underscore" option is allow multi value
385
+ // "op=bold,underscore" // option is allow multi value
243
386
// "fg=white;bg=blue;op=bold"
244
387
// "fg=white;op=bold,underscore"
388
+ //
245
389
// 256 color:
246
390
// "fg=167"
247
391
// "fg=167;bg=23"
248
392
// "fg=167;bg=23;op=bold"
249
- // true color:
393
+ //
394
+ // True color:
250
395
// // hex
251
396
// "fg=fc1cac"
252
397
// "fg=fc1cac;bg=c2c3c4"
@@ -270,31 +415,27 @@ func ParseCodeFromAttr(attr string) (code string) {
270
415
pos , val := item [1 ], item [2 ]
271
416
switch pos {
272
417
case "fg" :
273
- if c , ok := FgColors [val ]; ok { // basic
274
- codes = append (codes , c .String ())
275
- } else if c , ok := ExFgColors [val ]; ok { // extra
276
- codes = append (codes , c .String ())
418
+ if code , ok := attrFgs [val ]; ok { // attr fg
419
+ codes = append (codes , code )
277
420
} else if code := rgbHex256toCode (val , false ); code != "" {
278
421
codes = append (codes , code )
279
422
}
280
423
case "bg" :
281
- if c , ok := BgColors [val ]; ok { // basic bg
282
- codes = append (codes , c .String ())
283
- } else if c , ok := ExBgColors [val ]; ok { // extra bg
284
- codes = append (codes , c .String ())
424
+ if code , ok := attrBgs [val ]; ok { // attr bg
425
+ codes = append (codes , code )
285
426
} else if code := rgbHex256toCode (val , true ); code != "" {
286
427
codes = append (codes , code )
287
428
}
288
429
case "op" : // options allow multi value
289
430
if strings .Contains (val , "," ) {
290
431
ns := strings .Split (val , "," )
291
432
for _ , n := range ns {
292
- if c , ok := AllOptions [n ]; ok {
293
- codes = append (codes , c . String () )
433
+ if code , ok := attrOpts [n ]; ok { // attr ops
434
+ codes = append (codes , code )
294
435
}
295
436
}
296
- } else if c , ok := AllOptions [val ]; ok {
297
- codes = append (codes , c . String () )
437
+ } else if code , ok := attrOpts [val ]; ok {
438
+ codes = append (codes , code )
298
439
}
299
440
}
300
441
}
@@ -327,7 +468,6 @@ func ClearTag(s string) string {
327
468
if ! strings .Contains (s , "</>" ) {
328
469
return s
329
470
}
330
-
331
471
return stripRegex .ReplaceAllString (s , "" )
332
472
}
333
473
@@ -350,7 +490,6 @@ func WrapTag(s string, tag string) string {
350
490
if s == "" || tag == "" {
351
491
return s
352
492
}
353
-
354
493
return fmt .Sprintf ("<%s>%s</>" , tag , s )
355
494
}
356
495
@@ -410,12 +549,7 @@ func (tg Tag) Println(a ...interface{}) {
410
549
411
550
// Sprint render messages
412
551
func (tg Tag ) Sprint (a ... interface {}) string {
413
- name := string (tg )
414
- // if stl := GetStyle(name); !stl.IsEmpty() {
415
- // return stl.Render(args...)
416
- // }
417
-
418
- return RenderCode (GetTagCode (name ), a ... )
552
+ return RenderCode (GetTagCode (string (tg )), a ... )
419
553
}
420
554
421
555
// Sprintf format and render messages
0 commit comments