-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagged_string.go
264 lines (231 loc) · 6.19 KB
/
tagged_string.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2021 Nelson Germán Ghezzi. All rights reserved.
// Use of this source code is governed by a MIT license that
// can be found in the LICENSE file.
package coloring
import "strings"
const (
runeTagOpen = rune('<')
runeTagClose = rune('>')
runeTagClosePrefix = rune('/')
runeEscape = rune('\\')
tagBold = "bold"
tagBoldShort = "b"
tagFaint = "faint"
tagFaintShort = "f"
tagItalic = "italic"
tagItalicShort = "i"
tagUnderline = "underline"
tagUnderlineShort = "u"
tagBlink = "blink"
tagBlinkShort = "bl"
tagInvert = "invert"
tagInvertShort = "in"
tagConceal = "conceal"
tagConcealShort = "c"
tagStrikethrough = "strikethrough"
tagStrikethroughShort = "s"
tagColorBlack = "black"
tagColorRed = "red"
tagColorGreen = "green"
tagColorYellow = "yellow"
tagColorBlue = "blue"
tagColorMagenta = "magenta"
tagColorCyan = "cyan"
tagColorWhite = "white"
tagBackgroundColorBlack = "bg-black"
tagBackgroundColorRed = "bg-red"
tagBackgroundColorGreen = "bg-green"
tagBackgroundColorYellow = "bg-yellow"
tagBackgroundColorBlue = "bg-blue"
tagBackgroundColorMagenta = "bg-magenta"
tagBackgroundColorCyan = "bg-cyan"
tagBackgroundColorWhite = "bg-white"
tagReset = "reset"
tagResetShort = "r"
attributeBright = "bright"
)
// Tagged allows to apply style attributes to a string using an HTML-like
// tag syntax to mark the start and end of each attribute (bold, italics,
// text and background colors and more).
//
// For example, the following will be a valid string to pass to Tagged:
// "The <b>wolf</b> <i>howls</i> at the <b><yellow>moon</yellow></b>"
// That will result in the word "wolf" styled as bold text, the word
// "howls" in italics, and the word "moon" in bold and yellow color.
//
// Note that Tagged will not generate any console output by itself, but
// the returned string is ready to be used with console output functions
// like fmt.Println and others.
func Tagged(s string) string {
builder := Sentence()
runes := []rune(s)
regularTextStarts := 0
for i := 0; i < len(runes); i++ {
if runes[i] == runeEscape {
builder.Text(string(runes[regularTextStarts:i]))
i++
regularTextStarts = i
continue
}
if runes[i] != runeTagOpen {
continue
}
regularTextEnds := i
builder.Text(string(runes[regularTextStarts:regularTextEnds]))
i++
isClosingTag := false
if i < len(runes) && runes[i] == runeTagClosePrefix {
isClosingTag = true
i++
}
tagStartIndex := i
for i < len(runes) && runes[i] != runeTagClose {
i++
}
if i == len(runes) {
regularTextStarts = regularTextEnds
break
}
tagAndAttributes := strings.Split(string(runes[tagStartIndex:i]), " ")
applyTagAsStyleAttribute(tagAndAttributes[0], builder, isClosingTag, tagAndAttributes[1:])
regularTextStarts = i + 1
}
return builder.Text(string(runes[regularTextStarts:])).Reset().String()
}
func applyTagAsStyleAttribute(tag string, builder *SentenceBuilder, isClosingTag bool, attributes []string) {
isBrightColor := len(attributes) > 0 && attributes[0] == attributeBright
if isClosingTag {
applyClosingAttribute(tag, builder)
} else {
applyOpeningAttribute(tag, builder, isBrightColor)
}
}
//nolint:cyclop
func applyOpeningAttribute(tag string, builder *SentenceBuilder, isBrightColor bool) {
switch tag {
case tagBoldShort, tagBold:
builder.BoldStart()
case tagFaintShort, tagFaint:
builder.FaintStart()
case tagItalicShort, tagItalic:
builder.ItalicStart()
case tagUnderlineShort, tagUnderline:
builder.UnderlineStart()
case tagBlinkShort, tagBlink:
builder.BlinkStart()
case tagInvertShort, tagInvert:
builder.InvertStart()
case tagConcealShort, tagConceal:
builder.ConcealStart()
case tagStrikethroughShort, tagStrikethrough:
builder.StrikethroughStart()
case tagResetShort, tagReset:
builder.Reset()
default:
applyColorAttribute(tag, builder, isBrightColor)
}
}
func applyClosingAttribute(tag string, builder *SentenceBuilder) {
switch tag {
case tagBoldShort, tagBold:
builder.BoldEnd()
case tagFaintShort, tagFaint:
builder.FaintEnd()
case tagItalicShort, tagItalic:
builder.ItalicEnd()
case tagUnderlineShort, tagUnderline:
builder.UnderlineEnd()
case tagBlinkShort, tagBlink:
builder.BlinkEnd()
case tagInvertShort, tagInvert:
builder.InvertEnd()
case tagConcealShort, tagConceal:
builder.ConcealEnd()
case tagStrikethroughShort, tagStrikethrough:
builder.StrikethroughEnd()
default:
resetColorAttribute(tag, builder)
}
}
const (
brightColorDelta = BRIGHTBLACK
invalidColor = -1
)
func applyColorAttribute(tag string, builder *SentenceBuilder, isBrightColor bool) {
color := getColorFromTag(tag)
if color == invalidColor {
return
}
if isBrightColor {
color += brightColorDelta
}
switch tag {
case
tagColorBlack,
tagColorRed,
tagColorGreen,
tagColorYellow,
tagColorBlue,
tagColorMagenta,
tagColorCyan,
tagColorWhite:
builder.ColorSet(color)
case
tagBackgroundColorBlack,
tagBackgroundColorRed,
tagBackgroundColorGreen,
tagBackgroundColorYellow,
tagBackgroundColorBlue,
tagBackgroundColorMagenta,
tagBackgroundColorCyan,
tagBackgroundColorWhite:
builder.BackgroundSet(color)
}
}
func getColorFromTag(tag string) int {
colorName := strings.TrimPrefix(tag, "bg-")
switch colorName {
case tagColorBlack:
return BLACK
case tagColorRed:
return RED
case tagColorGreen:
return GREEN
case tagColorYellow:
return YELLOW
case tagColorBlue:
return BLUE
case tagColorMagenta:
return MAGENTA
case tagColorCyan:
return CYAN
case tagColorWhite:
return WHITE
default:
return invalidColor
}
}
func resetColorAttribute(tag string, builder *SentenceBuilder) {
switch tag {
case
tagColorBlack,
tagColorRed,
tagColorGreen,
tagColorYellow,
tagColorBlue,
tagColorMagenta,
tagColorCyan,
tagColorWhite:
builder.ColorReset()
case
tagBackgroundColorBlack,
tagBackgroundColorRed,
tagBackgroundColorGreen,
tagBackgroundColorYellow,
tagBackgroundColorBlue,
tagBackgroundColorMagenta,
tagBackgroundColorCyan,
tagBackgroundColorWhite:
builder.BackgroundReset()
}
}