-
Notifications
You must be signed in to change notification settings - Fork 14
/
ansi_windows.go
510 lines (419 loc) · 10.5 KB
/
ansi_windows.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
// Copyright 2013-2015 Bowery, Inc.
package prompt
import (
"bytes"
"os"
"unicode/utf8"
"unsafe"
)
// keyEventType is the key event type for an input record.
const keyEventType = 0x0001
var (
readConsoleInput = kernel.NewProc("ReadConsoleInputW")
)
// inputRecord describes a input event from a console.
type inputRecord struct {
eventType uint16
// Magic to get around the union C type, cast
// event to the type using unsafe.Pointer.
_ [2]byte
event [16]byte
}
// keyEventRecord describes a keyboard event.
type keyEventRecord struct {
keyDown int32
repeatCount uint16
virtualKeyCode uint16
virtualScanCode uint16
char uint16
controlKeyState uint32
}
// AnsiReader is an io.Reader that reads from a given file and converts Windows
// key codes to their equivalent ANSI escape codes.
type AnsiReader struct {
fd uintptr
buf []rune
}
// NewAnsiReader creates a AnsiReader from the given input file.
func NewAnsiReader(in *os.File) *AnsiReader {
return &AnsiReader{fd: in.Fd()}
}
// Read reads data from the input converting to ANSI escape codes that can be
// read over multiple Reads.
func (ar *AnsiReader) Read(b []byte) (int, error) {
if len(b) == 0 {
return 0, nil
}
if len(ar.buf) == 0 {
var runes []rune
var read uint32
rec := new(inputRecord)
for runes == nil {
ret, _, err := readConsoleInput.Call(ar.fd, uintptr(unsafe.Pointer(rec)),
1, uintptr(unsafe.Pointer(&read)))
if ret == 0 {
return 0, err
}
if rec.eventType != keyEventType {
continue
}
ke := (*keyEventRecord)(unsafe.Pointer(&rec.event))
if ke.keyDown == 0 {
continue
}
shift := false
if ke.controlKeyState&shiftKey != 0 {
shift = true
}
ctrl := false
if ke.controlKeyState&leftCtrlKey != 0 || ke.controlKeyState&rightCtrlKey != 0 {
ctrl = true
}
alt := false
if ke.controlKeyState&leftAltKey != 0 || ke.controlKeyState&rightAltKey != 0 {
alt = true
}
// Backspace, Return, Space.
if ke.char == ctrlH || ke.char == returnKey || ke.char == spaceKey {
code := string(returnKey)
if ke.char == ctrlH {
code = string(backKey)
} else if ke.char == spaceKey {
code = string(spaceKey)
}
if alt {
code = string(escKey) + code
}
runes = []rune(code)
break
}
// Generate runes for the chars and key codes.
if ke.char > 0 {
runes = []rune{rune(ke.char)}
} else {
code := string(escKey)
switch ke.virtualKeyCode {
case f1Key:
if ctrl {
continue
}
code += ar.shortFunction("P", shift, ctrl, alt)
case f2Key:
code += ar.shortFunction("Q", shift, ctrl, alt)
case f3Key:
code += ar.shortFunction("R", shift, ctrl, alt)
case f4Key:
code += ar.shortFunction("S", shift, ctrl, alt)
case f5Key:
code += ar.longFunction("15", shift, ctrl, alt)
case f6Key:
code += ar.longFunction("17", shift, ctrl, alt)
case f7Key:
code += ar.longFunction("18", shift, ctrl, alt)
case f8Key:
code += ar.longFunction("19", shift, ctrl, alt)
case f9Key:
code += ar.longFunction("20", shift, ctrl, alt)
case f10Key:
code += ar.longFunction("21", shift, ctrl, alt)
case f11Key:
code += ar.longFunction("23", shift, ctrl, alt)
case f12Key:
code += ar.longFunction("24", shift, ctrl, alt)
case insertKey:
if shift || ctrl {
continue
}
code += ar.longFunction("2", shift, ctrl, alt)
case deleteKey:
code += ar.longFunction("3", shift, ctrl, alt)
case homeKey:
code += "OH"
case endKey:
code += "OF"
case pgupKey:
if shift {
continue
}
code += ar.longFunction("5", shift, ctrl, alt)
case pgdownKey:
if shift {
continue
}
code += ar.longFunction("6", shift, ctrl, alt)
case upKey:
code += ar.arrow("A", shift, ctrl, alt)
case downKey:
code += ar.arrow("B", shift, ctrl, alt)
case leftKey:
code += ar.arrow("D", shift, ctrl, alt)
case rightKey:
code += ar.arrow("C", shift, ctrl, alt)
default:
continue
}
runes = []rune(code)
}
}
ar.buf = runes
}
// Get items from the buffer.
var n int
for i, r := range ar.buf {
if utf8.RuneLen(r) > len(b) {
ar.buf = ar.buf[i:]
return n, nil
}
nr := utf8.EncodeRune(b, r)
b = b[nr:]
n += nr
}
ar.buf = nil
return n, nil
}
// shortFunction creates a short function code.
func (ar *AnsiReader) shortFunction(ident string, shift, ctrl, alt bool) string {
code := "O"
if shift {
code += "1;2"
} else if ctrl {
code += "1;5"
} else if alt {
code += "1;3"
}
return code + ident
}
// longFunction creates a long function code.
func (ar *AnsiReader) longFunction(ident string, shift, ctrl, alt bool) string {
code := "["
code += ident
if shift {
code += ";2"
} else if ctrl {
code += ";5"
} else if alt {
code += ";3"
}
return code + "~"
}
// arrow creates an arrow code.
func (ar *AnsiReader) arrow(ident string, shift, ctrl, alt bool) string {
code := "["
if shift {
code += "1;2"
} else if ctrl {
code += "1;5"
} else if alt {
code += "1;3"
}
return code + ident
}
// AnsiWriter is an io.Writer that writes to a given file and converts ANSI
// escape codes to their equivalent Windows functionality.
type AnsiWriter struct {
file *os.File
buf []byte
}
// NewAnsiWriter creates a AnsiWriter from the given output.
func NewAnsiWriter(out *os.File) *AnsiWriter {
return &AnsiWriter{file: out}
}
// Write writes the buffer filtering out ANSI escape codes and converting to
// the Windows functionality needed. ANSI escape codes may be found over multiple
// Writes.
func (aw *AnsiWriter) Write(b []byte) (int, error) {
needsProcessing := bytes.Contains(b, []byte(string(escKey)))
if len(aw.buf) > 0 {
needsProcessing = true
}
if !needsProcessing {
return aw.file.Write(b)
}
var p []byte
for _, char := range b {
// Found the beginning of an escape.
if char == escKey {
aw.buf = append(aw.buf, char)
continue
}
// Function identifiers.
if len(aw.buf) == 1 && (char == '_' || char == 'P' || char == '[' ||
char == ']' || char == '^' || char == ' ' || char == '#' ||
char == '%' || char == '(' || char == ')' || char == '*' ||
char == '+') {
aw.buf = append(aw.buf, char)
continue
}
// Cursor functions.
if len(aw.buf) == 1 && (char == '7' || char == '8') {
// Add another char before because finish skips 2 items.
aw.buf = append(aw.buf, '_', char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// Keyboard functions.
if len(aw.buf) == 1 && (char == '=' || char == '>') {
aw.buf = append(aw.buf, char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// Bottom left function.
if len(aw.buf) == 1 && char == 'F' {
// Add extra char for finish.
aw.buf = append(aw.buf, '_', char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// Reset function.
if len(aw.buf) == 1 && char == 'c' {
// Add extra char for finish.
aw.buf = append(aw.buf, '_', char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// Space functions.
if len(aw.buf) >= 2 && aw.buf[1] == ' ' && (char == 'F' || char == 'G' ||
char == 'L' || char == 'M' || char == 'N') {
aw.buf = append(aw.buf, char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// Number functions.
if len(aw.buf) >= 2 && aw.buf[1] == '#' && (char >= '3' && char <= '6') ||
char == '8' {
aw.buf = append(aw.buf, char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// Percentage functions.
if len(aw.buf) >= 2 && aw.buf[1] == '%' && (char == '@' || char == 'G') {
aw.buf = append(aw.buf, char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// Character set functions.
if len(aw.buf) >= 2 && (aw.buf[1] == '(' || aw.buf[1] == ')' ||
aw.buf[1] == '*' || aw.buf[1] == '+') && (char == '0' ||
(char >= '4' && char <= '7') || char == '=' || (char >= 'A' &&
char <= 'C') || char == 'E' || char == 'H' || char == 'K' ||
char == 'Q' || char == 'R' || char == 'Y') {
aw.buf = append(aw.buf, char)
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// APC functions.
if len(aw.buf) >= 2 && aw.buf[1] == '_' {
aw.buf = append(aw.buf, char)
// End of APC.
if char == '\\' && aw.buf[len(aw.buf)-1] == escKey {
err := aw.finish(nil)
if err != nil {
return 0, err
}
}
continue
}
// DC functions.
if len(aw.buf) >= 2 && aw.buf[1] == 'P' {
aw.buf = append(aw.buf, char)
// End of DC.
if char == '\\' && aw.buf[len(aw.buf)-1] == escKey {
err := aw.finish(nil)
if err != nil {
return 0, err
}
}
continue
}
// CSI functions.
if len(aw.buf) >= 2 && aw.buf[1] == '[' {
aw.buf = append(aw.buf, char)
// End of CSI.
if char == '@' || (char >= 'A' && char <= 'M') || char == 'P' ||
char == 'S' || char == 'T' || char == 'X' || char == 'Z' ||
char == '`' || (char >= 'b' && char <= 'd') || (char >= 'f' &&
char <= 'i') || (char >= 'l' && char <= 'n') || (char >= 'p' &&
char <= 't') || char == 'w' || char == 'x' || char == 'z' ||
char == '{' || char == '|' {
err := aw.finish(nil)
if err != nil {
return 0, err
}
}
continue
}
// OSC functions.
if len(aw.buf) >= 2 && aw.buf[1] == ']' {
aw.buf = append(aw.buf, char)
// Capture incomplete code.
if len(aw.buf) == 4 && aw.buf[2] == '0' && char == ';' {
err := aw.finish(nil)
if err != nil {
return 0, err
}
continue
}
// End of OSC.
if (char == '\\' && aw.buf[len(aw.buf)-1] == escKey) || char == ctrlG {
err := aw.finish(nil)
if err != nil {
return 0, err
}
}
continue
}
// PM functions.
if len(aw.buf) >= 2 && aw.buf[1] == '^' {
aw.buf = append(aw.buf, char)
// End of PM.
if char == '\\' && aw.buf[len(aw.buf)-1] == escKey {
err := aw.finish(nil)
if err != nil {
return 0, err
}
}
continue
}
// Normal character, resets escape buffer.
if len(aw.buf) > 0 {
aw.buf = nil
}
p = append(p, char)
}
_, err := aw.file.Write(p)
return len(b), err
}
// finish finishes an ANSI escape code and calls the parsing function. Afterwards
// the escape buffer is emptied.
func (aw *AnsiWriter) finish(parse func([]byte) error) error {
var err error
if parse != nil {
err = parse(aw.buf[2:])
}
aw.buf = nil
return err
}