forked from jesseduffield/gocui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescape.go
587 lines (520 loc) · 13.2 KB
/
escape.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocui
import (
"strconv"
"strings"
"sync"
"github.com/go-errors/errors"
)
type escapeInterpreter struct {
state escapeState
curch rune
csiParam []string
oscParam []rune
curFgColor, curBgColor Attribute
mode OutputMode
mutex sync.Mutex
instruction instruction
}
/// new algorithm: if we hit an escape key start a sequence
// if we then hit a terminator key, process the sequence
const (
NONE = 1 << iota
CURSOR_UP
CURSOR_DOWN
CURSOR_LEFT
CURSOR_RIGHT
CURSOR_MOVE
CLEAR_SCREEN
ERASE_IN_LINE
INSERT_CHARACTER
DELETE
SAVE_CURSOR_POSITION
RESTORE_CURSOR_POSITION
WRITE
SWITCH_TO_ALTERNATE_SCREEN
SWITCH_BACK_FROM_ALTERNATE_SCREEN
SET_SCROLL_MARGINS
INSERT_LINES
DELETE_LINES
)
type instruction struct {
kind int
param1 int
param2 int
toWrite []rune
}
type escapeState int
const (
stateNone escapeState = iota
stateEscape
stateCSI
stateParams
stateCharacterSet
stateOSC
)
var directionMap = map[rune]int{
'A': CURSOR_UP,
'B': CURSOR_DOWN,
'C': CURSOR_RIGHT,
'D': CURSOR_LEFT,
}
var (
errNotCSI = errors.New("Not a CSI escape sequence")
errCSIParseError = errors.New("CSI escape sequence parsing error")
errCSITooLong = errors.New("CSI escape sequence is too long")
)
// runes in case of error will output the non-parsed runes as a string.
func (ei *escapeInterpreter) runes() []rune {
ei.mutex.Lock()
defer ei.mutex.Unlock()
switch ei.state {
case stateNone:
return []rune{0x1b}
case stateEscape:
return []rune{0x1b, ei.curch}
case stateCSI:
return []rune{0x1b, '[', ei.curch}
case stateParams:
ret := []rune{0x1b, '['}
for i, s := range ei.csiParam {
ret = append(ret, []rune(s)...)
if i < len(ei.csiParam)-1 {
ret = append(ret, ';')
}
}
return append(ret, ei.curch)
}
return nil
}
// newEscapeInterpreter returns an escapeInterpreter that will be able to parse
// terminal escape sequences.
func newEscapeInterpreter(mode OutputMode) *escapeInterpreter {
ei := &escapeInterpreter{
state: stateNone,
curFgColor: ColorDefault,
curBgColor: ColorDefault,
mode: mode,
instruction: instruction{kind: NONE},
}
return ei
}
// reset sets the escapeInterpreter in initial state.
func (ei *escapeInterpreter) reset() {
ei.mutex.Lock()
defer ei.mutex.Unlock()
ei.state = stateNone
ei.curFgColor = ColorDefault
ei.curBgColor = ColorDefault
ei.csiParam = nil
ei.oscParam = nil
}
func (ei *escapeInterpreter) instructionRead() {
ei.instruction.kind = NONE
}
func (ei *escapeInterpreter) parseOSCParams() {
str := string(ei.oscParam)
params := strings.Split(str, ";")
switch params[0] {
case "0", "2":
// ignoring attempts at setting the title
return
case "11":
// ignoring for now
// ei.instruction.kind = WRITE
// ei.instruction.toWrite = []rune{0x1b, ']', '1', '1', ';', 'r', 'g', 'b', ':', '0', '0', '/', '0', '0', '/', '0', '0', 0x1b, '\\'}
default:
// ignoring for now
}
}
// parseOne parses a rune. If isEscape is true, it means that the rune is part
// of an escape sequence, and as such should not be printed verbatim. Otherwise,
// it's not an escape sequence.
func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error) {
ei.mutex.Lock()
defer ei.mutex.Unlock()
// Sanity checks
if len(ei.csiParam) > 20 {
return false, errCSITooLong
}
if len(ei.csiParam) > 0 && len(ei.csiParam[len(ei.csiParam)-1]) > 255 {
return false, errCSITooLong
}
ei.curch = ch
switch ei.state {
case stateNone:
if ch == 0x1b {
ei.state = stateEscape
return true, nil
}
return false, nil
case stateEscape:
switch ch {
case '[':
ei.state = stateCSI
return true, nil
case ']':
ei.state = stateOSC
return true, nil
case '(':
ei.state = stateCharacterSet
return true, nil
case '=':
// set Keypad Numeric Mode
// not implemented
ei.state = stateNone
return true, nil
case '>':
// set Keypad Application Mode
// not implemented
ei.state = stateNone
return true, nil
default:
return false, errNotCSI
}
case stateOSC:
// either we have a bell in which case we parse what we've gotten so far
// or we append to our params
if ch == '\a' {
ei.parseOSCParams()
ei.state = stateNone
return true, nil
} else {
ei.oscParam = append(ei.oscParam, ch)
return true, nil
}
case stateCharacterSet:
// doing nothing for now.
// see 'ESC ( C ' in https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Functions-using-CSI-_-ordered-by-the-final-character_s_
ei.state = stateNone
return true, nil
case stateCSI:
switch {
case ch >= '0' && ch <= '9', ch == '?', ch == '>', ch == ';':
ei.csiParam = append(ei.csiParam, "")
case ch == 'A', ch == 'B', ch == 'C', ch == 'D':
ei.csiParam = append(ei.csiParam, "1")
case ch == 'H', ch == 'f', ch == 'm', ch == 'K', ch == 'J':
ei.csiParam = append(ei.csiParam, "0")
case ch == 's':
ei.instruction.kind = SAVE_CURSOR_POSITION
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'u':
ei.instruction.kind = RESTORE_CURSOR_POSITION
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'L':
ei.instruction.param1 = 1
ei.instruction.kind = INSERT_LINES
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'M':
ei.instruction.param1 = 1
ei.instruction.kind = DELETE_LINES
ei.state = stateNone
ei.csiParam = nil
return true, nil
default:
return false, errCSIParseError
}
ei.state = stateParams
fallthrough
case stateParams:
switch {
case ch >= '0' && ch <= '9', ch == '?', ch == '>':
ei.csiParam[len(ei.csiParam)-1] += string(ch)
return true, nil
case ch == ';':
ei.csiParam = append(ei.csiParam, "")
return true, nil
case ch == 'm':
var err error
switch ei.mode {
case OutputNormal:
err = ei.outputNormal()
case Output256:
err = ei.output256()
}
if err != nil {
return false, errCSIParseError
}
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'A', ch == 'B', ch == 'C', ch == 'D':
p, err := strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
ei.instruction.kind = directionMap[ch]
ei.instruction.param1 = p
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'J':
ei.instruction.kind = CLEAR_SCREEN
ei.instruction.param1, err = strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'S':
panic("got an S")
case ch == 'K':
p, err := strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
ei.instruction.kind = ERASE_IN_LINE
ei.instruction.param1 = p
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'r':
p, err := strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
p2, err := strconv.Atoi(ei.csiParam[1])
if err != nil {
return false, errCSIParseError
}
ei.instruction.kind = SET_SCROLL_MARGINS
ei.instruction.param1 = p
ei.instruction.param2 = p2
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'L':
ei.instruction.param1 = 1
if len(ei.csiParam) > 0 {
ei.instruction.param1, err = strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
}
ei.instruction.kind = INSERT_LINES
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'M':
ei.instruction.param1 = 1
if len(ei.csiParam) > 0 {
ei.instruction.param1, err = strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
}
ei.instruction.kind = DELETE_LINES
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'h':
// see https://vt100.net/docs/vt100-ug/chapter3.html
switch ei.csiParam[0] {
// case "?25":
// // wants us to show the cursor but we never hide it in the first place
// case "?2004":
// // it's trying to set bracketed paste mode. Not sure what to do here
case "?1049":
ei.instruction.kind = SWITCH_TO_ALTERNATE_SCREEN
// case "?1":
// // setting cursor key to application. unimplemented
// case "?12":
// // see https://www.inwap.com/pdp10/ansicode.txt
// // [12h = SRM - Send Receive Mode, transmit without local echo
// // unimplemented
// case "?1000":
// // unimplemented
// case "?1015":
// // unimplemented
// case "?1006":
// // unimplemented
// case "?1002":
// // unimplemented
default:
ei.state = stateNone
ei.csiParam = nil
return true, nil
return false, errCSIParseError
}
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'l':
// see https://vt100.net/docs/vt100-ug/chapter3.html
switch ei.csiParam[0] {
// case "?25":
// // wants us to show the cursor but we never hide it in the first place
// case "?2004":
// // it's trying to set bracketed paste mode. Not sure what to do here
case "?1049":
ei.instruction.kind = SWITCH_BACK_FROM_ALTERNATE_SCREEN
// switch to alternate screen. unimplemented
// case "?1":
// // setting cursor key to application. unimplemented
// case "?12":
// // see https://www.inwap.com/pdp10/ansicode.txt
// // [12h = SRM - Send Receive Mode, transmit without local echo
// // unimplemented
// case "?1000":
// // unimplemented
// case "?1015":
// // unimplemented
// case "?1006":
// // unimplemented
// case "?1002":
// // unimplemented
default:
ei.state = stateNone
ei.csiParam = nil
return true, nil
return false, errCSIParseError
}
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'c':
if ei.csiParam[0] != ">" {
return false, errCSIParseError
}
// I don't think we need to care about this. See https://stackoverflow.com/questions/29939026/what-is-the-ansi-escape-code-sequence-escc
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == '@':
p, err := strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
if p > 1 {
panic("don't yet support inserting more than one character")
}
ei.instruction.kind = INSERT_CHARACTER
ei.instruction.param1 = p
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'P':
p, err := strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
ei.instruction.kind = DELETE
ei.instruction.param1 = p
ei.state = stateNone
ei.csiParam = nil
return true, nil
case ch == 'H', ch == 'f':
y := 0
if len(ei.csiParam) > 0 {
y, err = strconv.Atoi(ei.csiParam[0])
if err != nil {
return false, errCSIParseError
}
}
x := 0
if len(ei.csiParam) > 1 {
x, err = strconv.Atoi(ei.csiParam[1])
if err != nil {
return false, errCSIParseError
}
}
ei.instruction.kind = CURSOR_MOVE
ei.instruction.param1 = y
ei.instruction.param2 = x
ei.state = stateNone
ei.csiParam = nil
return true, nil
default:
return false, errCSIParseError
}
}
return false, nil
}
// outputNormal provides 8 different colors:
// black, red, green, yellow, blue, magenta, cyan, white
func (ei *escapeInterpreter) outputNormal() error {
for _, param := range ei.csiParam {
p, err := strconv.Atoi(param)
if err != nil {
return errCSIParseError
}
switch {
case p >= 30 && p <= 37:
ei.curFgColor |= Attribute(p - 30 + 1)
case p == 39:
ei.curFgColor |= ColorDefault
case p >= 40 && p <= 47:
ei.curBgColor |= Attribute(p - 40 + 1)
case p == 49:
ei.curBgColor |= ColorDefault
case p == 1:
ei.curFgColor |= AttrBold
case p == 4:
ei.curFgColor |= AttrUnderline
case p == 7:
ei.curFgColor |= AttrReverse
case p == 0:
ei.curFgColor = ColorDefault
ei.curBgColor = ColorDefault
}
}
return nil
}
// output256 allows you to leverage the 256-colors terminal mode:
// 0x01 - 0x08: the 8 colors as in OutputNormal
// 0x09 - 0x10: Color* | AttrBold
// 0x11 - 0xe8: 216 different colors
// 0xe9 - 0x1ff: 24 different shades of grey
func (ei *escapeInterpreter) output256() error {
if len(ei.csiParam) < 3 {
return ei.outputNormal()
}
mode, err := strconv.Atoi(ei.csiParam[1])
if err != nil {
return errCSIParseError
}
if mode != 5 {
return ei.outputNormal()
}
fgbg, err := strconv.Atoi(ei.csiParam[0])
if err != nil {
return errCSIParseError
}
color, err := strconv.Atoi(ei.csiParam[2])
if err != nil {
return errCSIParseError
}
switch fgbg {
case 38:
ei.curFgColor = Attribute(color + 1)
for _, param := range ei.csiParam[3:] {
p, err := strconv.Atoi(param)
if err != nil {
return errCSIParseError
}
switch {
case p == 1:
ei.curFgColor |= AttrBold
case p == 4:
ei.curFgColor |= AttrUnderline
case p == 7:
ei.curFgColor |= AttrReverse
}
}
case 48:
ei.curBgColor = Attribute(color + 1)
default:
return errCSIParseError
}
return nil
}