-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathstate.go
707 lines (641 loc) · 14.4 KB
/
state.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
package terminal
import (
"log"
"sync"
)
const (
tabspaces = 8
)
const (
attrReverse = 1 << iota
attrUnderline
attrBold
attrGfx
attrItalic
attrBlink
attrWrap
)
const (
cursorDefault = 1 << iota
cursorWrapNext
cursorOrigin
)
// ModeFlag represents various terminal mode states.
type ModeFlag uint32
// Terminal modes
const (
ModeWrap ModeFlag = 1 << iota
ModeInsert
ModeAppKeypad
ModeAltScreen
ModeCRLF
ModeMouseButton
ModeMouseMotion
ModeReverse
ModeKeyboardLock
ModeHide
ModeEcho
ModeAppCursor
ModeMouseSgr
Mode8bit
ModeBlink
ModeFBlink
ModeFocus
ModeMouseX10
ModeMouseMany
ModeMouseMask = ModeMouseButton | ModeMouseMotion | ModeMouseX10 | ModeMouseMany
)
// ChangeFlag represents possible state changes of the terminal.
type ChangeFlag uint32
// Terminal changes to occur in VT.ReadState
const (
ChangedScreen ChangeFlag = 1 << iota
ChangedTitle
)
type glyph struct {
c rune
mode int16
fg, bg Color
}
type line []glyph
type cursor struct {
attr glyph
x, y int
state uint8
}
type parseState func(c rune)
// State represents the terminal emulation state. Use Lock/Unlock
// methods to synchronize data access with VT.
type State struct {
DebugLogger *log.Logger
mu sync.Mutex
changed ChangeFlag
cols, rows int
lines []line
altLines []line
dirty []bool // line dirtiness
anydirty bool
cur, curSaved cursor
top, bottom int // scroll limits
mode ModeFlag
state parseState
str strEscape
csi csiEscape
numlock bool
tabs []bool
title string
}
func (t *State) logf(format string, args ...interface{}) {
if t.DebugLogger != nil {
t.DebugLogger.Printf(format, args...)
}
}
func (t *State) logln(s string) {
if t.DebugLogger != nil {
t.DebugLogger.Println(s)
}
}
func (t *State) lock() {
t.mu.Lock()
}
func (t *State) unlock() {
t.mu.Unlock()
}
// Lock locks the state object's mutex.
func (t *State) Lock() {
t.mu.Lock()
}
// Unlock resets change flags and unlocks the state object's mutex.
func (t *State) Unlock() {
t.resetChanges()
t.mu.Unlock()
}
// Cell returns the character code, foreground color, and background
// color at position (x, y) relative to the top left of the terminal.
func (t *State) Cell(x, y int) (ch rune, fg Color, bg Color) {
return t.lines[y][x].c, Color(t.lines[y][x].fg), Color(t.lines[y][x].bg)
}
// Cursor returns the current position of the cursor.
func (t *State) Cursor() (int, int) {
return t.cur.x, t.cur.y
}
// CursorVisible returns the visible state of the cursor.
func (t *State) CursorVisible() bool {
return t.mode&ModeHide == 0
}
// Mode tests if mode is currently set.
func (t *State) Mode(mode ModeFlag) bool {
return t.mode&mode != 0
}
// Title returns the current title set via the tty.
func (t *State) Title() string {
return t.title
}
/*
// ChangeMask returns a bitfield of changes that have occured by VT.
func (t *State) ChangeMask() ChangeFlag {
return t.changed
}
*/
// Changed returns true if change has occured.
func (t *State) Changed(change ChangeFlag) bool {
return t.changed&change != 0
}
// resetChanges resets the change mask and dirtiness.
func (t *State) resetChanges() {
for i := range t.dirty {
t.dirty[i] = false
}
t.anydirty = false
t.changed = 0
}
func (t *State) saveCursor() {
t.curSaved = t.cur
}
func (t *State) restoreCursor() {
t.cur = t.curSaved
t.moveTo(t.cur.x, t.cur.y)
}
func (t *State) put(c rune) {
t.state(c)
}
func (t *State) putTab(forward bool) {
x := t.cur.x
if forward {
if x == t.cols {
return
}
for x++; x < t.cols && !t.tabs[x]; x++ {
}
} else {
if x == 0 {
return
}
for x--; x > 0 && !t.tabs[x]; x-- {
}
}
t.moveTo(x, t.cur.y)
}
func (t *State) newline(firstCol bool) {
y := t.cur.y
if y == t.bottom {
cur := t.cur
t.cur = t.defaultCursor()
t.scrollUp(t.top, 1)
t.cur = cur
} else {
y++
}
if firstCol {
t.moveTo(0, y)
} else {
t.moveTo(t.cur.x, y)
}
}
// table from st, which in turn is from rxvt :)
var gfxCharTable = [62]rune{
'↑', '↓', '→', '←', '█', '▚', '☃', // A - G
0, 0, 0, 0, 0, 0, 0, 0, // H - O
0, 0, 0, 0, 0, 0, 0, 0, // P - W
0, 0, 0, 0, 0, 0, 0, ' ', // X - _
'◆', '▒', '␉', '␌', '␍', '␊', '°', '±', // ` - g
'', '␋', '┘', '┐', '┌', '└', '┼', '⎺', // h - o
'⎻', '─', '⎼', '⎽', '├', '┤', '┴', '┬', // p - w
'│', '≤', '≥', 'π', '≠', '£', '·', // x - ~
}
func (t *State) setChar(c rune, attr *glyph, x, y int) {
if attr.mode&attrGfx != 0 {
if c >= 0x41 && c <= 0x7e && gfxCharTable[c-0x41] != 0 {
c = gfxCharTable[c-0x41]
}
}
t.changed |= ChangedScreen
t.dirty[y] = true
t.lines[y][x] = *attr
t.lines[y][x].c = c
//if t.options.BrightBold && attr.mode&attrBold != 0 && attr.fg < 8 {
if attr.mode&attrBold != 0 && attr.fg < 8 {
t.lines[y][x].fg = attr.fg + 8
}
if attr.mode&attrReverse != 0 {
t.lines[y][x].fg = attr.bg
t.lines[y][x].bg = attr.fg
}
}
func (t *State) defaultCursor() cursor {
c := cursor{}
c.attr.fg = DefaultFG
c.attr.bg = DefaultBG
return c
}
func (t *State) reset() {
t.cur = t.defaultCursor()
t.saveCursor()
for i := range t.tabs {
t.tabs[i] = false
}
for i := tabspaces; i < len(t.tabs); i += tabspaces {
t.tabs[i] = true
}
t.top = 0
t.bottom = t.rows - 1
t.mode = ModeWrap
t.clear(0, 0, t.rows-1, t.cols-1)
t.moveTo(0, 0)
}
// TODO: definitely can improve allocs
func (t *State) resize(cols, rows int) bool {
if cols == t.cols && rows == t.rows {
return false
}
if cols < 1 || rows < 1 {
return false
}
slide := t.cur.y - rows + 1
if slide > 0 {
copy(t.lines, t.lines[slide:slide+rows])
copy(t.altLines, t.altLines[slide:slide+rows])
}
lines, altLines, tabs := t.lines, t.altLines, t.tabs
t.lines = make([]line, rows)
t.altLines = make([]line, rows)
t.dirty = make([]bool, rows)
t.tabs = make([]bool, cols)
minrows := min(rows, t.rows)
mincols := min(cols, t.cols)
t.changed |= ChangedScreen
for i := 0; i < rows; i++ {
t.dirty[i] = true
t.lines[i] = make(line, cols)
t.altLines[i] = make(line, cols)
}
for i := 0; i < minrows; i++ {
copy(t.lines[i], lines[i])
copy(t.altLines[i], altLines[i])
}
copy(t.tabs, tabs)
if cols > t.cols {
i := t.cols - 1
for i > 0 && !tabs[i] {
i--
}
for i += tabspaces; i < len(tabs); i += tabspaces {
tabs[i] = true
}
}
t.cols = cols
t.rows = rows
t.setScroll(0, rows-1)
t.moveTo(t.cur.x, t.cur.y)
for i := 0; i < 2; i++ {
if mincols < cols && minrows > 0 {
t.clear(mincols, 0, cols-1, minrows-1)
}
if cols > 0 && minrows < rows {
t.clear(0, minrows, cols-1, rows-1)
}
t.swapScreen()
}
return slide > 0
}
func (t *State) clear(x0, y0, x1, y1 int) {
if x0 > x1 {
x0, x1 = x1, x0
}
if y0 > y1 {
y0, y1 = y1, y0
}
x0 = clamp(x0, 0, t.cols-1)
x1 = clamp(x1, 0, t.cols-1)
y0 = clamp(y0, 0, t.rows-1)
y1 = clamp(y1, 0, t.rows-1)
t.changed |= ChangedScreen
for y := y0; y <= y1; y++ {
t.dirty[y] = true
for x := x0; x <= x1; x++ {
t.lines[y][x] = t.cur.attr
t.lines[y][x].c = ' '
}
}
}
func (t *State) clearAll() {
t.clear(0, 0, t.cols-1, t.rows-1)
}
func (t *State) moveAbsTo(x, y int) {
if t.cur.state&cursorOrigin != 0 {
y += t.top
}
t.moveTo(x, y)
}
func (t *State) moveTo(x, y int) {
var miny, maxy int
if t.cur.state&cursorOrigin != 0 {
miny = t.top
maxy = t.bottom
} else {
miny = 0
maxy = t.rows - 1
}
x = clamp(x, 0, t.cols-1)
y = clamp(y, miny, maxy)
t.changed |= ChangedScreen
t.cur.state &^= cursorWrapNext
t.cur.x = x
t.cur.y = y
}
func (t *State) swapScreen() {
t.lines, t.altLines = t.altLines, t.lines
t.mode ^= ModeAltScreen
t.dirtyAll()
}
func (t *State) dirtyAll() {
t.changed |= ChangedScreen
for y := 0; y < t.rows; y++ {
t.dirty[y] = true
}
}
func (t *State) setScroll(top, bottom int) {
top = clamp(top, 0, t.rows-1)
bottom = clamp(bottom, 0, t.rows-1)
if top > bottom {
top, bottom = bottom, top
}
t.top = top
t.bottom = bottom
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func clamp(val, min, max int) int {
if val < min {
return min
} else if val > max {
return max
}
return val
}
func between(val, min, max int) bool {
if val < min || val > max {
return false
}
return true
}
func (t *State) scrollDown(orig, n int) {
n = clamp(n, 0, t.bottom-orig+1)
t.clear(0, t.bottom-n+1, t.cols-1, t.bottom)
t.changed |= ChangedScreen
for i := t.bottom; i >= orig+n; i-- {
t.lines[i], t.lines[i-n] = t.lines[i-n], t.lines[i]
t.dirty[i] = true
t.dirty[i-n] = true
}
// TODO: selection scroll
}
func (t *State) scrollUp(orig, n int) {
n = clamp(n, 0, t.bottom-orig+1)
t.clear(0, orig, t.cols-1, orig+n-1)
t.changed |= ChangedScreen
for i := orig; i <= t.bottom-n; i++ {
t.lines[i], t.lines[i+n] = t.lines[i+n], t.lines[i]
t.dirty[i] = true
t.dirty[i+n] = true
}
// TODO: selection scroll
}
func (t *State) modMode(set bool, bit ModeFlag) {
if set {
t.mode |= bit
} else {
t.mode &^= bit
}
}
func (t *State) setMode(priv bool, set bool, args []int) {
if priv {
for _, a := range args {
switch a {
case 1: // DECCKM - cursor key
t.modMode(set, ModeAppCursor)
case 5: // DECSCNM - reverse video
mode := t.mode
t.modMode(set, ModeReverse)
if mode != t.mode {
// TODO: redraw
}
case 6: // DECOM - origin
if set {
t.cur.state |= cursorOrigin
} else {
t.cur.state &^= cursorOrigin
}
t.moveAbsTo(0, 0)
case 7: // DECAWM - auto wrap
t.modMode(set, ModeWrap)
// IGNORED:
case 0, // error
2, // DECANM - ANSI/VT52
3, // DECCOLM - column
4, // DECSCLM - scroll
8, // DECARM - auto repeat
18, // DECPFF - printer feed
19, // DECPEX - printer extent
42, // DECNRCM - national characters
12: // att610 - start blinking cursor
break
case 25: // DECTCEM - text cursor enable mode
t.modMode(!set, ModeHide)
case 9: // X10 mouse compatibility mode
t.modMode(false, ModeMouseMask)
t.modMode(set, ModeMouseX10)
case 1000: // report button press
t.modMode(false, ModeMouseMask)
t.modMode(set, ModeMouseButton)
case 1002: // report motion on button press
t.modMode(false, ModeMouseMask)
t.modMode(set, ModeMouseMotion)
case 1003: // enable all mouse motions
t.modMode(false, ModeMouseMask)
t.modMode(set, ModeMouseMany)
case 1004: // send focus events to tty
t.modMode(set, ModeFocus)
case 1006: // extended reporting mode
t.modMode(set, ModeMouseSgr)
case 1034:
t.modMode(set, Mode8bit)
case 1049, // = 1047 and 1048
47, 1047:
alt := t.mode&ModeAltScreen != 0
if alt {
t.clear(0, 0, t.cols-1, t.rows-1)
}
if !set || !alt {
t.swapScreen()
}
if a != 1049 {
break
}
fallthrough
case 1048:
if set {
t.saveCursor()
} else {
t.restoreCursor()
}
case 1001:
// mouse highlight mode; can hang the terminal by design when
// implemented
case 1005:
// utf8 mouse mode; will confuse applications not supporting
// utf8 and luit
case 1015:
// urxvt mangled mouse mode; incompatiblt and can be mistaken
// for other control codes
default:
t.logf("unknown private set/reset mode %d\n", a)
}
}
} else {
for _, a := range args {
switch a {
case 0: // Error (ignored)
case 2: // KAM - keyboard action
t.modMode(set, ModeKeyboardLock)
case 4: // IRM - insertion-replacement
t.modMode(set, ModeInsert)
t.logln("insert mode not implemented")
case 12: // SRM - send/receive
t.modMode(set, ModeEcho)
case 20: // LNM - linefeed/newline
t.modMode(set, ModeCRLF)
case 34:
t.logln("right-to-left mode not implemented")
case 96:
t.logln("right-to-left copy mode not implemented")
default:
t.logf("unknown set/reset mode %d\n", a)
}
}
}
}
func (t *State) setAttr(attr []int) {
if len(attr) == 0 {
attr = []int{0}
}
for i := 0; i < len(attr); i++ {
a := attr[i]
switch a {
case 0:
t.cur.attr.mode &^= attrReverse | attrUnderline | attrBold | attrItalic | attrBlink
t.cur.attr.fg = DefaultFG
t.cur.attr.bg = DefaultBG
case 1:
t.cur.attr.mode |= attrBold
case 3:
t.cur.attr.mode |= attrItalic
case 4:
t.cur.attr.mode |= attrUnderline
case 5, 6: // slow, rapid blink
t.cur.attr.mode |= attrBlink
case 7:
t.cur.attr.mode |= attrReverse
case 21, 22:
t.cur.attr.mode &^= attrBold
case 23:
t.cur.attr.mode &^= attrItalic
case 24:
t.cur.attr.mode &^= attrUnderline
case 25, 26:
t.cur.attr.mode &^= attrBlink
case 27:
t.cur.attr.mode &^= attrReverse
case 38:
if i+2 < len(attr) && attr[i+1] == 5 {
i += 2
if between(attr[i], 0, 255) {
t.cur.attr.fg = Color(attr[i])
} else {
t.logf("bad fgcolor %d\n", attr[i])
}
} else {
t.logf("gfx attr %d unknown\n", a)
}
case 39:
t.cur.attr.fg = DefaultFG
case 48:
if i+2 < len(attr) && attr[i+1] == 5 {
i += 2
if between(attr[i], 0, 255) {
t.cur.attr.bg = Color(attr[i])
} else {
t.logf("bad bgcolor %d\n", attr[i])
}
} else {
t.logf("gfx attr %d unknown\n", a)
}
case 49:
t.cur.attr.bg = DefaultBG
default:
if between(a, 30, 37) {
t.cur.attr.fg = Color(a - 30)
} else if between(a, 40, 47) {
t.cur.attr.bg = Color(a - 40)
} else if between(a, 90, 97) {
t.cur.attr.fg = Color(a - 90 + 8)
} else if between(a, 100, 107) {
t.cur.attr.bg = Color(a - 100 + 8)
} else {
t.logf("gfx attr %d unknown\n", a)
}
}
}
}
func (t *State) insertBlanks(n int) {
src := t.cur.x
dst := src + n
size := t.cols - dst
t.changed |= ChangedScreen
t.dirty[t.cur.y] = true
if dst >= t.cols {
t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y)
} else {
copy(t.lines[t.cur.y][dst:dst+size], t.lines[t.cur.y][src:src+size])
t.clear(src, t.cur.y, dst-1, t.cur.y)
}
}
func (t *State) insertBlankLines(n int) {
if t.cur.y < t.top || t.cur.y > t.bottom {
return
}
t.scrollDown(t.cur.y, n)
}
func (t *State) deleteLines(n int) {
if t.cur.y < t.top || t.cur.y > t.bottom {
return
}
t.scrollUp(t.cur.y, n)
}
func (t *State) deleteChars(n int) {
src := t.cur.x + n
dst := t.cur.x
size := t.cols - src
t.changed |= ChangedScreen
t.dirty[t.cur.y] = true
if src >= t.cols {
t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y)
} else {
copy(t.lines[t.cur.y][dst:dst+size], t.lines[t.cur.y][src:src+size])
t.clear(t.cols-n, t.cur.y, t.cols-1, t.cur.y)
}
}
func (t *State) setTitle(title string) {
t.changed |= ChangedTitle
t.title = title
}