forked from liamg/darktile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csi.go
464 lines (397 loc) · 12.9 KB
/
csi.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
package terminal
import (
"fmt"
"strconv"
"strings"
)
type csiSequenceHandler func(params []string, intermediate string, terminal *Terminal) error
type csiMapping struct {
id rune
handler csiSequenceHandler
description string
expectedParams *expectedParams
}
type expectedParams struct {
min uint8
max uint8
}
var csiSequences = []csiMapping{
{id: 'c', handler: csiSendDeviceAttributesHandler, description: " Send Device Attributes (Primary/Secondary/Tertiary DA)"},
{id: 'd', handler: csiLinePositionAbsolute, expectedParams: &expectedParams{min: 0, max: 1}, description: "Line Position Absolute [row] (default = [1,column]) (VPA)"},
{id: 'f', handler: csiCursorPositionHandler, description: "Horizontal and Vertical Position [row;column] (default = [1,1]) (HVP)"},
{id: 'h', handler: csiSetModeHandler, expectedParams: &expectedParams{min: 1, max: 1}, description: "Set Mode (SM)"},
{id: 'l', handler: csiResetModeHandler, expectedParams: &expectedParams{min: 1, max: 1}, description: "Reset Mode (RM)"},
{id: 'm', handler: sgrSequenceHandler, description: "Character Attributes (SGR)"},
{id: 'n', handler: csiDeviceStatusReportHandler, description: "Device Status Report (DSR)"},
{id: 'r', handler: csiSetMarginsHandler, expectedParams: &expectedParams{min: 0, max: 2}, description: "Set Scrolling Region [top;bottom] (default = full size of window) (DECSTBM), VT100"},
{id: 't', handler: csiWindowManipulation, description: "Window manipulation"},
{id: 'A', handler: csiCursorUpHandler, description: "Cursor Up Ps Times (default = 1) (CUU)"},
{id: 'B', handler: csiCursorDownHandler, description: "Cursor Down Ps Times (default = 1) (CUD)"},
{id: 'C', handler: csiCursorForwardHandler, description: "Cursor Forward Ps Times (default = 1) (CUF)"},
{id: 'D', handler: csiCursorBackwardHandler, description: "Cursor Backward Ps Times (default = 1) (CUB)"},
{id: 'E', handler: csiCursorNextLineHandler, description: "Cursor Next Line Ps Times (default = 1) (CNL)"},
{id: 'F', handler: csiCursorPrecedingLineHandler, description: "Cursor Preceding Line Ps Times (default = 1) (CPL)"},
{id: 'G', handler: csiCursorCharacterAbsoluteHandler, description: "Cursor Character Absolute [column] (default = [row,1]) (CHA)"},
{id: 'H', handler: csiCursorPositionHandler, description: "Cursor Position [row;column] (default = [1,1]) (CUP)"},
{id: 'J', handler: csiEraseInDisplayHandler, description: "Erase in Display (ED), VT100"},
{id: 'K', handler: csiEraseInLineHandler, description: "Erase in Line (EL), VT100"},
{id: 'L', handler: csiInsertLinesHandler, description: "Insert Ps Line(s) (default = 1) (IL)"},
{id: 'M', handler: csiDeleteLinesHandler, description: "Delete Ps Line(s) (default = 1) (DL)"},
{id: 'P', handler: csiDeleteHandler, description: " Delete Ps Character(s) (default = 1) (DCH)"},
{id: 'S', handler: csiScrollUpHandler, description: "Scroll up Ps lines (default = 1) (SU), VT420, ECMA-48"},
{id: 'T', handler: csiScrollDownHandler, description: "Scroll down Ps lines (default = 1) (SD), VT420"},
{id: 'X', handler: csiEraseCharactersHandler, description: "Erase Ps Character(s) (default = 1) (ECH"},
{id: '@', handler: csiInsertBlankCharactersHandler, description: "Insert Ps (Blank) Character(s) (default = 1) (ICH)"},
}
func csiHandler(pty chan rune, terminal *Terminal) error {
var final rune
var b rune
param := ""
intermediate := ""
CSI:
for {
b = <-pty
switch true {
case b >= 0x30 && b <= 0x3F:
param = param + string(b)
case b >= 0x20 && b <= 0x2F:
//intermediate? useful?
intermediate += string(b)
case b >= 0x40 && b <= 0x7e:
final = b
break CSI
}
}
params := strings.Split(param, ";")
if param == "" {
params = []string{}
}
for _, sequence := range csiSequences {
if sequence.id == final {
if sequence.expectedParams != nil && (uint8(len(params)) < sequence.expectedParams.min || uint8(len(params)) > sequence.expectedParams.max) {
continue
}
x, y := terminal.ActiveBuffer().CursorColumn(), terminal.ActiveBuffer().CursorLine()
err := sequence.handler(params, intermediate, terminal)
terminal.logger.Debugf("CSI 0x%02X (ESC[%s%s%s) %s - %d,%d -> %d,%d", final, param, intermediate, string(final), sequence.description, x, y, terminal.ActiveBuffer().CursorColumn(), terminal.ActiveBuffer().CursorLine())
return err
}
}
return fmt.Errorf("Unknown CSI control sequence: 0x%02X (ESC[%s%s%s)", final, param, intermediate, string(final))
}
func csiSendDeviceAttributesHandler(params []string, intermediate string, terminal *Terminal) error {
if len(params) > 0 && len(params[0]) > 0 && params[0][0] == '>' { // secondary
_ = terminal.Write([]byte("\x1b[0;0;0c")) // report VT100
return nil
}
return fmt.Errorf("Unsupported SDA identifier")
}
func csiDeviceStatusReportHandler(params []string, intermediate string, terminal *Terminal) error {
if len(params) == 0 {
return fmt.Errorf("Missing Device Status Report identifier")
}
switch params[0] {
case "5":
_ = terminal.Write([]byte("\x1b[0n")) // everything is cool
case "6": // report cursor position
_ = terminal.Write([]byte(fmt.Sprintf(
"\x1b[%d;%dR",
terminal.ActiveBuffer().CursorLine()+1,
terminal.ActiveBuffer().CursorColumn()+1,
)))
default:
return fmt.Errorf("Unknown Device Status Report identifier: %s", params[0])
}
return nil
}
func csiCursorUpHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 0 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.ActiveBuffer().MovePosition(0, -int16(distance))
return nil
}
func csiCursorDownHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 0 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.ActiveBuffer().MovePosition(0, int16(distance))
return nil
}
func csiCursorForwardHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 0 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.ActiveBuffer().MovePosition(int16(distance), 0)
return nil
}
func csiCursorBackwardHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 0 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.ActiveBuffer().MovePosition(-int16(distance), 0)
return nil
}
func csiCursorNextLineHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 0 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.ActiveBuffer().MovePosition(0, int16(distance))
terminal.ActiveBuffer().SetPosition(0, terminal.ActiveBuffer().CursorLine())
return nil
}
func csiCursorPrecedingLineHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 0 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.ActiveBuffer().MovePosition(0, -int16(distance))
terminal.ActiveBuffer().SetPosition(0, terminal.ActiveBuffer().CursorLine())
return nil
}
func csiCursorCharacterAbsoluteHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 0 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || params[0] == "" {
distance = 1
}
}
terminal.ActiveBuffer().SetPosition(uint16(distance-1), terminal.ActiveBuffer().CursorLine())
return nil
}
func csiCursorPositionHandler(params []string, intermediate string, terminal *Terminal) error {
x, y := 1, 1
if len(params) == 2 {
var err error
if params[0] != "" {
y, err = strconv.Atoi(string(params[0]))
if err != nil || y < 1 {
y = 1
}
}
if params[1] != "" {
x, err = strconv.Atoi(string(params[1]))
if err != nil || x < 1 {
x = 1
}
}
}
terminal.ActiveBuffer().SetPosition(uint16(x-1), uint16(y-1))
return nil
}
func csiScrollUpHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 1 {
return fmt.Errorf("Not supported")
}
if len(params) == 1 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.logger.Debugf("Scrolling up %d", distance)
terminal.ScrollUp(uint16(distance))
return nil
}
func csiInsertBlankCharactersHandler(params []string, intermediate string, terminal *Terminal) error {
count := 1
if len(params) > 1 {
return fmt.Errorf("Not supported")
}
if len(params) == 1 {
var err error
count, err = strconv.Atoi(params[0])
if err != nil || count < 1 {
count = 1
}
}
terminal.ActiveBuffer().InsertBlankCharacters(count)
return nil
}
func csiInsertLinesHandler(params []string, intermediate string, terminal *Terminal) error {
count := 1
if len(params) > 1 {
return fmt.Errorf("Not supported")
}
if len(params) == 1 {
var err error
count, err = strconv.Atoi(params[0])
if err != nil || count < 1 {
count = 1
}
}
terminal.ActiveBuffer().InsertLines(count)
return nil
}
func csiDeleteLinesHandler(params []string, intermediate string, terminal *Terminal) error {
count := 1
if len(params) > 1 {
return fmt.Errorf("Not supported")
}
if len(params) == 1 {
var err error
count, err = strconv.Atoi(params[0])
if err != nil || count < 1 {
count = 1
}
}
terminal.ActiveBuffer().DeleteLines(count)
return nil
}
func csiScrollDownHandler(params []string, intermediate string, terminal *Terminal) error {
distance := 1
if len(params) > 1 {
return fmt.Errorf("Not supported")
}
if len(params) == 1 {
var err error
distance, err = strconv.Atoi(params[0])
if err != nil || distance < 1 {
distance = 1
}
}
terminal.logger.Debugf("Scrolling down %d", distance)
terminal.ScrollDown(uint16(distance))
return nil
}
// DECSTBM
func csiSetMarginsHandler(params []string, intermediate string, terminal *Terminal) error {
top := 1
bottom := int(terminal.ActiveBuffer().ViewHeight())
if len(params) > 2 {
return fmt.Errorf("Not set margins")
}
if len(params) > 0 {
var err error
top, err = strconv.Atoi(params[0])
if err != nil || top < 1 {
top = 1
}
if len(params) > 1 {
var err error
bottom, err = strconv.Atoi(params[1])
if err != nil || bottom > int(terminal.ActiveBuffer().ViewHeight()) || bottom < 1 {
bottom = int(terminal.ActiveBuffer().ViewHeight())
}
}
}
top--
bottom--
terminal.ActiveBuffer().SetVerticalMargins(uint(top), uint(bottom))
terminal.ActiveBuffer().SetPosition(0, 0)
return nil
}
func csiEraseCharactersHandler(params []string, intermediate string, terminal *Terminal) error {
count := 1
if len(params) > 0 {
var err error
count, err = strconv.Atoi(params[0])
if err != nil || count < 1 {
count = 1
}
}
terminal.ActiveBuffer().EraseCharacters(count)
return nil
}
func csiResetModeHandler(params []string, intermediate string, terminal *Terminal) error {
return csiSetMode(strings.Join(params, ""), false, terminal)
}
func csiSetModeHandler(params []string, intermediate string, terminal *Terminal) error {
return csiSetMode(strings.Join(params, ""), true, terminal)
}
func csiWindowManipulation(params []string, intermediate string, terminal *Terminal) error {
return fmt.Errorf("Window manipulation is not yet supported")
}
func csiLinePositionAbsolute(params []string, intermediate string, terminal *Terminal) error {
row := 1
if len(params) > 0 {
var err error
row, err = strconv.Atoi(params[0])
if err != nil || row < 1 {
row = 1
}
}
terminal.ActiveBuffer().SetPosition(terminal.ActiveBuffer().CursorColumn(), uint16(row-1))
return nil
}
func csiDeleteHandler(params []string, intermediate string, terminal *Terminal) error {
n := 1
if len(params) >= 1 {
var err error
n, err = strconv.Atoi(params[0])
if err != nil || n < 1 {
n = 1
}
}
terminal.ActiveBuffer().DeleteChars(n)
return nil
}
// CSI Ps J
func csiEraseInDisplayHandler(params []string, intermediate string, terminal *Terminal) error {
n := "0"
if len(params) > 0 {
n = params[0]
}
switch n {
case "0", "":
terminal.ActiveBuffer().EraseDisplayFromCursor()
case "1":
terminal.ActiveBuffer().EraseDisplayToCursor()
case "2", "3":
terminal.ActiveBuffer().EraseDisplay()
default:
return fmt.Errorf("Unsupported ED: CSI %s J", n)
}
return nil
}
// CSI Ps K
func csiEraseInLineHandler(params []string, intermediate string, terminal *Terminal) error {
n := "0"
if len(params) > 0 {
n = params[0]
}
switch n {
case "0", "": //erase adter cursor
terminal.ActiveBuffer().EraseLineFromCursor()
case "1": // erase to cursor inclusive
terminal.ActiveBuffer().EraseLineToCursor()
case "2": // erase entire
terminal.ActiveBuffer().EraseLine()
default:
return fmt.Errorf("Unsupported EL: CSI %s K", n)
}
return nil
}