-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansi.go
145 lines (138 loc) · 3.59 KB
/
ansi.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
package brimtext
import (
"bytes"
"strconv"
"strings"
)
// ANSIEscapeCodes is the defining structure for the more commonly used
// ANSIEscape global variable.
type ANSIEscapeCodes struct {
Reset []byte
Bold []byte
BBlack, BRed, BGreen, BYellow, BBlue, BMagenta, BCyan, BWhite []byte
FBlack, FRed, FGreen, FYellow, FBlue, FMagenta, FCyan, FWhite []byte
}
// ANSIEscape provides ease of access to common ANSI Escape Codes.
var ANSIEscape = ANSIEscapeCodes{
Reset: []byte{27, '[', '0', 'm'},
Bold: []byte{27, '[', '1', 'm'},
BBlack: []byte{27, '[', '4', '0', 'm'},
BRed: []byte{27, '[', '4', '1', 'm'},
BGreen: []byte{27, '[', '4', '2', 'm'},
BYellow: []byte{27, '[', '4', '3', 'm'},
BBlue: []byte{27, '[', '4', '4', 'm'},
BMagenta: []byte{27, '[', '4', '5', 'm'},
BCyan: []byte{27, '[', '4', '6', 'm'},
BWhite: []byte{27, '[', '4', '7', 'm'},
FBlack: []byte{27, '[', '3', '0', 'm'},
FRed: []byte{27, '[', '3', '1', 'm'},
FGreen: []byte{27, '[', '3', '2', 'm'},
FYellow: []byte{27, '[', '3', '3', 'm'},
FBlue: []byte{27, '[', '3', '4', 'm'},
FMagenta: []byte{27, '[', '3', '5', 'm'},
FCyan: []byte{27, '[', '3', '6', 'm'},
FWhite: []byte{27, '[', '3', '7', 'm'},
}
// ClosestANSIForegroundString translates the CSS-style color (e.g. "#ac8"
// "#ffee66") string to the closest ANSIEscape sequence for that foreground
// color.
func ClosestANSIForegroundString(value string) []byte {
value = strings.ToLower(value)
if value == "" {
return []byte{}
} else if value[0] == '#' {
value = value[1:]
}
if len(value) == 3 {
red, _ := strconv.ParseInt(value[:1], 16, 0)
green, _ := strconv.ParseInt(value[1:2], 16, 0)
blue, _ := strconv.ParseInt(value[2:], 16, 0)
red <<= 4
green <<= 4
blue <<= 4
return ClosestANSIForeground(int(red), int(green), int(blue))
} else if len(value) == 6 {
red, _ := strconv.ParseInt(value[:2], 16, 0)
green, _ := strconv.ParseInt(value[2:4], 16, 0)
blue, _ := strconv.ParseInt(value[4:], 16, 0)
return ClosestANSIForeground(int(red), int(green), int(blue))
} else {
return []byte{}
}
}
// ClosestANSIForeground translates the RGB values to the closest ANSIEscape
// sequence for that foreground color.
func ClosestANSIForeground(red int, green int, blue int) []byte {
var sequence bytes.Buffer
if red < 96 {
red = 0
} else if red < 175 {
red = 1
} else {
red = 2
}
if green < 96 {
green = 0
} else if green < 175 {
green = 1
} else {
green = 2
}
if blue < 96 {
blue = 0
} else if blue < 175 {
blue = 1
} else {
blue = 2
}
if red == 2 || green == 2 || blue == 2 {
sequence.Write(ANSIEscape.Bold)
}
if red == 0 {
if green == 0 {
if blue == 0 {
sequence.Write(ANSIEscape.FBlack)
} else {
sequence.Write(ANSIEscape.FBlue)
}
} else {
if blue == 0 {
sequence.Write(ANSIEscape.FGreen)
} else {
sequence.Write(ANSIEscape.FCyan)
}
}
} else if green == 0 {
if blue == 0 {
sequence.Write(ANSIEscape.FRed)
} else {
sequence.Write(ANSIEscape.FMagenta)
}
} else {
if blue == 0 {
sequence.Write(ANSIEscape.FYellow)
} else {
sequence.Write(ANSIEscape.FWhite)
}
}
return sequence.Bytes()
}
func StripANSIEscapes(v string) string {
bs := []byte(v)
ln := len(bs)
for i := 0; i < ln; i++ {
if bs[i] == 27 {
for j := i; j < ln; j++ {
if bs[j] == 'm' {
copy(bs[i:], bs[j+1:])
ln -= j + 1 - i
break
}
}
}
}
return string(bs[:ln])
}
func RuneLenStripANSIEscapes(v string) int {
return len([]rune(StripANSIEscapes(v)))
}