forked from owenthereal/ccat
/
colorize.go
94 lines (79 loc) · 1.91 KB
/
colorize.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bytes"
"fmt"
"strings"
)
const esc = "\033["
var ColorCodes = map[string]string{
"": "",
"reset": esc + "39;49;00m",
"bold": esc + "01m",
"faint": esc + "02m",
"standout": esc + "03m",
"underline": esc + "04m",
"blink": esc + "05m",
"overline": esc + "06m",
}
func init() {
darkColors := []string{
"black",
"darkred",
"darkgreen",
"brown",
"darkblue",
"purple",
"teal",
"lightgray",
}
lightColors := []string{
"darkgray",
"red",
"green",
"yellow",
"blue",
"fuchsia",
"turquoise",
"white",
}
for i, x := 0, 30; i < len(darkColors); i, x = i+1, x+1 {
ColorCodes[darkColors[i]] = esc + fmt.Sprintf("%dm", x)
ColorCodes[lightColors[i]] = esc + fmt.Sprintf("%d;01m", x)
}
ColorCodes["darkteal"] = ColorCodes["turquoise"]
ColorCodes["darkyellow"] = ColorCodes["brown"]
ColorCodes["fuscia"] = ColorCodes["fuchsia"]
ColorCodes["white"] = ColorCodes["bold"]
}
/*
Format ``text`` with a color and/or some attributes::
color normal color
*color* bold color
_color_ underlined color
+color+ blinking color
*/
func Colorize(attr, text string) string {
if attr == "" {
return text
}
result := new(bytes.Buffer)
if strings.HasPrefix(attr, "+") && strings.HasSuffix(attr, "+") {
result.WriteString(ColorCodes["blink"])
attr = strings.TrimPrefix(attr, "+")
attr = strings.TrimSuffix(attr, "+")
}
if strings.HasPrefix(attr, "*") && strings.HasSuffix(attr, "*") {
result.WriteString(ColorCodes["bold"])
attr = strings.TrimPrefix(attr, "*")
attr = strings.TrimSuffix(attr, "*")
}
if strings.HasPrefix(attr, "_") && strings.HasSuffix(attr, "_") {
result.WriteString(ColorCodes["underline"])
attr = strings.TrimPrefix(attr, "_")
attr = strings.TrimSuffix(attr, "_")
}
result.WriteString(ColorCodes[attr])
result.WriteString(text)
result.WriteString(ColorCodes["reset"])
return result.String()
}