-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgr.go
296 lines (252 loc) · 6.66 KB
/
sgr.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
// go-ansisgr provides an SGR (Select Graphic Rendition, a part of ANSI Escape Sequence) parser.
package ansisgr
import (
"unicode"
)
const (
attrReset = 0
attrBold = 1 << iota
attrDim
attrItalic
attrUnderline
attrBlink
attrReverse
attrInvisible
attrStrikethrough
)
const (
colorValid = 1 << (24 + iota)
colorIs16
colorIs256
colorIsRGB
)
// Style represents a set of attributes, foreground color, and background color.
type Style struct {
attr int
foreground int
background int
}
// Mode represents a color syntax.
type Mode int
const (
// ModeNone is used to when color is not specified.
ModeNone Mode = iota
// Mode16 represents 16 colors.
Mode16
// Mode256 represents 256 colors.
Mode256
// ModeRGB represents RGB colors (a.k.a. True Color).
ModeRGB
)
// Color represents color value specified by SGR.
type Color struct{ v int }
// Mode returns the color's Mode.
func (c Color) Mode() Mode {
switch {
case c.v&colorIs16 == colorIs16:
return Mode16
case c.v&colorIs256 == colorIs256:
return Mode256
case c.v&colorIsRGB == colorIsRGB:
return ModeRGB
}
return ModeNone
}
// RGB returns red, green, and blue color values. It assumes that c.Mode() == ModeRGB.
func (c Color) RGB() (int, int, int) {
return 0xff0000 & c.v >> 16, 0x00ff00 & c.v >> 8, 0x0000ff & c.v
}
// Value returns the color value represented by int.
// For example, '\x1b[31m' is 31, '\x1b[38;5;116m' is 116, and '\x1b[38;2;10;20;30' is 660510 (0x0a141e).
func (c Color) Value() int { return c.v & 0xffffff }
// Foreground returns the foreground color. the second return value indicates whether the color is valid or not.
func (s *Style) Foreground() (Color, bool) {
return Color{v: s.foreground}, s.foreground&colorValid == colorValid
}
// Background returns the background color. the second return value indicates whether the color is valid or not.
func (s *Style) Background() (Color, bool) {
return Color{v: s.background}, s.background&colorValid == colorValid
}
// Bold indicates whether bold is enabled.
func (s *Style) Bold() bool { return s.attr&attrBold == attrBold }
// Dim indicates whether dim (faint) is enabled.
func (s *Style) Dim() bool { return s.attr&attrDim == attrDim }
// Italic indicates whether italic is enabled.
func (s *Style) Italic() bool { return s.attr&attrItalic == attrItalic }
// Underline indicates whether underline is enabled.
func (s *Style) Underline() bool { return s.attr&attrUnderline == attrUnderline }
// Blink indicates whether blink is enabled.
func (s *Style) Blink() bool { return s.attr&attrBlink == attrBlink }
// Reverse indicates whether reverse (hidden) is enabled.
func (s *Style) Reverse() bool { return s.attr&attrReverse == attrReverse }
// Invisible indicates whether invisible is enabled.
func (s *Style) Invisible() bool { return s.attr&attrInvisible == attrInvisible }
// Strikethrough indicates whether strikethrough is enabled.
func (s *Style) Strikethrough() bool { return s.attr&attrStrikethrough == attrStrikethrough }
// Iterator is an iterator over a parsed string.
type Iterator struct {
runes []rune
i int
style Style
}
// NewIterator returns a new Iterator.
func NewIterator(s string) *Iterator {
return &Iterator{i: -1, runes: []rune(s)}
}
// Next returns a next rune and its style. the third return value indicates there is a next value.
func (a *Iterator) Next() (rune, Style, bool) {
for a.next() {
r := a.runes[a.i]
if r != 0x1b {
return r, a.style, true
}
if ok := a.next(); ok && a.runes[a.i] != '[' {
continue
}
args := a.consumeSequence()
LOOP:
for len(args) != 0 {
if args[0] == 38 {
offset, consumed := consumeAs256OrRGB(args)
args = args[offset:]
if consumed.valid {
a.style.foreground = consumed.v
continue
}
} else if args[0] == 48 {
offset, consumed := consumeAs256OrRGB(args)
args = args[offset:]
if consumed.valid {
a.style.background = consumed.v
continue
}
}
for _, arg := range args {
switch arg {
case 38, 48:
if len(args) != 1 {
continue LOOP
}
// Ignore when the arg is the last element.
case 0:
a.style.attr = attrReset
a.style.foreground = 0 | colorIs16
a.style.background = 0 | colorIs16
case 39:
a.style.foreground = 0 | colorIs16
case 49:
a.style.background = 0 | colorIs16
case 1:
a.style.attr |= attrBold
case 2:
a.style.attr |= attrDim
case 3:
a.style.attr |= attrItalic
case 4:
a.style.attr |= attrUnderline
case 5:
a.style.attr |= attrBlink
case 7:
a.style.attr |= attrReverse
case 8:
a.style.attr |= attrInvisible
case 9:
a.style.attr |= attrStrikethrough
case 22:
if a.style.attr&attrBold == attrBold {
a.style.attr ^= attrBold
}
if a.style.attr&attrDim == attrDim {
a.style.attr ^= attrDim
}
case 23:
a.style.attr ^= attrItalic
case 24:
a.style.attr ^= attrUnderline
case 25:
a.style.attr ^= attrBlink
case 27:
a.style.attr ^= attrReverse
case 28:
a.style.attr ^= attrInvisible
case 29:
a.style.attr ^= attrStrikethrough
default:
switch {
case (arg >= 30 && arg <= 37):
a.style.foreground = arg | colorIs16 | colorValid
case arg >= 40 && arg <= 47:
a.style.background = arg | colorIs16 | colorValid
case arg >= 90 && arg <= 97:
a.style.foreground = arg | colorIs16 | colorValid
case arg >= 100 && arg <= 107:
a.style.background = arg | colorIs16 | colorValid
}
}
args = args[1:]
}
}
}
return 0, Style{}, false
}
func (a *Iterator) next() bool {
a.i++
return a.i < len(a.runes)
}
func (a *Iterator) consumeSequence() []int {
var args []int
var val int
for a.next() {
switch a.runes[a.i] {
case 'm':
return append(args, val)
case ';':
args = append(args, val)
val = 0
default:
if !unicode.IsDigit(a.runes[a.i]) {
return nil // Broken sequence, ignore
}
val = val*10 + int(a.runes[a.i]-'0')
}
}
return nil
}
type consumedValue struct {
v int
valid bool
}
func consumeAs256OrRGB(args []int) (offset int, v consumedValue) {
if len(args) < 2 || (args[0] != 38 && args[0] != 48) {
return 0, v
}
switch args[1] {
case 5: // 256
if l := len(args); l < 3 {
return l, v
}
if args[2] > 255 {
return 3, v
}
return 3, consumedValue{
v: args[2] | colorValid | colorIs256,
valid: true,
}
case 2: // RGB
if l := len(args); l < 5 {
return l, v
}
var val int
for i := 0; i < 3; i++ {
if args[i+2] > 255 {
return i + 2 + 1, v
}
val |= args[i+2] << (8 * (2 - i))
}
return 5, consumedValue{
v: val | colorValid | colorIsRGB,
valid: true,
}
}
return 2, v
}