/
print_colors.go
executable file
·376 lines (334 loc) · 9.09 KB
/
print_colors.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright © 2019 Hedzr Yeh.
*/
package cmdr
import (
"bufio"
"fmt"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/hedzr/cmdr/tool"
"golang.org/x/net/html"
)
// some refs:
// - github.com/labstack/gommon/color
//
const (
defaultTimestampFormat = time.RFC3339 //nolint:deadcode,unused,varcheck //keep it
// https://en.wikipedia.org/wiki/ANSI_escape_code
// https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97
// FgBlack terminal color code
FgBlack = 30
// FgRed terminal color code
FgRed = 31
// FgGreen terminal color code
FgGreen = 32
// FgYellow terminal color code
FgYellow = 33
// FgBlue terminal color code
FgBlue = 34
// FgMagenta terminal color code
FgMagenta = 35
// FgCyan terminal color code
FgCyan = 36
// FgLightGray terminal color code
FgLightGray = 37
// FgDarkGray terminal color code
FgDarkGray = 90
// FgLightRed terminal color code
FgLightRed = 91
// FgLightGreen terminal color code
FgLightGreen = 92
// FgLightYellow terminal color code
FgLightYellow = 93
// FgLightBlue terminal color code
FgLightBlue = 94
// FgLightMagenta terminal color code
FgLightMagenta = 95
// FgLightCyan terminal color code
FgLightCyan = 96
// FgWhite terminal color code
FgWhite = 97
// BgNormal terminal color code
BgNormal = 0
// BgBoldOrBright terminal color code
BgBoldOrBright = 1
// BgDim terminal color code
BgDim = 2
// BgItalic terminal color code
BgItalic = 3
// BgUnderline terminal color code
BgUnderline = 4
// BgUlink terminal color code
BgUlink = 5
// BgInverse _
BgInverse = 7
// BgHidden terminal color code
BgHidden = 8
// BgStrikeout terminal color code
BgStrikeout = 9
// DarkColor terminal color code
DarkColor = FgLightGray
)
var (
// onceColorPrintTranslator sync.Once
cpt colorPrintTranslator
cptNC = colorPrintTranslator{noColorMode: true}
)
func getCPT() ColorTranslator {
cptLocal := &cpt
if GetNoColorMode() {
cptLocal = &cptNC
}
return cptLocal
}
// ColorTranslator _
type ColorTranslator interface {
Translate(s string, initialFg int) string
}
type colorPrintTranslator struct {
noColorMode bool // strip color code simply
}
func (c *colorPrintTranslator) Translate(s string, initialFg int) string {
return c.TranslateTo(s, initialFg)
}
func (c *colorPrintTranslator) resetColors(sb *strings.Builder, states []int) func() {
return func() {
var st string
st = "\x1b[0m"
(*sb).WriteString(st)
if len(states) > 0 {
st = fmt.Sprintf("\x1b[%dm", states[len(states)-1])
(*sb).WriteString(st)
}
}
}
func (c *colorPrintTranslator) colorize(sb *strings.Builder, states []int, walker *func(node *html.Node, level int)) func(node *html.Node, clr int, representation string, level int) {
return func(node *html.Node, clr int, representation string, level int) {
if representation != "" {
(*sb).WriteString(fmt.Sprintf("\x1b[%sm", representation))
} else {
(*sb).WriteString(fmt.Sprintf("\x1b[%dm", clr))
}
states = append(states, clr)
for child := node.FirstChild; child != nil; child = child.NextSibling {
(*walker)(child, level+1)
}
states = states[0 : len(states)-1]
c.resetColors(sb, states)()
}
}
func (c *colorPrintTranslator) TranslateTo(s string, initialState int) string {
if c.noColorMode {
return c._ss(s)
}
node, err := html.Parse(bufio.NewReader(strings.NewReader(s)))
if err != nil {
return c._sz(s)
}
return c.translateTo(node, s, initialState)
}
func (c *colorPrintTranslator) translateTo(root *html.Node, s string, initialState int) string {
states := []int{initialState}
var sb strings.Builder
var walker func(node *html.Node, level int)
colorize := c.colorize(&sb, states, &walker)
// nilfn := func(node *html.Node, level int) {}
colorizeIt := func(clr int) func(node *html.Node, level int) {
return func(node *html.Node, level int) {
colorize(node, clr, "", level)
}
}
m := map[string]func(node *html.Node, level int){
"html": nil, "head": nil, "body": nil,
"b": colorizeIt(BgBoldOrBright), "strong": colorizeIt(BgBoldOrBright), "em": colorizeIt(BgBoldOrBright),
"i": colorizeIt(BgItalic), "cite": colorizeIt(BgItalic),
"u": colorizeIt(BgUnderline),
"mark": colorizeIt(BgInverse),
"del": colorizeIt(BgStrikeout),
}
walker = func(node *html.Node, level int) {
switch node.Type {
case html.DocumentNode, html.DoctypeNode, html.CommentNode:
case html.ErrorNode:
case html.ElementNode:
if fn, ok := m[node.Data]; ok {
if fn != nil {
fn(node, level)
return
}
}
switch node.Data {
case "font":
for _, a := range node.Attr {
if a.Key == "color" {
clr := c.toColorInt(a.Val)
colorize(node, clr, "", level)
return
}
}
case "kbd", "code":
colorize(node, 51, "51;1", level)
return
default:
// Logger.Debugf("%v, %v, lvl #%d\n", node.Type, node.Data, level)
// sb.WriteString(node.Data)
}
case html.TextNode:
// Logger.Debugf("%v, %v, lvl #%d\n", node.Type, node.Data, level)
sb.WriteString(node.Data)
return
default:
// sb.WriteString(node.Data)
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
walker(child, level+1)
}
}
walker(root, 0)
return sb.String()
}
func (c *colorPrintTranslator) _sz(s string) string {
return s
}
func (c *colorPrintTranslator) _ss(s string) string {
if tool.IsTtyEscaped(s) {
clean := tool.StripEscapes(s)
return c.stripHTMLTags(clean)
}
return c.stripHTMLTags(s)
}
var (
onceoptCM sync.Once
cptCM map[string]int
)
func (c *colorPrintTranslator) toColorInt(s string) int {
onceoptCM.Do(func() {
cptCM = map[string]int{
"black": FgBlack,
"red": FgRed,
"green": FgGreen,
"yellow": FgYellow,
"blue": FgBlue,
"magenta": FgMagenta,
"cyan": FgCyan,
"lightgray": FgLightGray, "light-gray": FgLightGray,
"darkgray": FgDarkGray, "dark-gray": FgDarkGray,
"lightred": FgLightRed, "light-red": FgLightRed,
"lightgreen": FgLightGreen, "light-green": FgLightGreen,
"lightyellow": FgLightYellow, "light-yellow": FgLightYellow,
"lightblue": FgLightBlue, "light-blue": FgLightBlue,
"lightmagenta": FgLightMagenta, "light-magenta": FgLightMagenta,
"lightcyan": FgLightCyan, "light-cyan": FgLightCyan,
"white": FgWhite,
}
})
if i, ok := cptCM[strings.ToLower(s)]; ok {
return i
}
return 0
}
func (c *colorPrintTranslator) stripLeftTabs(s string) string {
r := c.stripLeftTabsOnly(s)
return c.Translate(r, 0)
}
func (c *colorPrintTranslator) stripLeftTabsOnly(s string) string {
var lines []string
tabs := 1000
var emptyLines []int
var sb strings.Builder
var line int
noLastLF := !strings.HasSuffix(s, "\n")
scanner := bufio.NewScanner(bufio.NewReader(strings.NewReader(s)))
for scanner.Scan() {
str := scanner.Text()
i, n, allTabs := 0, len(str), true
for ; i < n; i++ {
if str[i] != '\t' {
allTabs = false
if tabs > i && i > 0 {
tabs = i
break
}
}
}
if i == n && allTabs {
emptyLines = append(emptyLines, line)
}
lines = append(lines, str)
line++
}
pad := strings.Repeat("\t", tabs)
for i, str := range lines {
switch {
case strings.HasPrefix(str, pad):
sb.WriteString(str[tabs:])
case inIntSlice(i, emptyLines):
default:
sb.WriteString(str)
}
if noLastLF && i == len(lines)-1 {
break
}
sb.WriteRune('\n')
}
return sb.String()
}
func inIntSlice(i int, slice []int) bool {
for _, n := range slice {
if n == i {
return true
}
}
return false
}
// StripLeftTabs strips the least left side tab chars from lines.
// StripLeftTabs strips html tags too.
func StripLeftTabs(s string) string { return cptNC.stripLeftTabs(s) }
// StripLeftTabsOnly strips the least left side tab chars from lines.
func StripLeftTabsOnly(s string) string { return cptNC.stripLeftTabsOnly(s) }
// StripHTMLTags aggressively strips HTML tags from a string.
// It will only keep anything between `>` and `<`.
func StripHTMLTags(s string) string { return cptNC.stripHTMLTags(s) }
const (
htmlTagStart = 60 // Unicode `<`
htmlTagEnd = 62 // Unicode `>`
)
// Aggressively strips HTML tags from a string.
// It will only keep anything between `>` and `<`.
func (c *colorPrintTranslator) stripHTMLTags(s string) string {
// Setup a string builder and allocate enough memory for the new string.
var builder strings.Builder
builder.Grow(len(s) + utf8.UTFMax)
in := false // True if we are inside an HTML tag.
start := 0 // The index of the previous start tag character `<`
end := 0 // The index of the previous end tag character `>`
for i, c := range s {
// If this is the last character and we are not in an HTML tag, save it.
if (i+1) == len(s) && end >= start {
builder.WriteString(s[end:])
}
// Keep going if the character is not `<` or `>`
if c != htmlTagStart && c != htmlTagEnd {
continue
}
if c == htmlTagStart {
// Only update the start if we are not in a tag.
// This make sure we strip out `<<br>` not just `<br>`
if !in {
start = i
}
in = true
// Write the valid string between the close and start of the two tags.
builder.WriteString(s[end:start])
continue
}
// else c == htmlTagEnd
in = false
end = i + 1
}
s = builder.String()
return s
}