-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescapes.go
724 lines (604 loc) · 16.2 KB
/
escapes.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
package termemu
import (
"bufio"
"bytes"
"strconv"
"strings"
)
type dupReader struct {
reader *bufio.Reader
buf *bytes.Buffer
t *terminal
}
func (r *dupReader) ReadByte() (byte, error) {
b, err := r.reader.ReadByte()
if err == nil && r.t.dup != nil {
r.t.dup.Write([]byte{b})
}
if r.buf != nil {
r.buf.Write([]byte{b})
}
return b, err
}
func (r *dupReader) ReadRune() (rune, int, error) {
b, n, err := r.reader.ReadRune()
if err == nil {
byts := []byte(string(b))
if r.t.dup != nil {
r.t.dup.Write(byts)
}
if r.buf != nil {
r.buf.Write(byts)
}
}
return b, n, err
}
func (r *dupReader) Buffered() int {
return r.reader.Buffered()
}
func (t *terminal) ptyReadLoop() {
r := &dupReader{
reader: bufio.NewReader(t.pty),
buf: nil,
t: t,
}
for {
b, _, err := r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return
}
// printables
runes := []rune{}
for {
if b < 32 || b > 126 && b < 128 {
break
}
runes = append(runes, rune(b))
if r.Buffered() == 0 {
break
}
b, _, err = r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return
}
}
if len(runes) > 0 {
t.WithLock(func() {
t.screen().writeRunes(runes)
})
debugPrintf(debugTxt, "\033[32mtxt: %#v\033[0m %v\n", string(runes), len(runes))
if r.Buffered() == 0 {
continue
}
}
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
switch b {
case 0: // NUL Null byte, ignore
case 5: // ENQ ^E Return Terminal Status
debugPrintln(debugTodo, "TODO: ENQ")
case 7: // BEL ^G Bell
t.WithLock(func() {
t.frontend.Bell()
})
case 8: // BS ^H Backspace
t.WithLock(func() {
t.screen().moveCursor(-1, 0, false, false)
})
case 9: // HT ^I Horizontal TAB
debugPrintln(debugTodo, "TODO: tab")
case 10: // LF ^J Linefeed (newline)
t.WithLock(func() {
t.screen().moveCursor(0, 1, true, true)
})
case 11: // VT ^K Vertical TAB
debugPrintln(debugTodo, "TODO: vtab")
case 12: // FF ^L Formfeed (also: New page NP)
t.WithLock(func() {
t.screen().moveCursor(0, 1, false, true)
})
case 13: // CR ^M Carriage Return
t.WithLock(func() {
t.screen().moveCursor(-t.screen().cursorPos.X, 0, true, true)
})
case 27: // ESC ^[ Escape Character
var cmdBytes bytes.Buffer
r.buf = &cmdBytes
t.WithLock(func() {
success := t.handleCommand(r)
r.buf = nil
cmd := cmdBytes.Bytes()
if success {
debugPrintf(debugCmd, "%v cmd: %#v\n", t.screen().cursorPos, string(cmd))
if string(cmd) == "[?25l" {
// hide cursor
if t.dup != nil {
t.dup.Write([]byte(string("\033[?25h")))
}
}
} else {
debugPrintf(debugTodo, "TODO: Unhandled command: %#v\n", string(cmd))
}
})
continue
case 127: // DEL Delete Character
debugPrintln(debugTodo, "TODO: delete character")
default:
debugPrintf(debugTodo, "TODO: unhandled char %v %#v\n", b, string(b))
continue
}
}
}
func (t *terminal) handleCommand(r *dupReader) bool {
b, _, err := r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return false
}
// short commands
switch b {
case 'c': // reset
debugPrintln(debugTodo, "TODO: cmd: reset") // TODO
case 'D': // Index, scroll down if necessary
t.screen().moveCursor(0, 1, false, true)
case 'M': // Reverse index, scroll up if necessary
t.screen().moveCursor(0, -1, false, true)
case '[': // CSI Control Sequence Introducer
return t.handleCmdCSI(r)
case ']': // OSC Operating System Commands
return t.handleCmdOSC(r)
case '(': // G0
fallthrough
case ')': // G1
fallthrough
case '*': // G2
fallthrough
case '+': // G3
C, _, err := r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return false
}
debugPrintf(debugCharSet, "TODO: Character Set %c %c\n", b, C)
case '=': // Application Keypad
debugPrintln(debugTodo, "TODO: Application Keypad") // TODO
case '>': // Normal Keypad
debugPrintln(debugTodo, "TODO: Normal Keypad") // TODO
/*case 'l': // Memory Lock
debugPrintln("Memory Lock") // TODO
case 'm': // Memory Unlock
debugPrintln("Memory Unlock") // TODO*/
default:
return false
}
return true
}
func (t *terminal) handleCmdCSI(r *dupReader) bool {
b, _, err := r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return false
}
var prefix = []byte{}
if b == '?' || b == '>' {
prefix = append(prefix, byte(b))
b, _, err = r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return false
}
}
paramBytes := []byte{}
for b == ';' || (b >= '0' && b <= '9') {
paramBytes = append(paramBytes, byte(b))
b, _, err = r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return false
}
}
paramParts := strings.Split(string(paramBytes), ";")
if len(paramParts) == 1 && paramParts[0] == "" {
paramParts = []string{}
}
params := make([]int, len(paramParts))
for i, p := range paramParts {
params[i], _ = strconv.Atoi(p)
}
if string(prefix) == "" {
switch b {
case 'A': // Move cursor up
if len(params) == 0 {
params = []int{1}
}
t.screen().moveCursor(0, -params[0], false, true)
case 'B': // Move cursor down
if len(params) == 0 {
params = []int{1}
}
t.screen().moveCursor(0, params[0], false, true)
case 'C': // Move cursor forward
if len(params) == 0 {
params = []int{1}
}
t.screen().moveCursor(params[0], 0, false, false)
case 'D': // Move cursor backward
if len(params) == 0 {
params = []int{1}
}
t.screen().moveCursor(-params[0], 0, false, false)
case 'G': // Cursor Character Absolute
if len(params) == 0 {
params = []int{1}
}
t.screen().setCursorPos(params[0]-1, t.screen().cursorPos.Y)
case 'c': // Send Device Attributes
if len(params) == 0 {
params = []int{1}
}
switch params[0] {
case 0:
t.pty.Write([]byte("\033[?1;2c"))
}
case 'd': // Line Position Absolute
if len(params) == 0 {
params = []int{1}
}
t.screen().setCursorPos(t.screen().cursorPos.X, params[0]-1)
case 'f', 'H': // Cursor Home
x := 1
y := 1
if len(params) >= 1 {
y = params[0]
}
if len(params) >= 2 {
x = params[1]
}
// debugPrintf("cursor home: %v, %v\n", x, y)
t.screen().setCursorPos(x-1, y-1)
case 'h', 'l': // h=Set, l=Reset Mode
var value bool
if b == 'h' {
value = true
}
if len(params) != 1 {
debugPrintln(debugTodo, "TODO: Unhandled CSI mode params: ", params, b)
return false
}
switch params[0] {
case 4:
debugPrintln(debugTodo, "TODO: Insert Mode = ", value) // TODO
default:
debugPrintln(debugTodo, "TODO: Unhandled CSI mode param: ", params[0])
return false
}
case 'm': // Set color/mode
if len(params) == 0 {
params = []int{0}
}
fc := t.screen().frontColor
bc := t.screen().backColor
for i := 0; i < len(params); i++ {
p := params[i]
switch {
case p == 0: // reset mode
fc = ColWhite
bc = ColBlack
case p >= 1 && p <= 8:
fc = fc.SetMode(ColorModes[p-1])
case p == 22:
fc = fc.ResetMode(ModeBold).ResetMode(ModeDim)
case p == 23:
fc = fc.ResetMode(ModeItalic)
case p == 24:
fc = fc.ResetMode(ModeUnderline)
case p == 27:
fc = fc.ResetMode(ModeReverse)
case p >= 30 && p <= 37:
fc = fc.SetColor(Colors8[p-30])
case p == 39: // default color
fc = fc.SetColor(ColWhite)
case p >= 40 && p <= 47:
bc = bc.SetColor(Colors8[p-40])
case p == 49: // default color
bc = bc.SetColor(ColBlack)
case p == 38 || p == 48: // extended set color
if i+2 < len(params) {
switch params[i+1] {
case 5: // 256 color
if p == 38 {
fc = fc.SetColor(Color(params[i+2] & 0xff))
} else {
bc = bc.SetColor(Color(params[i+2] & 0xff))
}
i += 2
case 2: // RGB Color
if i+4 < len(params) {
if p == 38 {
fc = fc.SetColorRGB(params[i+2], params[i+3], params[i+4])
} else {
bc = bc.SetColorRGB(params[i+2], params[i+3], params[i+4])
}
i += 4
}
default:
debugPrintln(debugTodo, "TODO: unhandled extended color: ", params[i+1])
continue
}
}
case p >= 90 && p <= 97:
fc = fc.SetColor(Color(p - 90 + 8))
case p >= 100 && p <= 107:
bc = bc.SetColor(Color(p - 100 + 8))
default:
debugPrintln(debugTodo, "TODO: Unhandled set color: ", p)
return false
}
// debugPrintf("m %v: %x, %x\n", p, fc, bc)
t.screen().setColors(fc, bc)
}
case 't': // Window manipulation
debugPrintln(debugTodo, "TODO: Unhandled window manipulation: ", params)
case 'K': // Erase
// eraseRegion clamps the region to the window, so we don't have to be too careful here
// debugPrintln("erase: ", params)
switch {
case len(params) == 0 || params[0] == 0: // Erase to end of line
t.screen().eraseRegion(Region{
X: t.screen().cursorPos.X,
Y: t.screen().cursorPos.Y,
X2: t.screen().size.X,
Y2: t.screen().cursorPos.Y + 1,
}, CRClear)
case params[0] == 1: // Erase to start of line
t.screen().eraseRegion(Region{
X: 0,
Y: t.screen().cursorPos.Y,
X2: t.screen().cursorPos.X,
Y2: t.screen().cursorPos.Y + 1,
}, CRClear)
case params[0] == 2: // Erase entire line
t.screen().eraseRegion(Region{
X: 0,
Y: t.screen().cursorPos.Y,
X2: t.screen().size.X,
Y2: t.screen().cursorPos.Y + 1,
}, CRClear)
default:
debugPrintln(debugTodo, "TODO: Unhandled K params: ", params)
return false
}
case 'J': // Erase Lines
// eraseRegion clamps the region to the window, so we don't have to be too careful here
// debugPrintln("erase lines: ", params)
switch {
case len(params) == 0 || params[0] == 0: // Erase to bottom of screen
t.screen().eraseRegion(Region{
X: 0,
Y: t.screen().cursorPos.Y,
X2: t.screen().size.X,
Y2: t.screen().size.Y,
}, CRClear)
case params[0] == 1: // Erase to top of screen
t.screen().eraseRegion(Region{
X: 0,
Y: 0,
X2: t.screen().size.X,
Y2: t.screen().cursorPos.Y,
}, CRClear)
case params[0] == 2: // Erase screen and home cursor
t.screen().eraseRegion(Region{
X: 0,
Y: 0,
X2: t.screen().size.X,
Y2: t.screen().size.Y,
}, CRClear)
t.screen().setCursorPos(0, 0)
default:
debugPrintln(debugTodo, "TODO: Unhandled J params: ", params)
return false
}
case 'L': // Insert lines, scroll down
if len(params) == 0 {
params = []int{1}
}
t.screen().scroll(t.screen().cursorPos.Y, t.screen().bottomMargin, params[0])
case 'M': // Delete lines, scroll up
if len(params) == 0 {
params = []int{1}
}
t.screen().scroll(t.screen().cursorPos.Y, t.screen().bottomMargin, -params[0])
case 'S': // Scroll up
if len(params) == 0 {
params = []int{1}
}
t.screen().scroll(t.screen().topMargin, t.screen().bottomMargin, -params[0])
case 'T': // Scroll down
if len(params) == 0 {
params = []int{1}
}
t.screen().scroll(t.screen().topMargin, t.screen().bottomMargin, params[0])
case 'P': // Delete n characters
if len(params) == 0 {
params = []int{1}
}
t.screen().eraseRegion(Region{
X: t.screen().cursorPos.X,
Y: t.screen().cursorPos.Y,
X2: t.screen().cursorPos.X + params[0],
Y2: t.screen().cursorPos.Y + 1,
}, CRClear)
debugPrintln(debugTodo, "TODO: Delete", params[0], "chars")
case 'X': // Erase from cursor pos to the right
if len(params) == 0 {
params = []int{1}
}
t.screen().eraseRegion(Region{
X: t.screen().cursorPos.X,
Y: t.screen().cursorPos.Y,
X2: t.screen().cursorPos.X + params[0],
Y2: t.screen().cursorPos.Y + 1,
}, CRClear)
case 'r': // Set Scroll margins
top := 1
bottom := t.screen().size.Y
if len(params) >= 1 {
top = params[0]
}
if len(params) >= 2 {
bottom = params[1]
}
// debugPrintf("cursor home: %v, %v\n", x, y)
t.screen().setScrollMarginTopBottom(top-1, bottom-1)
case 'n': // Device Status Report
default:
debugPrintf(debugTodo, "TODO: Unhandled CSI Command: %v %#v\n", params, string(b))
return true
}
} else if string(prefix) == "?" {
switch b {
case 'h', 'l': // h == set, l == reset for various modes
var value bool
if b == 'h' {
value = true
}
for _, p := range params {
switch p {
case 1: // Application / Normal Cursor Keys
debugPrintln(debugTodo, "TODO: Application Cursor Keys =", value) // TODO
case 7: // Wraparound
t.screen().autoWrap = value
case 9: // Send MouseXY on press
debugPrintln(debugTodo, "TODO: Send MouseXY on press =", value) // TODO
if value {
t.setViewInt(VIMouseMode, MMPress)
} else {
t.setViewInt(VIMouseMode, MMNone)
}
case 12: // Blink Cursor
t.setViewFlag(VFBlinkCursor, value)
case 25: // Show Cursor
t.setViewFlag(VFShowCursor, value)
case 1000: // Send MouseXY on press/release
if value {
t.setViewInt(VIMouseMode, MMPressRelease)
} else {
t.setViewInt(VIMouseMode, MMNone)
}
case 1002: // Cell Motion Mouse Tracking
if value {
t.setViewInt(VIMouseMode, MMPressReleaseMove)
} else {
t.setViewInt(VIMouseMode, MMNone)
}
case 1003: // All Motion Mouse Tracking
if value {
t.setViewInt(VIMouseMode, MMPressReleaseMoveAll)
} else {
t.setViewInt(VIMouseMode, MMNone)
}
case 1004: // Report focus changed
t.setViewFlag(VFReportFocus, value)
case 1005: // xterm UTF-8 extended mouse reporting
if value {
t.setViewInt(VIMouseEncoding, MEUTF8)
} else {
t.setViewInt(VIMouseEncoding, MEX10)
}
case 1006: // xterm SGR extended mouse reporting
if value {
t.setViewInt(VIMouseEncoding, MESGR)
} else {
t.setViewInt(VIMouseEncoding, MEX10)
}
case 1034:
debugPrintf(debugTodo, "TODO: Interpret Meta key = %v\n", value)
case 1049: // Save/Restore cursor and alternate screen
t.switchScreen()
case 2004: // Bracketed paste
t.setViewFlag(VFBracketedPaste, value)
default:
debugPrintf(debugTodo, "TODO: Unhandled flag: %#v %v, %v %#v\n", string(prefix), params, p, string(b))
}
}
default:
debugPrintf(debugTodo, "TODO: Unhandled ? command: %#v %v, %#v\n", string(prefix), params, string(b))
}
} else if string(prefix) == ">" {
switch b {
case 'c': // Send Device Attributes
attrs := "\x1b[>1;4402;0c"
n, err := t.pty.WriteString(attrs)
if err != nil || n != len(attrs) {
debugPrintln(debugErrors, "Error sending device attrs:", err)
}
default:
return false
}
} else {
debugPrintf(debugTodo, "TODO: Unhandled prefix: %#v %v, %#v\n", string(prefix), params, string(b))
return true
}
return true
}
func (t *terminal) handleCmdOSC(r *dupReader) bool {
paramBytes := []byte{}
var err error
var b rune
for {
b, _, err = r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return false
}
if b >= '0' && b <= '9' {
paramBytes = append(paramBytes, byte(b))
} else {
break
}
}
param, _ := strconv.Atoi(string(paramBytes))
param2 := []byte{}
if b == ';' {
// skip the ';'
for {
b, _, err = r.ReadRune()
if err != nil {
debugPrintln(debugErrors, err)
return false
}
if b == 7 || b == 0x9c { // BEL , ST
break
}
if len(param2) > 0 && param2[len(param2)-1] == 27 && b == '\\' { // ESC \ is also ST
param2 = param2[:len(param2)-1]
break
}
param2 = append(param2, byte(b))
}
} else if b != 7 && b != 0x9c { // BEL, ST
debugPrintln(debugErrors, "OSC command number not followed by ;, BEL, or ST?", b)
return false
}
switch param {
case 0:
t.setViewString(VSWindowTitle, string(param2))
case 2:
t.setViewString(VSWindowTitle, string(param2))
case 4:
debugPrintf(debugTodo, "TODO: change color : %#v\n", string(param2))
case 6:
t.setViewString(VSCurrentDirectory, string(param2))
case 7:
t.setViewString(VSCurrentFile, string(param2))
case 104:
debugPrintln(debugTodo, "TODO: Reset Color Palette", string(param2))
case 112:
debugPrintln(debugTodo, "TODO: Reset Cursor Color", string(param2))
default:
debugPrintln(debugTodo, "TODO: Unhandled OSC Command: ", param, string(b))
return false
}
return true
}