| 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 |
package color |
| 30 |
|
| 31 |
import ( |
| 32 |
"bytes" |
| 33 |
"errors" |
| 34 |
"fmt" |
| 35 |
"io" |
| 36 |
"log" |
| 37 |
) |
| 38 |
|
| 39 |
const ( |
| 40 |
EscapeChar = '@' |
| 41 |
ResetCode = "\033[0m" |
| 42 |
) |
| 43 |
|
| 44 |
|
| 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 |
|
| 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 |
|
| 103 |
func compileColorSyntax(input, output *bytes.Buffer) { |
| 104 |
i, _, err := input.ReadRune() |
| 105 |
if err != nil { |
| 106 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 167 |
func Print(a ...interface{}) (int, error) { |
| 168 |
a = append(a, ResetCode) |
| 169 |
compileValues(&a) |
| 170 |
return fmt.Print(a...) |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
func Println(a ...interface{}) (int, error) { |
| 175 |
a = append(a, ResetCode) |
| 176 |
compileValues(&a) |
| 177 |
return fmt.Println(a...) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 209 |
func Sprint(a ...interface{}) string { |
| 210 |
a = append(a, ResetCode) |
| 211 |
compileValues(&a) |
| 212 |
return fmt.Sprint(a...) |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
func Sprintf(format string, a ...interface{}) string { |
| 217 |
format += ResetCode |
| 218 |
format = compile(format) |
| 219 |
return fmt.Sprintf(format, a...) |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
func Errorf(format string, a ...interface{}) error { |
| 224 |
return errors.New(Sprintf(format, a...)) |
| 225 |
} |