-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
551 lines (431 loc) · 12.5 KB
/
parse.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package bunt
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"sort"
"strconv"
"strings"
)
var (
escapeSeqRegExp = regexp.MustCompile(`\x1b\[(\d+(;\d+)*)m`)
boldMarker = regexp.MustCompile(`\*([^*]+?)\*`)
italicMarker = regexp.MustCompile(`_([^_]+?)_`)
underlineMarker = regexp.MustCompile(`~([^~]+?)~`)
colorMarker = regexp.MustCompile(`(#?\w+)\{([^}]+?)\}`)
)
// ParseOption defines parser options
type ParseOption func(*String) error
// ProcessTextAnnotations specifies whether during parsing bunt-specific text
// annotations like *bold*, or _italic_ should be processed.
func ProcessTextAnnotations() ParseOption {
return func(s *String) error {
return processTextAnnotations(s)
}
}
// ParseStream reads from the input reader and parses all supported ANSI
// sequences that are relevant for colored strings.
//
// Please notes: This function is still under development and should replace
// the `ParseString` function code eventually.
func ParseStream(in io.Reader, opts ...ParseOption) (*String, error) {
var input *bufio.Reader
switch typed := in.(type) {
case *bufio.Reader:
input = typed
default:
input = bufio.NewReader(in)
}
type seq struct {
values string
suffix rune
}
var readSGR = func() seq {
var buf bytes.Buffer
for {
r, _, err := input.ReadRune()
if err == io.EOF {
break
}
switch r {
case 'h', 'l', 'm', 'r', 'A', 'B', 'C', 'D', 'H', 'f', 'g', 'K', 'J', 'y', 'q':
return seq{
values: buf.String(),
suffix: r,
}
default:
buf.WriteRune(r)
}
}
panic("failed to parse ANSI sequence")
}
var skipUntil = func(end rune) {
for {
r, _, err := input.ReadRune()
if err == io.EOF {
panic("reached end of file before reaching end identifier")
}
if r == end {
return
}
}
}
var asUint = func(s string) uint {
tmp, err := strconv.ParseUint(s, 10, 8)
if err != nil {
panic(err)
}
return uint(tmp)
}
var result String
var line String
var lineIdx uint
var lineCap uint
var settings uint64
var readANSISeq = func() {
r, _, err := input.ReadRune()
if err == io.EOF {
return
}
// https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
switch r {
case '[':
var seq = readSGR()
switch seq.suffix {
case 'm': // colors
settings, err = parseSelectGraphicRenditionEscapeSequence(seq.values)
if err != nil {
panic(err)
}
case 'D': // move cursor left n lines
n := asUint(seq.values)
if n > lineIdx {
panic("move cursor value is out of bounds")
}
lineIdx -= n
case 'K': // clear line
var start, end uint
switch seq.values {
case "", "0":
start, end = lineIdx, lineCap
case "1":
start, end = 0, lineIdx
case "2":
start, end = 0, lineCap
}
for i := start; i < end; i++ {
line[i].Symbol = ' '
line[i].Settings = 0
}
default:
// ignoring all other sequences
}
case ']':
skipUntil('\a')
}
}
var add = func(r rune) {
var cr = ColoredRune{Symbol: r, Settings: settings}
if lineIdx < lineCap {
line[lineIdx] = cr
lineIdx++
} else {
line = append(line, cr)
lineIdx++
lineCap++
}
}
var del = func() {
line = line[:len(line)-1]
lineIdx--
lineCap--
}
var flush = func() {
// Prepare to remove trailing spaces by finding the last non-space rune
var endIdx = len(line) - 1
for ; endIdx >= 0; endIdx-- {
if line[endIdx].Symbol != ' ' {
break
}
}
result = append(result, line[:endIdx+1]...)
line = String{}
lineIdx = 0
lineCap = 0
}
var newline = func() {
flush()
result = append(result, ColoredRune{Symbol: '\n'})
}
for {
r, _, err := input.ReadRune()
if err != nil && err == io.EOF {
break
}
switch r {
case '\x1b':
readANSISeq()
case '\r':
lineIdx = 0
case '\n':
newline()
case '\b':
del()
default:
add(r)
}
}
flush()
// Process optional parser options
for _, opt := range opts {
if err := opt(&result); err != nil {
return nil, err
}
}
return &result, nil
}
// ParseString parses a string that can contain both ANSI escape code Select
// Graphic Rendition (SGR) codes and Markdown style text annotations, for
// example *bold* or _italic_.
// SGR details : https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
func ParseString(input string, opts ...ParseOption) (*String, error) {
var (
pointer int
current uint64
err error
)
// Special case: the escape sequence without any parameter is equivalent to
// the reset escape sequence.
input = strings.Replace(input, "\x1b[m", "\x1b[0m", -1)
// Ignore 'Set cursor key to application' sequence
input = strings.Replace(input, "\x1b[?1h", "", -1)
// Ignore keypad mode settings
input = strings.Replace(input, "\x1b=", "", -1)
input = strings.Replace(input, "\x1b>", "", -1)
// Ignore clear line from cursor right
input = strings.Replace(input, "\x1b[K", "", -1)
// Ignore known mode settings
input = regexp.MustCompile(`\x1b\[\?.+[lh]`).ReplaceAllString(input, "")
// Ignore this unknown sequence, which seems to be an conditional check
input = regexp.MustCompile(`\x1b\]11;\?.`).ReplaceAllString(input, "")
var result String
var applyToResult = func(str string, mask uint64) {
for _, r := range str {
result = append(result, ColoredRune{r, mask})
}
}
for _, submatch := range escapeSeqRegExp.FindAllStringSubmatchIndex(input, -1) {
fullMatchStart, fullMatchEnd := submatch[0], submatch[1]
settingsStart, settingsEnd := submatch[2], submatch[3]
applyToResult(input[pointer:fullMatchStart], current)
current, err = parseSelectGraphicRenditionEscapeSequence(input[settingsStart:settingsEnd])
if err != nil {
return nil, err
}
pointer = fullMatchEnd
}
// Flush the remaining input string part into the result
applyToResult(input[pointer:], current)
// Process optional parser options
for _, opt := range opts {
if err = opt(&result); err != nil {
return nil, err
}
}
return &result, nil
}
func parseSelectGraphicRenditionEscapeSequence(escapeSeq string) (uint64, error) {
values := []uint8{}
for _, x := range strings.Split(escapeSeq, ";") {
// Note: This only works, because of the regular expression only matching
// digits. Therefore, it should be okay to omit the error.
value, _ := strconv.Atoi(x)
values = append(values, uint8(value))
}
result := uint64(0)
for i := 0; i < len(values); i++ {
switch values[i] {
case 1: // bold
result |= boldMask
case 3: // italic
result |= italicMask
case 4: // underline
result |= underlineMask
case 30: // Black
result |= fgRGBMask(1, 1, 1)
case 31: // Red
result |= fgRGBMask(222, 56, 43)
case 32: // Green
result |= fgRGBMask(57, 181, 74)
case 33: // Yellow
result |= fgRGBMask(255, 199, 6)
case 34: // Blue
result |= fgRGBMask(0, 111, 184)
case 35: // Magenta
result |= fgRGBMask(118, 38, 113)
case 36: // Cyan
result |= fgRGBMask(44, 181, 233)
case 37: // White
result |= fgRGBMask(204, 204, 204)
case 90: // Bright Black (Gray)
result |= fgRGBMask(128, 128, 128)
case 91: // Bright Red
result |= fgRGBMask(255, 0, 0)
case 92: // Bright Green
result |= fgRGBMask(0, 255, 0)
case 93: // Bright Yellow
result |= fgRGBMask(255, 255, 0)
case 94: // Bright Blue
result |= fgRGBMask(0, 0, 255)
case 95: // Bright Magenta
result |= fgRGBMask(255, 0, 255)
case 96: // Bright Cyan
result |= fgRGBMask(0, 255, 255)
case 97: // Bright White
result |= fgRGBMask(255, 255, 255)
case 40: // Black
result |= bgRGBMask(1, 1, 1)
case 41: // Red
result |= bgRGBMask(222, 56, 43)
case 42: // Green
result |= bgRGBMask(57, 181, 74)
case 43: // Yellow
result |= bgRGBMask(255, 199, 6)
case 44: // Blue
result |= bgRGBMask(0, 111, 184)
case 45: // Magenta
result |= bgRGBMask(118, 38, 113)
case 46: // Cyan
result |= bgRGBMask(44, 181, 233)
case 47: // White
result |= bgRGBMask(204, 204, 204)
case 100: // Bright Black (Gray)
result |= bgRGBMask(128, 128, 128)
case 101: // Bright Red
result |= bgRGBMask(255, 0, 0)
case 102: // Bright Green
result |= bgRGBMask(0, 255, 0)
case 103: // Bright Yellow
result |= bgRGBMask(255, 255, 0)
case 104: // Bright Blue
result |= bgRGBMask(0, 0, 255)
case 105: // Bright Magenta
result |= bgRGBMask(255, 0, 255)
case 106: // Bright Cyan
result |= bgRGBMask(0, 255, 255)
case 107: // Bright White
result |= bgRGBMask(255, 255, 255)
case 38: // foreground color
switch {
case len(values) > 4 && values[i+1] == 2:
result |= fgRGBMask(uint64(values[i+2]), uint64(values[i+3]), uint64(values[i+4]))
i += 4
case len(values) > 2 && values[i+1] == 5:
r, g, b := lookUp8bitColor(values[i+2])
result |= fgRGBMask(uint64(r), uint64(g), uint64(b))
i += 2
default:
return 0, fmt.Errorf("unsupported foreground color selection '%v'", values)
}
case 48: // background color
switch {
case len(values) > 4 && values[i+1] == 2:
result |= bgRGBMask(uint64(values[i+2]), uint64(values[i+3]), uint64(values[i+4]))
i += 4
case len(values) > 2 && values[i+1] == 5:
r, g, b := lookUp8bitColor(values[i+2])
result |= bgRGBMask(uint64(r), uint64(g), uint64(b))
i += 2
default:
return 0, fmt.Errorf("unsupported background color selection '%v'", values)
}
}
}
return result, nil
}
func processTextAnnotations(text *String) error {
var buffer bytes.Buffer
for _, coloredRune := range *text {
_ = buffer.WriteByte(byte(coloredRune.Symbol))
}
raw := buffer.String()
toBeDeleted := []int{}
// Process text annotation markers for bold, italic and underline
helperMap := map[uint64]*regexp.Regexp{
boldMask: boldMarker,
italicMask: italicMarker,
underlineMask: underlineMarker,
}
for mask, regex := range helperMap {
for _, match := range regex.FindAllStringSubmatchIndex(raw, -1) {
fullMatchStart, fullMatchEnd := match[0], match[1]
textStart, textEnd := match[2], match[3]
for i := textStart; i < textEnd; i++ {
(*text)[i].Settings |= mask
}
toBeDeleted = append(toBeDeleted, fullMatchStart, fullMatchEnd-1)
}
}
// Process text annotation markers that specify a foreground color for a
// specific part of the text
for _, match := range colorMarker.FindAllStringSubmatchIndex(raw, -1) {
fullMatchStart, fullMatchEnd := match[0], match[1]
colorName := raw[match[2]:match[3]]
textStart, textEnd := match[4], match[5]
color := lookupColorByName(colorName)
if color == nil {
return fmt.Errorf("unable to find color by name: %s", colorName)
}
r, g, b := color.RGB255()
for i := textStart; i < textEnd; i++ {
(*text)[i].Settings |= fgMask
(*text)[i].Settings |= uint64(r) << 8
(*text)[i].Settings |= uint64(g) << 16
(*text)[i].Settings |= uint64(b) << 24
}
for i := fullMatchStart; i < fullMatchEnd; i++ {
if i < textStart || i > textEnd-1 {
toBeDeleted = append(toBeDeleted, i)
}
}
}
// Finally, sort the runes to be deleted in descending order and delete them
// one by one to get rid of the text annotation markers
sort.Slice(toBeDeleted, func(i, j int) bool {
return toBeDeleted[i] > toBeDeleted[j]
})
for _, idx := range toBeDeleted {
(*text) = append((*text)[:idx], (*text)[idx+1:]...)
}
return nil
}
func fgRGBMask(r, g, b uint64) uint64 {
return fgMask | r<<8 | g<<16 | b<<24
}
func bgRGBMask(r, g, b uint64) uint64 {
return bgMask | r<<32 | g<<40 | b<<48
}
func lookUp8bitColor(n uint8) (r, g, b uint8) {
return colorPalette8bit[n].RGB255()
}