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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
|
// Package parser implements a VT/ANSI terminal parser based on Paul Williams'
// state diagram: https://www.vt100.net/emu/dec_ansi_parser
package parser
import (
"fmt"
"strings"
"avt-go/types"
)
// ---- Function (parsed terminal commands) -----------------------------------
type AnsiMode int
const (
AnsiModeInsert AnsiMode = 4
AnsiModeNewLine AnsiMode = 20
)
type DecMode int
const (
DecModeCursorKeys DecMode = 1
DecModeOrigin DecMode = 6
DecModeAutoWrap DecMode = 7
DecModeTextCursorEnable DecMode = 25
DecModeAltScreenBuffer DecMode = 1047
DecModeSaveCursor DecMode = 1048
DecModeSaveCursorAltScreen DecMode = 1049
)
type EdScope int
const (
EdScopeBelow EdScope = iota
EdScopeAbove
EdScopeAll
EdScopeSavedLines
)
type ElScope int
const (
ElScopeToRight ElScope = iota
ElScopeToLeft
ElScopeAll
)
type CtcOp int
const (
CtcOpSet CtcOp = iota
CtcOpClearCurrentColumn
CtcOpClearAll
)
type TbcScope int
const (
TbcScopeCurrentColumn TbcScope = iota
TbcScopeAll
)
type XtwinopsOp struct {
Cols, Rows uint16
}
type SgrOp interface{ isSgrOp() }
type SgrReset struct{}
type SgrSetBoldIntensity struct{}
type SgrSetFaintIntensity struct{}
type SgrSetItalic struct{}
type SgrSetUnderline struct{}
type SgrSetBlink struct{}
type SgrSetInverse struct{}
type SgrSetStrikethrough struct{}
type SgrResetIntensity struct{}
type SgrResetItalic struct{}
type SgrResetUnderline struct{}
type SgrResetBlink struct{}
type SgrResetInverse struct{}
type SgrResetStrikethrough struct{}
type SgrSetForegroundColor struct{ Color types.Color }
type SgrResetForegroundColor struct{}
type SgrSetBackgroundColor struct{ Color types.Color }
type SgrResetBackgroundColor struct{}
func (SgrReset) isSgrOp() {}
func (SgrSetBoldIntensity) isSgrOp() {}
func (SgrSetFaintIntensity) isSgrOp() {}
func (SgrSetItalic) isSgrOp() {}
func (SgrSetUnderline) isSgrOp() {}
func (SgrSetBlink) isSgrOp() {}
func (SgrSetInverse) isSgrOp() {}
func (SgrSetStrikethrough) isSgrOp() {}
func (SgrResetIntensity) isSgrOp() {}
func (SgrResetItalic) isSgrOp() {}
func (SgrResetUnderline) isSgrOp() {}
func (SgrResetBlink) isSgrOp() {}
func (SgrResetInverse) isSgrOp() {}
func (SgrResetStrikethrough) isSgrOp() {}
func (SgrSetForegroundColor) isSgrOp() {}
func (SgrResetForegroundColor) isSgrOp() {}
func (SgrSetBackgroundColor) isSgrOp() {}
func (SgrResetBackgroundColor) isSgrOp() {}
// Function represents a single parsed terminal command.
type Function interface{ isFunction() }
type FuncBs struct{}
type FuncCbt struct{ N uint16 }
type FuncCha struct{ N uint16 }
type FuncCht struct{ N uint16 }
type FuncCnl struct{ N uint16 }
type FuncCpl struct{ N uint16 }
type FuncCr struct{}
type FuncCtc struct{ Op CtcOp }
type FuncCub struct{ N uint16 }
type FuncCud struct{ N uint16 }
type FuncCuf struct{ N uint16 }
type FuncCup struct{ Row, Col uint16 }
type FuncCuu struct{ N uint16 }
type FuncDch struct{ N uint16 }
type FuncDecaln struct{}
type FuncDecrc struct{}
type FuncDecrst struct{ Modes []DecMode }
type FuncDecsc struct{}
type FuncDecset struct{ Modes []DecMode }
type FuncDecstbm struct{ Top, Bottom uint16 }
type FuncDecstr struct{}
type FuncDl struct{ N uint16 }
type FuncEch struct{ N uint16 }
type FuncEd struct{ Scope EdScope }
type FuncEl struct{ Scope ElScope }
type FuncG1d4 struct{ Charset types.Charset }
type FuncGzd4 struct{ Charset types.Charset }
type FuncHt struct{}
type FuncHts struct{}
type FuncIch struct{ N uint16 }
type FuncIl struct{ N uint16 }
type FuncLf struct{}
type FuncNel struct{}
type FuncPrint struct{ Char rune }
type FuncRep struct{ N uint16 }
type FuncRi struct{}
type FuncRis struct{}
type FuncRm struct{ Modes []AnsiMode }
type FuncScorc struct{}
type FuncScosc struct{}
type FuncSd struct{ N uint16 }
type FuncSgr struct{ Ops []SgrOp }
type FuncSi struct{}
type FuncSm struct{ Modes []AnsiMode }
type FuncSo struct{}
type FuncSu struct{ N uint16 }
type FuncTbc struct{ Scope TbcScope }
type FuncVpa struct{ N uint16 }
type FuncVpr struct{ N uint16 }
type FuncXtwinops struct{ Op XtwinopsOp }
func (FuncBs) isFunction() {}
func (FuncCbt) isFunction() {}
func (FuncCha) isFunction() {}
func (FuncCht) isFunction() {}
func (FuncCnl) isFunction() {}
func (FuncCpl) isFunction() {}
func (FuncCr) isFunction() {}
func (FuncCtc) isFunction() {}
func (FuncCub) isFunction() {}
func (FuncCud) isFunction() {}
func (FuncCuf) isFunction() {}
func (FuncCup) isFunction() {}
func (FuncCuu) isFunction() {}
func (FuncDch) isFunction() {}
func (FuncDecaln) isFunction() {}
func (FuncDecrc) isFunction() {}
func (FuncDecrst) isFunction() {}
func (FuncDecsc) isFunction() {}
func (FuncDecset) isFunction() {}
func (FuncDecstbm) isFunction() {}
func (FuncDecstr) isFunction() {}
func (FuncDl) isFunction() {}
func (FuncEch) isFunction() {}
func (FuncEd) isFunction() {}
func (FuncEl) isFunction() {}
func (FuncG1d4) isFunction() {}
func (FuncGzd4) isFunction() {}
func (FuncHt) isFunction() {}
func (FuncHts) isFunction() {}
func (FuncIch) isFunction() {}
func (FuncIl) isFunction() {}
func (FuncLf) isFunction() {}
func (FuncNel) isFunction() {}
func (FuncPrint) isFunction() {}
func (FuncRep) isFunction() {}
func (FuncRi) isFunction() {}
func (FuncRis) isFunction() {}
func (FuncRm) isFunction() {}
func (FuncScorc) isFunction() {}
func (FuncScosc) isFunction() {}
func (FuncSd) isFunction() {}
func (FuncSgr) isFunction() {}
func (FuncSi) isFunction() {}
func (FuncSm) isFunction() {}
func (FuncSo) isFunction() {}
func (FuncSu) isFunction() {}
func (FuncTbc) isFunction() {}
func (FuncVpa) isFunction() {}
func (FuncVpr) isFunction() {}
func (FuncXtwinops) isFunction() {}
// ---- Parser state ----------------------------------------------------------
type State int
const (
StateGround State = iota
StateEscape
StateEscapeIntermediate
StateCsiEntry
StateCsiParam
StateCsiIntermediate
StateCsiIgnore
StateDcsEntry
StateDcsParam
StateDcsIntermediate
StateDcsPassthrough
StateDcsIgnore
StateOscString
StateSosPmApcString
)
const paramsLen = 32
const maxParamLen = 6
type param struct {
curPart int
parts [maxParamLen]uint16
}
func (p *param) clear() {
for i := 0; i <= p.curPart; i++ {
p.parts[i] = 0
}
p.curPart = 0
}
func (p *param) addPart() {
if p.curPart < maxParamLen-1 {
p.curPart++
}
}
func (p *param) addDigit(d uint8) {
n := &p.parts[p.curPart]
*n = uint16(10*uint32(*n) + uint32(d))
}
func (p *param) asU16() uint16 {
return p.parts[0]
}
func (p *param) getParts() []uint16 {
return p.parts[:p.curPart+1]
}
func (p *param) String() string {
parts := p.getParts()
if len(parts) == 0 {
return ""
}
s := fmt.Sprintf("%d", parts[0])
for _, v := range parts[1:] {
s += fmt.Sprintf(":%d", v)
}
return s
}
// Parser is the VT state machine.
type Parser struct {
State State
params [paramsLen]param
curParam int
intermediate *rune
}
func New() *Parser {
return &Parser{}
}
// Feed processes one rune and returns a Function if one was emitted.
func (p *Parser) Feed(input rune) Function {
// Map anything >= 0xa0 to 0x41 for state-machine matching
input2 := input
if input2 >= '\u00a0' {
input2 = '\u0041'
}
switch {
case p.State == StateGround && input2 >= '\u0020' && input2 <= '\u007f':
return FuncPrint{Char: input}
case p.State == StateCsiParam && input2 >= '\u0030' && input2 <= '\u003b':
p.feedParam(input)
case input2 == '\u001b':
p.State = StateEscape
p.doClear()
case p.State == StateEscape && input2 == '\u005b':
p.State = StateCsiEntry
p.doClear()
case (p.State == StateCsiParam || p.State == StateCsiEntry || p.State == StateCsiIntermediate) &&
input2 >= '\u0040' && input2 <= '\u007e':
p.State = StateGround
return p.csiDispatch(input)
case (p.State == StateCsiEntry && input2 >= '\u0030' && input2 <= '\u0039') ||
(p.State == StateCsiEntry && input2 == '\u003b'):
p.State = StateCsiParam
p.feedParam(input)
case p.State == StateGround &&
((input2 >= '\u0000' && input2 <= '\u0017') || input2 == '\u0019' || (input2 >= '\u001c' && input2 <= '\u001f')):
return p.execute(input)
case p.State == StateOscString && input2 >= '\u0020' && input2 <= '\u007f':
// osc_put - ignored
case p.State == StateEscape && input2 >= '\u0020' && input2 <= '\u002f':
p.State = StateEscapeIntermediate
p.collect(input)
case (p.State == StateEscapeIntermediate && input2 >= '\u0030' && input2 <= '\u007e') ||
(p.State == StateEscape && input2 >= '\u0030' && input2 <= '\u004f') ||
(p.State == StateEscape && input2 >= '\u0051' && input2 <= '\u0057') ||
(p.State == StateEscape && input2 == '\u0059') ||
(p.State == StateEscape && input2 == '\u005a') ||
(p.State == StateEscape && input2 == '\u005c') ||
(p.State == StateEscape && input2 >= '\u0060' && input2 <= '\u007e'):
p.State = StateGround
return p.escDispatch(input)
case p.State == StateCsiEntry && input2 >= '\u003c' && input2 <= '\u003f':
p.State = StateCsiParam
p.collect(input)
case p.State == StateDcsPassthrough &&
((input2 >= '\u0000' && input2 <= '\u0017') || input2 == '\u0019' || (input2 >= '\u001c' && input2 <= '\u007e')):
// put - ignored
case p.State == StateCsiIgnore && input2 >= '\u0040' && input2 <= '\u007e':
p.State = StateGround
case (p.State == StateCsiParam && input2 >= '\u003c' && input2 <= '\u003f') ||
(p.State == StateCsiIntermediate && input2 >= '\u0030' && input2 <= '\u003f') ||
(p.State == StateCsiEntry && input2 == '\u003a'):
p.State = StateCsiIgnore
case p.State == StateEscape && input2 == '\u005d':
p.State = StateOscString
case p.State == StateOscString && input2 == '\u0007':
// xterm non-ANSI ST
p.State = StateGround
case input2 == '\u0018' || input2 == '\u001a' ||
(input2 >= '\u0080' && input2 <= '\u008f') ||
(input2 >= '\u0091' && input2 <= '\u0097') ||
input2 == '\u0099' || input2 == '\u009a':
p.State = StateGround
return p.execute(input)
case p.State == StateEscape && input2 == '\u0050':
p.State = StateDcsEntry
p.doClear()
case (p.State == StateCsiParam || p.State == StateCsiEntry) && input2 >= '\u0020' && input2 <= '\u002f':
p.State = StateCsiIntermediate
p.collect(input)
case (p.State == StateDcsParam && input2 >= '\u0030' && input2 <= '\u0039') ||
(p.State == StateDcsParam && input2 == '\u003b'):
p.feedParam(input)
case p.State == StateDcsEntry && input2 >= '\u003c' && input2 <= '\u003f':
p.State = StateDcsParam
p.collect(input)
case (p.State == StateDcsEntry || p.State == StateDcsParam || p.State == StateDcsIntermediate) &&
input2 >= '\u0040' && input2 <= '\u007e':
p.State = StateDcsPassthrough
case (p.State == StateDcsEntry || p.State == StateDcsParam) && input2 >= '\u0020' && input2 <= '\u002f':
p.State = StateDcsIntermediate
p.collect(input)
case (p.State == StateCsiIntermediate || p.State == StateEscapeIntermediate || p.State == StateDcsIntermediate) &&
input2 >= '\u0020' && input2 <= '\u002f':
p.collect(input)
case (p.State == StateDcsEntry && input2 == '\u003a') ||
(p.State == StateDcsIntermediate && input2 >= '\u0030' && input2 <= '\u003f') ||
(p.State == StateDcsParam && input2 == '\u003a') ||
(p.State == StateDcsParam && input2 >= '\u003c' && input2 <= '\u003f'):
p.State = StateDcsIgnore
case (p.State == StateDcsEntry && input2 >= '\u0030' && input2 <= '\u0039') ||
(p.State == StateDcsEntry && input2 == '\u003b'):
p.State = StateDcsParam
p.feedParam(input)
case isEscOrCsiState(p.State) &&
((input2 >= '\u0000' && input2 <= '\u0017') || input2 == '\u0019' || (input2 >= '\u001c' && input2 <= '\u001f')):
return p.execute(input)
case p.State == StateEscape && (input2 == '\u0058' || input2 == '\u005e' || input2 == '\u005f'):
p.State = StateSosPmApcString
case input2 == '\u0098' || input2 == '\u009e' || input2 == '\u009f':
p.State = StateSosPmApcString
case input2 == '\u009c':
p.State = StateGround
case input2 == '\u009d':
p.State = StateOscString
case input2 == '\u0090':
p.State = StateDcsEntry
p.doClear()
case input2 == '\u009b':
p.State = StateCsiEntry
p.doClear()
// ignored ranges (DEL in most states, CsiIgnore params, DCS C0, DcsIgnore/SosPmApc ranges)
default:
// ignore
}
return nil
}
func isEscOrCsiState(s State) bool {
switch s {
case StateEscape, StateEscapeIntermediate,
StateCsiEntry, StateCsiParam, StateCsiIntermediate, StateCsiIgnore:
return true
}
return false
}
func (p *Parser) execute(input rune) Function {
switch input {
case '\u0008':
return FuncBs{}
case '\u0009':
return FuncHt{}
case '\u000a', '\u000b', '\u000c':
return FuncLf{}
case '\u000d':
return FuncCr{}
case '\u000e':
return FuncSo{}
case '\u000f':
return FuncSi{}
case '\u0084':
return FuncLf{}
case '\u0085':
return FuncNel{}
case '\u0088':
return FuncHts{}
case '\u008d':
return FuncRi{}
}
return nil
}
func (p *Parser) doClear() {
for i := 0; i <= p.curParam; i++ {
p.params[i].clear()
}
p.curParam = 0
p.intermediate = nil
}
func (p *Parser) collect(input rune) {
p.intermediate = &input
}
func (p *Parser) feedParam(input rune) {
if input == ';' {
p.curParam++
if p.curParam == paramsLen {
p.curParam = paramsLen - 1
}
} else if input == ':' {
p.params[p.curParam].addPart()
} else {
p.params[p.curParam].addDigit(uint8(input) - 0x30)
}
}
func (p *Parser) escDispatch(input rune) Function {
inter := p.intermediate
switch {
case inter == nil && input >= '@' && input <= '_':
return p.execute(rune(input + 0x40))
case inter == nil && input == '7':
return FuncDecsc{}
case inter == nil && input == '8':
return FuncDecrc{}
case inter == nil && input == 'c':
p.State = StateGround
return FuncRis{}
case inter != nil && *inter == '#' && input == '8':
return FuncDecaln{}
case inter != nil && *inter == '(' && input == '0':
return FuncGzd4{Charset: types.CharsetDrawing}
case inter != nil && *inter == '(':
return FuncGzd4{Charset: types.CharsetAscii}
case inter != nil && *inter == ')' && input == '0':
return FuncG1d4{Charset: types.CharsetDrawing}
case inter != nil && *inter == ')':
return FuncG1d4{Charset: types.CharsetAscii}
}
return nil
}
func (p *Parser) csiDispatch(input rune) Function {
ps := p.params[:]
n := p.curParam
asU16 := func(i int) uint16 { return ps[i].asU16() }
switch {
case p.intermediate == nil && input == '@':
return FuncIch{N: asU16(0)}
case p.intermediate == nil && input == 'A':
return FuncCuu{N: asU16(0)}
case p.intermediate == nil && input == 'B':
return FuncCud{N: asU16(0)}
case p.intermediate == nil && input == 'C':
return FuncCuf{N: asU16(0)}
case p.intermediate == nil && input == 'D':
return FuncCub{N: asU16(0)}
case p.intermediate == nil && input == 'E':
return FuncCnl{N: asU16(0)}
case p.intermediate == nil && input == 'F':
return FuncCpl{N: asU16(0)}
case p.intermediate == nil && input == 'G':
return FuncCha{N: asU16(0)}
case p.intermediate == nil && input == 'H':
return FuncCup{Row: asU16(0), Col: asU16(1)}
case p.intermediate == nil && input == 'I':
return FuncCht{N: asU16(0)}
case p.intermediate == nil && input == 'J':
switch asU16(0) {
case 0:
return FuncEd{Scope: EdScopeBelow}
case 1:
return FuncEd{Scope: EdScopeAbove}
case 2:
return FuncEd{Scope: EdScopeAll}
case 3:
return FuncEd{Scope: EdScopeSavedLines}
}
case p.intermediate == nil && input == 'K':
switch asU16(0) {
case 0:
return FuncEl{Scope: ElScopeToRight}
case 1:
return FuncEl{Scope: ElScopeToLeft}
case 2:
return FuncEl{Scope: ElScopeAll}
}
case p.intermediate == nil && input == 'L':
return FuncIl{N: asU16(0)}
case p.intermediate == nil && input == 'M':
return FuncDl{N: asU16(0)}
case p.intermediate == nil && input == 'P':
return FuncDch{N: asU16(0)}
case p.intermediate == nil && input == 'S':
return FuncSu{N: asU16(0)}
case p.intermediate == nil && input == 'T':
return FuncSd{N: asU16(0)}
case p.intermediate == nil && input == 'W':
switch asU16(0) {
case 0:
return FuncCtc{Op: CtcOpSet}
case 2:
return FuncCtc{Op: CtcOpClearCurrentColumn}
case 5:
return FuncCtc{Op: CtcOpClearAll}
}
case p.intermediate == nil && input == 'X':
return FuncEch{N: asU16(0)}
case p.intermediate == nil && input == 'Z':
return FuncCbt{N: asU16(0)}
case p.intermediate == nil && input == '`':
return FuncCha{N: asU16(0)}
case p.intermediate == nil && input == 'a':
return FuncCuf{N: asU16(0)}
case p.intermediate == nil && input == 'b':
return FuncRep{N: asU16(0)}
case p.intermediate == nil && input == 'd':
return FuncVpa{N: asU16(0)}
case p.intermediate == nil && input == 'e':
return FuncVpr{N: asU16(0)}
case p.intermediate == nil && input == 'f':
return FuncCup{Row: asU16(0), Col: asU16(1)}
case p.intermediate == nil && input == 'g':
switch asU16(0) {
case 0:
return FuncTbc{Scope: TbcScopeCurrentColumn}
case 3:
return FuncTbc{Scope: TbcScopeAll}
}
case p.intermediate == nil && input == 'h':
return FuncSm{Modes: collectAnsiModes(ps[:n+1])}
case p.intermediate == nil && input == 'l':
return FuncRm{Modes: collectAnsiModes(ps[:n+1])}
case p.intermediate == nil && input == 'm':
return FuncSgr{Ops: decodeSgr(ps[:n+1])}
case p.intermediate == nil && input == 'r':
return FuncDecstbm{Top: asU16(0), Bottom: asU16(1)}
case p.intermediate == nil && input == 's':
return FuncScosc{}
case p.intermediate == nil && input == 't':
if asU16(0) == 8 {
return FuncXtwinops{Op: XtwinopsOp{Cols: asU16(2), Rows: asU16(1)}}
}
case p.intermediate == nil && input == 'u':
return FuncScorc{}
case p.intermediate != nil && *p.intermediate == '!' && input == 'p':
return FuncDecstr{}
case p.intermediate != nil && *p.intermediate == '?' && input == 'h':
return FuncDecset{Modes: collectDecModes(ps[:n+1])}
case p.intermediate != nil && *p.intermediate == '?' && input == 'l':
return FuncDecrst{Modes: collectDecModes(ps[:n+1])}
}
return nil
}
func collectAnsiModes(ps []param) []AnsiMode {
var modes []AnsiMode
for i := range ps {
if m, ok := parseAnsiMode(ps[i].asU16()); ok {
modes = append(modes, m)
}
}
return modes
}
func parseAnsiMode(v uint16) (AnsiMode, bool) {
switch v {
case 4:
return AnsiModeInsert, true
case 20:
return AnsiModeNewLine, true
}
return 0, false
}
func collectDecModes(ps []param) []DecMode {
var modes []DecMode
for i := range ps {
if m, ok := parseDecMode(ps[i].asU16()); ok {
modes = append(modes, m)
}
}
return modes
}
func parseDecMode(v uint16) (DecMode, bool) {
switch v {
case 1:
return DecModeCursorKeys, true
case 6:
return DecModeOrigin, true
case 7:
return DecModeAutoWrap, true
case 25:
return DecModeTextCursorEnable, true
case 47:
return DecModeAltScreenBuffer, true // legacy of 1047
case 1047:
return DecModeAltScreenBuffer, true
case 1048:
return DecModeSaveCursor, true
case 1049:
return DecModeSaveCursorAltScreen, true
}
return 0, false
}
func decodeSgr(ps []param) []SgrOp {
var ops []SgrOp
i := 0
for i < len(ps) {
parts := ps[i].getParts()
switch {
case matchParts(parts, 0):
ops = append(ops, SgrReset{})
i++
case matchParts(parts, 1):
ops = append(ops, SgrSetBoldIntensity{})
i++
case matchParts(parts, 2):
ops = append(ops, SgrSetFaintIntensity{})
i++
case matchParts(parts, 3):
ops = append(ops, SgrSetItalic{})
i++
case matchParts(parts, 4):
ops = append(ops, SgrSetUnderline{})
i++
case matchParts(parts, 5):
ops = append(ops, SgrSetBlink{})
i++
case matchParts(parts, 7):
ops = append(ops, SgrSetInverse{})
i++
case matchParts(parts, 9):
ops = append(ops, SgrSetStrikethrough{})
i++
case matchParts(parts, 21), matchParts(parts, 22):
ops = append(ops, SgrResetIntensity{})
i++
case matchParts(parts, 23):
ops = append(ops, SgrResetItalic{})
i++
case matchParts(parts, 24):
ops = append(ops, SgrResetUnderline{})
i++
case matchParts(parts, 25):
ops = append(ops, SgrResetBlink{})
i++
case matchParts(parts, 27):
ops = append(ops, SgrResetInverse{})
i++
case matchParts(parts, 29):
ops = append(ops, SgrResetStrikethrough{})
i++
case len(parts) == 1 && parts[0] >= 30 && parts[0] <= 37:
ops = append(ops, SgrSetForegroundColor{Color: types.ColorIndexed{N: uint8(parts[0] - 30)}})
i++
case len(parts) >= 5 && parts[0] == 38 && parts[1] == 2:
// 38:2:r:g:b or 38:2:_:r:g:b (sub-param form)
if len(parts) >= 6 {
ops = append(ops, SgrSetForegroundColor{Color: types.ColorRGB{R: uint8(parts[3]), G: uint8(parts[4]), B: uint8(parts[5])}})
} else {
ops = append(ops, SgrSetForegroundColor{Color: types.ColorRGB{R: uint8(parts[2]), G: uint8(parts[3]), B: uint8(parts[4])}})
}
i++
case len(parts) >= 3 && parts[0] == 38 && parts[1] == 5:
ops = append(ops, SgrSetForegroundColor{Color: types.ColorIndexed{N: uint8(parts[2])}})
i++
case len(parts) == 1 && parts[0] == 38:
// multi-param form: next params are the color
switch {
case i+1 < len(ps) && ps[i+1].asU16() == 2:
if i+4 < len(ps) {
r := ps[i+2].asU16()
g := ps[i+3].asU16()
b := ps[i+4].asU16()
ops = append(ops, SgrSetForegroundColor{Color: types.ColorRGB{R: uint8(r), G: uint8(g), B: uint8(b)}})
i += 5
} else {
i += 2
}
case i+1 < len(ps) && ps[i+1].asU16() == 5:
if i+2 < len(ps) {
idx := ps[i+2].asU16()
ops = append(ops, SgrSetForegroundColor{Color: types.ColorIndexed{N: uint8(idx)}})
i += 3
} else {
i += 2
}
default:
i++
}
case matchParts(parts, 39):
ops = append(ops, SgrResetForegroundColor{})
i++
case len(parts) == 1 && parts[0] >= 40 && parts[0] <= 47:
ops = append(ops, SgrSetBackgroundColor{Color: types.ColorIndexed{N: uint8(parts[0] - 40)}})
i++
case len(parts) >= 5 && parts[0] == 48 && parts[1] == 2:
if len(parts) >= 6 {
ops = append(ops, SgrSetBackgroundColor{Color: types.ColorRGB{R: uint8(parts[3]), G: uint8(parts[4]), B: uint8(parts[5])}})
} else {
ops = append(ops, SgrSetBackgroundColor{Color: types.ColorRGB{R: uint8(parts[2]), G: uint8(parts[3]), B: uint8(parts[4])}})
}
i++
case len(parts) >= 3 && parts[0] == 48 && parts[1] == 5:
ops = append(ops, SgrSetBackgroundColor{Color: types.ColorIndexed{N: uint8(parts[2])}})
i++
case len(parts) == 1 && parts[0] == 48:
switch {
case i+1 < len(ps) && ps[i+1].asU16() == 2:
if i+4 < len(ps) {
r := ps[i+2].asU16()
g := ps[i+3].asU16()
b := ps[i+4].asU16()
ops = append(ops, SgrSetBackgroundColor{Color: types.ColorRGB{R: uint8(r), G: uint8(g), B: uint8(b)}})
i += 5
} else {
i += 2
}
case i+1 < len(ps) && ps[i+1].asU16() == 5:
if i+2 < len(ps) {
idx := ps[i+2].asU16()
ops = append(ops, SgrSetBackgroundColor{Color: types.ColorIndexed{N: uint8(idx)}})
i += 3
} else {
i += 2
}
default:
i++
}
case matchParts(parts, 49):
ops = append(ops, SgrResetBackgroundColor{})
i++
case len(parts) == 1 && parts[0] >= 90 && parts[0] <= 97:
ops = append(ops, SgrSetForegroundColor{Color: types.ColorIndexed{N: uint8(parts[0] - 90 + 8)}})
i++
case len(parts) == 1 && parts[0] >= 100 && parts[0] <= 107:
ops = append(ops, SgrSetBackgroundColor{Color: types.ColorIndexed{N: uint8(parts[0] - 100 + 8)}})
i++
default:
i++
}
}
return ops
}
func matchParts(parts []uint16, v uint16) bool {
return len(parts) == 1 && parts[0] == v
}
// ---- Dump ------------------------------------------------------------------
// Dump serialises the in-progress parser state back to an escape sequence
// prefix (used by Vt.Dump).
func (p *Parser) Dump() string {
var sb strings.Builder
switch p.State {
case StateGround:
// nothing
case StateEscape:
sb.WriteRune('\u001b')
case StateEscapeIntermediate:
sb.WriteRune('\u001b')
if p.intermediate != nil {
sb.WriteRune(*p.intermediate)
}
case StateCsiEntry:
sb.WriteRune('\u009b')
case StateCsiParam:
sb.WriteRune('\u009b')
if p.intermediate != nil {
sb.WriteRune(*p.intermediate)
}
parts := make([]string, p.curParam+1)
for i := 0; i <= p.curParam; i++ {
parts[i] = p.params[i].String()
}
sb.WriteString(strings.Join(parts, ";"))
case StateCsiIntermediate:
sb.WriteRune('\u009b')
if p.intermediate != nil {
sb.WriteRune(*p.intermediate)
}
case StateCsiIgnore:
sb.WriteString("\u009b\u003a")
case StateDcsEntry:
sb.WriteRune('\u0090')
case StateDcsIntermediate:
sb.WriteRune('\u0090')
if p.intermediate != nil {
sb.WriteRune(*p.intermediate)
}
case StateDcsParam:
sb.WriteRune('\u0090')
if p.intermediate != nil {
sb.WriteRune(*p.intermediate)
}
parts := make([]string, p.curParam+1)
for i := 0; i <= p.curParam; i++ {
parts[i] = p.params[i].String()
}
sb.WriteString(strings.Join(parts, ";"))
case StateDcsPassthrough:
sb.WriteRune('\u0090')
if p.intermediate != nil {
sb.WriteRune(*p.intermediate)
}
sb.WriteRune('\u0040')
case StateDcsIgnore:
sb.WriteString("\u0090\u003a")
case StateOscString:
sb.WriteRune('\u009d')
case StateSosPmApcString:
sb.WriteRune('\u0098')
}
return sb.String()
}
// DumpFunctions serialises a slice of Functions to an escape sequence string.
func DumpFunctions(funs []Function) string {
var sb strings.Builder
for _, f := range funs {
dumpFunction(&sb, f)
}
return sb.String()
}
func dumpFunction(sb *strings.Builder, fun Function) {
switch f := fun.(type) {
case FuncBs:
sb.WriteRune('\u0008')
case FuncCbt:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'Z')
case FuncCha:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'G')
case FuncCht:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'I')
case FuncCnl:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'E')
case FuncCpl:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'F')
case FuncCr:
sb.WriteRune('\r')
case FuncCtc:
var param int
switch f.Op {
case CtcOpSet:
param = 0
case CtcOpClearCurrentColumn:
param = 2
case CtcOpClearAll:
param = 5
}
pushCSI(sb, nil, []string{fmt.Sprintf("%d", param)}, 'W')
case FuncCub:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'D')
case FuncCud:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'B')
case FuncCuf:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'C')
case FuncCup:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.Row), fmt.Sprintf("%d", f.Col)}, 'H')
case FuncCuu:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'A')
case FuncDch:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'P')
case FuncDecaln:
pushESC(sb, runePtr('#'), '8')
case FuncDecrc:
pushESC(sb, nil, '8')
case FuncDecrst:
params := decModesToParams(f.Modes)
pushCSI(sb, runePtr('?'), params, 'l')
case FuncDecsc:
pushESC(sb, nil, '7')
case FuncDecset:
params := decModesToParams(f.Modes)
pushCSI(sb, runePtr('?'), params, 'h')
case FuncDecstbm:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.Top), fmt.Sprintf("%d", f.Bottom)}, 'r')
case FuncDecstr:
pushCSI(sb, runePtr('!'), nil, 'p')
case FuncDl:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'M')
case FuncEch:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'X')
case FuncEd:
var param int
switch f.Scope {
case EdScopeBelow:
param = 0
case EdScopeAbove:
param = 1
case EdScopeAll:
param = 2
case EdScopeSavedLines:
param = 3
}
pushCSI(sb, nil, []string{fmt.Sprintf("%d", param)}, 'J')
case FuncEl:
var param int
switch f.Scope {
case ElScopeToRight:
param = 0
case ElScopeToLeft:
param = 1
case ElScopeAll:
param = 2
}
pushCSI(sb, nil, []string{fmt.Sprintf("%d", param)}, 'K')
case FuncG1d4:
ch := 'B'
if f.Charset == types.CharsetDrawing {
ch = '0'
}
pushESC(sb, runePtr(')'), rune(ch))
case FuncGzd4:
ch := 'B'
if f.Charset == types.CharsetDrawing {
ch = '0'
}
pushESC(sb, runePtr('('), rune(ch))
case FuncHt:
sb.WriteRune('\t')
case FuncHts:
pushESC(sb, nil, 'H')
case FuncIch:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, '@')
case FuncIl:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'L')
case FuncLf:
sb.WriteRune('\n')
case FuncNel:
pushESC(sb, nil, 'E')
case FuncPrint:
sb.WriteRune(f.Char)
case FuncRep:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'b')
case FuncRi:
pushESC(sb, nil, 'M')
case FuncRis:
pushESC(sb, nil, 'c')
case FuncRm:
params := ansiModesToParams(f.Modes)
pushCSI(sb, nil, params, 'l')
case FuncScorc:
pushCSI(sb, nil, nil, 'u')
case FuncScosc:
pushCSI(sb, nil, nil, 's')
case FuncSd:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'T')
case FuncSgr:
dumpSgr(sb, f.Ops)
case FuncSi:
sb.WriteRune('\u000f')
case FuncSm:
params := ansiModesToParams(f.Modes)
pushCSI(sb, nil, params, 'h')
case FuncSo:
sb.WriteRune('\u000e')
case FuncSu:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'S')
case FuncTbc:
var param int
switch f.Scope {
case TbcScopeCurrentColumn:
param = 0
case TbcScopeAll:
param = 3
}
pushCSI(sb, nil, []string{fmt.Sprintf("%d", param)}, 'g')
case FuncVpa:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'd')
case FuncVpr:
pushCSI(sb, nil, []string{fmt.Sprintf("%d", f.N)}, 'e')
case FuncXtwinops:
pushCSI(sb, nil, []string{"8", fmt.Sprintf("%d", f.Op.Rows), fmt.Sprintf("%d", f.Op.Cols)}, 't')
}
}
func pushESC(sb *strings.Builder, intermediate *rune, finalChar rune) {
sb.WriteRune('\u001b')
if intermediate != nil {
sb.WriteRune(*intermediate)
}
sb.WriteRune(finalChar)
}
func pushCSI(sb *strings.Builder, intermediate *rune, params []string, finalChar rune) {
sb.WriteRune('\u001b')
sb.WriteRune('[')
if intermediate != nil {
sb.WriteRune(*intermediate)
}
for i, p := range params {
if i > 0 {
sb.WriteRune(';')
}
sb.WriteString(p)
}
sb.WriteRune(finalChar)
}
func runePtr(r rune) *rune { return &r }
func decModesToParams(modes []DecMode) []string {
params := make([]string, len(modes))
for i, m := range modes {
params[i] = fmt.Sprintf("%d", int(m))
}
return params
}
func ansiModesToParams(modes []AnsiMode) []string {
params := make([]string, len(modes))
for i, m := range modes {
params[i] = fmt.Sprintf("%d", int(m))
}
return params
}
func DumpSgrColor(color types.Color, base uint8) string {
switch c := color.(type) {
case types.ColorIndexed:
if c.N < 8 {
return fmt.Sprintf("%d", base+c.N)
} else if c.N < 16 {
return fmt.Sprintf("%d", base+52+c.N)
}
return fmt.Sprintf("%d:5:%d", base+8, c.N)
case types.ColorRGB:
return fmt.Sprintf("%d:2:%d:%d:%d", base+8, c.R, c.G, c.B)
}
return ""
}
func dumpSgr(sb *strings.Builder, ops []SgrOp) {
if len(ops) == 0 {
sb.WriteString("\x1b[38;2m")
return
}
var params []string
for _, op := range ops {
switch o := op.(type) {
case SgrReset:
params = append(params, "0")
case SgrSetBoldIntensity:
params = append(params, "1")
case SgrSetFaintIntensity:
params = append(params, "2")
case SgrSetItalic:
params = append(params, "3")
case SgrSetUnderline:
params = append(params, "4")
case SgrSetBlink:
params = append(params, "5")
case SgrSetInverse:
params = append(params, "7")
case SgrSetStrikethrough:
params = append(params, "9")
case SgrResetIntensity:
params = append(params, "22")
case SgrResetItalic:
params = append(params, "23")
case SgrResetUnderline:
params = append(params, "24")
case SgrResetBlink:
params = append(params, "25")
case SgrResetInverse:
params = append(params, "27")
case SgrResetStrikethrough:
params = append(params, "29")
case SgrSetForegroundColor:
params = append(params, DumpSgrColor(o.Color, 30))
case SgrResetForegroundColor:
params = append(params, "39")
case SgrSetBackgroundColor:
params = append(params, DumpSgrColor(o.Color, 40))
case SgrResetBackgroundColor:
params = append(params, "49")
}
}
pushCSI(sb, nil, params, 'm')
}
|