-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.swift
More file actions
1362 lines (1227 loc) · 48.8 KB
/
tests.swift
File metadata and controls
1362 lines (1227 loc) · 48.8 KB
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
#!/usr/bin/env swift
// tests.swift — Automated tests for quickTerminal's terminal engine
// Run: swift tests.swift
// Note: This file imports the Terminal and Cell types by re-declaring minimal stubs,
// then tests the core parsing logic in isolation.
import Foundation
// ============================================================================
// MARK: - Minimal Type Stubs (matching quickTerminal.swift)
// ============================================================================
struct TextAttrs: Equatable {
var fg: Int = 7
var bg: Int = 0
var bold = false
var dim = false
var italic = false
var underline: UInt8 = 0 // 0=none, 1=single, 2=double, 3=curly, 4=dotted, 5=dashed
var blink: UInt8 = 0 // 0=none, 1=slow, 2=rapid
var inverse = false
var strikethrough = false
var fgRGB: (UInt8, UInt8, UInt8)? = nil
var bgRGB: (UInt8, UInt8, UInt8)? = nil
var hidden = false
var overline = false
var ulColor: Int = -1
var ulRGB: (UInt8, UInt8, UInt8)? = nil
static func == (lhs: TextAttrs, rhs: TextAttrs) -> Bool {
lhs.fg == rhs.fg && lhs.bg == rhs.bg && lhs.bold == rhs.bold &&
lhs.dim == rhs.dim && lhs.italic == rhs.italic &&
lhs.underline == rhs.underline && lhs.blink == rhs.blink &&
lhs.inverse == rhs.inverse &&
lhs.hidden == rhs.hidden && lhs.strikethrough == rhs.strikethrough &&
lhs.overline == rhs.overline &&
lhs.fgRGB?.0 == rhs.fgRGB?.0 && lhs.fgRGB?.1 == rhs.fgRGB?.1 && lhs.fgRGB?.2 == rhs.fgRGB?.2 &&
lhs.bgRGB?.0 == rhs.bgRGB?.0 && lhs.bgRGB?.1 == rhs.bgRGB?.1 && lhs.bgRGB?.2 == rhs.bgRGB?.2 &&
lhs.ulColor == rhs.ulColor &&
lhs.ulRGB?.0 == rhs.ulRGB?.0 && lhs.ulRGB?.1 == rhs.ulRGB?.1 && lhs.ulRGB?.2 == rhs.ulRGB?.2
}
}
struct Cell {
var char: Unicode.Scalar = " "
var attrs = TextAttrs()
var width: UInt8 = 1
var hyperlink: String? = nil
}
// ============================================================================
// MARK: - Test Framework
// ============================================================================
var testsPassed = 0
var testsFailed = 0
var currentTest = ""
func test(_ name: String, _ body: () -> Void) {
currentTest = name
let before = testsFailed
body()
if testsFailed == before {
print(" ✓ \(name)")
}
}
func section(_ name: String) {
print("\n── \(name) ──")
}
func check(_ condition: Bool, _ message: String = "", file: String = #file, line: Int = #line) {
if condition {
testsPassed += 1
} else {
testsFailed += 1
print(" FAIL: \(currentTest) — \(message) (line \(line))")
}
}
func assertEqual<T: Equatable>(_ a: T, _ b: T, _ message: String = "", file: String = #file, line: Int = #line) {
if a == b {
testsPassed += 1
} else {
testsFailed += 1
print(" FAIL: \(currentTest) — expected \(b), got \(a) \(message) (line \(line))")
}
}
// ============================================================================
// MARK: - Terminal Engine (extracted for testing)
// We create a minimal Terminal that matches the real one's parsing behavior.
// ============================================================================
// Re-use the actual Terminal class by compiling against the main file would be ideal,
// but since it's a single-file app with Cocoa dependencies, we test the core logic
// by creating a standalone test terminal.
class TestTerminal {
var cols: Int
var rows: Int
var cursorX = 0
var cursorY = 0
var pendingWrap = false
var grid: [[Cell]]
var attrs = TextAttrs()
var savedX = 0, savedY = 0
var savedAttrs = TextAttrs()
var savedPendingWrap = false
var savedG0IsGraphics = false, savedG1IsGraphics = false
var savedUseG1 = false, savedOriginMode = false, savedAutoWrap = true
var scrollTop = 0
var scrollBottom: Int
var scrollback: [[Cell]] = []
var altGrid: [[Cell]]? = nil
var altX = 0, altY = 0
var cursorVisible = true
var appCursorMode = false
var appKeypadMode = false
var autoWrapMode = true
var originMode = false
var reverseVideoMode = false
var bracketedPasteMode = false
var cursorStyle = 0
var mouseMode = 0
var mouseEncoding = 0
var focusReportingMode = false
var synchronizedOutput = false
var charsetG0IsGraphics = false
var charsetG1IsGraphics = false
var useG1 = false
var tabStops = Set<Int>()
var kittyKbdStack: [Int] = []
var insertMode = false
var lastResponse = ""
static func emptyGrid(_ cols: Int, _ rows: Int) -> [[Cell]] {
Array(repeating: Array(repeating: Cell(), count: cols), count: rows)
}
init(cols: Int, rows: Int) {
self.cols = cols
self.rows = rows
self.scrollBottom = rows - 1
self.grid = Self.emptyGrid(cols, rows)
resetTabStops()
}
func resetTabStops() {
tabStops.removeAll()
for i in stride(from: 0, to: cols, by: 8) { tabStops.insert(i) }
}
// Simplified process: feed raw bytes
func feed(_ str: String) {
let data = Array(str.utf8)
var i = 0
while i < data.count {
let b = data[i]
// C1 control codes (multi-byte UTF-8: 0xC2 0x80-0x9F)
if b == 0xC2 && i + 1 < data.count && data[i+1] >= 0x80 && data[i+1] <= 0x9F {
let c1 = data[i+1]
switch c1 {
case 0x84: lf() // IND
case 0x85: cursorX = 0; lf() // NEL
case 0x8D: rlf() // RI
case 0x9B: // CSI
i += 2
var params: [Int] = []
var subParams: [[Int]] = []
var colonSub: [Int] = []
var cur = ""
var prefix: UInt8 = 0
if i < data.count && (data[i] == 0x3F || data[i] == 0x3E) {
prefix = data[i]; i += 1
}
while i < data.count {
let c = data[i]
if c >= 0x30 && c <= 0x39 {
cur.append(Character(UnicodeScalar(c)))
} else if c == 0x3B {
if !colonSub.isEmpty {
colonSub.append(Int(cur) ?? 0)
params.append(colonSub[0])
subParams.append(colonSub); colonSub = []
} else {
params.append(Int(cur) ?? 0)
subParams.append([])
}
cur = ""
} else if c == 0x3A {
colonSub.append(Int(cur) ?? 0); cur = ""
} else if c >= 0x40 && c <= 0x7E {
if !colonSub.isEmpty {
colonSub.append(Int(cur) ?? 0)
params.append(colonSub[0])
subParams.append(colonSub)
} else {
params.append(Int(cur) ?? 0)
subParams.append([])
}
cur = ""; colonSub = []
if prefix == 0x3F { doCSIQuestion(params, c) }
else { doCSI(params, c, subParams) }
break
} else { break }
i += 1
}
i += 1; continue
case 0x9D: // OSC
i += 2
var oscBuf = ""
while i < data.count {
if data[i] == 0x07 { break }
if data[i] == 0x1B && i + 1 < data.count && data[i+1] == 0x5C { i += 1; break }
// ST as C1 (0xC2 0x9C)
if data[i] == 0xC2 && i + 1 < data.count && data[i+1] == 0x9C { i += 1; break }
oscBuf.append(Character(UnicodeScalar(data[i])))
i += 1
}
handleOSC(oscBuf)
i += 1; continue
default: break
}
i += 2; continue
}
if b == 0x1B && i + 1 < data.count {
// ESC sequence
i += 1
let next = data[i]
if next == 0x5B { // CSI
i += 1
var params: [Int] = []
var subParams: [[Int]] = []
var colonSub: [Int] = []
var cur = ""
var prefix: UInt8 = 0
// Check for ? or > prefix
if i < data.count && (data[i] == 0x3F || data[i] == 0x3E) {
prefix = data[i]
i += 1
}
while i < data.count {
let c = data[i]
if c >= 0x30 && c <= 0x39 {
cur.append(Character(UnicodeScalar(c)))
} else if c == 0x3B {
if !colonSub.isEmpty {
colonSub.append(Int(cur) ?? 0)
params.append(colonSub[0])
subParams.append(colonSub); colonSub = []
} else {
params.append(Int(cur) ?? 0)
subParams.append([])
}
cur = ""
} else if c == 0x3A {
colonSub.append(Int(cur) ?? 0); cur = ""
} else if c >= 0x40 && c <= 0x7E {
if !colonSub.isEmpty {
colonSub.append(Int(cur) ?? 0)
params.append(colonSub[0])
subParams.append(colonSub)
} else {
params.append(Int(cur) ?? 0)
subParams.append([])
}
cur = ""; colonSub = []
if prefix == 0x3F {
doCSIQuestion(params, c)
} else {
doCSI(params, c, subParams)
}
break
} else {
break
}
i += 1
}
} else if next == 0x5D { // OSC
i += 1
var oscBuf = ""
while i < data.count {
if data[i] == 0x07 { break } // BEL terminates
if data[i] == 0x1B && i + 1 < data.count && data[i+1] == 0x5C { i += 1; break } // ST
oscBuf.append(Character(UnicodeScalar(data[i])))
i += 1
}
handleOSC(oscBuf)
} else if next == 0x37 { // DECSC
savedX = cursorX; savedY = cursorY; savedPendingWrap = pendingWrap
savedAttrs = attrs
} else if next == 0x38 { // DECRC
cursorX = savedX; cursorY = savedY; pendingWrap = savedPendingWrap
attrs = savedAttrs
} else if next == 0x44 { // IND
lf()
} else if next == 0x4D { // RI
rlf()
} else if next == 0x45 { // NEL
cursorX = 0; lf()
} else if next == 0x63 { // RIS
fullReset()
} else if next == 0x3D { // DECKPAM
appKeypadMode = true
} else if next == 0x3E { // DECKPNM
appKeypadMode = false
} else if next == 0x23 { // ESC # — intermediate
i += 1
if i < data.count && data[i] == 0x38 { // DECALN
for r in 0..<rows {
for c in 0..<cols {
grid[r][c] = Cell(char: "E", attrs: TextAttrs(), width: 1)
}
}
cursorX = 0; cursorY = 0
scrollTop = 0; scrollBottom = rows - 1
}
}
} else if b == 0x0A { // LF
lf()
} else if b == 0x0D { // CR
cursorX = 0
} else if b == 0x08 { // BS
if cursorX > 0 { cursorX -= 1 }
} else if b == 0x09 { // TAB
var next = cursorX + 1
while next < cols && !tabStops.contains(next) { next += 1 }
cursorX = min(next, cols - 1)
} else if b >= 0x20 && b < 0x80 {
if cursorX >= cols {
if autoWrapMode { cursorX = 0; lf() }
else { cursorX = cols - 1 }
}
// IRM: insert mode — shift cells right
if insertMode {
for c in stride(from: cols - 1, through: cursorX + 1, by: -1) {
grid[cursorY][c] = grid[cursorY][c - 1]
}
}
grid[cursorY][cursorX].char = Unicode.Scalar(b)
grid[cursorY][cursorX].attrs = attrs
cursorX += 1
}
i += 1
}
}
func lf() {
if cursorY == scrollBottom {
scrollUp(1)
} else if cursorY < rows - 1 {
cursorY += 1
}
}
func rlf() {
if cursorY == scrollTop {
scrollDown(1)
} else if cursorY > 0 {
cursorY -= 1
}
}
func scrollUp(_ n: Int) {
guard scrollTop >= 0 && scrollBottom < rows && scrollTop < scrollBottom else { return }
for _ in 0..<n {
if altGrid == nil { scrollback.append(grid[scrollTop]) }
grid.remove(at: scrollTop)
grid.insert(Array(repeating: Cell(), count: cols), at: scrollBottom)
}
}
func scrollDown(_ n: Int) {
guard scrollTop >= 0 && scrollBottom < rows && scrollTop < scrollBottom else { return }
for _ in 0..<n {
grid.remove(at: scrollBottom)
grid.insert(Array(repeating: Cell(), count: cols), at: scrollTop)
}
}
func fullReset() {
cursorX = 0; cursorY = 0; attrs = TextAttrs()
scrollTop = 0; scrollBottom = rows - 1
appCursorMode = false; appKeypadMode = false; autoWrapMode = true; originMode = false
cursorVisible = true; cursorStyle = 0
mouseMode = 0; mouseEncoding = 0; insertMode = false
altGrid = nil
grid = Self.emptyGrid(cols, rows)
resetTabStops()
}
func doCSI(_ p: [Int], _ f: UInt8, _ sub: [[Int]] = []) {
switch f {
case 0x41: cursorY = max(0, cursorY - max(1, p.first ?? 1)) // CUU
case 0x42: cursorY = min(rows - 1, cursorY + max(1, p.first ?? 1)) // CUD
case 0x43: cursorX = min(cols - 1, cursorX + max(1, p.first ?? 1)) // CUF
case 0x44: cursorX = max(0, cursorX - max(1, p.first ?? 1)) // CUB
case 0x47: cursorX = min(cols - 1, max(0, (p.first ?? 1) - 1)) // CHA
case 0x48, 0x66: // CUP / HVP
let r = (p.count > 0 && p[0] > 0) ? p[0] : 1
let c = (p.count > 1 && p[1] > 0) ? p[1] : 1
if originMode {
cursorY = max(scrollTop, min(scrollBottom, scrollTop + r - 1))
} else {
cursorY = min(rows - 1, max(0, r - 1))
}
cursorX = min(cols - 1, max(0, c - 1))
case 0x4A: // ED
let mode = p.first ?? 0
if mode == 2 {
grid = Self.emptyGrid(cols, rows)
} else if mode == 3 {
scrollback.removeAll()
grid = Self.emptyGrid(cols, rows)
} else if mode == 0 {
for c in cursorX..<cols { grid[cursorY][c] = Cell() }
for r in (cursorY+1)..<rows { grid[r] = Array(repeating: Cell(), count: cols) }
} else if mode == 1 {
for c in 0...cursorX { grid[cursorY][c] = Cell() }
for r in 0..<cursorY { grid[r] = Array(repeating: Cell(), count: cols) }
}
case 0x4B: // EL
let mode = p.first ?? 0
if mode == 0 { for c in cursorX..<cols { grid[cursorY][c] = Cell() } }
else if mode == 1 { for c in 0...min(cursorX, cols-1) { grid[cursorY][c] = Cell() } }
else if mode == 2 { grid[cursorY] = Array(repeating: Cell(), count: cols) }
case 0x4C: // IL
guard cursorY >= scrollTop, cursorY <= scrollBottom, scrollBottom < grid.count else { return }
for _ in 0..<max(1, p.first ?? 1) {
grid.insert(Array(repeating: Cell(), count: cols), at: cursorY)
grid.remove(at: scrollBottom + 1)
}
case 0x4D: // DL
guard cursorY >= scrollTop, cursorY <= scrollBottom, cursorY < grid.count, scrollBottom < grid.count else { return }
for _ in 0..<max(1, p.first ?? 1) {
grid.remove(at: cursorY)
grid.insert(Array(repeating: Cell(), count: cols), at: scrollBottom)
}
case 0x68: // SM (set mode)
for mode in (p.isEmpty ? [0] : p) {
if mode == 4 { insertMode = true }
}
case 0x6C: // RM (reset mode)
for mode in (p.isEmpty ? [0] : p) {
if mode == 4 { insertMode = false }
}
case 0x6D: applySGR(p, sub) // SGR
case 0x72: // DECSTBM
let top = max(0, (p.count > 0 ? p[0] : 1) - 1)
let bot = (p.count > 1 ? p[1] : rows) - 1
scrollTop = min(top, rows - 1)
scrollBottom = max(scrollTop, min(rows - 1, bot))
cursorX = 0; cursorY = originMode ? scrollTop : 0
case 0x73: savedX = cursorX; savedY = cursorY // SCOSC
case 0x75: cursorX = savedX; cursorY = savedY // SCORC
case 0x6E: // DSR
if p.first == 6 { lastResponse = "\u{1B}[\(cursorY + 1);\(cursorX + 1)R" }
else if p.first == 5 { lastResponse = "\u{1B}[0n" }
default: break
}
}
func doCSIQuestion(_ p: [Int], _ f: UInt8) {
for n in p {
if f == 0x68 { // set
switch n {
case 1: appCursorMode = true
case 5: reverseVideoMode = true
case 6: originMode = true; cursorX = 0; cursorY = scrollTop
case 7: autoWrapMode = true
case 25: cursorVisible = true
case 47, 1047:
altGrid = grid; altX = cursorX; altY = cursorY
grid = Self.emptyGrid(cols, rows)
cursorX = 0; cursorY = 0
case 1049:
savedX = cursorX; savedY = cursorY; savedAttrs = attrs
altGrid = grid; grid = Self.emptyGrid(cols, rows)
cursorX = 0; cursorY = 0
case 1000: mouseMode = 1000
case 1002: mouseMode = 1002
case 1003: mouseMode = 1003
case 1006: mouseEncoding = 1006
case 2004: bracketedPasteMode = true
case 2026: synchronizedOutput = true
default: break
}
} else if f == 0x6C { // reset
switch n {
case 1: appCursorMode = false
case 5: reverseVideoMode = false
case 6: originMode = false; cursorX = 0; cursorY = 0
case 7: autoWrapMode = false
case 25: cursorVisible = false
case 47, 1047:
if let ag = altGrid { grid = ag; cursorX = altX; cursorY = altY; altGrid = nil }
pendingWrap = false
case 1049:
if let ag = altGrid { grid = ag; altGrid = nil }
cursorX = savedX; cursorY = savedY; attrs = savedAttrs
case 1000, 1002, 1003: mouseMode = 0
case 1006: mouseEncoding = 0
case 2004: bracketedPasteMode = false
case 2026: synchronizedOutput = false
default: break
}
}
}
}
func applySGR(_ p: [Int], _ sub: [[Int]] = []) {
var i = 0
let params = p.isEmpty ? [0] : p
while i < params.count {
let sp = i < sub.count ? sub[i] : []
switch params[i] {
case 0: attrs = TextAttrs()
case 1: attrs.bold = true
case 2: attrs.dim = true
case 3: attrs.italic = true
case 4:
if !sp.isEmpty {
let style = sp.count > 1 ? sp[1] : (sp.count == 1 ? sp[0] : 1)
attrs.underline = UInt8(clamping: style)
} else {
attrs.underline = 1
}
case 5: attrs.blink = 1
case 6: attrs.blink = 2
case 7: attrs.inverse = true
case 8: attrs.hidden = true
case 9: attrs.strikethrough = true
case 21: attrs.underline = 2
case 22: attrs.bold = false; attrs.dim = false
case 23: attrs.italic = false
case 24: attrs.underline = 0
case 25: attrs.blink = 0
case 27: attrs.inverse = false
case 28: attrs.hidden = false
case 29: attrs.strikethrough = false
case 30...37: attrs.fg = params[i] - 30; attrs.fgRGB = nil
case 38:
if i+1 < params.count && params[i+1] == 5 && i+2 < params.count {
attrs.fg = params[i+2]; attrs.fgRGB = nil; i += 2
} else if i+1 < params.count && params[i+1] == 2 && i+4 < params.count {
attrs.fgRGB = (UInt8(clamping: params[i+2]), UInt8(clamping: params[i+3]), UInt8(clamping: params[i+4]))
i += 4
}
case 39: attrs.fg = 7; attrs.fgRGB = nil
case 40...47: attrs.bg = params[i] - 40; attrs.bgRGB = nil
case 48:
if i+1 < params.count && params[i+1] == 5 && i+2 < params.count {
attrs.bg = params[i+2]; attrs.bgRGB = nil; i += 2
} else if i+1 < params.count && params[i+1] == 2 && i+4 < params.count {
attrs.bgRGB = (UInt8(clamping: params[i+2]), UInt8(clamping: params[i+3]), UInt8(clamping: params[i+4]))
i += 4
}
case 49: attrs.bg = 0; attrs.bgRGB = nil
case 53: attrs.overline = true
case 55: attrs.overline = false
case 58:
if i+1 < params.count && params[i+1] == 5 && i+2 < params.count {
attrs.ulColor = params[i+2]; attrs.ulRGB = nil; i += 2
} else if i+1 < params.count && params[i+1] == 2 && i+4 < params.count {
attrs.ulRGB = (UInt8(clamping: params[i+2]), UInt8(clamping: params[i+3]), UInt8(clamping: params[i+4]))
attrs.ulColor = -1; i += 4
}
case 59: attrs.ulColor = -1; attrs.ulRGB = nil
case 90...97: attrs.fg = params[i] - 90 + 8; attrs.fgRGB = nil
case 100...107: attrs.bg = params[i] - 100 + 8; attrs.bgRGB = nil
default: break
}
i += 1
}
}
func handleOSC(_ buf: String) {
guard let sep = buf.firstIndex(of: ";") else { return }
let code = Int(buf[buf.startIndex..<sep]) ?? -1
let _ = String(buf[buf.index(after: sep)...])
switch code {
case 0, 2: break // title
default: break
}
}
/// Get visible text content as string
func screenText() -> String {
var lines: [String] = []
for row in 0..<rows {
var line = ""
for col in 0..<cols {
line.append(String(grid[row][col].char))
}
while line.last == " " { line.removeLast() }
lines.append(line)
}
while lines.last?.isEmpty == true { lines.removeLast() }
return lines.joined(separator: "\n")
}
}
// ============================================================================
// MARK: - Tests
// ============================================================================
print("Running quickTerminal tests...\n")
section("Basic Text Output")
test("Basic character output") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("Hello")
assertEqual(t.screenText(), "Hello")
assertEqual(t.cursorX, 5)
assertEqual(t.cursorY, 0)
}
test("Line feed and carriage return") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("Line1\r\nLine2")
assertEqual(t.screenText(), "Line1\nLine2")
assertEqual(t.cursorY, 1)
}
test("Auto-wrap at end of line") {
let t = TestTerminal(cols: 5, rows: 3)
t.feed("12345X")
assertEqual(String(t.grid[0][4].char), "5")
assertEqual(String(t.grid[1][0].char), "X")
assertEqual(t.cursorY, 1)
}
test("Backspace") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("AB\u{08}C")
assertEqual(String(t.grid[0][0].char), "A")
assertEqual(String(t.grid[0][1].char), "C")
}
section("Cursor Movement")
test("CUP - cursor position") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[5;10H")
assertEqual(t.cursorY, 4)
assertEqual(t.cursorX, 9)
}
test("CUU/CUD/CUF/CUB - cursor relative movement") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[10;10H")
t.feed("\u{1B}[3A") // up 3
assertEqual(t.cursorY, 6)
t.feed("\u{1B}[5B") // down 5
assertEqual(t.cursorY, 11)
t.feed("\u{1B}[4C") // right 4
assertEqual(t.cursorX, 13)
t.feed("\u{1B}[2D") // left 2
assertEqual(t.cursorX, 11)
}
test("CHA - cursor horizontal absolute") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[20G")
assertEqual(t.cursorX, 19)
}
section("Erase Commands")
test("ED 2 - erase entire display") {
let t = TestTerminal(cols: 10, rows: 3)
t.feed("XXXXXXXXXX\r\nYYYYY")
t.feed("\u{1B}[2J")
assertEqual(t.screenText(), "")
}
test("EL 0 - erase from cursor to end of line") {
let t = TestTerminal(cols: 10, rows: 3)
t.feed("0123456789")
t.feed("\u{1B}[1;5H") // move to col 5
t.feed("\u{1B}[K")
assertEqual(String(t.grid[0].map { String($0.char) }.joined()), "0123 ")
}
test("EL 1 - erase from start to cursor") {
let t = TestTerminal(cols: 10, rows: 3)
t.feed("0123456789")
t.feed("\u{1B}[1;5H")
t.feed("\u{1B}[1K")
assertEqual(String(t.grid[0].map { String($0.char) }.joined()), " 56789")
}
test("EL 2 - erase entire line") {
let t = TestTerminal(cols: 10, rows: 3)
t.feed("0123456789")
t.feed("\u{1B}[2K")
assertEqual(String(t.grid[0].map { String($0.char) }.joined()), " ")
}
section("SGR Attributes")
test("SGR bold, italic, underline") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[1;3;4mX")
check(t.grid[0][0].attrs.bold, "bold should be set")
check(t.grid[0][0].attrs.italic, "italic should be set")
check(t.grid[0][0].attrs.underline > 0, "underline should be set")
}
test("SGR reset") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[1;3mX\u{1B}[0mY")
check(t.grid[0][0].attrs.bold, "first char bold")
check(!t.grid[0][1].attrs.bold, "second char not bold after reset")
check(!t.grid[0][1].attrs.italic, "second char not italic after reset")
}
test("SGR 256-color foreground") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[38;5;196mX")
assertEqual(t.grid[0][0].attrs.fg, 196)
}
test("SGR truecolor foreground") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[38;2;255;128;0mX")
assertEqual(t.grid[0][0].attrs.fgRGB?.0, 255)
assertEqual(t.grid[0][0].attrs.fgRGB?.1, 128)
assertEqual(t.grid[0][0].attrs.fgRGB?.2, 0)
}
test("SGR 256-color background") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[48;5;42mX")
assertEqual(t.grid[0][0].attrs.bg, 42)
}
test("SGR bright colors") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[91mX")
assertEqual(t.grid[0][0].attrs.fg, 9) // bright red = 8 + 1
t.feed("\u{1B}[102mY")
assertEqual(t.grid[0][1].attrs.bg, 10) // bright green = 8 + 2
}
test("SGR 58 underline color (256-color)") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[4;58;5;196mX")
check(t.grid[0][0].attrs.underline > 0, "underline should be set")
assertEqual(t.grid[0][0].attrs.ulColor, 196)
}
test("SGR 58 underline color (truecolor)") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[4;58;2;255;0;128mX")
check(t.grid[0][0].attrs.underline > 0, "underline should be set")
assertEqual(t.grid[0][0].attrs.ulRGB?.0, 255)
assertEqual(t.grid[0][0].attrs.ulRGB?.1, 0)
assertEqual(t.grid[0][0].attrs.ulRGB?.2, 128)
}
test("SGR 59 resets underline color") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[58;5;196m\u{1B}[59mX")
assertEqual(t.grid[0][0].attrs.ulColor, -1)
check(t.grid[0][0].attrs.ulRGB == nil, "ulRGB should be nil after reset")
}
test("SGR inverse") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[7mX\u{1B}[27mY")
check(t.grid[0][0].attrs.inverse, "inverse on")
check(!t.grid[0][1].attrs.inverse, "inverse off")
}
test("SGR strikethrough") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[9mX\u{1B}[29mY")
check(t.grid[0][0].attrs.strikethrough, "strikethrough on")
check(!t.grid[0][1].attrs.strikethrough, "strikethrough off")
}
section("Scroll Region & Lines")
test("DECSTBM - scroll region") {
let t = TestTerminal(cols: 10, rows: 5)
t.feed("\u{1B}[2;4r")
assertEqual(t.scrollTop, 1)
assertEqual(t.scrollBottom, 3)
assertEqual(t.cursorX, 0)
assertEqual(t.cursorY, 0)
}
section("Insert/Delete Lines")
test("IL - insert lines") {
let t = TestTerminal(cols: 5, rows: 5)
t.feed("AAAAA\r\nBBBBB\r\nCCCCC\r\nDDDDD\r\nEEEEE")
t.feed("\u{1B}[2;1H") // move to row 2
t.feed("\u{1B}[1L") // insert 1 line
assertEqual(String(t.grid[1].map { String($0.char) }.joined()), " ")
assertEqual(String(t.grid[2].map { String($0.char) }.joined()), "BBBBB")
}
test("DL - delete lines") {
let t = TestTerminal(cols: 5, rows: 5)
t.feed("AAAAA\r\nBBBBB\r\nCCCCC\r\nDDDDD\r\nEEEEE")
t.feed("\u{1B}[2;1H") // move to row 2
t.feed("\u{1B}[1M") // delete 1 line
assertEqual(String(t.grid[1].map { String($0.char) }.joined()), "CCCCC")
assertEqual(String(t.grid[2].map { String($0.char) }.joined()), "DDDDD")
}
section("DEC Private Modes")
test("DECCKM - application cursor mode") {
let t = TestTerminal(cols: 80, rows: 24)
check(!t.appCursorMode, "default off")
t.feed("\u{1B}[?1h")
check(t.appCursorMode, "set on")
t.feed("\u{1B}[?1l")
check(!t.appCursorMode, "set off")
}
test("DECTCEM - cursor visibility") {
let t = TestTerminal(cols: 80, rows: 24)
check(t.cursorVisible, "default visible")
t.feed("\u{1B}[?25l")
check(!t.cursorVisible, "hidden")
t.feed("\u{1B}[?25h")
check(t.cursorVisible, "visible again")
}
test("Alternate screen buffer (1049)") {
let t = TestTerminal(cols: 10, rows: 5)
t.feed("MainText")
assertEqual(t.cursorX, 8)
t.feed("\u{1B}[?1049h") // enter alt screen
check(t.altGrid != nil, "alt grid should exist")
assertEqual(t.cursorX, 0)
assertEqual(t.cursorY, 0)
t.feed("AltText")
t.feed("\u{1B}[?1049l") // exit alt screen
check(t.altGrid == nil, "alt grid should be cleared")
assertEqual(t.screenText(), "MainText")
}
test("Mouse tracking modes") {
let t = TestTerminal(cols: 80, rows: 24)
assertEqual(t.mouseMode, 0)
t.feed("\u{1B}[?1000h")
assertEqual(t.mouseMode, 1000)
t.feed("\u{1B}[?1002h")
assertEqual(t.mouseMode, 1002)
t.feed("\u{1B}[?1003h")
assertEqual(t.mouseMode, 1003)
t.feed("\u{1B}[?1003l")
assertEqual(t.mouseMode, 0)
}
test("SGR mouse encoding mode") {
let t = TestTerminal(cols: 80, rows: 24)
assertEqual(t.mouseEncoding, 0)
t.feed("\u{1B}[?1006h")
assertEqual(t.mouseEncoding, 1006)
t.feed("\u{1B}[?1006l")
assertEqual(t.mouseEncoding, 0)
}
test("Bracketed paste mode") {
let t = TestTerminal(cols: 80, rows: 24)
check(!t.bracketedPasteMode, "default off")
t.feed("\u{1B}[?2004h")
check(t.bracketedPasteMode, "enabled")
t.feed("\u{1B}[?2004l")
check(!t.bracketedPasteMode, "disabled")
}
test("Synchronized output") {
let t = TestTerminal(cols: 80, rows: 24)
check(!t.synchronizedOutput, "default off")
t.feed("\u{1B}[?2026h")
check(t.synchronizedOutput, "enabled")
t.feed("\u{1B}[?2026l")
check(!t.synchronizedOutput, "disabled")
}
section("DECSC / DECRC")
test("DECSC/DECRC saves and restores full state") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[1;3m") // bold + italic
t.feed("\u{1B}[5;10H") // position 5,10
t.feed("\u{1B}7") // save
t.feed("\u{1B}[0m") // reset attrs
t.feed("\u{1B}[1;1H") // move to 1,1
assertEqual(t.cursorY, 0)
check(!t.attrs.bold, "attrs reset")
t.feed("\u{1B}8") // restore
assertEqual(t.cursorY, 4)
assertEqual(t.cursorX, 9)
check(t.savedAttrs.bold, "saved attrs should have bold")
}
section("Keypad & Reset")
test("Application keypad mode (ESC = / ESC >)") {
let t = TestTerminal(cols: 80, rows: 24)
check(!t.appKeypadMode, "default off")
t.feed("\u{1B}=")
check(t.appKeypadMode, "enabled")
t.feed("\u{1B}>")
check(!t.appKeypadMode, "disabled")
}
test("RIS - full reset") {
let t = TestTerminal(cols: 10, rows: 5)
t.feed("XXXXXXXXXX")
t.feed("\u{1B}[1;3;7m")
t.feed("\u{1B}[?1h")
t.feed("\u{1B}[?1000h")
t.feed("\u{1B}c") // full reset
assertEqual(t.cursorX, 0)
assertEqual(t.cursorY, 0)
check(!t.attrs.bold)
check(!t.appCursorMode)
assertEqual(t.mouseMode, 0)
assertEqual(t.screenText(), "")
}
section("DSR & SCOSC/SCORC")
test("DSR 6 - cursor position report") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[10;20H")
t.feed("\u{1B}[6n")
assertEqual(t.lastResponse, "\u{1B}[10;20R")
}
test("DSR 5 - device status OK") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[5n")
assertEqual(t.lastResponse, "\u{1B}[0n")
}
test("SCOSC/SCORC - save/restore cursor (CSI s/u)") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\u{1B}[5;10H")
t.feed("\u{1B}[s")
t.feed("\u{1B}[1;1H")
assertEqual(t.cursorY, 0)
t.feed("\u{1B}[u")
assertEqual(t.cursorY, 4)
assertEqual(t.cursorX, 9)
}
section("Scrollback & Tab Stops")
test("Scrollback accumulates lines") {
let t = TestTerminal(cols: 5, rows: 3)
t.feed("Line1\r\nLine2\r\nLine3\r\nLine4")
check(t.scrollback.count >= 1, "scrollback should have entries")
assertEqual(String(t.scrollback[0].prefix(5).map { String($0.char) }.joined()), "Line1")
}
test("Default tab stops every 8 columns") {
let t = TestTerminal(cols: 80, rows: 24)
t.feed("\t")
assertEqual(t.cursorX, 8)
t.feed("\t")
assertEqual(t.cursorX, 16)
}
section("Reverse Index & Wrap")
test("Reverse index at top scrolls down") {
let t = TestTerminal(cols: 5, rows: 3)
t.feed("AAAAA\r\nBBBBB\r\nCCCCC")
t.feed("\u{1B}[1;1H") // move to top
t.feed("\u{1B}M") // reverse index
assertEqual(String(t.grid[0].map { String($0.char) }.joined()), " ")
assertEqual(String(t.grid[1].map { String($0.char) }.joined()), "AAAAA")
}
test("Auto-wrap mode off prevents wrapping") {
let t = TestTerminal(cols: 5, rows: 3)
t.feed("\u{1B}[?7l") // disable auto-wrap
t.feed("123456789")
assertEqual(t.cursorY, 0)
// Last char should be at col 4 (overwritten)
assertEqual(String(t.grid[0][4].char), "9")
}
section("Origin Mode")
test("CUP respects origin mode with scroll region") {
let t = TestTerminal(cols: 10, rows: 10)
t.feed("\u{1B}[3;7r") // set scroll region rows 3-7