-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ansi_color.go
218 lines (198 loc) · 6.22 KB
/
ansi_color.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"errors"
"fmt"
"strings"
"github.com/gookit/color"
)
var (
// Map for color names and their respective foreground [0] or background [1] color codes
colorMap = map[string][2]string{
"black": {"30", "40"},
"red": {"31", "41"},
"green": {"32", "42"},
"yellow": {"33", "43"},
"blue": {"34", "44"},
"magenta": {"35", "45"},
"cyan": {"36", "46"},
"white": {"37", "47"},
"default": {"39", "49"},
"darkGray": {"90", "100"},
"lightRed": {"91", "101"},
"lightGreen": {"92", "102"},
"lightYellow": {"93", "103"},
"lightBlue": {"94", "104"},
"lightMagenta": {"95", "105"},
"lightCyan": {"96", "106"},
"lightWhite": {"97", "107"},
}
)
// Returns the color code for a given color name
func getColorFromName(colorName string, isBackground bool) (string, error) {
colorMapOffset := 0
if isBackground {
colorMapOffset = 1
}
if colorCodes, found := colorMap[colorName]; found {
return colorCodes[colorMapOffset], nil
}
return "", errors.New("color name does not exist")
}
type colorWriter interface {
write(background, foreground, text string)
string() string
reset()
setColors(background, foreground string)
setParentColors(background, foreground string)
}
// AnsiColor writes colorized strings
type AnsiColor struct {
builder strings.Builder
ansi *ansiUtils
terminalBackground string
Colors *Color
ParentColors *Color
}
type Color struct {
Background string
Foreground string
}
const (
// Transparent implies a transparent color
Transparent = "transparent"
// Inherit takes the previous segment's color
Inherit = "inherit"
// Background takes the current segment's background color
Background = "background"
// Foreground takes the current segment's foreground color
Foreground = "foreground"
)
func (a *AnsiColor) setColors(background, foreground string) {
a.Colors = &Color{
Background: background,
Foreground: foreground,
}
}
func (a *AnsiColor) setParentColors(background, foreground string) {
a.ParentColors = &Color{
Background: background,
Foreground: foreground,
}
}
// Gets the ANSI color code for a given color string.
// This can include a valid hex color in the format `#FFFFFF`,
// but also a name of one of the first 16 ANSI colors like `lightBlue`.
func (a *AnsiColor) getAnsiFromColorString(colorString string, isBackground bool) string {
if colorString == Transparent || len(colorString) == 0 {
return colorString
}
colorFromName, err := getColorFromName(colorString, isBackground)
if err == nil {
return colorFromName
}
style := color.HEX(colorString, isBackground)
if style.IsEmpty() {
return ""
}
return style.String()
}
func (a *AnsiColor) writeColoredText(background, foreground, text string) {
// Avoid emitting empty strings with color codes
if text == "" || (foreground == Transparent && background == Transparent) {
return
}
// default to white fg if empty, empty backgrond is supported
if len(foreground) == 0 {
foreground = a.getAnsiFromColorString("white", false)
}
if foreground == Transparent && len(background) != 0 && len(a.terminalBackground) != 0 {
fgAnsiColor := a.getAnsiFromColorString(a.terminalBackground, false)
coloredText := fmt.Sprintf(a.ansi.colorFull, background, fgAnsiColor, text)
a.builder.WriteString(coloredText)
return
}
if foreground == Transparent && len(background) != 0 {
coloredText := fmt.Sprintf(a.ansi.colorTransparent, background, text)
a.builder.WriteString(coloredText)
return
} else if len(background) == 0 || background == Transparent {
coloredText := fmt.Sprintf(a.ansi.colorSingle, foreground, text)
a.builder.WriteString(coloredText)
return
}
coloredText := fmt.Sprintf(a.ansi.colorFull, background, foreground, text)
a.builder.WriteString(coloredText)
}
func (a *AnsiColor) writeAndRemoveText(background, foreground, text, textToRemove, parentText string) string {
a.writeColoredText(background, foreground, text)
return strings.Replace(parentText, textToRemove, "", 1)
}
func (a *AnsiColor) write(background, foreground, text string) {
if len(text) == 0 {
return
}
getAnsiColors := func(background, foreground string) (string, string) {
if background == Background {
background = a.Colors.Background
}
if background == Foreground {
background = a.Colors.Foreground
}
if foreground == Foreground {
foreground = a.Colors.Foreground
}
if foreground == Background {
foreground = a.Colors.Background
}
if background == Inherit && a.ParentColors != nil {
background = a.ParentColors.Background
}
if background == Inherit && a.ParentColors == nil {
background = Transparent
}
if foreground == Inherit && a.ParentColors != nil {
foreground = a.ParentColors.Foreground
}
if foreground == Inherit && a.ParentColors == nil {
foreground = Transparent
}
inverted := foreground == Transparent && len(background) != 0
background = a.getAnsiFromColorString(background, !inverted)
foreground = a.getAnsiFromColorString(foreground, false)
return background, foreground
}
bgAnsi, fgAnsi := getAnsiColors(background, foreground)
text = a.ansi.escapeText(text)
text = a.ansi.formatText(text)
text = a.ansi.generateHyperlink(text)
// first we match for any potentially valid colors enclosed in <>
match := findAllNamedRegexMatch(`<(?P<foreground>[^,>]+)?,?(?P<background>[^>]+)?>(?P<content>[^<]*)<\/>`, text)
for i := range match {
fg := match[i]["foreground"]
bg := match[i]["background"]
if fg == Transparent && len(bg) == 0 {
bg = background
}
bg, fg = getAnsiColors(bg, fg)
// set colors if they are empty
if len(bg) == 0 {
bg = bgAnsi
}
if len(fg) == 0 {
fg = fgAnsi
}
escapedTextSegment := match[i]["text"]
innerText := match[i]["content"]
textBeforeColorOverride := strings.Split(text, escapedTextSegment)[0]
text = a.writeAndRemoveText(bgAnsi, fgAnsi, textBeforeColorOverride, textBeforeColorOverride, text)
text = a.writeAndRemoveText(bg, fg, innerText, escapedTextSegment, text)
}
// color the remaining part of text with background and foreground
a.writeColoredText(bgAnsi, fgAnsi, text)
}
func (a *AnsiColor) string() string {
return a.builder.String()
}
func (a *AnsiColor) reset() {
a.builder.Reset()
}