forked from hinshun/vt10x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
203 lines (191 loc) · 4.17 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
package vt10x
func isControlCode(c rune) bool {
return c < 0x20 || c == 0177
}
func (t *State) parse(c rune) {
t.logf("%q", string(c))
if isControlCode(c) {
if t.handleControlCodes(c) || t.cur.Attr.Mode&attrGfx == 0 {
return
}
}
// TODO: update selection; see st.c:2450
if t.mode&ModeWrap != 0 && t.cur.State&cursorWrapNext != 0 {
t.lines[t.cur.Y][t.cur.X].Mode |= attrWrap
t.newline(true)
}
if t.mode&ModeInsert != 0 && t.cur.X+1 < t.cols {
// TODO: move shiz, look at st.c:2458
t.logln("insert mode not implemented")
}
t.setChar(c, &t.cur.Attr, t.cur.X, t.cur.Y)
if t.cur.X+1 < t.cols {
t.moveTo(t.cur.X+1, t.cur.Y)
} else {
t.cur.State |= cursorWrapNext
}
}
func (t *State) parseEsc(c rune) {
if t.handleControlCodes(c) {
return
}
next := t.parse
t.logf("%q", string(c))
switch c {
case '[':
next = t.parseEscCSI
case '#':
next = t.parseEscTest
case 'P', // DCS - Device Control String
'_', // APC - Application Program Command
'^', // PM - Privacy Message
']', // OSC - Operating System Command
'k': // old title set compatibility
t.str.reset()
t.str.typ = c
next = t.parseEscStr
case '(': // set primary charset G0
next = t.parseEscAltCharset
case ')', // set secondary charset G1 (ignored)
'*', // set tertiary charset G2 (ignored)
'+': // set quaternary charset G3 (ignored)
case 'D': // IND - linefeed
if t.cur.Y == t.bottom {
t.scrollUp(t.top, 1)
} else {
t.moveTo(t.cur.X, t.cur.Y+1)
}
case 'E': // NEL - next line
t.newline(true)
case 'H': // HTS - horizontal tab stop
t.tabs[t.cur.X] = true
case 'M': // RI - reverse index
if t.cur.Y == t.top {
t.scrollDown(t.top, 1)
} else {
t.moveTo(t.cur.X, t.cur.Y-1)
}
case 'Z': // DECID - identify terminal
// TODO: write to our writer our id
case 'c': // RIS - reset to initial state
t.reset()
case '=': // DECPAM - application keypad
t.mode |= ModeAppKeypad
case '>': // DECPNM - normal keypad
t.mode &^= ModeAppKeypad
case '7': // DECSC - save cursor
t.saveCursor()
case '8': // DECRC - restore cursor
t.restoreCursor()
case '\\': // ST - stop
default:
t.logf("unknown ESC sequence '%c'\n", c)
}
t.state = next
}
func (t *State) parseEscCSI(c rune) {
if t.handleControlCodes(c) {
return
}
t.logf("%q", string(c))
if t.csi.put(byte(c)) {
t.state = t.parse
t.handleCSI()
}
}
func (t *State) parseEscStr(c rune) {
t.logf("%q", string(c))
switch c {
case '\033':
t.state = t.parseEscStrEnd
case '\a': // backwards compatiblity to xterm
t.state = t.parse
t.handleSTR()
default:
t.str.put(c)
}
}
func (t *State) parseEscStrEnd(c rune) {
if t.handleControlCodes(c) {
return
}
t.logf("%q", string(c))
t.state = t.parse
if c == '\\' {
t.handleSTR()
}
}
func (t *State) parseEscAltCharset(c rune) {
if t.handleControlCodes(c) {
return
}
t.logf("%q", string(c))
switch c {
case '0': // line drawing set
t.cur.Attr.Mode |= attrGfx
case 'B': // USASCII
t.cur.Attr.Mode &^= attrGfx
case 'A', // UK (ignored)
'<', // multinational (ignored)
'5', // Finnish (ignored)
'C', // Finnish (ignored)
'K': // German (ignored)
default:
t.logf("unknown alt. charset '%c'\n", c)
}
t.state = t.parse
}
func (t *State) parseEscTest(c rune) {
if t.handleControlCodes(c) {
return
}
// DEC screen alignment test
if c == '8' {
for y := 0; y < t.rows; y++ {
for x := 0; x < t.cols; x++ {
t.setChar('E', &t.cur.Attr, x, y)
}
}
}
t.state = t.parse
}
func (t *State) handleControlCodes(c rune) bool {
if !isControlCode(c) {
return false
}
switch c {
// HT
case '\t':
t.putTab(true)
// BS
case '\b':
t.moveTo(t.cur.X-1, t.cur.Y)
// CR
case '\r':
t.moveTo(0, t.cur.Y)
// LF, VT, LF
case '\f', '\v', '\n':
// go to first col if mode is set
t.newline(t.mode&ModeCRLF != 0)
// BEL
case '\a':
// TODO: emit sound
// TODO: window alert if not focused
// ESC
case 033:
t.csi.reset()
t.state = t.parseEsc
// SO, SI
case 016, 017:
// different charsets not supported. apps should use the correct
// alt charset escapes, probably for line drawing
// SUB, CAN
case 032, 030:
t.csi.reset()
// ignore ENQ, NUL, XON, XOFF, DEL
case 005, 000, 021, 023, 0177:
default:
return false
}
return true
}