...
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 package aurora
37
38 import (
39 "fmt"
40 "strconv"
41 "unicode/utf8"
42 )
43
44
45
46 type Value interface {
47
48
49 fmt.Stringer
50
51 fmt.Formatter
52
53 Color() Color
54
55 Value() interface{}
56
57
58 tail() Color
59 setTail(Color) Value
60
61
62
63
64 Bleach() Value
65
66 Reset() Value
67
68
69
70
71
72
73 Bold() Value
74
75 Faint() Value
76
77
78
79 DoublyUnderline() Value
80
81 Fraktur() Value
82
83
84
85 Italic() Value
86
87 Underline() Value
88
89
90
91 SlowBlink() Value
92
93
94 RapidBlink() Value
95
96 Blink() Value
97
98
99
100 Reverse() Value
101
102 Inverse() Value
103
104
105 Conceal() Value
106
107 Hidden() Value
108
109
110
111 CrossedOut() Value
112
113 StrikeThrough() Value
114
115
116 Framed() Value
117
118 Encircled() Value
119
120
121 Overlined() Value
122
123
124
125
126
127
128 Black() Value
129
130 Red() Value
131
132 Green() Value
133
134 Yellow() Value
135
136
137
138 Brown() Value
139
140 Blue() Value
141
142 Magenta() Value
143
144 Cyan() Value
145
146 White() Value
147
148
149
150
151 BrightBlack() Value
152
153 BrightRed() Value
154
155 BrightGreen() Value
156
157 BrightYellow() Value
158
159 BrightBlue() Value
160
161 BrightMagenta() Value
162
163 BrightCyan() Value
164
165 BrightWhite() Value
166
167
168
169
170
171
172
173
174
175
176
177 Index(n uint8) Value
178
179 Gray(n uint8) Value
180
181
182
183
184
185
186 BgBlack() Value
187
188 BgRed() Value
189
190 BgGreen() Value
191
192 BgYellow() Value
193
194
195
196 BgBrown() Value
197
198 BgBlue() Value
199
200 BgMagenta() Value
201
202 BgCyan() Value
203
204 BgWhite() Value
205
206
207
208
209 BgBrightBlack() Value
210
211 BgBrightRed() Value
212
213 BgBrightGreen() Value
214
215 BgBrightYellow() Value
216
217 BgBrightBlue() Value
218
219 BgBrightMagenta() Value
220
221 BgBrightCyan() Value
222
223 BgBrightWhite() Value
224
225
226
227
228
229
230
231
232
233
234
235 BgIndex(n uint8) Value
236
237 BgGray(n uint8) Value
238
239
240
241
242
243
244 Colorize(color Color) Value
245 }
246
247
248
249 type valueClear struct {
250 value interface{}
251 }
252
253 func (vc valueClear) String() string { return fmt.Sprint(vc.value) }
254
255 func (vc valueClear) Color() Color { return 0 }
256 func (vc valueClear) Value() interface{} { return vc.value }
257
258 func (vc valueClear) tail() Color { return 0 }
259 func (vc valueClear) setTail(Color) Value { return vc }
260
261 func (vc valueClear) Bleach() Value { return vc }
262 func (vc valueClear) Reset() Value { return vc }
263
264 func (vc valueClear) Bold() Value { return vc }
265 func (vc valueClear) Faint() Value { return vc }
266 func (vc valueClear) DoublyUnderline() Value { return vc }
267 func (vc valueClear) Fraktur() Value { return vc }
268 func (vc valueClear) Italic() Value { return vc }
269 func (vc valueClear) Underline() Value { return vc }
270 func (vc valueClear) SlowBlink() Value { return vc }
271 func (vc valueClear) RapidBlink() Value { return vc }
272 func (vc valueClear) Blink() Value { return vc }
273 func (vc valueClear) Reverse() Value { return vc }
274 func (vc valueClear) Inverse() Value { return vc }
275 func (vc valueClear) Conceal() Value { return vc }
276 func (vc valueClear) Hidden() Value { return vc }
277 func (vc valueClear) CrossedOut() Value { return vc }
278 func (vc valueClear) StrikeThrough() Value { return vc }
279 func (vc valueClear) Framed() Value { return vc }
280 func (vc valueClear) Encircled() Value { return vc }
281 func (vc valueClear) Overlined() Value { return vc }
282
283 func (vc valueClear) Black() Value { return vc }
284 func (vc valueClear) Red() Value { return vc }
285 func (vc valueClear) Green() Value { return vc }
286 func (vc valueClear) Yellow() Value { return vc }
287 func (vc valueClear) Brown() Value { return vc }
288 func (vc valueClear) Blue() Value { return vc }
289 func (vc valueClear) Magenta() Value { return vc }
290 func (vc valueClear) Cyan() Value { return vc }
291 func (vc valueClear) White() Value { return vc }
292 func (vc valueClear) BrightBlack() Value { return vc }
293 func (vc valueClear) BrightRed() Value { return vc }
294 func (vc valueClear) BrightGreen() Value { return vc }
295 func (vc valueClear) BrightYellow() Value { return vc }
296 func (vc valueClear) BrightBlue() Value { return vc }
297 func (vc valueClear) BrightMagenta() Value { return vc }
298 func (vc valueClear) BrightCyan() Value { return vc }
299 func (vc valueClear) BrightWhite() Value { return vc }
300 func (vc valueClear) Index(uint8) Value { return vc }
301 func (vc valueClear) Gray(uint8) Value { return vc }
302
303 func (vc valueClear) BgBlack() Value { return vc }
304 func (vc valueClear) BgRed() Value { return vc }
305 func (vc valueClear) BgGreen() Value { return vc }
306 func (vc valueClear) BgYellow() Value { return vc }
307 func (vc valueClear) BgBrown() Value { return vc }
308 func (vc valueClear) BgBlue() Value { return vc }
309 func (vc valueClear) BgMagenta() Value { return vc }
310 func (vc valueClear) BgCyan() Value { return vc }
311 func (vc valueClear) BgWhite() Value { return vc }
312 func (vc valueClear) BgBrightBlack() Value { return vc }
313 func (vc valueClear) BgBrightRed() Value { return vc }
314 func (vc valueClear) BgBrightGreen() Value { return vc }
315 func (vc valueClear) BgBrightYellow() Value { return vc }
316 func (vc valueClear) BgBrightBlue() Value { return vc }
317 func (vc valueClear) BgBrightMagenta() Value { return vc }
318 func (vc valueClear) BgBrightCyan() Value { return vc }
319 func (vc valueClear) BgBrightWhite() Value { return vc }
320 func (vc valueClear) BgIndex(uint8) Value { return vc }
321 func (vc valueClear) BgGray(uint8) Value { return vc }
322 func (vc valueClear) Colorize(Color) Value { return vc }
323
324 func (vc valueClear) Format(s fmt.State, verb rune) {
325
326
327
328
329
330
331
332
333 format := make([]byte, 1, 10)
334 format[0] = '%'
335 var f byte
336 for i := 0; i < len(availFlags); i++ {
337 if f = availFlags[i]; s.Flag(int(f)) {
338 format = append(format, f)
339 }
340 }
341 var width, prec int
342 var ok bool
343 if width, ok = s.Width(); ok {
344 format = strconv.AppendInt(format, int64(width), 10)
345 }
346 if prec, ok = s.Precision(); ok {
347 format = append(format, '.')
348 format = strconv.AppendInt(format, int64(prec), 10)
349 }
350 if verb > utf8.RuneSelf {
351 format = append(format, string(verb)...)
352 } else {
353 format = append(format, byte(verb))
354 }
355 fmt.Fprintf(s, string(format), vc.value)
356 }
357
358
359
360 type value struct {
361 value interface{}
362 color Color
363 tailColor Color
364 }
365
366 func (v value) String() string {
367 if v.color != 0 {
368 if v.tailColor != 0 {
369 return esc + v.color.Nos(true) + "m" +
370 fmt.Sprint(v.value) +
371 esc + v.tailColor.Nos(true) + "m"
372 }
373 return esc + v.color.Nos(false) + "m" + fmt.Sprint(v.value) + clear
374 }
375 return fmt.Sprint(v.value)
376 }
377
378 func (v value) Color() Color { return v.color }
379
380 func (v value) Bleach() Value {
381 v.color, v.tailColor = 0, 0
382 return v
383 }
384
385 func (v value) Reset() Value {
386 v.color, v.tailColor = 0, 0
387 return v
388 }
389
390 func (v value) tail() Color { return v.tailColor }
391
392 func (v value) setTail(t Color) Value {
393 v.tailColor = t
394 return v
395 }
396
397 func (v value) Value() interface{} { return v.value }
398
399 func (v value) Format(s fmt.State, verb rune) {
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 format := make([]byte, 0, 128)
423 if v.color != 0 {
424 format = append(format, esc...)
425 format = v.color.appendNos(format, v.tailColor != 0)
426 format = append(format, 'm')
427 }
428 format = append(format, '%')
429 var f byte
430 for i := 0; i < len(availFlags); i++ {
431 if f = availFlags[i]; s.Flag(int(f)) {
432 format = append(format, f)
433 }
434 }
435 var width, prec int
436 var ok bool
437 if width, ok = s.Width(); ok {
438 format = strconv.AppendInt(format, int64(width), 10)
439 }
440 if prec, ok = s.Precision(); ok {
441 format = append(format, '.')
442 format = strconv.AppendInt(format, int64(prec), 10)
443 }
444 if verb > utf8.RuneSelf {
445 format = append(format, string(verb)...)
446 } else {
447 format = append(format, byte(verb))
448 }
449 if v.color != 0 {
450 if v.tailColor != 0 {
451
452 format = append(format, esc...)
453 format = v.tailColor.appendNos(format, true)
454 format = append(format, 'm')
455 } else {
456 format = append(format, clear...)
457 }
458 }
459 fmt.Fprintf(s, string(format), v.value)
460 }
461
462 func (v value) Bold() Value {
463 v.color = (v.color &^ FaintFm) | BoldFm
464 return v
465 }
466
467 func (v value) Faint() Value {
468 v.color = (v.color &^ BoldFm) | FaintFm
469 return v
470 }
471
472 func (v value) DoublyUnderline() Value {
473 v.color |= DoublyUnderlineFm
474 return v
475 }
476
477 func (v value) Fraktur() Value {
478 v.color |= FrakturFm
479 return v
480 }
481
482 func (v value) Italic() Value {
483 v.color |= ItalicFm
484 return v
485 }
486
487 func (v value) Underline() Value {
488 v.color |= UnderlineFm
489 return v
490 }
491
492 func (v value) SlowBlink() Value {
493 v.color = (v.color &^ RapidBlinkFm) | SlowBlinkFm
494 return v
495 }
496
497 func (v value) RapidBlink() Value {
498 v.color = (v.color &^ SlowBlinkFm) | RapidBlinkFm
499 return v
500 }
501
502 func (v value) Blink() Value {
503 return v.SlowBlink()
504 }
505
506 func (v value) Reverse() Value {
507 v.color |= ReverseFm
508 return v
509 }
510
511 func (v value) Inverse() Value {
512 return v.Reverse()
513 }
514
515 func (v value) Conceal() Value {
516 v.color |= ConcealFm
517 return v
518 }
519
520 func (v value) Hidden() Value {
521 return v.Conceal()
522 }
523
524 func (v value) CrossedOut() Value {
525 v.color |= CrossedOutFm
526 return v
527 }
528
529 func (v value) StrikeThrough() Value {
530 return v.CrossedOut()
531 }
532
533 func (v value) Framed() Value {
534 v.color |= FramedFm
535 return v
536 }
537
538 func (v value) Encircled() Value {
539 v.color |= EncircledFm
540 return v
541 }
542
543 func (v value) Overlined() Value {
544 v.color |= OverlinedFm
545 return v
546 }
547
548 func (v value) Black() Value {
549 v.color = (v.color &^ maskFg) | BlackFg
550 return v
551 }
552
553 func (v value) Red() Value {
554 v.color = (v.color &^ maskFg) | RedFg
555 return v
556 }
557
558 func (v value) Green() Value {
559 v.color = (v.color &^ maskFg) | GreenFg
560 return v
561 }
562
563 func (v value) Yellow() Value {
564 v.color = (v.color &^ maskFg) | YellowFg
565 return v
566 }
567
568 func (v value) Brown() Value {
569 return v.Yellow()
570 }
571
572 func (v value) Blue() Value {
573 v.color = (v.color &^ maskFg) | BlueFg
574 return v
575 }
576
577 func (v value) Magenta() Value {
578 v.color = (v.color &^ maskFg) | MagentaFg
579 return v
580 }
581
582 func (v value) Cyan() Value {
583 v.color = (v.color &^ maskFg) | CyanFg
584 return v
585 }
586
587 func (v value) White() Value {
588 v.color = (v.color &^ maskFg) | WhiteFg
589 return v
590 }
591
592 func (v value) BrightBlack() Value {
593 v.color = (v.color &^ maskFg) | BrightFg | BlackFg
594 return v
595 }
596
597 func (v value) BrightRed() Value {
598 v.color = (v.color &^ maskFg) | BrightFg | RedFg
599 return v
600 }
601
602 func (v value) BrightGreen() Value {
603 v.color = (v.color &^ maskFg) | BrightFg | GreenFg
604 return v
605 }
606
607 func (v value) BrightYellow() Value {
608 v.color = (v.color &^ maskFg) | BrightFg | YellowFg
609 return v
610 }
611
612 func (v value) BrightBlue() Value {
613 v.color = (v.color &^ maskFg) | BrightFg | BlueFg
614 return v
615 }
616
617 func (v value) BrightMagenta() Value {
618 v.color = (v.color &^ maskFg) | BrightFg | MagentaFg
619 return v
620 }
621
622 func (v value) BrightCyan() Value {
623 v.color = (v.color &^ maskFg) | BrightFg | CyanFg
624 return v
625 }
626
627 func (v value) BrightWhite() Value {
628 v.color = (v.color &^ maskFg) | BrightFg | WhiteFg
629 return v
630 }
631
632 func (v value) Index(n uint8) Value {
633 v.color = (v.color &^ maskFg) | (Color(n) << shiftFg) | flagFg
634 return v
635 }
636
637 func (v value) Gray(n uint8) Value {
638 if n > 23 {
639 n = 23
640 }
641 v.color = (v.color &^ maskFg) | (Color(232+n) << shiftFg) | flagFg
642 return v
643 }
644
645 func (v value) BgBlack() Value {
646 v.color = (v.color &^ maskBg) | BlackBg
647 return v
648 }
649
650 func (v value) BgRed() Value {
651 v.color = (v.color &^ maskBg) | RedBg
652 return v
653 }
654
655 func (v value) BgGreen() Value {
656 v.color = (v.color &^ maskBg) | GreenBg
657 return v
658 }
659
660 func (v value) BgYellow() Value {
661 v.color = (v.color &^ maskBg) | YellowBg
662 return v
663 }
664
665 func (v value) BgBrown() Value {
666 return v.BgYellow()
667 }
668
669 func (v value) BgBlue() Value {
670 v.color = (v.color &^ maskBg) | BlueBg
671 return v
672 }
673
674 func (v value) BgMagenta() Value {
675 v.color = (v.color &^ maskBg) | MagentaBg
676 return v
677 }
678
679 func (v value) BgCyan() Value {
680 v.color = (v.color &^ maskBg) | CyanBg
681 return v
682 }
683
684 func (v value) BgWhite() Value {
685 v.color = (v.color &^ maskBg) | WhiteBg
686 return v
687 }
688
689 func (v value) BgBrightBlack() Value {
690 v.color = (v.color &^ maskBg) | BrightBg | BlackBg
691 return v
692 }
693
694 func (v value) BgBrightRed() Value {
695 v.color = (v.color &^ maskBg) | BrightBg | RedBg
696 return v
697 }
698
699 func (v value) BgBrightGreen() Value {
700 v.color = (v.color &^ maskBg) | BrightBg | GreenBg
701 return v
702 }
703
704 func (v value) BgBrightYellow() Value {
705 v.color = (v.color &^ maskBg) | BrightBg | YellowBg
706 return v
707 }
708
709 func (v value) BgBrightBlue() Value {
710 v.color = (v.color &^ maskBg) | BrightBg | BlueBg
711 return v
712 }
713
714 func (v value) BgBrightMagenta() Value {
715 v.color = (v.color &^ maskBg) | BrightBg | MagentaBg
716 return v
717 }
718
719 func (v value) BgBrightCyan() Value {
720 v.color = (v.color &^ maskBg) | BrightBg | CyanBg
721 return v
722 }
723
724 func (v value) BgBrightWhite() Value {
725 v.color = (v.color &^ maskBg) | BrightBg | WhiteBg
726 return v
727 }
728
729 func (v value) BgIndex(n uint8) Value {
730 v.color = (v.color &^ maskBg) | (Color(n) << shiftBg) | flagBg
731 return v
732 }
733
734 func (v value) BgGray(n uint8) Value {
735 if n > 23 {
736 n = 23
737 }
738 v.color = (v.color &^ maskBg) | (Color(232+n) << shiftBg) | flagBg
739 return v
740 }
741
742 func (v value) Colorize(color Color) Value {
743 v.color = color
744 return v
745 }
746
View as plain text