Debian Codegraph @

term/color.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package term
6
7 /*
8 Some simple functions to add colors and attributes to terminals.
9
10 The base colors are types implementing the Stringer interface, this makes
11 it very simple to give a color to arbitrary strings. Also handy to have the raw string still
12 available for comparisons and such.
13
14 g := Green("Green world")
15 fmt.Println("Hello",g)
16 fmt.Println(Red("Warning!"))
17
18 var col fmt.Stringer
19 switch {
20 case atk == 0:
21 col = Blue("5 FADE OUT")
22 case atk < 4:
23 col = Green("4 DOUBLE TAKE")
24 case atk <10:
25 col = Yellow("3 ROUND HOUSE")
26 case atk <50:
27 col = Red("2 FAST PACE")
28 case atk >= 50:
29 col = Blinking("1 COCKED PISTOL")
30 }
31 fmt.Println("Defcon: ",col)
32 */
33
34 import (
35 "errors"
36 "fmt"
37 "math/rand"
38 "strconv"
39 )
40
41 type stringer interface {
42 String() string
43 }
44
45 // colorEnable toggles colors on/off.
46 var colorEnable = true
47
48 // ColorEnable activates the terminal colors , this is the default.
49 func ColorEnable() {
50 colorEnable = true
51 }
52
53 // ColorDisable disables the terminal colors.
54 func ColorDisable() {
55 colorEnable = false
56 }
57
58 // Terminal Color and modifier codes
59 const (
60 CSI = "\033["
61 FgBlack = "30"
62 FgRed = "31"
63 FgGreen = "32"
64 FgYellow = "33"
65 FgBlue = "34"
66 FgMagenta = "35"
67 FgCyan = "36"
68 FgWhite = "37"
69 FgDefault = "39"
70 F256 = "38"
71 BgBlack = "40"
72 BgRed = "41"
73 BgGreen = "42"
74 BgYellow = "43"
75 BgBlue = "44"
76 BgMagenta = "45"
77 BgCyan = "46"
78 BgWhite = "47"
79 BgDefault = "49"
80 Bg256 = "48"
81 Blink = "5"
82 Ital = "3"
83 Underln = "4"
84 Faint = "2"
85 Bld = "1"
86 NoMode = "0"
87 )
88
89 // Standard colors
90 // Foreground
91
92 // Green implements the Stringer interface to print string foreground in Green color.
93 type Green string
94
95 // Blue implements the Stringer interface to print string foreground in Blue color.
96 type Blue string
97
98 // Red implements the Stringer interface to print string foreground in Red color.
99 type Red string
100
101 // Yellow implements the Stringer interface to print string foreground in Yellow color.
102 type Yellow string
103
104 // Magenta implements the Stringer interface to print string foreground in Magenta color.
105 type Magenta string
106
107 // Cyan implements the Stringer interface to print string foreground in Cyan color.
108 type Cyan string
109
110 // White implements the Stringer interface to print string foreground in White color.
111 type White string
112
113 // Black implements the Stringer interface to print string foreground in Black color.
114 type Black string
115
116 // Random implements the Stringer interface to print string foreground in Random color.
117 type Random string
118
119 // Background
120
121 // BGreen implements the Stringer interface to print string background in Green color.
122 type BGreen string
123
124 // BBlue implements the Stringer interface to print string background in Blue color.
125 type BBlue string
126
127 // BRed implements the Stringer interface to print string background in Red color.
128 type BRed string
129
130 // BYellow implements the Stringer interface to print string background in Yellow color.
131 type BYellow string
132
133 // BRandom implements the Stringer interface to print string background in Random color.
134 type BRandom string
135
136 // BMagenta implements the Stringer interface to print string background in Magenta color.
137 type BMagenta string
138
139 // BCyan implements the Stringer interface to print string background in Cyan color.
140 type BCyan string
141
142 // BWhite implements the Stringer interface to print string background in White color.
143 type BWhite string
144
145 // BBlack implements the Stringer interface to print string background in Black color.
146 type BBlack string
147
148 // Set color
149
150 // Color is the type returned by the colour setters to print any terminal colour.
151 type Color string
152
153 // ColorRandom implements the Stringer interface to print string Random color.
154 type ColorRandom string
155
156 // Color256Random implements the Stringer interface to print string random 256 color Term style.
157 type Color256Random string
158
159 // Some modifiers
160
161 // Blinking implements the Stringer interface to print string in Blinking mode.
162 type Blinking string
163
164 // Underline implements the Stringer interface to print string in Underline mode.
165 type Underline string
166
167 // Bold implements the Stringer interface to print string in Bold mode.
168 type Bold string
169
170 //type Bright string -- Doesn't seem to work well
171
172 // Italic implements the Stringer interface to print string foreground in Italic color.
173 type Italic string
174
175 // colType takes all the base color types and generates proper modifiers.
176 func colType(col stringer) string {
177 nMode := FgDefault
178 var mode, res string
179 switch c := col.(type) {
180 case Black:
181 mode = FgBlack
182 res = string(c)
183 case Red:
184 mode = FgRed
185 res = string(c)
186 case Green:
187 mode = FgGreen
188 res = string(c)
189 case Yellow:
190 mode = FgYellow
191 res = string(c)
192 case Blue:
193 mode = FgBlue
194 res = string(c)
195 case Magenta:
196 mode = FgMagenta
197 res = string(c)
198 case Cyan:
199 mode = FgCyan
200 res = string(c)
201 case White:
202 mode = FgWhite
203 res = string(c)
204 case BBlack:
205 mode = BgBlack
206 res = string(c)
207 case BRed:
208 mode = BgRed
209 nMode = BgDefault
210 res = string(c)
211 case BGreen:
212 mode = BgGreen
213 nMode = BgDefault
214 res = string(c)
215 case BYellow:
216 mode = BgYellow
217 nMode = BgDefault
218 res = string(c)
219 case BBlue:
220 mode = BgBlue
221 nMode = BgDefault
222 res = string(c)
223 case BMagenta:
224 mode = BgMagenta
225 nMode = BgDefault
226 res = string(c)
227 case BCyan:
228 mode = BgCyan
229 nMode = BgDefault
230 res = string(c)
231 case BWhite:
232 mode = BgWhite
233 nMode = BgDefault
234 res = string(c)
235 case Blinking:
236 mode = Blink
237 nMode = NoMode
238 res = string(c)
239 case Italic:
240 mode = Ital
241 nMode = NoMode
242 res = string(c)
243 case Underline:
244 mode = Underln
245 nMode = NoMode
246 res = string(c)
247 case Bold:
248 nMode = NoMode
249 mode = Bld
250 res = string(c)
251 default:
252 return "unsupported type"
253 }
254 if !colorEnable {
255 return res
256 }
257 return CSI + mode + "m" + res + CSI + nMode + "m"
258 }
259
260 // Stringers for all the base colors , just fill it in with something and print it
261 // Foreground
262
263 // String implements the Stringer interface for type Green.
264 func (c Green) String() string {
265 return colType(c)
266 }
267
268 // Greenf returns a Green formatted string.
269 func Greenf(format string, a ...interface{}) string {
270 return colType(Green(fmt.Sprintf(format, a...)))
271 }
272
273 // String implements the Stringer interface for type Blue.
274 func (c Blue) String() string {
275 return colType(c)
276 }
277
278 // Bluef returns a Blue formatted string.
279 func Bluef(format string, a ...interface{}) string {
280 return colType(Blue(fmt.Sprintf(format, a...)))
281 }
282
283 // String implements the Stringer interface for type Red.
284 func (c Red) String() string {
285 return colType(c)
286 }
287
288 // Redf returns a Red formatted string.
289 func Redf(format string, a ...interface{}) string {
290 return colType(Red(fmt.Sprintf(format, a...)))
291 }
292
293 // String implements the Stringer interface for type Yellow.
294 func (c Yellow) String() string {
295 return colType(c)
296 }
297
298 // Yellowf returns a Yellow formatted string.
299 func Yellowf(format string, a ...interface{}) string {
300 return colType(Yellow(fmt.Sprintf(format, a...)))
301 }
302
303 // String implements the Stringer interface for type Magenta.
304 func (c Magenta) String() string {
305 return colType(c)
306 }
307
308 // Magentaf returns a Magenta formatted string.
309 func Magentaf(format string, a ...interface{}) string {
310 return colType(Magenta(fmt.Sprintf(format, a...)))
311 }
312
313 // String implements the Stringer interface for type White.
314 func (c White) String() string {
315 return colType(c)
316 }
317
318 // Whitef returns a White formatted string.
319 func Whitef(format string, a ...interface{}) string {
320 return colType(White(fmt.Sprintf(format, a...)))
321 }
322
323 // String implements the Stringer interface for type Black.
324 func (c Black) String() string {
325 return colType(c)
326 }
327
328 // Blackf returns a Black formatted string.
329 func Blackf(format string, a ...interface{}) string {
330 return colType(Black(fmt.Sprintf(format, a...)))
331 }
332
333 // String implements the Stringer interface for type Cyan.
334 func (c Cyan) String() string {
335 return colType(c)
336 }
337
338 // Cyanf returns a Cyan formatted string.
339 func Cyanf(format string, a ...interface{}) string {
340 return colType(Cyan(fmt.Sprintf(format, a...)))
341 }
342
343 // Background
344
345 // String implements the Stringer interface for type BGreen.
346 func (c BGreen) String() string {
347 return colType(c)
348 }
349
350 // BGreenf returns a BGreen formatted string.
351 func BGreenf(format string, a ...interface{}) string {
352 return colType(BGreen(fmt.Sprintf(format, a...)))
353 }
354
355 // String implements the Stringer interface for type BBlue.
356 func (c BBlue) String() string {
357 return colType(c)
358 }
359
360 // BBluef returns a BBlue formatted string.
361 func BBluef(format string, a ...interface{}) string {
362 return colType(BBlue(fmt.Sprintf(format, a...)))
363 }
364
365 // String implements the Stringer interface for type BRed.
366 func (c BRed) String() string {
367 return colType(c)
368 }
369
370 // BRedf returns a BRed formatted string.
371 func BRedf(format string, a ...interface{}) string {
372 return colType(BRed(fmt.Sprintf(format, a...)))
373 }
374
375 // String implements the Stringer interface for type BYellow.
376 func (c BYellow) String() string {
377 return colType(c)
378 }
379
380 // BYellowf returns a BYellow formatted string.
381 func BYellowf(format string, a ...interface{}) string {
382 return colType(BYellow(fmt.Sprintf(format, a...)))
383 }
384
385 // String implements the Stringer interface for type BMagenta.
386 func (c BMagenta) String() string {
387 return colType(c)
388 }
389
390 // BMagentaf returns a BMagenta formatted string.
391 func BMagentaf(format string, a ...interface{}) string {
392 return colType(BMagenta(fmt.Sprintf(format, a...)))
393 }
394
395 // String implements the Stringer interface for type BWhite.
396 func (c BWhite) String() string {
397 return colType(c)
398 }
399
400 // BWhitef returns a BWhite formatted string.
401 func BWhitef(format string, a ...interface{}) string {
402 return colType(BWhite(fmt.Sprintf(format, a...)))
403 }
404
405 // String implements the Stringer interface for type BBlack.
406 func (c BBlack) String() string {
407 return colType(c)
408 }
409
410 // BBlackf returns a BBlack formatted string.
411 func BBlackf(format string, a ...interface{}) string {
412 return colType(BBlack(fmt.Sprintf(format, a...)))
413 }
414
415 // String implements the Stringer interface for type BCyan.
416 func (c BCyan) String() string {
417 return colType(c)
418 }
419
420 // BCyanf returns a BCyan formatted string.
421 func BCyanf(format string, a ...interface{}) string {
422 return colType(BCyan(fmt.Sprintf(format, a...)))
423 }
424
425 // Modifier codes
426
427 // String implements the Stringer interface for type Blinking.
428 func (c Blinking) String() string {
429 return colType(c)
430 }
431
432 // String implements the Stringer interface for type Underline.
433 func (c Underline) String() string {
434 return colType(c)
435 }
436
437 // String implements the Stringer interface for type Bold.
438 func (c Bold) String() string {
439 return colType(c)
440 }
441
442 // String implements the Stringer interface for type Italic.
443 func (c Italic) String() string {
444 return colType(c)
445 }
446
447 // NewColor gives a type Color back with specified fg/bg colors set that can
448 // be printed with anything using the Stringer iface.
449 func NewColor(str string, fg string, bg string) (Color, error) {
450 if fg != "" {
451 ifg, err := strconv.Atoi(fg)
452 if err != nil {
453 return Color(""), err
454 }
455 if ifg < 30 && ifg > 37 {
456 return Color(""), errors.New("fg: " + fg + "not a valid color 30-37")
457 }
458 } else {
459 fg = FgDefault
460 }
461 if bg != "" {
462 ibg, err := strconv.Atoi(bg)
463 if err != nil {
464 return Color(""), err
465 }
466 if ibg < 40 && ibg > 47 {
467 return Color(""), errors.New("fg: " + fg + "not a valid color 40-47")
468 }
469 } else {
470 bg = BgDefault
471 }
472 return Color(CSI + fg + ";" + bg + "m" + str + CSI + FgDefault + ";" + BgDefault + "m"), nil
473 }
474
475 // String the stringer interface for all base color types.
476 func (c Color) String() string {
477 if !colorEnable {
478 clean := make([]byte, 0, len(c))
479 src := []byte(c)
480 L1:
481 for i := 0; i < len(src); i++ {
482 // Shortest possible mod.
483 if len(src) < i+4 {
484 clean = append(clean, src[i:]...)
485 return string(clean)
486 }
487 if string(src[i:i+2]) == CSI {
488 // Save current index incase this is not a term mod code.
489 s := i
490 // skip forward to end of mod
491 for i += 2; i < len(src); i++ {
492 switch src[i] {
493 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';':
494 // Legal characters in a term mod code.
495 continue
496 case 'm':
497 // End of the term mod code.
498 continue L1
499 default:
500 // Not a term mod code.
501 i = s
502 break
503 }
504 }
505 }
506 clean = append(clean, src[i])
507 }
508 return string(clean)
509 }
510 return string(c)
511 }
512
513 // NewColor256 gives a type Color back using Term 256 color that can be printed with anything using the Stringer iface.
514 func NewColor256(str string, fg string, bg string) (Color, error) {
515 if fg != "" {
516 ifg, err := strconv.Atoi(fg)
517 if err != nil {
518 return Color(""), err
519 }
520 if ifg < 0 && ifg > 256 {
521 return Color(""), errors.New("fg: " + fg + " not a valid color 0-256")
522 }
523 }
524 if bg != "" {
525 ibg, err := strconv.Atoi(bg)
526 if err != nil {
527 return Color(""), err
528 }
529 if ibg < 0 && ibg > 256 {
530 return Color(""), errors.New("bg: " + bg + " not a valid color 0-256")
531 }
532 }
533 tstr := CSI + F256 + ";5;" + fg + ";" + Bg256 + ";5;" + bg + "m"
534 tstr += str
535 return Color(tstr + CSI + FgDefault + ";5;" + BgDefault + ";5;" + "m"), nil
536 }
537
538 // NewColorRGB takes R G B and returns a ColorRGB type that can be printed by anything using the Stringer iface.
539 // Only Konsole to my knowledge that supports 24bit color
540 func NewColorRGB(str string, red uint8, green uint8, blue uint8) Color {
541 ired := strconv.Itoa(int(red))
542 igreen := strconv.Itoa(int(green))
543 iblue := strconv.Itoa(int(blue))
544 tstr := CSI + F256 + ";2;" + ired + ";" + igreen + ";" + iblue + "m"
545 tstr += str
546 return Color(tstr + CSI + FgDefault + ";5;" + BgDefault + ";5;" + "m")
547 }
548
549 // String is a random color stringer.
550 func (c ColorRandom) String() string {
551 if !colorEnable {
552 return string(c)
553 }
554 ifg := rand.Int()%8 + 30
555 ibg := rand.Int()%8 + 40
556 res := CSI + strconv.Itoa(ifg) + ";" + strconv.Itoa(ibg) + "m"
557 res += string(c)
558 res += CSI + strconv.Itoa(ifg) + ";" + strconv.Itoa(ibg) + "m"
559 return res
560 }
561
562 // String gives a random fg color everytime it's printed.
563 func (c Random) String() string {
564 if !colorEnable {
565 return string(c)
566 }
567 ifg := int(rand.Int()%8 + 30)
568 res := CSI + strconv.Itoa(ifg) + "m"
569 res += string(c) + strconv.Itoa(int(ifg))
570 res += CSI + FgDefault + "m"
571 return res
572 }
573
574 // String gives a random bg color everytime it's printed.
575 func (c BRandom) String() string {
576 if !colorEnable {
577 return string(c)
578 }
579 ibg := rand.Int()%8 + 40
580 res := CSI + strconv.Itoa(ibg) + "m"
581 res += string(c) + strconv.Itoa(ibg)
582 res += CSI + BgDefault + "m"
583 return res
584 }
585
586 // NewCombo Takes a combination of modes and return a string with them all combined.
587 func NewCombo(s string, mods ...string) Color {
588 var col, bcol, mod bool
589 modstr := CSI
590 tracking := make(map[string]bool)
591 for _, m := range mods {
592 switch m {
593 case FgBlack, FgRed, FgGreen, FgYellow, FgBlue, FgMagenta, FgCyan, FgWhite:
594 if col {
595 continue
596 }
597 col = true
598 case BgBlack, BgRed, BgGreen, BgYellow, BgBlue, BgMagenta, BgCyan, BgWhite:
599 if bcol {
600 continue
601 }
602 bcol = true
603 case Bld, Faint, Ital, Underln, Blink:
604 if tracking[m] {
605 continue
606 }
607 tracking[m] = true
608 mod = true
609 default:
610 continue
611 }
612 modstr += m + ";"
613 }
614 end := CSI
615 if col {
616 end += FgDefault
617 }
618 if bcol {
619 if col {
620 end += ";"
621 }
622 end += BgDefault
623 }
624 if mod {
625 if col || bcol {
626 end += ";"
627 }
628 end += NoMode
629 }
630 end += "m"
631 modstr = modstr[:len(modstr)-1] + "m"
632 modstr += s
633 modstr += end
634 return Color(modstr)
635 }
636
637 // TestTerm tries out most of the functions in this package and return
638 // a colourful string. Could be used to check what your terminal supports.
639 func TestTerm() string {
640 res := "Standard 8:\n"
641 res += "Fg:\t"
642 for c := 30; c < 38; c++ {
643 tres, _ := NewColor("#", strconv.Itoa(c), "")
644 res += tres.String()
645 }
646 res += "\nBg:\t"
647 for c := 40; c < 48; c++ {
648 tres, _ := NewColor(" ", "", strconv.Itoa(c))
649 res += tres.String()
650 }
651 res += "\nStandard 16:\t"
652 for c := 0; c < 16; c++ {
653 tcol, _ := NewColor256(" ", "", strconv.Itoa(c))
654 res += tcol.String()
655 }
656 res += "\n"
657 res += "256 Col:\n"
658 // 6x6x6 cubes are trendy
659 for row, base := 1, 0; row <= 6; row++ {
660 base = (row * 6) + 9 // Step over the first 16 base colors
661 for cubes := 1; cubes <= 6; cubes++ {
662 for column := 1; column <= 6; column++ {
663 tcol, _ := NewColor256(" ", "", strconv.Itoa(base+column))
664 res += tcol.String()
665 }
666 base += 36 // 6 * 6
667 }
668 res += "\n"
669 }
670 // Grayscale left.
671 res += "Grayscales:\n"
672 for c := 232; c <= 255; c++ {
673 tcol, _ := NewColor256(" ", "", strconv.Itoa(c))
674 res += tcol.String()
675 }
676 return res
677 }