forked from itchyny/gojq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
75 lines (68 loc) · 1.45 KB
/
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
package cli
import (
"bytes"
"fmt"
"strings"
)
var noColor bool
func newColor(c string) []byte {
return []byte("\x1b[" + c + "m")
}
func setColor(buf *bytes.Buffer, color []byte) {
if !noColor {
buf.Write(color)
}
}
var (
resetColor = newColor("0") // Reset
nullColor = newColor("90") // Bright black
falseColor = newColor("33") // Yellow
trueColor = newColor("33") // Yellow
numberColor = newColor("36") // Cyan
stringColor = newColor("32") // Green
objectKeyColor = newColor("34;1") // Bold Blue
arrayColor = []byte(nil) // No color
objectColor = []byte(nil) // No color
)
func validColor(x string) bool {
var num bool
for _, c := range x {
if '0' <= c && c <= '9' {
num = true
} else if c == ';' && num {
num = false
} else {
return false
}
}
return num || x == ""
}
func setColors(colors string) error {
var i int
var color string
for _, target := range []*[]byte{
&nullColor, &falseColor, &trueColor, &numberColor,
&stringColor, &objectKeyColor, &arrayColor, &objectColor,
} {
if i < len(colors) {
if j := strings.IndexByte(colors[i:], ':'); j >= 0 {
color = colors[i : i+j]
i += j + 1
} else {
color = colors[i:]
i = len(colors)
}
if !validColor(color) {
return fmt.Errorf("invalid color: %q", color)
}
if color == "" {
*target = nil
} else {
*target = newColor(color)
}
} else {
*target = nil
}
}
return nil
}