Debian Codegraph @

color/color.go

raw symbols SCIP
1 // The colors package provide a simple way to bring colorful characters to terminal interface.
2 //
3 // This example will output the text with a Blue foreground and a Black background
4 // color.Println("@{bK}Example Text")
5 //
6 // This one will output the text with a red foreground
7 // color.Println("@rExample Text")
8 //
9 // This one will escape the @
10 // color.Println("@@")
11 //
12 // Full color syntax code
13 // @{rgbcmykwRGBCMYKW} foreground/background color
14 // r/R: Red
15 // g/G: Green
16 // b/B: Blue
17 // c/C: Cyan
18 // m/M: Magenta
19 // y/Y: Yellow
20 // k/K: Black
21 // w/W: White
22 // @{|} Reset format style
23 // @{!./_} Bold / Dim / Italic / Underline
24 // @{^&} Blink / Fast blink
25 // @{*} High intensity foreground color
26 // @{?} Reverse the foreground and background color
27 // @{-} Hide the text
28 // Note some of the functions are not widely supported, like "Fast blink" and "Italic".
29 package color
30
31 import (
32 "bytes"
33 "errors"
34 "fmt"
35 "io"
36 "log"
37 )
38
39 const (
40 EscapeChar = '@' // Escape character for color syntax
41 ResetCode = "\033[0m" // Short for reset to default style
42 )
43
44 // Mapping from character to concrete escape code.
45 var codeMap = map[int]int{
46 '|': 0,
47 '!': 1,
48 '.': 2,
49 '/': 3,
50 '_': 4,
51 '^': 5,
52 '&': 6,
53 '?': 7,
54 '-': 8,
55 '*': 60,
56
57 'k': 30,
58 'r': 31,
59 'g': 32,
60 'y': 33,
61 'b': 34,
62 'm': 35,
63 'c': 36,
64 'w': 37,
65 'd': 39,
66
67 'K': 40,
68 'R': 41,
69 'G': 42,
70 'Y': 43,
71 'B': 44,
72 'M': 45,
73 'C': 46,
74 'W': 47,
75 'D': 49,
76 }
77
78 // Compile color syntax string like "rG" to escape code.
79 func Colorize(x string) string {
80 attr := 0
81 fg := 39
82 bg := 49
83
84 for _, key := range x {
85 c, ok := codeMap[int(key)]
86 switch {
87 case !ok:
88 log.Printf("Wrong color syntax: %c", key)
89 case 0 <= c && c <= 8:
90 attr = c
91 case 30 <= c && c <= 37:
92 fg = c
93 case 40 <= c && c <= 47:
94 bg = c
95 case c == 60:
96 fg += c
97 }
98 }
99 return fmt.Sprintf("\033[%d;%d;%dm", attr, fg, bg)
100 }
101
102 // Handle state after meeting one '@'
103 func compileColorSyntax(input, output *bytes.Buffer) {
104 i, _, err := input.ReadRune()
105 if err != nil {
106 // EOF got
107 log.Print("Parse failed on color syntax")
108 return
109 }
110
111 switch i {
112 default:
113 output.WriteString(Colorize(string(i)))
114 case '{':
115 color := bytes.NewBufferString("")
116 for {
117 i, _, err := input.ReadRune()
118 if err != nil {
119 log.Print("Parse failed on color syntax")
120 break
121 }
122 if i == '}' {
123 break
124 }
125 color.WriteRune(i)
126 }
127 output.WriteString(Colorize(color.String()))
128 case EscapeChar:
129 output.WriteRune(EscapeChar)
130 }
131 }
132
133 // Compile the string and replace color syntax with concrete escape code.
134 func compile(x string) string {
135 if x == "" {
136 return ""
137 }
138
139 input := bytes.NewBufferString(x)
140 output := bytes.NewBufferString("")
141
142 for {
143 i, _, err := input.ReadRune()
144 if err != nil {
145 break
146 }
147 switch i {
148 default:
149 output.WriteRune(i)
150 case EscapeChar:
151 compileColorSyntax(input, output)
152 }
153 }
154 return output.String()
155 }
156
157 // Compile multiple values, only do compiling on string type.
158 func compileValues(a *[]interface{}) {
159 for i, x := range *a {
160 if str, ok := x.(string); ok {
161 (*a)[i] = compile(str)
162 }
163 }
164 }
165
166 // Similar to fmt.Print, will reset the color at the end.
167 func Print(a ...interface{}) (int, error) {
168 a = append(a, ResetCode)
169 compileValues(&a)
170 return fmt.Print(a...)
171 }
172
173 // Similar to fmt.Println, will reset the color at the end.
174 func Println(a ...interface{}) (int, error) {
175 a = append(a, ResetCode)
176 compileValues(&a)
177 return fmt.Println(a...)
178 }
179
180 // Similar to fmt.Printf, will reset the color at the end.
181 func Printf(format string, a ...interface{}) (int, error) {
182 format += ResetCode
183 format = compile(format)
184 return fmt.Printf(format, a...)
185 }
186
187 // Similar to fmt.Fprint, will reset the color at the end.
188 func Fprint(w io.Writer, a ...interface{}) (int, error) {
189 a = append(a, ResetCode)
190 compileValues(&a)
191 return fmt.Fprint(w, a...)
192 }
193
194 // Similar to fmt.Fprintln, will reset the color at the end.
195 func Fprintln(w io.Writer, a ...interface{}) (int, error) {
196 a = append(a, ResetCode)
197 compileValues(&a)
198 return fmt.Fprintln(w, a...)
199 }
200
201 // Similar to fmt.Fprintf, will reset the color at the end.
202 func Fprintf(w io.Writer, format string, a ...interface{}) (int, error) {
203 format += ResetCode
204 format = compile(format)
205 return fmt.Fprintf(w, format, a...)
206 }
207
208 // Similar to fmt.Sprint, will reset the color at the end.
209 func Sprint(a ...interface{}) string {
210 a = append(a, ResetCode)
211 compileValues(&a)
212 return fmt.Sprint(a...)
213 }
214
215 // Similar to fmt.Sprintf, will reset the color at the end.
216 func Sprintf(format string, a ...interface{}) string {
217 format += ResetCode
218 format = compile(format)
219 return fmt.Sprintf(format, a...)
220 }
221
222 // Similar to fmt.Errorf, will reset the color at the end.
223 func Errorf(format string, a ...interface{}) error {
224 return errors.New(Sprintf(format, a...))
225 }