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
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const build_options = @import("terminal_options");
const testing = std.testing;
const apc = @import("apc.zig");
const clipboard = @import("clipboard.zig");
const csi = @import("csi.zig");
const dcs = @import("dcs.zig");
const device_attributes = @import("device_attributes.zig");
const device_status = @import("device_status.zig");
const stream = @import("stream.zig");
const Action = stream.Action;
const Screen = @import("Screen.zig");
const color = @import("color.zig");
const modes = @import("modes.zig");
const osc = @import("osc.zig");
const osc_color = @import("osc/parsers/color.zig");
const kitty_clipboard = @import("kitty/clipboard.zig");
const kitty_color = @import("kitty/color.zig");
const paste_pkg = @import("paste.zig");
const kitty_dnd = @import("kitty/dnd.zig");
const size_report = @import("size_report.zig");
const simd = @import("../simd/main.zig");
const terminfo = @import("../terminfo/main.zig");
const Terminal = @import("Terminal.zig");
const log = std.log.scoped(.stream_terminal);
/// This is a Stream implementation that processes actions against
/// a Terminal and updates the Terminal state.
pub const Stream = stream.Stream(Handler);
/// A stream handler that updates terminal state. By default, it is
/// readonly in the sense that it only updates terminal state and ignores
/// all other sequences that require a response or otherwise have side
/// effects (e.g. clipboards).
///
/// You can manually set various effects callbacks in the `effects` field
/// to implement certain effects such as bells, titles, clipboard, etc.
pub const Handler = struct {
/// The terminal state to modify.
terminal: *Terminal,
/// True after an error prevented a terminal-owned semantic update.
///
/// When an error happens during terminal processing, streams continue
/// forward and remain best-effort. A terminal can't really stop in
/// the middle it must go on. But this is flagged to true to let
/// consumers know some sort of unhandle-able error state happened
/// (e.g. an allocation failure).
///
/// Only non-handled outcomes set this. Gracefully handled outcomes
/// that don't meaningfully negatively impact the terminal state
/// such as hitting Kitty image limits, failure to write a response,
/// do not flag this.
semantic_failure: bool = false,
/// Callbacks for certain effects that handlers may have. These
/// may or may not fully replace internal handling of certain effects,
/// but they allow for the handler to trigger or query external
/// effects.
effects: Effects = .readonly,
/// Whether CSI 21 t may report the terminal title. This is disabled by
/// default because reporting an attacker-controlled title to the pty can
/// inject text into the input stream of the foreground process.
title_report: bool = false,
/// The APC command handler maintains the APC state. APC is like
/// CSI or OSC, but it is a private escape sequence that is used
/// to send commands to the terminal emulator. This is used by
/// the kitty graphics protocol.
apc_handler: apc.Handler = .{},
/// The DCS command handler maintains state for DCS queries.
dcs_handler: dcs.Handler = .{},
/// The in-flight Kitty clipboard protocol (OSC 5522) write
/// transaction, if any. Null means no transaction is active.
/// Heap-allocated since transactions are rare and short-lived.
kitty_clipboard_write: ?*kitty_clipboard.WriteState = null,
/// Kitty clipboard protocol (OSC 5522) session password grants,
/// recorded when a clipboard_read or clipboard_write reply asks to
/// remember the user's decision.
kitty_clipboard_grants: kitty_clipboard.Grants = .{},
/// Maximum total decoded bytes accumulated by one Kitty clipboard
/// protocol (OSC 5522) write transaction, captured when the
/// transaction begins. Data beyond the limit fails the transaction
/// with EFBIG.
kitty_clipboard_write_max_bytes: usize = kitty_clipboard.max_write_size,
/// Called for sequence identifiers not supported by this library.
/// Currently, only APC is reported. Content is borrowed and only valid
/// for the duration of the callback. Set `apc_handler.unknown_max_bytes`
/// before starting the Stream to enable APC capture.
unknown_sequence: ?*const fn (*Handler, UnknownSequence) void = null,
/// The name of the terminfo entry this terminal runs as, reported in
/// response to an XTGETTCAP query for "TN".
///
/// The memory must remain valid for the lifetime of the handler.
/// Empty names and names longer than `max_terminfo_name_bytes` are
/// silently ignored.
terminfo_name: ?[]const u8 = null,
/// Maximum byte length accepted for `terminfo_name`.
pub const max_terminfo_name_bytes = 128;
pub const Effects = struct {
/// Called when the terminal needs to write data back to the pty,
/// e.g. in response to a DECRQM query. The data is only valid
/// during the lifetime of the call so callers must copy it
/// if it needs to be stored or used after the call returns.
write_pty: ?*const fn (*Handler, []const u8) void,
/// Called when the bell is rung (BEL).
bell: ?*const fn (*Handler) void,
/// Called when the running program requests a desktop notification
/// via OSC 9 or OSC 777. The title and body are borrowed and only
/// valid for the duration of the callback.
desktop_notification: ?*const fn (*Handler, Action.ShowDesktopNotification) void,
/// Called when drag and drop protocol state changes in a way the
/// embedder may need to act on: the running program registering
/// or unregistering to accept drops, answering a drag, or
/// concluding a drop. The event says what changed; the details
/// are read from `handler.terminal.kitty_dnd` (Kitty's OSC 72 is
/// the only drag and drop protocol today). Native drag events
/// flow the other way, by calling `kitty.dnd.State` directly.
drag_and_drop: ?*const fn (*Handler, kitty_dnd.Event) void,
/// Called in response to a color scheme DSR query (CSI ? 996 n).
/// Returns the current color scheme. Return null to silently
/// ignore the query.
color_scheme: ?*const fn (*Handler) ?device_status.ColorScheme,
/// Called in response to a device attributes query (CSI c,
/// CSI > c, CSI = c). Returns the response to encode and
/// write back to the pty.
device_attributes: ?*const fn (*Handler) device_attributes.Attributes,
/// Called in response to ENQ (0x05). Returns the raw response
/// bytes to write back to the pty. The returned memory must be
/// valid for the lifetime of the call.
enquiry: ?*const fn (*Handler) []const u8,
/// Called for XTWINOPS size queries (CSI 14/16/18 t) and when VT input
/// enables in-band size reports (mode 2048). Returns the current
/// terminal geometry used for encoding. Return null to suppress the
/// XTWINOPS response or mode 2048 report.
size: ?*const fn (*Handler) ?size_report.Size,
/// Called when the terminal title changes via escape sequences
/// (e.g. OSC 0/2). The new title can be queried via
/// handler.terminal.getTitle().
title_changed: ?*const fn (*Handler) void,
/// Called when the terminal pwd changes via escape sequences
/// (e.g. OSC 7). The new pwd can be queried via
/// handler.terminal.getPwd().
pwd_changed: ?*const fn (*Handler) void,
/// Called when the running program reports progress via OSC 9;4.
progress_report: ?*const fn (*Handler, osc.Command.ProgressReport) void,
/// Called when the running program writes to a clipboard.
clipboard_write: ?*const fn (*Handler, clipboard.Write) void,
/// Called when the running program requests clipboard contents
/// (OSC 52 with a "?" payload, or a Kitty clipboard (OSC 5522)
/// read). Answering one lets the program read the user's
/// clipboard, so the embedder is expected to mediate consent.
///
/// Reads are synchronous: the callback must answer through
/// `read.reply` before it returns, so an embedder that needs to
/// ask the user must block (e.g. run a modal prompt) while the
/// stream waits. Returning without a reply, or replying with any
/// failure, answers the program with an empty clipboard (OSC 52)
/// or the matching protocol status (OSC 5522) so it doesn't hang.
/// If this is null, OSC 52 reads are ignored and OSC 5522 reads
/// are refused with EPERM.
///
/// OSC 5522 requests carry the program's MIME list, name, and
/// password grant state; a reply that sets `remember` records a
/// session grant so later requests with the same password arrive
/// with `granted` set. Kitty itself serves a request for only the
/// targets listing (`list` with no `mimes`) without prompting.
///
/// Installing this also enables Kitty paste events (mode 5522):
/// `paste` sends the program an event instead of the text, and
/// the program's follow-up read arrives here with `granted` set
/// since the user already pasted. See `paste`.
clipboard_read: ?*const fn (*Handler, clipboard.Read) void,
/// Called in response to an XTVERSION query. Returns the version
/// string to report (e.g. "ghostty 1.2.3"). The returned memory
/// must be valid for the lifetime of the call. The maximum length
/// is 256 bytes; longer strings will be silently ignored.
xtversion: ?*const fn (*Handler) []const u8,
/// No effects means that the stream effectively becomes readonly
/// that only affects pure terminal state and ignores all side
/// effects beyond that.
pub const readonly: Effects = .{
.bell = null,
.clipboard_read = null,
.clipboard_write = null,
.color_scheme = null,
.desktop_notification = null,
.device_attributes = null,
.drag_and_drop = null,
.enquiry = null,
.progress_report = null,
.size = null,
.title_changed = null,
.pwd_changed = null,
.write_pty = null,
.xtversion = null,
};
};
/// A sequence unsupported by the active handler. Payload data is borrowed
/// only for the duration of the handler callback.
pub const UnknownSequence = union(enum) {
apc: String,
/// Content between a string sequence's introducer and terminator.
pub const String = struct {
content: []const u8,
truncated: bool,
};
};
pub fn init(terminal: *Terminal) Handler {
return .{
.terminal = terminal,
};
}
pub fn deinit(self: *Handler) void {
self.kittyClipboardAbort();
self.kitty_clipboard_grants.deinit(self.terminal.gpa());
self.apc_handler.deinit();
self.dcs_handler.deinit();
}
/// Resize the terminal and apply any side effects (if supported)
/// as a result of that.
///
/// This is different than a direct `Terminal.resize` operation
/// because it also handles the side effects like mode 2048 in-band
/// size reports if write_pty is set.
pub fn resize(self: *Handler, value: Terminal.Resize) !void {
try self.terminal.resize(self.terminal.gpa(), value);
// Mode 2048 reports require complete, current cell pixel geometry.
const cell_size = value.cell_size_px orelse return;
// If we have no in-band size reports enabled then do nothing.
if (!self.terminal.modes.get(.in_band_size_reports)) return;
// If we have no write_pty effect, do nothing.
const write_pty = self.effects.write_pty orelse return;
// The maximum mode-2048 response is covered by size_report's maximum
// value test. Reserve the final byte for the callback's sentinel.
var buf: [128]u8 = undefined;
var writer: std.Io.Writer = .fixed(buf[0 .. buf.len - 1]);
size_report.encode(&writer, .mode_2048, .{
.rows = value.rows,
.columns = value.cols,
.cell_width = cell_size.width,
.cell_height = cell_size.height,
}) catch unreachable;
buf[writer.end] = 0;
write_pty(self, buf[0..writer.end :0]);
}
/// A paste request; see `paste`.
pub const Paste = paste_pkg.Request;
pub const PasteError = Allocator.Error || std.Io.RandomSecureError || error{
/// The data could inject commands and allow_unsafe was false.
/// Nothing was written.
UnsafePaste,
/// The contents reader failed. Nothing was written.
ReadFailed,
/// No write_pty effect is set, so nothing can be written.
NoWritePty,
};
/// The size of the chunks a paste streams to write_pty in.
pub const paste_chunk_size = 4096;
/// Paste into the terminal, applying the terminal's current state
/// as necessary to owner mode 5522, bracketed paste, unsafe paste, etc.
/// Returns true if anything was written to the pty.
///
/// The output streams to write_pty in chunks of `paste_chunk_size`.
/// The contents are read at most once and only the pasted text
/// representation is ever read, buffered whole while it is checked
/// and encoded; see `terminal.paste`.
pub fn paste(self: *Handler, req: Paste) PasteError!bool {
if (self.effects.write_pty == null) return error.NoWritePty;
var buf: [paste_chunk_size]u8 = undefined;
var pty: PtyWriter = .init(self, &buf);
// Delivered on error too: a partial paste has its frame closed
// and the program must see that.
defer pty.writer.flush() catch unreachable;
return paste_pkg.paste(.{
.terminal = self.terminal,
.alloc = self.terminal.gpa(),
// Paste events need the program's follow-up Kitty
// clipboard read served.
.kitty_clipboard = if (self.effects.clipboard_read != null) .{
.grants = &self.kitty_clipboard_grants,
.io = self.terminal.io(),
} else null,
.writer = &pty.writer,
}, req) catch |err| switch (err) {
// The pty writer never fails.
error.WriteFailed => unreachable,
error.ReadFailed,
error.OutOfMemory,
error.UnsafePaste,
error.EntropyUnavailable,
error.Canceled,
=> |e| e,
};
}
pub fn vt(
self: *Handler,
comptime action: Action.Tag,
value: Action.Value(action),
) void {
self.vtFallible(action, value) catch |err| {
self.semantic_failure = true;
log.warn("error handling VT action action={} err={}", .{ action, err });
};
}
fn unknownSequence(self: *Handler, value: UnknownSequence) void {
const func = self.unknown_sequence orelse return;
func(self, value);
}
inline fn vtFallible(
self: *Handler,
comptime action: Action.Tag,
value: Action.Value(action),
) !void {
switch (action) {
.print => try self.terminal.print(value.cp),
.print_slice => try self.terminal.printSlice(value.cps),
.print_repeat => try self.terminal.printRepeat(value),
.backspace => self.terminal.backspace(),
.carriage_return => self.terminal.carriageReturn(),
.linefeed => try self.terminal.linefeed(),
.index => try self.terminal.index(),
.next_line => {
try self.terminal.index();
self.terminal.carriageReturn();
},
.reverse_index => self.terminal.reverseIndex(),
.cursor_up => self.terminal.cursorUp(value.value),
.cursor_down => self.terminal.cursorDown(value.value),
.cursor_left => self.terminal.cursorLeft(value.value),
.cursor_right => self.terminal.cursorRight(value.value),
.cursor_pos => self.terminal.setCursorPos(value.row, value.col),
.cursor_col => self.terminal.setCursorPos(self.terminal.screens.active.cursor.y + 1, value.value),
.cursor_row => self.terminal.setCursorPos(value.value, self.terminal.screens.active.cursor.x + 1),
.cursor_col_relative => self.terminal.setCursorPos(
self.terminal.screens.active.cursor.y + 1,
self.terminal.screens.active.cursor.x + 1 +| value.value,
),
.cursor_row_relative => self.terminal.setCursorPos(
self.terminal.screens.active.cursor.y + 1 +| value.value,
self.terminal.screens.active.cursor.x + 1,
),
.cursor_style => self.terminal.setCursorStyle(value),
.erase_display_below => self.terminal.eraseDisplay(.below, value),
.erase_display_above => self.terminal.eraseDisplay(.above, value),
.erase_display_complete => self.terminal.eraseDisplay(.complete, value),
.erase_display_scrollback => self.terminal.eraseDisplay(.scrollback, value),
.erase_display_scroll_complete => self.terminal.eraseDisplay(.scroll_complete, value),
.erase_line_right => self.terminal.eraseLine(.right, value),
.erase_line_left => self.terminal.eraseLine(.left, value),
.erase_line_complete => self.terminal.eraseLine(.complete, value),
.erase_line_right_unless_pending_wrap => self.terminal.eraseLine(.right_unless_pending_wrap, value),
.delete_chars => self.terminal.deleteChars(value),
.erase_chars => self.terminal.eraseChars(value),
.insert_lines => self.terminal.insertLines(value),
.insert_blanks => self.terminal.insertBlanks(value),
.delete_lines => self.terminal.deleteLines(value),
.scroll_up => try self.terminal.scrollUp(value),
.scroll_down => self.terminal.scrollDown(value),
.horizontal_tab => self.horizontalTab(value),
.horizontal_tab_back => self.horizontalTabBack(value),
.tab_clear_current => self.terminal.tabClear(.current),
.tab_clear_all => self.terminal.tabClear(.all),
.tab_set => self.terminal.tabSet(),
.tab_reset => self.terminal.tabReset(),
.set_mode => try self.setMode(value.mode, true),
.reset_mode => try self.setMode(value.mode, false),
.save_mode => self.terminal.modes.save(value.mode),
.restore_mode => {
const v = self.terminal.modes.restore(value.mode);
try self.setMode(value.mode, v);
},
.top_and_bottom_margin => self.terminal.setTopAndBottomMargin(value.top_left, value.bottom_right),
.left_and_right_margin => self.terminal.setLeftAndRightMargin(value.top_left, value.bottom_right),
.left_and_right_margin_ambiguous => {
if (self.terminal.modes.get(.enable_left_and_right_margin)) {
self.terminal.setLeftAndRightMargin(0, 0);
} else {
self.terminal.saveCursor();
}
},
.save_cursor => self.terminal.saveCursor(),
.restore_cursor => self.terminal.restoreCursor(),
.invoke_charset => self.terminal.invokeCharset(value.bank, value.charset, value.locking),
.configure_charset => self.terminal.configureCharset(value.slot, value.charset),
.set_attribute => switch (value) {
.unknown => {},
else => try self.terminal.setAttribute(value),
},
.protected_mode_off => self.terminal.setProtectedMode(.off),
.protected_mode_iso => self.terminal.setProtectedMode(.iso),
.protected_mode_dec => self.terminal.setProtectedMode(.dec),
.mouse_shift_capture => self.terminal.flags.mouse_shift_capture = if (value) .true else .false,
.kitty_keyboard_push => self.terminal.screens.active.kitty_keyboard.push(value.flags),
.kitty_keyboard_pop => self.terminal.screens.active.kitty_keyboard.pop(@intCast(value)),
.kitty_keyboard_set => self.terminal.screens.active.kitty_keyboard.set(.set, value.flags),
.kitty_keyboard_set_or => self.terminal.screens.active.kitty_keyboard.set(.@"or", value.flags),
.kitty_keyboard_set_not => self.terminal.screens.active.kitty_keyboard.set(.not, value.flags),
.modify_key_format => {
self.terminal.flags.modify_other_keys_2 = false;
switch (value) {
.other_keys_numeric => self.terminal.flags.modify_other_keys_2 = true,
else => {},
}
},
.active_status_display => self.terminal.status_display = value,
.decaln => try self.terminal.decaln(),
.full_reset => {
self.terminal.fullReset();
// Full reset clears grants
self.kitty_clipboard_grants.deinit(self.terminal.gpa());
self.kitty_clipboard_grants = .{};
// Clear the progress bar
self.progressReport(.{ .state = .remove });
},
.start_hyperlink => try self.terminal.screens.active.startHyperlink(value.uri, value.id),
.end_hyperlink => self.terminal.screens.active.endHyperlink(),
.semantic_prompt => try self.terminal.semanticPrompt(value),
.mouse_shape => self.terminal.mouse_shape = value,
.color_operation => self.colorOperation(
&value.requests,
value.terminator,
) catch |err| log.warn("error reporting OSC color err={}", .{err}),
.kitty_color_report => self.kittyColorOperation(value) catch |err| {
log.warn("error reporting Kitty colors err={}", .{err});
},
.kitty_clipboard => self.kittyClipboard(value) catch |err| {
// Clipboard operations are external effects, not terminal
// state; a failed transaction was already answered.
log.warn("error handling kitty clipboard err={}", .{err});
},
// APC
.apc_start => self.apc_handler.start(),
.apc_put => self.apc_handler.feed(self.terminal.gpa(), value),
.apc_put_slice => self.apc_handler.feedSlice(self.terminal.gpa(), value.bytes),
.apc_end => self.apcEnd(value.terminated),
// Effect-based handlers
.bell => self.bell(),
.show_desktop_notification => self.desktopNotification(value),
.device_attributes => self.reportDeviceAttributes(value),
.device_status => self.deviceStatus(value.request),
.enquiry => self.reportEnquiry(),
.kitty_keyboard_query => self.queryKittyKeyboard(),
.request_mode => self.requestMode(value.mode),
.request_mode_unknown => self.requestModeUnknown(value.mode, value.ansi),
.size_report => self.reportSize(value),
.window_title => try self.windowTitle(value.title),
.report_pwd => try self.reportPwd(value.url),
.progress_report => self.progressReport(value),
.xtversion => self.reportXtversion(),
.clipboard_contents => self.clipboardContents(
value.kind,
value.data,
value.terminator,
) catch |err| {
// Clipboard operations are external effects, not terminal state.
log.warn("error handling clipboard operation err={}", .{err});
},
.kitty_dnd => self.kittyDnd(value) catch |err| {
// Drag and drop is a self-contained subsystem: an OOM
// updating its state or a failure writing a response
// degrades it without corrupting terminal state, so we log.
log.warn("error handling kitty dnd err={}", .{err});
},
.dcs_hook => try self.dcsHook(value),
.dcs_put => try self.dcsPut(value),
.dcs_unhook => try self.dcsUnhook(),
// Have no terminal-modifying effect
.title_push,
.title_pop,
=> {},
}
}
inline fn writePty(self: *Handler, data: []const u8) void {
const func = self.effects.write_pty orelse return;
func(self, data);
}
fn dcsHook(self: *Handler, value: Action.Value(.dcs_hook)) !void {
var cmd = self.dcs_handler.hook(
self.terminal.gpa(),
value,
) orelse return;
defer cmd.deinit();
try self.dcsCommand(&cmd);
}
fn dcsPut(self: *Handler, value: u8) !void {
var cmd = self.dcs_handler.put(value) orelse return;
defer cmd.deinit();
try self.dcsCommand(&cmd);
}
fn dcsUnhook(self: *Handler) !void {
var cmd = self.dcs_handler.unhook() orelse return;
defer cmd.deinit();
try self.dcsCommand(&cmd);
}
fn dcsCommand(self: *Handler, cmd: *dcs.Command) !void {
switch (cmd.*) {
.decrqss => |request| {
var response: [
dcs.Command.DECRQSS.max_response_bytes + 1
]u8 = undefined;
const encoded = try request.encode(
self.terminal,
response[0 .. response.len - 1],
);
response[encoded.len] = 0;
self.writePty(response[0..encoded.len :0]);
},
.xtgettcap => |*gettcap| {
if (self.effects.write_pty == null) return;
const map = comptime terminfo.ghostty.xtgettcapMap();
while (gettcap.next()) |key| {
if (std.mem.eql(u8, key, encoded_tn_key)) {
self.writeTerminfoName();
continue;
}
self.writePty(map.get(key) orelse continue);
}
},
.tmux => {},
}
}
// Hex-encoded "TN", the XTGETTCAP key naming the terminfo entry.
// The static map also carries this key with Ghostty's own name, so
// it is intercepted before the lookup: an embedder that never
// configured a name must not be reported as Ghostty's entry.
const encoded_tn_key = &std.fmt.bytesToHex("TN", .upper);
/// Answer an XTGETTCAP "TN" query from the configured terminfo name.
/// Unset, empty, or over-long names leave the query unanswered.
fn writeTerminfoName(self: *Handler) void {
const name = self.terminfo_name orelse return;
if (name.len == 0 or name.len > max_terminfo_name_bytes) return;
// Fixed upper bound for an encoded "TN" reply calculated
// at comptime from our max terminfo size.
const max_tn_response_bytes =
comptime "\x1bP1+r".len + encoded_tn_key.len + "=".len +
(max_terminfo_name_bytes * 2) + "\x1b\\".len +
1; // null terminator
// Values are hex-encoded uppercase, matching the static map. The
// buffer fits any name allowed above, so the print cannot fail.
var buf: [max_tn_response_bytes]u8 = undefined;
self.writePty(std.fmt.bufPrintZ(
&buf,
"\x1bP1+r" ++ encoded_tn_key ++ "={X}\x1b\\",
.{name},
) catch unreachable);
}
fn bell(self: *Handler) void {
const func = self.effects.bell orelse return;
func(self);
}
fn desktopNotification(
self: *Handler,
notification: Action.ShowDesktopNotification,
) void {
const func = self.effects.desktop_notification orelse return;
func(self, notification);
}
fn progressReport(self: *Handler, report: osc.Command.ProgressReport) void {
const func = self.effects.progress_report orelse return;
func(self, report);
}
fn clipboardContents(
self: *Handler,
kind: u8,
data: []const u8,
terminator: osc.Terminator,
) !void {
const location: clipboard.Location = switch (kind) {
's' => .selection,
'p' => .primary,
else => .standard,
};
// OSC 52 uses a "?" payload to request the clipboard contents.
if (data.len == 1 and data[0] == '?') {
self.clipboardRead(location, terminator);
return;
}
const func = self.effects.clipboard_write orelse return;
// OSC 52 uses an empty payload to clear the selected clipboard.
if (data.len == 0) {
func(self, .{
.location = location,
.contents = &.{},
.name = "",
.granted = false,
.can_remember = false,
.reply_ctx = self,
.reply_fn = &ignoreWriteReply,
});
return;
}
// Decode the base64 payload with the SIMD decoder (the same one
// used for Kitty clipboard payloads) rather than the scalar std
// implementation; clipboard payloads can be megabytes. The
// Kitty clipboard spec governs OSC 52 base64 handling too: a
// request with characters outside the base64 alphabet is
// discarded entirely (never partially decoded), while a
// missing-padding tail is tolerated since OSC 52 has no way
// to report errors to the client.
const alloc = self.terminal.gpa();
const buf = try alloc.alloc(u8, simd.base64.maxLen(data));
defer alloc.free(buf);
const decoded = simd.base64.decodeStrict(data, buf, .optional) catch {
log.warn("OSC 52 clipboard write is not valid base64, ignoring", .{});
return;
};
const contents = [_]clipboard.Content{.{
.mime = "text/plain",
.data = decoded,
}};
func(self, .{
.location = location,
.contents = &contents,
.name = "",
.granted = false,
.can_remember = false,
.reply_ctx = self,
.reply_fn = &ignoreWriteReply,
});
}
/// Reply target for clipboard writes on protocols without a write
/// acknowledgement (OSC 52, OSC 1337 Copy): the reply is accepted
/// and discarded.
fn ignoreWriteReply(_: *anyopaque, _: clipboard.Write.Result) void {}
fn clipboardRead(
self: *Handler,
location: clipboard.Location,
terminator: osc.Terminator,
) void {
const func = self.effects.clipboard_read orelse return;
var state: ClipboardReadState = .{
.handler = self,
.location = location,
.terminator = terminator,
};
func(self, .{
.location = location,
.mimes = &.{"text/plain"},
.list = false,
.name = "",
.granted = false,
.can_remember = false,
.reply_ctx = &state,
.reply_fn = &ClipboardReadState.reply,
});
// The program is waiting on us, so a callback that returned
// without a (successful) reply gets an empty clipboard rather
// than silence.
if (!state.replied) state.respond("") catch |err| {
log.warn("error replying to clipboard read err={}", .{err});
};
}
/// Reply state for one synchronous clipboard read. This lives on the
/// clipboardRead stack frame, so it is only valid during the callback.
const ClipboardReadState = struct {
handler: *Handler,
location: clipboard.Location,
terminator: osc.Terminator,
replied: bool = false,
fn reply(ctx: *anyopaque, result: clipboard.Read.Result) void {
const self: *ClipboardReadState = @ptrCast(@alignCast(ctx));
if (self.replied) {
log.warn("clipboard read replied more than once, ignoring", .{});
return;
}
// OSC 52 carries a single text value.
const data: []const u8 = switch (result) {
.denied, .unsupported, .busy, .io_error => "",
.success => |s| for (s.contents) |c| {
if (clipboard.isTextMime(c.mime)) break c.data;
} else "",
};
self.respond(data) catch |err| {
// Leave replied unset so clipboardRead falls back to the
// empty reply.
log.warn("error replying to clipboard read err={}", .{err});
return;
};
self.replied = true;
}
fn respond(
self: *ClipboardReadState,
data: []const u8,
) error{ OutOfMemory, WriteFailed }!void {
const handler = self.handler;
var stack = std.heap.stackFallback(256, handler.terminal.gpa());
const alloc = stack.get();
var aw: std.Io.Writer.Allocating = .init(alloc);
defer aw.deinit();
const kind: u8 = switch (self.location) {
.selection => 's',
.primary => 'p',
.standard, _ => 'c',
};
try aw.writer.print("\x1b]52;{c};", .{kind});
try std.base64.standard.Encoder.encodeWriter(&aw.writer, data);
try aw.writer.writeAll(self.terminator.string());
const written = try aw.toOwnedSliceSentinel(0);
defer alloc.free(written);
handler.writePty(written);
}
};
/// Handle one Kitty clipboard protocol (OSC 5522) packet.
fn kittyClipboard(
self: *Handler,
v: Action.Value(.kitty_clipboard),
) error{OutOfMemory}!void {
// Decode and validate the metadata.
var arena: std.heap.ArenaAllocator = .init(self.terminal.gpa());
defer arena.deinit();
const meta = (kitty_clipboard.Metadata.parse(
arena.allocator(),
v.metadata,
) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.InvalidValue => {
const state = self.kitty_clipboard_write orelse return;
switch (kitty_clipboard.Metadata.operation(v.metadata) orelse return) {
.wdata, .walias => self.kittyClipboardFinish(
state,
.EINVAL,
v.terminator,
),
.read, .write => {},
}
return;
},
}) orelse return;
const payload = v.payload orelse "";
switch (meta.op) {
.read => try self.kittyClipboardRead(&meta, payload, v.terminator),
.write => try self.kittyClipboardWriteBegin(&meta, v.terminator),
.wdata => try self.kittyClipboardData(&meta, payload, v.terminator),
.walias => try self.kittyClipboardAlias(&meta, payload, v.terminator),
}
}
fn kittyClipboardRead(
self: *Handler,
meta: *const kitty_clipboard.Metadata,
payload: []const u8,
terminator: osc.Terminator,
) error{OutOfMemory}!void {
// The payload is the requested MIME list. Kitty drops a read
// request with an undecodable payload without any response.
const alloc = self.terminal.gpa();
const decoded = kitty_clipboard.Payload.init(
alloc,
payload,
) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.Invalid => return,
};
defer decoded.deinit(alloc);
if (!decoded.isValidUtf8()) return;
// Without a clipboard_read effect nothing can serve the read.
// EPERM is the protocol's denial so clients degrade gracefully.
const func = self.effects.clipboard_read orelse {
self.kittyClipboardRespond(&.{
.op = .read,
.status = .EPERM,
.id = meta.id,
.terminator = terminator,
});
return;
};
// The targets type ('.') asks for the listing of available
// types rather than data. Requested types beyond the cap are
// dropped and simply never served, which is how the protocol
// reports an unavailable type anyway.
var mimes_buf: [kitty_clipboard.max_read_mimes][]const u8 = undefined;
const mimes, const list = mimes: {
var targets = false;
var len: usize = 0;
var it = decoded.mimeIterator();
while (it.next()) |mime| {
if (std.mem.eql(u8, mime, kitty_clipboard.targets_mime)) {
targets = true;
continue;
}
if (len == mimes_buf.len) continue;
mimes_buf[len] = mime;
len += 1;
}
break :mimes .{ mimes_buf[0..len], targets };
};
// Per the spec a password without a name is no password. A
// stored grant for it lets the embedder skip its prompt. A
// prompt-exempt request never consults the grants: the
// embedder serves it without a prompt anyway, and consuming a
// one-time paste password on a listing would burn the grant
// before the follow-up data read.
const pw: []const u8 = if (meta.name.len > 0) meta.pw else "";
const granted = !kitty_clipboard.readPromptExempt(mimes.len) and
self.kitty_clipboard_grants.use(alloc, pw, .read);
var state: KittyClipboardReadState = .{
.handler = self,
.primary = meta.loc == .primary,
.id = meta.id,
.pw = pw,
.mimes = mimes,
.list = list,
.terminator = terminator,
};
func(self, .{
.location = meta.loc,
.mimes = mimes,
.list = list,
.name = meta.name,
.granted = granted,
.can_remember = pw.len > 0,
.reply_ctx = &state,
.reply_fn = &KittyClipboardReadState.reply,
});
// The program is waiting on us, so a callback that returned
// without a reply is answered as a denial rather than silence.
if (!state.replied) state.respondStatus(.EPERM);
}
/// Reply state for one synchronous Kitty clipboard read. This lives
/// on the kittyClipboardRead stack frame, so it is only valid during
/// the callback.
const KittyClipboardReadState = struct {
handler: *Handler,
primary: bool,
id: []const u8,
/// The effective password, empty when the request had none.
pw: []const u8,
/// The requested types; only these are served from a reply.
mimes: []const []const u8,
list: bool,
terminator: osc.Terminator,
replied: bool = false,
fn reply(ctx: *anyopaque, result: clipboard.Read.Result) void {
const self: *KittyClipboardReadState = @ptrCast(@alignCast(ctx));
if (self.replied) {
log.warn("clipboard read replied more than once, ignoring", .{});
return;
}
self.replied = true;
const success = switch (result) {
.denied => return self.respondStatus(.EPERM),
.unsupported => return self.respondStatus(.ENOSYS),
.busy => return self.respondStatus(.EBUSY),
.io_error => return self.respondStatus(.EIO),
.success => |s| s,
};
// Remembering is only offered when the request carried a
// usable password.
if (success.remember and self.pw.len > 0) {
self.handler.kitty_clipboard_grants.grant(
self.handler.terminal.gpa(),
self.pw,
.read,
false,
) catch |err| {
log.warn("error recording clipboard grant err={}", .{err});
};
}
self.respondSuccess(&success) catch |err| {
log.warn("error replying to clipboard read err={}", .{err});
self.respondStatus(.EIO);
};
}
/// Answer with a single status packet.
fn respondStatus(
self: *const KittyClipboardReadState,
status: kitty_clipboard.Status,
) void {
self.handler.kittyClipboardRespond(&.{
.op = .read,
.status = status,
.id = self.id,
.terminator = self.terminator,
});
}
/// Answer with the full success sequence (OK, listing, DATA
/// chunks, DONE), serving only the requested representations
/// in request order.
fn respondSuccess(
self: *const KittyClipboardReadState,
success: *const clipboard.Read.Result.Success,
) error{ OutOfMemory, WriteFailed }!void {
const handler = self.handler;
if (handler.effects.write_pty == null) return;
var served_buf: [kitty_clipboard.max_read_mimes]clipboard.Content = undefined;
var served_len: usize = 0;
for (self.mimes) |mime| {
for (success.contents) |content| {
if (!std.mem.eql(u8, content.mime, mime)) continue;
served_buf[served_len] = content;
served_len += 1;
break;
}
}
// Status packets fit on the stack; DATA packets carry the
// clipboard contents and fall back to the heap.
var stack = std.heap.stackFallback(1024, handler.terminal.gpa());
const alloc = stack.get();
var aw: std.Io.Writer.Allocating = .init(alloc);
defer aw.deinit();
try (kitty_clipboard.ReadSuccess{
.primary = self.primary,
.id = self.id,
.list = self.list,
.available = success.available,
.contents = served_buf[0..served_len],
.terminator = self.terminator,
}).encode(&aw.writer);
const written = try aw.toOwnedSliceSentinel(0);
defer alloc.free(written);
handler.writePty(written);
}
};
fn kittyClipboardWriteBegin(
self: *Handler,
meta: *const kitty_clipboard.Metadata,
terminator: osc.Terminator,
) error{OutOfMemory}!void {
// A new write silently replaces any in-flight transaction.
self.kittyClipboardAbort();
// Without a clipboard_write effect a commit can never succeed,
// so fail the transaction up front instead of spooling data
// we'd only throw away. Later wdata packets are ignored.
if (self.effects.clipboard_write == null) {
self.kittyClipboardRespond(&.{
.op = .write,
.status = .ENOSYS,
.id = meta.id,
.terminator = terminator,
});
return;
}
// Setup our write state
const alloc = self.terminal.gpa();
const state = try alloc.create(kitty_clipboard.WriteState);
errdefer alloc.destroy(state);
state.* = try .init(alloc, meta, .{
.max_size = self.kitty_clipboard_write_max_bytes,
});
self.kitty_clipboard_write = state;
}
fn kittyClipboardData(
self: *Handler,
meta: *const kitty_clipboard.Metadata,
payload: []const u8,
terminator: osc.Terminator,
) error{OutOfMemory}!void {
// Data without a transaction is silently ignored.
const state = self.kitty_clipboard_write orelse return;
// A wdata packet without a MIME type commits the transaction.
if (meta.mime.len == 0) return self.kittyClipboardCommit(
state,
terminator,
);
state.data(
self.terminal.gpa(),
meta,
payload,
) catch |err| switch (err) {
// Failing to spool matches kitty's EIO for a failed buffer
// write.
error.OutOfMemory => {
self.kittyClipboardFinish(
state,
.EIO,
terminator,
);
return error.OutOfMemory;
},
// Data over the write limit aborts the transaction and is
// reported to the client.
error.TooLarge => self.kittyClipboardFinish(
state,
.EFBIG,
terminator,
),
// An invalid base64 payload stream aborts the transaction.
error.Invalid => self.kittyClipboardFinish(
state,
.EINVAL,
terminator,
),
};
}
fn kittyClipboardAlias(
self: *Handler,
meta: *const kitty_clipboard.Metadata,
payload: []const u8,
terminator: osc.Terminator,
) error{OutOfMemory}!void {
// Aliases without a transaction are silently ignored. Once a
// transaction exists, a missing target MIME type is invalid and
// aborts the transaction.
const state = self.kitty_clipboard_write orelse return;
if (meta.mime.len == 0) return self.kittyClipboardFinish(
state,
.EINVAL,
terminator,
);
state.alias(
self.terminal.gpa(),
meta,
payload,
) catch |err| switch (err) {
error.OutOfMemory => {
self.kittyClipboardFinish(
state,
.EIO,
terminator,
);
return error.OutOfMemory;
},
// An undecodable alias payload aborts the transaction.
error.Invalid => self.kittyClipboardFinish(
state,
.EINVAL,
terminator,
),
};
}
fn kittyClipboardCommit(
self: *Handler,
state: *kitty_clipboard.WriteState,
terminator: osc.Terminator,
) error{OutOfMemory}!void {
const alloc = self.terminal.gpa();
const committed = state.commit(alloc) catch |err| switch (err) {
error.OutOfMemory => {
self.kittyClipboardFinish(state, .EIO, terminator);
return error.OutOfMemory;
},
// The last MIME type's payload stream was not correctly
// padded, which aborts the transaction.
error.Invalid => {
self.kittyClipboardFinish(state, .EINVAL, terminator);
return;
},
};
defer committed.deinit(alloc);
// The effect can't be null here (checked when the transaction
// began) but if an embedder cleared it mid-transaction that's
// ENOSYS.
const func = self.effects.clipboard_write orelse {
self.kittyClipboardFinish(state, .ENOSYS, terminator);
return;
};
// Per the spec a password without a name is no password. A
// stored grant for it lets the embedder skip its prompt.
const pw: []const u8 = if (committed.name.len > 0) committed.pw else "";
const granted = self.kitty_clipboard_grants.use(alloc, pw, .write);
var reply_state: KittyClipboardWriteReplyState = .{
.handler = self,
.pw = pw,
};
func(self, .{
.location = committed.loc,
.contents = committed.contents,
.name = committed.name,
.granted = granted,
.can_remember = pw.len > 0,
.reply_ctx = &reply_state,
.reply_fn = &KittyClipboardWriteReplyState.reply,
});
// The program is waiting on the commit status, so a callback
// that returned without a reply is answered as a denial rather
// than silence.
self.kittyClipboardFinish(
state,
reply_state.status orelse .EPERM,
terminator,
);
}
/// Reply state for one synchronous Kitty clipboard write. This lives
/// on the kittyClipboardCommit stack frame, so it is only valid
/// during the callback.
const KittyClipboardWriteReplyState = struct {
handler: *Handler,
/// The effective password, empty when the request had none.
pw: []const u8,
/// The replied commit status, mapped 1:1 from the reply result;
/// null until the callback replies.
status: ?kitty_clipboard.Status = null,
fn reply(ctx: *anyopaque, result: clipboard.Write.Result) void {
const self: *KittyClipboardWriteReplyState = @ptrCast(@alignCast(ctx));
if (self.status != null) {
log.warn("clipboard write replied more than once, ignoring", .{});
return;
}
self.status = switch (result) {
.denied => .EPERM,
.unsupported => .ENOSYS,
.busy => .EBUSY,
.invalid_data => .EINVAL,
.io_error => .EIO,
.success => |success| status: {
// Remembering is only offered when the request
// carried a usable password.
if (success.remember and self.pw.len > 0) {
self.handler.kitty_clipboard_grants.grant(
self.handler.terminal.gpa(),
self.pw,
.write,
false,
) catch |err| {
log.warn("error recording clipboard grant err={}", .{err});
};
}
break :status .DONE;
},
};
}
};
/// Answer a write transaction with its final status and drop it.
/// The id echoed is the one from the transaction's opening write
/// packet, matching kitty.
fn kittyClipboardFinish(
self: *Handler,
state: *const kitty_clipboard.WriteState,
status: kitty_clipboard.Status,
terminator: osc.Terminator,
) void {
self.kittyClipboardRespond(&.{
.op = .write,
.status = status,
.id = state.id,
.terminator = terminator,
});
self.kittyClipboardAbort();
}
/// Drop any in-flight write transaction without responding.
fn kittyClipboardAbort(self: *Handler) void {
if (self.kitty_clipboard_write) |state| {
const alloc = self.terminal.gpa();
state.deinit(alloc);
alloc.destroy(state);
self.kitty_clipboard_write = null;
}
}
/// Encode and write a single response packet. Unlike kitty, which
/// always terminates responses with ST, we echo the terminator of
/// the request being answered, matching our other OSC responses.
fn kittyClipboardRespond(
self: *Handler,
response: *const kitty_clipboard.Response,
) void {
if (self.effects.write_pty == null) return;
// Our responses carry at most a status and the echoed id so
// they virtually always fit on the stack.
var stack = std.heap.stackFallback(1024, self.terminal.gpa());
const alloc = stack.get();
var aw: std.Io.Writer.Allocating = .init(alloc);
defer aw.deinit();
response.encode(&aw.writer) catch return;
const resp = aw.toOwnedSliceSentinel(0) catch return;
defer alloc.free(resp);
self.writePty(resp);
}
/// Handle an OSC 72 drag and drop command.
fn kittyDnd(
self: *Handler,
v: Action.KittyDnd,
) (Allocator.Error || std.Io.Writer.Error)!void {
// Responses are usually small (queries, errors) but data
// serving can produce many chunks, so fall back to the heap.
var stack = std.heap.stackFallback(512, self.terminal.gpa());
const response_alloc = stack.get();
var aw: std.Io.Writer.Allocating = .init(response_alloc);
defer aw.deinit();
// The state is allocated on registration and owned by the
// terminal, so it uses the terminal's allocator, not the
// response's.
const event = try kitty_dnd.handleCommand(
&self.terminal.kitty_dnd,
self.terminal.gpa(),
&aw.writer,
v,
);
if (aw.written().len > 0) {
const written = aw.toOwnedSliceSentinel(0) catch return;
defer response_alloc.free(written);
self.writePty(written);
}
if (event) |ev| {
const func = self.effects.drag_and_drop orelse return;
func(self, ev);
}
}
fn reportDeviceAttributes(self: *Handler, req: device_attributes.Req) void {
const func = self.effects.device_attributes orelse return;
const attrs = func(self);
var stack = std.heap.stackFallback(128, self.terminal.gpa());
const alloc = stack.get();
var aw: std.Io.Writer.Allocating = .init(alloc);
defer aw.deinit();
attrs.encode(req, &aw.writer) catch return;
const written = aw.toOwnedSliceSentinel(0) catch return;
defer alloc.free(written);
self.writePty(written);
}
fn deviceStatus(self: *Handler, req: device_status.Request) void {
switch (req) {
.operating_status => self.writePty("\x1B[0n"),
.cursor_position => {
const pos: struct {
x: usize,
y: usize,
} = if (self.terminal.modes.get(.origin)) .{
.x = self.terminal.screens.active.cursor.x -| self.terminal.scrolling_region.left,
.y = self.terminal.screens.active.cursor.y -| self.terminal.scrolling_region.top,
} else .{
.x = self.terminal.screens.active.cursor.x,
.y = self.terminal.screens.active.cursor.y,
};
var buf: [64]u8 = undefined;
const resp = std.fmt.bufPrintZ(&buf, "\x1B[{};{}R", .{
pos.y + 1,
pos.x + 1,
}) catch return;
self.writePty(resp);
},
.color_scheme => {
const func = self.effects.color_scheme orelse return;
const scheme = func(self) orelse return;
var buf: [device_status.max_color_scheme_report_encode_size + 1]u8 = undefined;
var writer: std.Io.Writer = .fixed(buf[0..device_status.max_color_scheme_report_encode_size]);
device_status.encodeColorSchemeReport(&writer, scheme) catch return;
buf[writer.end] = 0;
self.writePty(buf[0..writer.end :0]);
},
.visibility => self.sendVisibilityReport(),
}
}
fn sendVisibilityReport(self: *Handler) void {
const write_pty = self.effects.write_pty orelse return;
var buf: [device_status.max_visibility_report_encode_size + 1]u8 = undefined;
var writer: std.Io.Writer = .fixed(buf[0..device_status.max_visibility_report_encode_size]);
device_status.encodeVisibilityReport(
&writer,
if (self.terminal.flags.visible) .potentially_visible else .not_visible,
) catch return;
buf[writer.end] = 0;
write_pty(self, buf[0..writer.end :0]);
}
fn reportEnquiry(self: *Handler) void {
const func = self.effects.enquiry orelse return;
const response = func(self);
if (response.len == 0) return;
var buf: [256]u8 = undefined;
if (response.len >= buf.len) return;
@memcpy(buf[0..response.len], response);
buf[response.len] = 0;
self.writePty(buf[0..response.len :0]);
}
fn reportXtversion(self: *Handler) void {
const version = if (self.effects.xtversion) |func| func(self) else "";
var buf: [288]u8 = undefined;
const resp = std.fmt.bufPrintZ(
&buf,
"\x1BP>|{s}\x1B\\",
.{if (version.len > 0) version else "libghostty"},
) catch return;
self.writePty(resp);
}
fn reportSize(self: *Handler, style: csi.SizeReportStyle) void {
// Almost all size reports will fit in 256 bytes so try that
// on the stack before falling back to a heap allocation.
var stack = std.heap.stackFallback(
256,
self.terminal.gpa(),
);
const alloc = stack.get();
// Allocating writing to accumulate the response.
var aw: std.Io.Writer.Allocating = .init(alloc);
defer aw.deinit();
// Build the response.
switch (style) {
.csi_21_t => {
if (!self.title_report) return;
const title = self.terminal.getTitle() orelse "";
aw.writer.print("\x1b]l{s}\x1b\\", .{title}) catch return;
},
.csi_14_t, .csi_16_t, .csi_18_t => {
const get_size = self.effects.size orelse return;
const s = get_size(self) orelse return;
const report_style: size_report.Style = switch (style) {
.csi_14_t => .csi_14_t,
.csi_16_t => .csi_16_t,
.csi_18_t => .csi_18_t,
.csi_21_t => unreachable,
};
size_report.encode(
&aw.writer,
report_style,
s,
) catch |err| {
log.warn("error encoding size report err={}", .{err});
return;
};
},
}
const resp = aw.toOwnedSliceSentinel(0) catch return;
defer alloc.free(resp);
self.writePty(resp);
}
fn reportMode2048(self: *Handler) void {
const get_size = self.effects.size orelse return;
const current = get_size(self) orelse return;
var buf: [128]u8 = undefined;
var writer: std.Io.Writer = .fixed(buf[0 .. buf.len - 1]);
size_report.encode(&writer, .mode_2048, current) catch return;
buf[writer.end] = 0;
self.writePty(buf[0..writer.end :0]);
}
fn windowTitle(self: *Handler, title_raw: []const u8) !void {
// Prevent DoS attacks by limiting title length.
const max_title_len = 1024;
const title = if (title_raw.len > max_title_len) title: {
log.warn("title length {d} exceeds max length {d}, truncating", .{
title_raw.len,
max_title_len,
});
break :title title_raw[0..max_title_len];
} else title_raw;
try self.terminal.setTitle(title);
const func = self.effects.title_changed orelse return;
func(self);
}
fn reportPwd(self: *Handler, url_raw: []const u8) !void {
// Prevent DoS attacks by limiting url length. Headroom for
// Linux PATH_MAX (4096) plus URI scheme/host and percent-encoding.
const max_url_len = 4096;
const url = if (url_raw.len > max_url_len) url: {
log.warn("pwd url length {d} exceeds max length {d}, truncating", .{
url_raw.len,
max_url_len,
});
break :url url_raw[0..max_url_len];
} else url_raw;
// We store the raw payload unparsed. Embedders read it via
// getPwd() and are responsible for decoding any URI scheme.
try self.terminal.setPwd(url);
const func = self.effects.pwd_changed orelse return;
func(self);
}
fn requestMode(self: *Handler, mode: modes.Mode) void {
var report = self.terminal.modes.getReport(.fromMode(mode));
// Kitty paste events (mode 5522) can't work without a clipboard
// read effect, so if that isn't set mark it as unrecognized.
if (mode == .kitty_paste_events and
self.effects.clipboard_read == null)
{
report.state = .not_recognized;
}
self.sendModeReport(report);
}
fn requestModeUnknown(self: *Handler, mode_raw: u16, ansi: bool) void {
const report = self.terminal.modes.getReport(.{
.value = @truncate(mode_raw),
.ansi = ansi,
});
self.sendModeReport(report);
}
fn sendModeReport(self: *Handler, report: modes.Report) void {
var buf: [modes.Report.max_size + 1]u8 = undefined;
var writer: std.Io.Writer = .fixed(&buf);
report.encode(&writer) catch |err| {
log.warn("error encoding mode report err={}", .{err});
return;
};
const len = writer.buffered().len;
buf[len] = 0;
self.writePty(buf[0..len :0]);
}
fn queryKittyKeyboard(self: *Handler) void {
// Max response is "\x1b[?31u\x00" (7 bytes): the flags are a u5 (max 31).
var buf: [32]u8 = undefined;
const resp = std.fmt.bufPrintZ(&buf, "\x1b[?{}u", .{
self.terminal.screens.active.kitty_keyboard.current().int(),
}) catch return;
self.writePty(resp);
}
inline fn horizontalTab(self: *Handler, count: u16) void {
for (0..count) |_| {
const x = self.terminal.screens.active.cursor.x;
self.terminal.horizontalTab();
if (x == self.terminal.screens.active.cursor.x) break;
}
}
inline fn horizontalTabBack(self: *Handler, count: u16) void {
for (0..count) |_| {
const x = self.terminal.screens.active.cursor.x;
self.terminal.horizontalTabBack();
if (x == self.terminal.screens.active.cursor.x) break;
}
}
fn setMode(self: *Handler, mode: modes.Mode, enabled: bool) !void {
// Set the mode on the terminal
self.terminal.modes.set(mode, enabled);
// Some modes require additional processing
switch (mode) {
.autorepeat,
.reverse_colors,
=> {},
.origin => self.terminal.setCursorPos(1, 1),
.enable_left_and_right_margin => if (!enabled) {
self.terminal.scrolling_region.left = 0;
self.terminal.scrolling_region.right = self.terminal.cols - 1;
},
.alt_screen_legacy => try self.terminal.switchScreenMode(.@"47", enabled),
.alt_screen => try self.terminal.switchScreenMode(.@"1047", enabled),
.alt_screen_save_cursor_clear_enter => try self.terminal.switchScreenMode(.@"1049", enabled),
.save_cursor => if (enabled) {
self.terminal.saveCursor();
} else {
self.terminal.restoreCursor();
},
.enable_mode_3 => {},
.@"132_column" => try self.terminal.deccolm(
self.terminal.screens.active.alloc,
if (enabled) .@"132_cols" else .@"80_cols",
),
.synchronized_output,
.linefeed,
.focus_event,
=> {},
// Enabling mode 2048 reports already-committed pixel geometry.
// Waiting for the next resize leaves late-enabling clients without
// the dimensions they need for their first image frame.
.in_band_size_reports => if (enabled) self.reportMode2048(),
.report_visibility => if (enabled) self.sendVisibilityReport(),
.mouse_event_x10 => {
if (enabled) {
self.terminal.flags.mouse_event = .x10;
} else {
self.terminal.flags.mouse_event = .none;
}
},
.mouse_event_normal => {
if (enabled) {
self.terminal.flags.mouse_event = .normal;
} else {
self.terminal.flags.mouse_event = .none;
}
},
.mouse_event_button => {
if (enabled) {
self.terminal.flags.mouse_event = .button;
} else {
self.terminal.flags.mouse_event = .none;
}
},
.mouse_event_any => {
if (enabled) {
self.terminal.flags.mouse_event = .any;
} else {
self.terminal.flags.mouse_event = .none;
}
},
.mouse_format_utf8 => self.terminal.flags.mouse_format = if (enabled) .utf8 else .x10,
.mouse_format_sgr => self.terminal.flags.mouse_format = if (enabled) .sgr else .x10,
.mouse_format_urxvt => self.terminal.flags.mouse_format = if (enabled) .urxvt else .x10,
.mouse_format_sgr_pixels => self.terminal.flags.mouse_format = if (enabled) .sgr_pixels else .x10,
else => {},
}
}
fn colorOperation(
self: *Handler,
requests: *const osc_color.List,
terminator: osc.Terminator,
) !void {
if (requests.count() == 0) return;
var stack = std.heap.stackFallback(1024, self.terminal.gpa());
const alloc = stack.get();
var response: std.Io.Writer.Allocating = .init(alloc);
defer response.deinit();
const writer = &response.writer;
var it = requests.constIterator(0);
while (it.next()) |req| {
switch (req.*) {
.set => |set| {
switch (set.target) {
.palette => |i| {
self.terminal.flags.dirty.palette = true;
self.terminal.colors.palette.set(i, set.color);
},
.dynamic => |dynamic| switch (dynamic) {
.foreground => self.terminal.colors.foreground.set(set.color),
.background => self.terminal.colors.background.set(set.color),
.cursor => self.terminal.colors.cursor.set(set.color),
.pointer_foreground,
.pointer_background,
.tektronix_foreground,
.tektronix_background,
.highlight_background,
.tektronix_cursor,
.highlight_foreground,
=> {},
},
.special => {},
}
},
.reset => |target| switch (target) {
.palette => |i| {
self.terminal.flags.dirty.palette = true;
self.terminal.colors.palette.reset(i);
},
.dynamic => |dynamic| switch (dynamic) {
.foreground => self.terminal.colors.foreground.reset(),
.background => self.terminal.colors.background.reset(),
.cursor => self.terminal.colors.cursor.reset(),
.pointer_foreground,
.pointer_background,
.tektronix_foreground,
.tektronix_background,
.highlight_background,
.tektronix_cursor,
.highlight_foreground,
=> {},
},
.special => {},
},
.reset_palette => {
const mask = &self.terminal.colors.palette.mask;
var mask_it = mask.iterator(.{});
while (mask_it.next()) |i| {
self.terminal.flags.dirty.palette = true;
self.terminal.colors.palette.reset(@intCast(i));
}
mask.* = .initEmpty();
},
.query => |target| {
if (self.effects.write_pty == null) continue;
const c = self.terminal.colorForXterm(target) orelse continue;
try writeXtermColorReport(writer, target, c, terminator);
},
.reset_special => {},
}
}
if (response.written().len > 0) {
const resp = try response.toOwnedSliceSentinel(0);
defer alloc.free(resp);
self.writePty(resp);
}
}
fn writeXtermColorReport(
writer: *std.Io.Writer,
target: osc_color.Target,
c: color.RGB,
terminator: osc.Terminator,
) !void {
switch (target) {
.palette => |i| {
try writer.print("\x1b]4;{d};", .{i});
try c.encodeRgb16(writer);
try writer.writeAll(terminator.string());
},
.dynamic => |dynamic| switch (dynamic) {
.foreground,
.background,
.cursor,
=> {
try writer.print("\x1b]{d};", .{@intFromEnum(dynamic)});
try c.encodeRgb16(writer);
try writer.writeAll(terminator.string());
},
.pointer_foreground,
.pointer_background,
.tektronix_foreground,
.tektronix_background,
.highlight_background,
.tektronix_cursor,
.highlight_foreground,
=> {},
},
.special => {},
}
}
fn kittyColorOperation(
self: *Handler,
request: kitty_color.OSC,
) !void {
var stack = std.heap.stackFallback(1024, self.terminal.gpa());
const alloc = stack.get();
var response: std.Io.Writer.Allocating = .init(alloc);
defer response.deinit();
const writer = &response.writer;
for (request.list.items) |item| {
switch (item) {
.set => |v| switch (v.key) {
.palette => |palette| {
self.terminal.flags.dirty.palette = true;
self.terminal.colors.palette.set(palette, v.color);
},
.special => |special| switch (special) {
.foreground => self.terminal.colors.foreground.set(v.color),
.background => self.terminal.colors.background.set(v.color),
.cursor => self.terminal.colors.cursor.set(v.color),
else => {},
},
},
.reset => |key| switch (key) {
.palette => |palette| {
self.terminal.flags.dirty.palette = true;
self.terminal.colors.palette.reset(palette);
},
.special => |special| switch (special) {
.foreground => self.terminal.colors.foreground.reset(),
.background => self.terminal.colors.background.reset(),
.cursor => self.terminal.colors.cursor.reset(),
else => {},
},
},
.query => |key| {
if (self.effects.write_pty == null) continue;
const c = self.terminal.colorForKitty(key) orelse {
if (!key.hasTerminalQueryColor()) continue;
if (response.written().len == 0) try writer.writeAll("\x1b]21");
try writer.print(";{f}=", .{key});
continue;
};
if (response.written().len == 0) try writer.writeAll("\x1b]21");
try writer.print(";{f}=", .{key});
try c.encodeRgb8(writer);
},
}
}
if (response.written().len > 0) {
try writer.writeAll(request.terminator.string());
const resp = try response.toOwnedSliceSentinel(0);
defer alloc.free(resp);
self.writePty(resp);
}
}
fn apcEnd(self: *Handler, terminated: bool) void {
const io = self.terminal.io();
const alloc = self.terminal.gpa();
var result = self.apc_handler.end() orelse return;
defer result.deinit(alloc);
switch (result) {
.unknown => |*unknown| {
if (terminated) self.unknownSequence(.{ .apc = .{
.content = unknown.content,
.truncated = unknown.truncated,
} });
},
.kitty => |*kitty_cmd| if (comptime build_options.kitty_graphics) {
if (self.terminal.kittyGraphics(
io,
alloc,
kitty_cmd,
)) |resp| resp: {
// Don't waste time encoding if we can't write responses
// anyways.
if (self.effects.write_pty == null) break :resp;
// Encode and write the response if we have one.
var buf: [1024]u8 = undefined;
var writer: std.Io.Writer = .fixed(&buf);
resp.encode(&writer) catch return;
writer.writeByte(0) catch return;
const final = writer.buffered();
if (final.len > 3) self.writePty(final[0 .. final.len - 1 :0]);
}
},
.glyph => |*glyph_req| if (comptime build_options.glyph_protocol) {
const resp = self.terminal.glyphProtocol(alloc, glyph_req);
if (resp) |r| resp_block: {
// Don't waste time encoding if we can't write responses
// anyways.
if (self.effects.write_pty == null) break :resp_block;
// Glyph responses are short and bounded by the protocol
// fields we emit, so this matches the Kitty response
// buffer size above with ample headroom.
var buf: [apc.glyph.Response.max_wire_bytes]u8 = undefined;
var writer: std.Io.Writer = .fixed(&buf);
r.formatWire(&writer) catch return;
writer.writeByte(0) catch return;
const final = writer.buffered();
self.writePty(final[0 .. final.len - 1 :0]);
}
},
}
}
};
/// A writer that delivers everything through the write_pty effect:
/// the buffer as it fills, and data that doesn't fit it directly.
/// Never fails, since the effect can't.
const PtyWriter = struct {
handler: *Handler,
writer: std.Io.Writer,
fn init(handler: *Handler, buffer: []u8) PtyWriter {
return .{
.handler = handler,
.writer = .{
.vtable = &.{ .drain = drain },
.buffer = buffer,
},
};
}
fn drain(
w: *std.Io.Writer,
data: []const []const u8,
splat: usize,
) std.Io.Writer.Error!usize {
const self: *PtyWriter = @alignCast(@fieldParentPtr("writer", w));
// Buffered bytes go first to keep the order.
if (w.end > 0) {
self.handler.writePty(w.buffer[0..w.end]);
w.end = 0;
}
var consumed: usize = 0;
for (data[0 .. data.len - 1]) |slice| {
if (slice.len > 0) self.handler.writePty(slice);
consumed += slice.len;
}
const pattern = data[data.len - 1];
for (0..splat) |_| {
if (pattern.len > 0) self.handler.writePty(pattern);
consumed += pattern.len;
}
return consumed;
}
};
test "resize clears synchronized output on unchanged cell dimensions" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
t.modes.set(.synchronized_output, true);
try s.handler.resize(.{
.cols = 80,
.rows = 24,
.cell_size_px = .{ .width = 9, .height = 18 },
});
try testing.expect(!t.modes.get(.synchronized_output));
try testing.expectEqual(@as(u32, 720), t.width_px);
try testing.expectEqual(@as(u32, 432), t.height_px);
}
test "unknown APC effect callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var count: usize = 0;
var content: [16]u8 = undefined;
var content_len: usize = undefined;
var truncated: bool = undefined;
fn unknownSequence(_: *Handler, value: Handler.UnknownSequence) void {
switch (value) {
.apc => |apc_value| {
content_len = apc_value.content.len;
@memcpy(content[0..apc_value.content.len], apc_value.content);
truncated = apc_value.truncated;
},
}
count += 1;
}
};
S.count = 0;
var handler: Handler = .init(&t);
handler.unknown_sequence = &S.unknownSequence;
handler.apc_handler.unknown_max_bytes = 8;
var s: Stream = .init(.{
.allocator = testing.allocator,
.handler = handler,
});
defer s.deinit();
// Unknown OSC commands retain their legacy behavior and are ignored.
s.nextSlice("\x1B]999;abcdef\x07");
s.nextSlice("\x1B_abcd;payload\x1B\\");
try testing.expectEqual(@as(usize, 1), S.count);
try testing.expectEqualStrings("abcd;pay", S.content[0..S.content_len]);
try testing.expect(S.truncated);
// Aborted unknown APCs are suppressed.
s.nextSlice("\x1B_Xpayload\x18");
try testing.expectEqual(@as(usize, 1), S.count);
}
test "resize reports mode 2048 geometry" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var response: [128]u8 = undefined;
var response_len: usize = 0;
fn writePty(_: *Handler, data: []const u8) void {
@memcpy(response[0..data.len], data);
response_len = data.len;
}
};
S.response_len = 0;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
t.modes.set(.in_band_size_reports, true);
try s.handler.resize(.{
.cols = 100,
.rows = 40,
.cell_size_px = .{ .width = 9, .height = 18 },
});
try testing.expectEqualStrings(
"\x1B[48;40;100;720;900t",
S.response[0..S.response_len],
);
}
test "resize suppresses mode 2048 reports" {
const S = struct {
var calls: usize = 0;
fn writePty(_: *Handler, _: []const u8) void {
calls += 1;
}
};
S.calls = 0;
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Disabled mode suppresses a report even with pixels and a callback.
try s.handler.resize(.{
.cols = 80,
.rows = 24,
.cell_size_px = .{ .width = 9, .height = 18 },
});
try testing.expectEqual(@as(usize, 0), S.calls);
// Missing pixel geometry suppresses a report even with the mode enabled.
t.modes.set(.in_band_size_reports, true);
try s.handler.resize(.{ .cols = 80, .rows = 24 });
try testing.expectEqual(@as(usize, 0), S.calls);
// A read-only stream has no write effect and remains successful.
var readonly_terminal: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer readonly_terminal.deinit(testing.allocator);
readonly_terminal.modes.set(.in_band_size_reports, true);
var readonly_stream: Stream = .init(.{
.allocator = testing.allocator,
.handler = .init(&readonly_terminal),
});
defer readonly_stream.deinit();
try readonly_stream.handler.resize(.{
.cols = 80,
.rows = 24,
.cell_size_px = .{ .width = 9, .height = 18 },
});
try testing.expectEqual(@as(usize, 0), S.calls);
}
test "resize failure preserves terminal state and does not write" {
var failing = testing.FailingAllocator.init(testing.allocator, .{});
const alloc = failing.allocator();
var t: Terminal = try .init(testing.io, alloc, .{ .cols = 10, .rows = 1 });
defer t.deinit(alloc);
const S = struct {
var called: bool = false;
fn writePty(_: *Handler, _: []const u8) void {
called = true;
}
};
S.called = false;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = alloc, .handler = handler });
defer s.deinit();
t.modes.set(.synchronized_output, true);
t.modes.set(.in_band_size_reports, true);
failing.fail_index = failing.alloc_index;
try testing.expectError(error.OutOfMemory, s.handler.resize(.{
.cols = 513,
.rows = 1,
.cell_size_px = .{ .width = 9, .height = 18 },
}));
try testing.expect(t.modes.get(.synchronized_output));
try testing.expect(!S.called);
try testing.expectEqual(@as(@TypeOf(t.cols), 10), t.cols);
try testing.expectEqual(@as(u32, 0), t.width_px);
try testing.expectEqual(@as(u32, 0), t.height_px);
}
test "resize effects do not change canonical terminal state" {
var authoritative: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 10, .rows = 5 },
);
defer authoritative.deinit(testing.allocator);
var readonly: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 10, .rows = 5 },
);
defer readonly.deinit(testing.allocator);
const S = struct {
fn writePty(_: *Handler, _: []const u8) void {}
};
var authoritative_handler: Handler = .init(&authoritative);
authoritative_handler.effects.write_pty = &S.writePty;
var authoritative_stream: Stream = .init(.{
.allocator = testing.allocator,
.handler = authoritative_handler,
});
defer authoritative_stream.deinit();
var readonly_stream: Stream = .init(.{
.allocator = testing.allocator,
.handler = .init(&readonly),
});
defer readonly_stream.deinit();
authoritative.modes.set(.in_band_size_reports, true);
readonly.modes.set(.in_band_size_reports, true);
const value: Terminal.Resize = .{
.cols = 20,
.rows = 10,
.cell_size_px = .{ .width = 9, .height = 18 },
};
try authoritative_stream.handler.resize(value);
try readonly_stream.handler.resize(value);
try testing.expectEqual(authoritative.cols, readonly.cols);
try testing.expectEqual(authoritative.rows, readonly.rows);
try testing.expectEqual(authoritative.width_px, readonly.width_px);
try testing.expectEqual(authoritative.height_px, readonly.height_px);
try testing.expect(std.meta.eql(authoritative.modes, readonly.modes));
try testing.expect(std.meta.eql(
authoritative.scrolling_region,
readonly.scrolling_region,
));
}
test "basic print" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("Hello");
try testing.expectEqual(@as(usize, 5), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Hello", str);
}
test "semantic failure is sticky while processing continues" {
var failing = testing.FailingAllocator.init(testing.allocator, .{});
const alloc = failing.allocator();
var t: Terminal = try .init(testing.io, alloc, .{ .cols = 10, .rows = 2 });
defer t.deinit(alloc);
var s: Stream = .init(.{ .allocator = alloc, .handler = .init(&t) });
defer s.deinit();
try testing.expect(!s.handler.semantic_failure);
// Setting the title is a terminal-owned semantic update. Force its
// allocation to fail at the central vtFallible boundary.
failing.fail_index = failing.alloc_index;
s.nextSlice("\x1B]2;unavailable\x1B\\");
try testing.expect(s.handler.semantic_failure);
// Later input and RIS remain best-effort and never clear the diagnostic.
failing.fail_index = std.math.maxInt(usize);
s.nextSlice("ignored");
s.nextSlice("\x1Bc");
s.nextSlice("OK");
try testing.expect(s.handler.semantic_failure);
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("OK", str);
// A new execution root starts without inheriting the diagnostic.
var fresh = Handler.init(&t);
defer fresh.deinit();
try testing.expect(!fresh.semantic_failure);
}
test "cursor movement" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Move cursor using escape sequences
s.nextSlice("Hello\x1B[1;1H");
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
// Move to position 2,3
s.nextSlice("\x1B[2;3H");
try testing.expectEqual(@as(usize, 2), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 1), t.screens.active.cursor.y);
}
test "erase operations" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 20, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Print some text
s.nextSlice("Hello World");
try testing.expectEqual(@as(usize, 11), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
// Move cursor to position 1,6 and erase from cursor to end of line
s.nextSlice("\x1B[1;6H");
s.nextSlice("\x1B[K");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Hello", str);
}
test "tabs" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("A\tB");
try testing.expectEqual(@as(usize, 9), t.screens.active.cursor.x);
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("A B", str);
}
test "modes" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Test wraparound mode
try testing.expect(t.modes.get(.wraparound));
s.nextSlice("\x1B[?7l"); // Disable wraparound
try testing.expect(!t.modes.get(.wraparound));
s.nextSlice("\x1B[?7h"); // Enable wraparound
try testing.expect(t.modes.get(.wraparound));
}
test "scrolling regions" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set scrolling region from line 5 to 20
s.nextSlice("\x1B[5;20r");
try testing.expectEqual(@as(usize, 4), t.scrolling_region.top);
try testing.expectEqual(@as(usize, 19), t.scrolling_region.bottom);
try testing.expectEqual(@as(usize, 0), t.scrolling_region.left);
try testing.expectEqual(@as(usize, 79), t.scrolling_region.right);
}
test "charsets" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Configure G0 as DEC special graphics
s.nextSlice("\x1B(0");
s.nextSlice("`"); // Should print diamond character
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("◆", str);
}
test "alt screen" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Write to primary screen
s.nextSlice("Primary");
try testing.expectEqual(.primary, t.screens.active_key);
// Switch to alt screen
s.nextSlice("\x1B[?1049h");
try testing.expectEqual(.alternate, t.screens.active_key);
// Write to alt screen
s.nextSlice("Alt");
// Switch back to primary
s.nextSlice("\x1B[?1049l");
try testing.expectEqual(.primary, t.screens.active_key);
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Primary", str);
}
test "cursor save and restore" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Move cursor to 10,15
s.nextSlice("\x1B[10;15H");
try testing.expectEqual(@as(usize, 14), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 9), t.screens.active.cursor.y);
// Save cursor
s.nextSlice("\x1B7");
// Move cursor elsewhere
s.nextSlice("\x1B[1;1H");
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
// Restore cursor
s.nextSlice("\x1B8");
try testing.expectEqual(@as(usize, 14), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 9), t.screens.active.cursor.y);
}
test "attributes" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set bold and write text
s.nextSlice("\x1B[1mBold\x1B[0m");
// Verify we can write attributes - just check the string was written
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Bold", str);
}
test "DECRQSS responses" {
var t: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer t.deinit(testing.allocator);
const S = struct {
var response: [dcs.Command.DECRQSS.max_response_bytes]u8 = undefined;
var response_len: usize = 0;
var calls: usize = 0;
fn reset() void {
response_len = 0;
calls = 0;
}
fn writePty(_: *Handler, data: []const u8) void {
@memcpy(response[0..data.len], data);
response_len = data.len;
calls += 1;
}
fn expectResponse(expected: []const u8) !void {
try testing.expectEqual(@as(usize, 1), calls);
try testing.expectEqualStrings(
expected,
response[0..response_len],
);
reset();
}
};
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// SGR
s.nextSlice("\x1B[1m\x1BP$qm\x1B\\");
try S.expectResponse("\x1BP1$r0;1m\x1B\\");
// Overline
s.nextSlice("\x1B[0;53m\x1BP$qm\x1B\\");
try S.expectResponse("\x1BP1$r0;53m\x1B\\");
// Requests larger than the parser's fixed request buffer are ignored,
// and the next DCS command must still be processed normally.
s.nextSlice("\x1BP$qfoo\x1B\\");
try testing.expectEqual(@as(usize, 0), S.calls);
try testing.expect(!s.handler.semantic_failure);
s.nextSlice("\x1BP$qm\x1B\\");
try S.expectResponse("\x1BP1$r0;53m\x1B\\");
}
test "DECRQSS without write effect is ignored" {
var t: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("\x1BP$qm\x1B\\");
try testing.expect(!s.handler.semantic_failure);
}
test "XTGETTCAP responses" {
var t: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer t.deinit(testing.allocator);
const S = struct {
var response: [128]u8 = undefined;
var response_len: usize = 0;
var calls: usize = 0;
fn reset() void {
response_len = 0;
calls = 0;
}
fn writePty(_: *Handler, data: []const u8) void {
@memcpy(response[0..data.len], data);
response_len = data.len;
calls += 1;
}
fn expectResponse(expected: []const u8) !void {
try testing.expectEqual(@as(usize, 1), calls);
try testing.expectEqualStrings(
expected,
response[0..response_len],
);
reset();
}
};
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// The full capability table comes from the static terminfo map; this
// checks the wiring for a valued and a valueless (boolean) capability.
s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("Co", .upper) ++ "\x1B\\");
try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("Co", .upper) ++ "=" ++
std.fmt.bytesToHex("256", .upper) ++ "\x1B\\");
s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("am", .upper) ++ "\x1B\\");
try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("am", .upper) ++ "\x1B\\");
// One response per requested key; lowercase hex is normalized by the
// DCS parser. The capture holds the last ("Co") reply.
s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("am", .lower) ++ ";" ++
std.fmt.bytesToHex("Co", .lower) ++ "\x1B\\");
try testing.expectEqual(@as(usize, 2), S.calls);
try testing.expectEqualStrings(
"\x1BP1+r" ++ std.fmt.bytesToHex("Co", .upper) ++ "=" ++
std.fmt.bytesToHex("256", .upper) ++ "\x1B\\",
S.response[0..S.response_len],
);
S.reset();
// Unknown and malformed keys are skipped without an error.
s.nextSlice("\x1BP+qWHO;5;GG\x1B\\");
try testing.expectEqual(@as(usize, 0), S.calls);
try testing.expect(!s.handler.semantic_failure);
}
test "XTGETTCAP without write effect is ignored" {
var t: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("TN", .upper) ++ ";" ++
std.fmt.bytesToHex("am", .upper) ++ "\x1B\\");
try testing.expect(!s.handler.semantic_failure);
}
test "XTGETTCAP TN responses" {
var t: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer t.deinit(testing.allocator);
const S = struct {
var response: [512]u8 = undefined;
var response_len: usize = 0;
var calls: usize = 0;
fn reset() void {
response_len = 0;
calls = 0;
}
fn writePty(_: *Handler, data: []const u8) void {
@memcpy(response[0..data.len], data);
response_len = data.len;
calls += 1;
}
fn expectResponse(expected: []const u8) !void {
try testing.expectEqual(@as(usize, 1), calls);
try testing.expectEqualStrings(
expected,
response[0..response_len],
);
reset();
}
};
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
const tn_query = "\x1BP+q" ++ std.fmt.bytesToHex("TN", .upper) ++ "\x1B\\";
// While no name is configured the query goes unanswered.
s.nextSlice(tn_query);
try testing.expectEqual(@as(usize, 0), S.calls);
// A configured name is reported hex-encoded.
s.handler.terminfo_name = "xterm-256color";
s.nextSlice(tn_query);
try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("TN", .upper) ++ "=" ++
std.fmt.bytesToHex("xterm-256color", .upper) ++ "\x1B\\");
// A maximum-length name is still reported in full.
const max_name = "a" ** Handler.max_terminfo_name_bytes;
s.handler.terminfo_name = max_name;
s.nextSlice(tn_query);
try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("TN", .upper) ++ "=" ++
std.fmt.bytesToHex(max_name.*, .upper) ++ "\x1B\\");
// An empty name is silent; "Co" is still answered.
s.handler.terminfo_name = "";
s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("TN", .upper) ++ ";" ++
std.fmt.bytesToHex("Co", .upper) ++ "\x1B\\");
try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("Co", .upper) ++ "=" ++
std.fmt.bytesToHex("256", .upper) ++ "\x1B\\");
// As are names beyond the maximum length.
s.handler.terminfo_name = "a" ** (Handler.max_terminfo_name_bytes + 1);
s.nextSlice(tn_query);
try testing.expectEqual(@as(usize, 0), S.calls);
try testing.expect(!s.handler.semantic_failure);
}
test "DCS command memory is released" {
var t: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
// A completed command transfers its allocation to Command; dcsCommand
// must release it even when there is no write effect.
s.nextSlice("\x1BP+q536D756C78\x1B\\");
// An incomplete command remains owned by the handler and must be released
// when the stream is deinitialized. testing.allocator detects either leak.
s.nextSlice("\x1BP+q536D756C78");
s.deinit();
}
test "DECALN screen alignment" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 3 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Run DECALN
s.nextSlice("\x1B#8");
// Verify entire screen is filled with 'E'
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("EEEEEEEEEE\nEEEEEEEEEE\nEEEEEEEEEE", str);
// Cursor should be at 1,1
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
}
test "full reset" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Make some changes
s.nextSlice("Hello");
s.nextSlice("\x1B[10;20H");
s.nextSlice("\x1B[5;20r"); // Set scroll region
s.nextSlice("\x1B[?7l"); // Disable wraparound
s.nextSlice("\x1B_25a1;r;cp=e0a0;AAAAAAAAAAAAAA==\x1B\\");
try testing.expect(t.glyph_glossary.contains(0xE0A0));
// Full reset
s.nextSlice("\x1Bc");
// Verify reset state
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
try testing.expectEqual(@as(usize, 0), t.scrolling_region.top);
try testing.expectEqual(@as(usize, 23), t.scrolling_region.bottom);
try testing.expect(t.modes.get(.wraparound));
try testing.expect(!t.glyph_glossary.contains(0xE0A0));
}
test "glyph protocol APC with write_pty callback" {
if (comptime !build_options.glyph_protocol) return error.SkipZigTest;
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var last_response: ?[:0]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (last_response) |old| testing.allocator.free(old);
last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM");
}
};
S.last_response = null;
defer if (S.last_response) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B_25a1;s\x1B\\");
try testing.expectEqualStrings("\x1B_25a1;s;fmt=glyf\x1B\\", S.last_response.?);
s.nextSlice("\x1B_25a1;r;cp=e0a0;AAAAAAAAAAAAAA==\x1B\\");
try testing.expectEqualStrings("\x1B_25a1;r;cp=e0a0;status=0\x1B\\", S.last_response.?);
try testing.expect(t.glyph_glossary.contains(0xE0A0));
}
test "ignores query actions" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// These should be ignored without error
s.nextSlice("\x1B[c"); // Device attributes
s.nextSlice("\x1B[5n"); // Device status report
s.nextSlice("\x1B[6n"); // Cursor position report
s.nextSlice("\x1B]4;0;?\x1B\\"); // OSC color query
s.nextSlice("\x1B]21;foreground=?\x1B\\"); // Kitty color query
s.nextSlice("\x1B]52;c;%%%invalid-base64%%%\x1B\\");
s.nextSlice("\x1B_Ga=p,i=999\x1B\\"); // Missing Kitty image
s.nextSlice("\x1B_25a1;r;cp=41;%%%invalid%%%\x1B\\"); // Rejected glyph
// Query, malformed input, protocol failure responses, and external-effect
// failures do not imply that terminal-owned semantic state diverged.
try testing.expect(!s.handler.semantic_failure);
// Terminal should still be functional
s.nextSlice("Test");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Test", str);
}
test "OSC 4 set and reset palette" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Save default color
const default_color_0 = t.colors.palette.original[0];
// Set color 0 to red
s.nextSlice("\x1b]4;0;rgb:ff/00/00\x1b\\");
try testing.expectEqual(@as(u8, 0xff), t.colors.palette.current[0].r);
try testing.expectEqual(@as(u8, 0x00), t.colors.palette.current[0].g);
try testing.expectEqual(@as(u8, 0x00), t.colors.palette.current[0].b);
try testing.expect(t.colors.palette.mask.isSet(0));
// Reset color 0
s.nextSlice("\x1b]104;0\x1b\\");
try testing.expectEqual(default_color_0, t.colors.palette.current[0]);
try testing.expect(!t.colors.palette.mask.isSet(0));
}
test "OSC 104 reset all palette colors" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set multiple colors
s.nextSlice("\x1b]4;0;rgb:ff/00/00\x1b\\");
s.nextSlice("\x1b]4;1;rgb:00/ff/00\x1b\\");
s.nextSlice("\x1b]4;2;rgb:00/00/ff\x1b\\");
try testing.expect(t.colors.palette.mask.isSet(0));
try testing.expect(t.colors.palette.mask.isSet(1));
try testing.expect(t.colors.palette.mask.isSet(2));
// Reset all palette colors
s.nextSlice("\x1b]104\x1b\\");
try testing.expectEqual(t.colors.palette.original[0], t.colors.palette.current[0]);
try testing.expectEqual(t.colors.palette.original[1], t.colors.palette.current[1]);
try testing.expectEqual(t.colors.palette.original[2], t.colors.palette.current[2]);
try testing.expect(!t.colors.palette.mask.isSet(0));
try testing.expect(!t.colors.palette.mask.isSet(1));
try testing.expect(!t.colors.palette.mask.isSet(2));
}
test "OSC 10 set and reset foreground color" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Initially unset
try testing.expect(t.colors.foreground.get() == null);
// Set foreground to red
s.nextSlice("\x1b]10;rgb:ff/00/00\x1b\\");
const fg = t.colors.foreground.get().?;
try testing.expectEqual(@as(u8, 0xff), fg.r);
try testing.expectEqual(@as(u8, 0x00), fg.g);
try testing.expectEqual(@as(u8, 0x00), fg.b);
// Reset foreground
s.nextSlice("\x1b]110\x1b\\");
try testing.expect(t.colors.foreground.get() == null);
}
test "OSC 11 set and reset background color" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
const default: color.RGB = .{ .r = 0x10, .g = 0x20, .b = 0x30 };
t.colors.background.default = default;
// Set background to green
s.nextSlice("\x1b]11;rgb:00/ff/00\x1b\\");
const bg = t.colors.background.get().?;
try testing.expectEqual(@as(u8, 0x00), bg.r);
try testing.expectEqual(@as(u8, 0xff), bg.g);
try testing.expectEqual(@as(u8, 0x00), bg.b);
// Reset background
s.nextSlice("\x1b]111\x1b\\");
try testing.expectEqual(default, t.colors.background.get().?);
try testing.expectEqual(null, t.colors.background.override);
// A reset color continues to follow later configuration changes.
const updated: color.RGB = .{ .r = 0x40, .g = 0x50, .b = 0x60 };
t.colors.background.default = updated;
try testing.expectEqual(updated, t.colors.background.get().?);
}
test "OSC 12 set and reset cursor color" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set cursor to blue
s.nextSlice("\x1b]12;rgb:00/00/ff\x1b\\");
const cursor = t.colors.cursor.get().?;
try testing.expectEqual(@as(u8, 0x00), cursor.r);
try testing.expectEqual(@as(u8, 0x00), cursor.g);
try testing.expectEqual(@as(u8, 0xff), cursor.b);
// Reset cursor
s.nextSlice("\x1b]112\x1b\\");
// After reset, cursor might be null (using default)
}
test "OSC color query responses" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
const S = struct {
var last_response: ?[:0]const u8 = null;
fn reset() void {
if (last_response) |old| testing.allocator.free(old);
last_response = null;
}
fn writePty(_: *Handler, data: []const u8) void {
reset();
last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM");
}
};
S.last_response = null;
defer S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1b]10;?\x1b\\");
try testing.expect(S.last_response == null);
s.nextSlice("\x1b]11;?\x1b\\");
try testing.expect(S.last_response == null);
s.nextSlice("\x1b]4;2;rgb:12/34/56;2;?\x1b\\");
try testing.expectEqualStrings(
"\x1b]4;2;rgb:1212/3434/5656\x1b\\",
S.last_response.?,
);
s.nextSlice("\x1b]10;rgb:01/02/03\x1b\\");
s.nextSlice("\x1b]11;rgb:04/05/06\x1b\\");
s.nextSlice("\x1b]12;rgb:07/08/09\x1b\\");
s.nextSlice("\x1b]10;?;?;?\x1b\\");
try testing.expectEqualStrings(
"\x1b]10;rgb:0101/0202/0303\x1b\\" ++
"\x1b]11;rgb:0404/0505/0606\x1b\\" ++
"\x1b]12;rgb:0707/0808/0909\x1b\\",
S.last_response.?,
);
s.nextSlice("\x1b]112\x1b\\");
s.nextSlice("\x1b]12;?\x07");
try testing.expectEqualStrings(
"\x1b]12;rgb:0101/0202/0303\x07",
S.last_response.?,
);
}
test "kitty color protocol set palette" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set palette color 5 to magenta using kitty protocol
s.nextSlice("\x1b]21;5=rgb:ff/00/ff\x1b\\");
try testing.expectEqual(@as(u8, 0xff), t.colors.palette.current[5].r);
try testing.expectEqual(@as(u8, 0x00), t.colors.palette.current[5].g);
try testing.expectEqual(@as(u8, 0xff), t.colors.palette.current[5].b);
try testing.expect(t.colors.palette.mask.isSet(5));
try testing.expect(t.flags.dirty.palette);
}
test "kitty color protocol reset palette" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set and then reset palette color
const original = t.colors.palette.original[7];
s.nextSlice("\x1b]21;7=rgb:aa/bb/cc\x1b\\");
try testing.expect(t.colors.palette.mask.isSet(7));
s.nextSlice("\x1b]21;7=\x1b\\");
try testing.expectEqual(original, t.colors.palette.current[7]);
try testing.expect(!t.colors.palette.mask.isSet(7));
}
test "kitty color protocol set foreground" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set foreground using kitty protocol
s.nextSlice("\x1b]21;foreground=rgb:12/34/56\x1b\\");
const fg = t.colors.foreground.get().?;
try testing.expectEqual(@as(u8, 0x12), fg.r);
try testing.expectEqual(@as(u8, 0x34), fg.g);
try testing.expectEqual(@as(u8, 0x56), fg.b);
}
test "kitty color protocol set background" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set background using kitty protocol
s.nextSlice("\x1b]21;background=rgb:78/9a/bc\x1b\\");
const bg = t.colors.background.get().?;
try testing.expectEqual(@as(u8, 0x78), bg.r);
try testing.expectEqual(@as(u8, 0x9a), bg.g);
try testing.expectEqual(@as(u8, 0xbc), bg.b);
}
test "kitty color protocol set cursor" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set cursor using kitty protocol
s.nextSlice("\x1b]21;cursor=rgb:de/f0/12\x1b\\");
const cursor = t.colors.cursor.get().?;
try testing.expectEqual(@as(u8, 0xde), cursor.r);
try testing.expectEqual(@as(u8, 0xf0), cursor.g);
try testing.expectEqual(@as(u8, 0x12), cursor.b);
}
test "kitty color protocol reset foreground" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set and reset foreground
s.nextSlice("\x1b]21;foreground=rgb:11/22/33\x1b\\");
try testing.expect(t.colors.foreground.get() != null);
s.nextSlice("\x1b]21;foreground=\x1b\\");
// After reset, should be unset
try testing.expect(t.colors.foreground.get() == null);
}
test "kitty color protocol query responses" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
const S = struct {
var last_response: ?[:0]const u8 = null;
fn reset() void {
if (last_response) |old| testing.allocator.free(old);
last_response = null;
}
fn writePty(_: *Handler, data: []const u8) void {
reset();
last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM");
}
};
S.last_response = null;
defer S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1b]21;background=?\x1b\\");
try testing.expectEqualStrings(
"\x1b]21;background=\x1b\\",
S.last_response.?,
);
s.nextSlice("\x1b]21;foreground=rgb:12/34/56;2=rgb:aa/bb/cc\x1b\\");
s.nextSlice("\x1b]21;foreground=?;background=?;2=?\x1b\\");
try testing.expectEqualStrings(
"\x1b]21;foreground=rgb:12/34/56;background=;2=rgb:aa/bb/cc\x1b\\",
S.last_response.?,
);
}
test "palette dirty flag set on color change" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Clear dirty flag
t.flags.dirty.palette = false;
// Setting palette color should set dirty flag
s.nextSlice("\x1b]4;0;rgb:ff/00/00\x1b\\");
try testing.expect(t.flags.dirty.palette);
// Clear and test reset
t.flags.dirty.palette = false;
s.nextSlice("\x1b]104;0\x1b\\");
try testing.expect(t.flags.dirty.palette);
// Clear and test kitty protocol
t.flags.dirty.palette = false;
s.nextSlice("\x1b]21;1=rgb:00/ff/00\x1b\\");
try testing.expect(t.flags.dirty.palette);
}
test "semantic prompt fresh line" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("Hello");
s.nextSlice("\x1b]133;L\x07");
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 1), t.screens.active.cursor.y);
}
test "semantic prompt fresh line new prompt" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Write some text and then send OSC 133;A (fresh_line_new_prompt)
s.nextSlice("Hello");
s.nextSlice("\x1b]133;A\x07");
// Should do a fresh line (carriage return + index)
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 1), t.screens.active.cursor.y);
// Should set cursor semantic_content to prompt
try testing.expectEqual(.prompt, t.screens.active.cursor.semantic_content);
// Test with redraw option
s.nextSlice("prompt$ ");
s.nextSlice("\x1b]133;A;redraw=1\x07");
try testing.expect(t.flags.shell_redraws_prompt == .true);
}
test "semantic prompt end of input, then start output" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Write some text and then send OSC 133;A (fresh_line_new_prompt)
s.nextSlice("Hello");
s.nextSlice("\x1b]133;A\x07");
s.nextSlice("prompt$ ");
s.nextSlice("\x1b]133;B\x07");
try testing.expectEqual(.input, t.screens.active.cursor.semantic_content);
s.nextSlice("\x1b]133;C\x07");
try testing.expectEqual(.output, t.screens.active.cursor.semantic_content);
}
test "semantic prompt prompt_start" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Write some text
s.nextSlice("Hello");
// OSC 133;P marks the start of a prompt (without fresh line behavior)
s.nextSlice("\x1b]133;P\x07");
try testing.expectEqual(.prompt, t.screens.active.cursor.semantic_content);
try testing.expectEqual(@as(usize, 5), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
}
test "semantic prompt new_command" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Write some text
s.nextSlice("Hello");
s.nextSlice("\x1b]133;N\x07");
// Should behave like fresh_line_new_prompt - cursor moves to column 0
// on next line since we had content
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 1), t.screens.active.cursor.y);
try testing.expectEqual(.prompt, t.screens.active.cursor.semantic_content);
}
test "semantic prompt new_command at column zero" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// OSC 133;N when already at column 0 should stay on same line
s.nextSlice("\x1b]133;N\x07");
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x);
try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y);
try testing.expectEqual(.prompt, t.screens.active.cursor.semantic_content);
}
test "semantic prompt end_prompt_start_input_terminate_eol clears on linefeed" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Set input terminated by EOL
s.nextSlice("\x1b]133;I\x07");
try testing.expectEqual(.input, t.screens.active.cursor.semantic_content);
// Linefeed should reset semantic content to output
s.nextSlice("\n");
try testing.expectEqual(.output, t.screens.active.cursor.semantic_content);
}
test "bell effect callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
// Test bell with null callback (default readonly effects) doesn't crash
{
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("\x07");
// Terminal should still be functional after bell
s.nextSlice("AfterBell");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("AfterBell", str);
}
t.fullReset();
// Test bell with a callback
{
const S = struct {
var bell_count: usize = 0;
fn bell(_: *Handler) void {
bell_count += 1;
}
};
S.bell_count = 0;
var handler: Handler = .init(&t);
handler.effects.bell = &S.bell;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x07");
try testing.expectEqual(@as(usize, 1), S.bell_count);
s.nextSlice("\x07\x07");
try testing.expectEqual(@as(usize, 3), S.bell_count);
}
}
test "desktop_notification effect callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
// A null callback (the default readonly effects) silently ignores
// notifications and leaves the terminal usable.
{
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("\x1B]9;Ignored\x1B\\AfterNotification");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("AfterNotification", str);
}
t.fullReset();
const S = struct {
var count: usize = 0;
var last_title: []const u8 = "";
var last_body: []const u8 = "";
fn desktopNotification(
_: *Handler,
notification: Action.ShowDesktopNotification,
) void {
count += 1;
last_title = notification.title;
last_body = notification.body;
}
};
S.count = 0;
S.last_title = "";
S.last_body = "";
var handler: Handler = .init(&t);
handler.effects.desktop_notification = &S.desktopNotification;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// OSC 9 is split across writes and carries only a body.
s.nextSlice("\x1B]9;Build ");
try testing.expectEqual(@as(usize, 0), S.count);
s.nextSlice("complete\x1B\\");
try testing.expectEqual(@as(usize, 1), S.count);
try testing.expectEqualStrings("", S.last_title);
try testing.expectEqualStrings("Build complete", S.last_body);
// OSC 777 preserves its separate title and body fields.
s.nextSlice("\x1B]777;notify;Codex;Needs attention\x07");
try testing.expectEqual(@as(usize, 2), S.count);
try testing.expectEqualStrings("Codex", S.last_title);
try testing.expectEqualStrings("Needs attention", S.last_body);
}
test "progress_report effect callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
// A null callback (the default readonly effects) silently ignores reports.
{
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("\x1B]9;4;1;25\x1B\\");
}
const S = struct {
var count: usize = 0;
var last_state: osc.Command.ProgressReport.State = .remove;
var last_progress: ?u8 = null;
fn progressReport(_: *Handler, report: osc.Command.ProgressReport) void {
count += 1;
last_state = report.state;
last_progress = report.progress;
}
};
S.count = 0;
S.last_state = .remove;
S.last_progress = null;
var handler: Handler = .init(&t);
handler.effects.progress_report = &S.progressReport;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
const cases = [_]struct {
sequence: []const u8,
state: osc.Command.ProgressReport.State,
progress: ?u8,
}{
.{ .sequence = "\x1B]9;4;0;\x1B\\", .state = .remove, .progress = null },
.{ .sequence = "\x1B]9;4;1;42\x07", .state = .set, .progress = 42 },
.{ .sequence = "\x1B]9;4;2;7\x1B\\", .state = .@"error", .progress = 7 },
.{ .sequence = "\x1B]9;4;3\x1B\\", .state = .indeterminate, .progress = null },
.{ .sequence = "\x1B]9;4;4;75\x1B\\", .state = .pause, .progress = 75 },
};
for (cases, 1..) |case, expected_count| {
// Split each sequence to verify parsing survives PTY read boundaries.
const midpoint = case.sequence.len / 2;
s.nextSlice(case.sequence[0..midpoint]);
try testing.expectEqual(expected_count - 1, S.count);
s.nextSlice(case.sequence[midpoint..]);
try testing.expectEqual(expected_count, S.count);
try testing.expectEqual(case.state, S.last_state);
try testing.expectEqual(case.progress, S.last_progress);
}
// A full reset (RIS) removes any active progress bar.
s.nextSlice("\x1B]9;4;1;50\x1B\\");
try testing.expectEqual(@as(usize, cases.len + 1), S.count);
try testing.expectEqual(osc.Command.ProgressReport.State.set, S.last_state);
s.nextSlice("\x1Bc");
try testing.expectEqual(@as(usize, cases.len + 2), S.count);
try testing.expectEqual(osc.Command.ProgressReport.State.remove, S.last_state);
try testing.expectEqual(@as(?u8, null), S.last_progress);
}
test "clipboard_write effect callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
// A null callback (the default readonly effects) silently ignores writes.
{
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("\x1B]52;c;aGVsbG8=\x1B\\");
// Terminal should still be functional after the ignored sequence
s.nextSlice("AfterClipboard");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("AfterClipboard", str);
}
t.fullReset();
const S = struct {
var count: usize = 0;
var result: clipboard.Write.Result = .{ .success = .{} };
var last_location: clipboard.Location = .standard;
var last_contents_len: usize = 0;
var last_mime: ?[]u8 = null;
var last_data: ?[]u8 = null;
fn clearCapture() void {
if (last_mime) |value| testing.allocator.free(value);
if (last_data) |value| testing.allocator.free(value);
last_mime = null;
last_data = null;
last_contents_len = 0;
}
fn clipboardWrite(_: *Handler, write: clipboard.Write) void {
clearCapture();
count += 1;
last_location = write.location;
last_contents_len = write.contents.len;
if (write.contents.len > 0) {
last_mime = testing.allocator.dupe(u8, write.contents[0].mime) catch
@panic("failed to capture clipboard MIME type");
last_data = testing.allocator.dupe(u8, write.contents[0].data) catch
@panic("failed to capture clipboard data");
}
write.reply(result);
}
};
S.count = 0;
S.result = .denied;
S.clearCapture();
defer S.clearCapture();
var handler: Handler = .init(&t);
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Selectors are normalized and payloads are decoded before the callback.
const cases = [_]struct {
sequence: []const u8,
location: clipboard.Location,
data: []const u8,
}{
.{ .sequence = "\x1B]52;c;aGVsbG8=\x1B\\", .location = .standard, .data = "hello" },
.{ .sequence = "\x1B]52;s;d29ybGQ=\x07", .location = .selection, .data = "world" },
.{ .sequence = "\x1B]52;p;cHJpbWFyeQ==\x1B\\", .location = .primary, .data = "primary" },
.{ .sequence = "\x1B]52;0;Y3V0\x1B\\", .location = .standard, .data = "cut" },
.{ .sequence = "\x1B]52;x;ZmFsbGJhY2s=\x1B\\", .location = .standard, .data = "fallback" },
.{ .sequence = "\x1B]52;c;YQBi\x1B\\", .location = .standard, .data = "a\x00b" },
// Missing padding is tolerated for OSC 52 since it has no way
// to report errors to the client, matching kitty.
.{ .sequence = "\x1B]52;c;dW5wYWRkZWQ\x1B\\", .location = .standard, .data = "unpadded" },
};
for (cases, 1..) |case, expected_count| {
s.nextSlice(case.sequence);
try testing.expectEqual(expected_count, S.count);
try testing.expectEqual(case.location, S.last_location);
try testing.expectEqual(@as(usize, 1), S.last_contents_len);
try testing.expectEqualStrings("text/plain", S.last_mime.?);
try testing.expectEqualSlices(u8, case.data, S.last_data.?);
}
// Empty data is a clear, represented by an empty contents slice.
s.nextSlice("\x1B]52;s;\x1B\\");
try testing.expectEqual(@as(usize, cases.len + 1), S.count);
try testing.expectEqual(clipboard.Location.selection, S.last_location);
try testing.expectEqual(@as(usize, 0), S.last_contents_len);
try testing.expect(S.last_mime == null);
try testing.expect(S.last_data == null);
// Reads and malformed base64 are ignored. The whole request is
// discarded on invalid characters (including whitespace) rather
// than decoding around them, per the Kitty clipboard spec that
// governs OSC 52 base64 handling.
s.nextSlice("\x1B]52;c;?\x1B\\");
s.nextSlice("\x1B]52;c;***\x1B\\");
s.nextSlice("\x1B]52;c;SGVs!!!bG8=\x1B\\");
s.nextSlice("\x1B]52;c;aGVs bG8=\x1B\\");
try testing.expectEqual(@as(usize, cases.len + 1), S.count);
// OSC 1337 Copy shares the normalized clipboard write path.
s.nextSlice("\x1B]1337;Copy=:aVRlcm0y\x1B\\");
try testing.expectEqual(@as(usize, cases.len + 2), S.count);
try testing.expectEqual(clipboard.Location.standard, S.last_location);
try testing.expectEqualStrings("text/plain", S.last_mime.?);
try testing.expectEqualStrings("iTerm2", S.last_data.?);
// Parsing across write boundaries still invokes exactly one atomic write.
s.nextSlice("\x1B]52;p;ZnJh");
s.nextSlice("Z21lbnRlZA==\x1B");
s.nextSlice("\\");
try testing.expectEqual(@as(usize, cases.len + 3), S.count);
try testing.expectEqual(clipboard.Location.primary, S.last_location);
try testing.expectEqualStrings("text/plain", S.last_mime.?);
try testing.expectEqualStrings("fragmented", S.last_data.?);
// Reply results are intentionally ignored for protocols without a
// write acknowledgement. The denied reply above did not stop later writes.
try testing.expect(S.result == .denied);
}
test "clipboard_read effect callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: std.ArrayList(u8) = .empty;
var count: usize = 0;
var last_location: clipboard.Location = .standard;
var last_mimes: []const []const u8 = &.{};
var last_list: bool = true;
var last_name: []const u8 = "unset";
var last_granted: bool = true;
var last_can_remember: bool = true;
var result: ?clipboard.Read.Result = .{ .success = .{ .contents = &.{.{
.mime = "text/plain",
.data = "hello",
}} } };
var reply_twice: bool = false;
fn writePty(_: *Handler, data: []const u8) void {
written.appendSlice(testing.allocator, data) catch @panic("OOM");
}
fn clipboardRead(_: *Handler, read: clipboard.Read) void {
count += 1;
last_location = read.location;
last_mimes = read.mimes;
last_list = read.list;
last_name = read.name;
last_granted = read.granted;
last_can_remember = read.can_remember;
if (result) |r| read.reply(r);
if (reply_twice) read.reply(.{ .success = .{ .contents = &.{.{
.mime = "text/plain",
.data = "again",
}} } });
}
};
defer S.written.deinit(testing.allocator);
// A null callback (the default readonly effects) silently ignores reads.
{
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B]52;c;?\x1B\\");
try testing.expectEqual(0, S.written.items.len);
}
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Success echoes the normalized selector and request terminator.
s.nextSlice("\x1B]52;c;?\x1B\\");
try testing.expectEqual(1, S.count);
try testing.expectEqual(clipboard.Location.standard, S.last_location);
try testing.expectEqual(1, S.last_mimes.len);
try testing.expectEqualStrings("text/plain", S.last_mimes[0]);
try testing.expect(!S.last_list);
try testing.expectEqualStrings("", S.last_name);
try testing.expect(!S.last_granted);
try testing.expect(!S.last_can_remember);
try testing.expectEqualStrings("\x1B]52;c;aGVsbG8=\x1B\\", S.written.items);
S.written.clearRetainingCapacity();
s.nextSlice("\x1B]52;p;?\x07");
try testing.expectEqual(clipboard.Location.primary, S.last_location);
try testing.expectEqualStrings("\x1B]52;p;aGVsbG8=\x07", S.written.items);
// Only the first text representation is used.
S.written.clearRetainingCapacity();
S.result = .{
.success = .{
.contents = &.{
.{ .mime = "image/png", .data = "\x89PNG" },
.{ .mime = "UTF8_STRING", .data = "hi" },
},
// OSC 52 has no session passwords, so remember is ignored.
.remember = true,
},
};
s.nextSlice("\x1B]52;s;?\x1B\\");
try testing.expectEqual(clipboard.Location.selection, S.last_location);
try testing.expectEqualStrings("\x1B]52;s;aGk=\x1B\\", S.written.items);
// Every failure, no text, and no reply all answer with an empty
// clipboard.
for ([_]?clipboard.Read.Result{
.denied,
.unsupported,
.busy,
.io_error,
.{ .success = .{} },
null,
}) |result| {
S.written.clearRetainingCapacity();
S.result = result;
s.nextSlice("\x1B]52;c;?\x1B\\");
try testing.expectEqualStrings("\x1B]52;c;\x1B\\", S.written.items);
}
// A second reply is ignored.
S.written.clearRetainingCapacity();
S.result = .{ .success = .{ .contents = &.{.{ .mime = "text/plain", .data = "hello" }} } };
S.reply_twice = true;
s.nextSlice("\x1B]52;c;?\x1B\\");
try testing.expectEqualStrings("\x1B]52;c;aGVsbG8=\x1B\\", S.written.items);
}
test "clipboard_write allocation failure is ignored" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var count: usize = 0;
fn clipboardWrite(_: *Handler, write: clipboard.Write) void {
count += 1;
write.reply(.{ .success = .{} });
}
};
S.count = 0;
var handler: Handler = .init(&t);
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Only the decoded scratch data uses the terminal allocator here. Swap in
// an allocator that always fails, then restore it before terminal teardown.
{
const alloc = t.screens.active.alloc;
t.screens.active.alloc = testing.failing_allocator;
defer t.screens.active.alloc = alloc;
s.nextSlice("\x1B]52;c;aGVsbG8=\x1B\\");
}
try testing.expectEqual(@as(usize, 0), S.count);
try testing.expect(!s.handler.semantic_failure);
}
/// Shared capture state for the Kitty clipboard (OSC 5522) tests below:
/// records every pty response and the most recent clipboard write.
const KittyClipboardCapture = struct {
var responses: [1024]u8 = undefined;
var responses_len: usize = 0;
// Write capture. A null write_result returns without replying.
var write_count: usize = 0;
var write_result: ?clipboard.Write.Result = .{ .success = .{} };
var write_reply_twice: bool = false;
var last_location: clipboard.Location = .standard;
var last_contents_len: usize = 0;
var last_mimes: [8][64]u8 = undefined;
var last_mime_lens: [8]usize = @splat(0);
var last_data: [8][256]u8 = undefined;
var last_data_lens: [8]usize = @splat(0);
var last_write_name: [64]u8 = undefined;
var last_write_name_len: usize = 0;
var last_write_granted: bool = false;
var last_write_can_remember: bool = false;
// Read capture. A null read_result returns without replying.
var read_count: usize = 0;
var read_result: ?clipboard.Read.Result = null;
var read_reply_twice: bool = false;
var last_read_location: clipboard.Location = .standard;
var last_read_mimes: [8][64]u8 = undefined;
var last_read_mime_lens: [8]usize = @splat(0);
var last_read_mimes_len: usize = 0;
var last_read_list: bool = false;
var last_read_name: [64]u8 = undefined;
var last_read_name_len: usize = 0;
var last_read_granted: bool = false;
var last_read_can_remember: bool = false;
fn reset() void {
responses_len = 0;
write_count = 0;
write_result = .{ .success = .{} };
write_reply_twice = false;
last_location = .standard;
last_contents_len = 0;
last_mime_lens = @splat(0);
last_data_lens = @splat(0);
last_write_name_len = 0;
last_write_granted = false;
last_write_can_remember = false;
read_count = 0;
read_result = null;
read_reply_twice = false;
last_read_location = .standard;
last_read_mime_lens = @splat(0);
last_read_mimes_len = 0;
last_read_list = false;
last_read_name_len = 0;
last_read_granted = false;
last_read_can_remember = false;
}
fn writePty(_: *Handler, data: []const u8) void {
@memcpy(responses[responses_len..][0..data.len], data);
responses_len += data.len;
}
fn clipboardWrite(_: *Handler, write: clipboard.Write) void {
write_count += 1;
last_location = write.location;
last_contents_len = write.contents.len;
for (write.contents[0..@min(write.contents.len, last_mimes.len)], 0..) |content, i| {
last_mime_lens[i] = content.mime.len;
@memcpy(last_mimes[i][0..content.mime.len], content.mime);
last_data_lens[i] = content.data.len;
@memcpy(last_data[i][0..content.data.len], content.data);
}
last_write_name_len = write.name.len;
@memcpy(last_write_name[0..write.name.len], write.name);
last_write_granted = write.granted;
last_write_can_remember = write.can_remember;
if (write_result) |r| write.reply(r);
if (write_reply_twice) write.reply(.io_error);
}
fn clipboardRead(_: *Handler, read: clipboard.Read) void {
read_count += 1;
last_read_location = read.location;
last_read_mimes_len = read.mimes.len;
for (read.mimes[0..@min(read.mimes.len, last_read_mimes.len)], 0..) |mime, i| {
last_read_mime_lens[i] = mime.len;
@memcpy(last_read_mimes[i][0..mime.len], mime);
}
last_read_list = read.list;
last_read_name_len = read.name.len;
@memcpy(last_read_name[0..read.name.len], read.name);
last_read_granted = read.granted;
last_read_can_remember = read.can_remember;
if (read_result) |r| read.reply(r);
if (read_reply_twice) read.reply(.denied);
}
fn responseSlice() []const u8 {
return responses[0..responses_len];
}
fn readMimeAt(i: usize) []const u8 {
return last_read_mimes[i][0..last_read_mime_lens[i]];
}
fn readName() []const u8 {
return last_read_name[0..last_read_name_len];
}
fn writeName() []const u8 {
return last_write_name[0..last_write_name_len];
}
fn mimeAt(i: usize) []const u8 {
return last_mimes[i][0..last_mime_lens[i]];
}
fn dataAt(i: usize) []const u8 {
return last_data[i][0..last_data_lens[i]];
}
};
test "kitty clipboard write transaction round trip" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Begin a write, stream two MIME types (one chunked), alias the
// plain text, and commit. Only the commit produces a response.
s.nextSlice("\x1B]5522;type=write:id=42\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); // "Ghost"
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;dHk=\x1B\\"); // "ty"
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9odG1s;PGI+aGk8L2I+\x1B\\"); // "<b>hi</b>"
// Alias "TEXT UTF8_STRING" -> text/plain.
s.nextSlice("\x1B]5522;type=walias:mime=dGV4dC9wbGFpbg==;VEVYVCBVVEY4X1NUUklORw==\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqual(@as(usize, 0), S.responses_len);
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqual(clipboard.Location.standard, S.last_location);
try testing.expectEqual(@as(usize, 4), S.last_contents_len);
try testing.expectEqualStrings("text/plain", S.mimeAt(0));
try testing.expectEqualStrings("Ghostty", S.dataAt(0));
try testing.expectEqualStrings("text/html", S.mimeAt(1));
try testing.expectEqualStrings("<b>hi</b>", S.dataAt(1));
try testing.expectEqualStrings("TEXT", S.mimeAt(2));
try testing.expectEqualStrings("Ghostty", S.dataAt(2));
try testing.expectEqualStrings("UTF8_STRING", S.mimeAt(3));
try testing.expectEqualStrings("Ghostty", S.dataAt(3));
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=DONE:id=42\x1B\\",
S.responseSlice(),
);
// A commit with no transaction in flight is silently ignored.
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
}
test "kitty clipboard write result maps to response status" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
const cases = [_]struct {
result: ?clipboard.Write.Result,
response: []const u8,
}{
.{ .result = .{ .success = .{} }, .response = "\x1B]5522;type=write:status=DONE\x1B\\" },
.{ .result = .denied, .response = "\x1B]5522;type=write:status=EPERM\x1B\\" },
.{ .result = .unsupported, .response = "\x1B]5522;type=write:status=ENOSYS\x1B\\" },
.{ .result = .busy, .response = "\x1B]5522;type=write:status=EBUSY\x1B\\" },
.{ .result = .invalid_data, .response = "\x1B]5522;type=write:status=EINVAL\x1B\\" },
.{ .result = .io_error, .response = "\x1B]5522;type=write:status=EIO\x1B\\" },
// No reply at all is a denial rather than silence.
.{ .result = null, .response = "\x1B]5522;type=write:status=EPERM\x1B\\" },
};
for (cases) |case| {
S.reset();
S.write_result = case.result;
// An immediately-committed write with no data is a clear.
s.nextSlice("\x1B]5522;type=write\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqual(@as(usize, 0), S.last_contents_len);
try testing.expectEqualStrings(case.response, S.responseSlice());
}
// A second reply is ignored.
S.reset();
S.write_reply_twice = true;
s.nextSlice("\x1B]5522;type=write\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=DONE\x1B\\",
S.responseSlice(),
);
// The response echoes the request terminator, unlike kitty which
// always uses ST.
S.reset();
s.nextSlice("\x1B]5522;type=write:loc=primary\x07");
s.nextSlice("\x1B]5522;type=wdata\x07");
try testing.expectEqual(clipboard.Location.primary, S.last_location);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=DONE\x07",
S.responseSlice(),
);
// A system without a primary selection answers a loc=primary write
// with ENOSYS, echoing the id.
S.reset();
S.write_result = .unsupported;
s.nextSlice("\x1B]5522;type=write:loc=primary:id=p1\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(clipboard.Location.primary, S.last_location);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=ENOSYS:id=p1\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard write without clipboard effect responds ENOSYS" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// The transaction fails as soon as it begins; the rest of it is
// ignored without further responses.
s.nextSlice("\x1B]5522;type=write:id=x\x1B\\");
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=ENOSYS:id=x\x1B\\",
S.responseSlice(),
);
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=ENOSYS:id=x\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard read without effect is denied with EPERM" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// The denial never includes loc (only OK responses do) and echoes
// the sanitized id.
s.nextSlice("\x1B]5522;type=read:loc=primary:id=*4 2*;dGV4dC9wbGFpbg==\x1B\\");
try testing.expectEqualStrings(
"\x1B]5522;type=read:status=EPERM:id=42\x1B\\",
S.responseSlice(),
);
// A missing payload is an empty MIME list, still answered.
S.reset();
s.nextSlice("\x1B]5522;type=read\x07");
try testing.expectEqualStrings(
"\x1B]5522;type=read:status=EPERM\x07",
S.responseSlice(),
);
// An undecodable payload is dropped without a response.
S.reset();
s.nextSlice("\x1B]5522;type=read;!!!\x1B\\");
try testing.expectEqual(@as(usize, 0), S.responses_len);
}
test "kitty clipboard read round trip" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
S.read_result = .{
.success = .{
.contents = &.{
// Unrequested representations are never served, and the
// served ones follow request order, not reply order.
.{ .mime = "image/png", .data = "\x89PNG" },
.{ .mime = "text/html", .data = "<b>hi</b>" },
.{ .mime = "text/plain", .data = "Ghostty" },
},
.available = &.{ "text/plain", "text/html" },
},
};
// Request the targets listing plus two types from the primary
// selection: ". text/plain text/html".
s.nextSlice("\x1B]5522;type=read:loc=primary:id=r1;LiB0ZXh0L3BsYWluIHRleHQvaHRtbA==\x1B\\");
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expectEqual(clipboard.Location.primary, S.last_read_location);
try testing.expectEqual(@as(usize, 2), S.last_read_mimes_len);
try testing.expectEqualStrings("text/plain", S.readMimeAt(0));
try testing.expectEqualStrings("text/html", S.readMimeAt(1));
try testing.expect(S.last_read_list);
try testing.expectEqualStrings("", S.readName());
try testing.expect(!S.last_read_granted);
try testing.expect(!S.last_read_can_remember);
try testing.expectEqualStrings(
"\x1B]5522;type=read:status=OK:loc=primary:id=r1\x1B\\" ++
"\x1B]5522;type=read:status=DATA:id=r1:mime=Lg==;dGV4dC9wbGFpbiB0ZXh0L2h0bWwK\x1B\\" ++
"\x1B]5522;type=read:status=DATA:id=r1:mime=dGV4dC9wbGFpbg==;R2hvc3R0eQ==\x1B\\" ++
"\x1B]5522;type=read:status=DATA:id=r1:mime=dGV4dC9odG1s;PGI+aGk8L2I+\x1B\\" ++
"\x1B]5522;type=read:status=DONE:id=r1\x1B\\",
S.responseSlice(),
);
// Without the listing request `available` is ignored. The response
// echoes the request terminator.
S.responses_len = 0;
s.nextSlice("\x1B]5522;type=read:id=r2;dGV4dC9wbGFpbg==\x07");
try testing.expectEqual(clipboard.Location.standard, S.last_read_location);
try testing.expectEqual(@as(usize, 1), S.last_read_mimes_len);
try testing.expect(!S.last_read_list);
try testing.expectEqualStrings(
"\x1B]5522;type=read:status=OK:id=r2\x07" ++
"\x1B]5522;type=read:status=DATA:id=r2:mime=dGV4dC9wbGFpbg==;R2hvc3R0eQ==\x07" ++
"\x1B]5522;type=read:status=DONE:id=r2\x07",
S.responseSlice(),
);
// A listing-only request carries no types.
S.responses_len = 0;
s.nextSlice("\x1B]5522;type=read;Lg==\x1B\\");
try testing.expectEqual(@as(usize, 0), S.last_read_mimes_len);
try testing.expect(S.last_read_list);
try testing.expectEqualStrings(
"\x1B]5522;type=read:status=OK\x1B\\" ++
"\x1B]5522;type=read:status=DATA:mime=Lg==;dGV4dC9wbGFpbiB0ZXh0L2h0bWwK\x1B\\" ++
"\x1B]5522;type=read:status=DONE\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard read result maps to response status" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
const cases = [_]struct {
result: ?clipboard.Read.Result,
response: []const u8,
}{
.{ .result = .denied, .response = "\x1B]5522;type=read:status=EPERM:id=x\x1B\\" },
.{ .result = .unsupported, .response = "\x1B]5522;type=read:status=ENOSYS:id=x\x1B\\" },
.{ .result = .busy, .response = "\x1B]5522;type=read:status=EBUSY:id=x\x1B\\" },
.{ .result = .io_error, .response = "\x1B]5522;type=read:status=EIO:id=x\x1B\\" },
// No reply at all is a denial rather than silence.
.{ .result = null, .response = "\x1B]5522;type=read:status=EPERM:id=x\x1B\\" },
// A success with nothing to serve is still OK then DONE.
.{ .result = .{ .success = .{} }, .response = "\x1B]5522;type=read:status=OK:id=x\x1B\\" ++
"\x1B]5522;type=read:status=DONE:id=x\x1B\\" },
};
for (cases) |case| {
S.reset();
S.read_result = case.result;
s.nextSlice("\x1B]5522;type=read:id=x;dGV4dC9wbGFpbg==\x1B\\");
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expectEqualStrings(case.response, S.responseSlice());
}
// A second reply is ignored.
S.reset();
S.read_result = .{ .success = .{ .contents = &.{.{ .mime = "text/plain", .data = "hello" }} } };
S.read_reply_twice = true;
s.nextSlice("\x1B]5522;type=read;dGV4dC9wbGFpbg==\x1B\\");
try testing.expectEqualStrings(
"\x1B]5522;type=read:status=OK\x1B\\" ++
"\x1B]5522;type=read:status=DATA:mime=dGV4dC9wbGFpbg==;aGVsbG8=\x1B\\" ++
"\x1B]5522;type=read:status=DONE\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard read caps requested types" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// "a/0 a/1 a/2 a/3 a/4 a/5 .": extras are dropped but the listing
// request after them still counts.
S.read_result = .{ .success = .{} };
s.nextSlice("\x1B]5522;type=read;YS8wIGEvMSBhLzIgYS8zIGEvNCBhLzUgLg==\x1B\\");
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expectEqual(kitty_clipboard.max_read_mimes, S.last_read_mimes_len);
try testing.expectEqualStrings("a/0", S.readMimeAt(0));
try testing.expectEqualStrings("a/3", S.readMimeAt(kitty_clipboard.max_read_mimes - 1));
try testing.expect(S.last_read_list);
}
test "kitty clipboard read password grants" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Every read requests a data type ("text/plain"): a request with
// no data types never consults the grants at all.
//
// pw="secret", name="app": the first request isn't granted but the
// reply may ask to remember it.
S.read_result = .{ .success = .{ .remember = true } };
s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0:name=YXBw;dGV4dC9wbGFpbg==\x1B\\");
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expectEqualStrings("app", S.readName());
try testing.expect(!S.last_read_granted);
try testing.expect(S.last_read_can_remember);
// The same password is now granted; a different one is not.
S.read_result = .{ .success = .{} };
s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0:name=YXBw;dGV4dC9wbGFpbg==\x1B\\");
try testing.expect(S.last_read_granted);
s.nextSlice("\x1B]5522;type=read:pw=b3RoZXI=:name=YXBw;dGV4dC9wbGFpbg==\x1B\\");
try testing.expect(!S.last_read_granted);
try testing.expect(S.last_read_can_remember);
// A password without a name doesn't count: it is neither granted
// nor rememberable, even if the reply asks.
S.read_result = .{ .success = .{ .remember = true } };
s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0;dGV4dC9wbGFpbg==\x1B\\");
try testing.expectEqualStrings("", S.readName());
try testing.expect(!S.last_read_granted);
try testing.expect(!S.last_read_can_remember);
s.nextSlice("\x1B]5522;type=read:pw=b3RoZXI=;dGV4dC9wbGFpbg==\x1B\\");
try testing.expect(!S.last_read_can_remember);
S.read_result = .{ .success = .{} };
s.nextSlice("\x1B]5522;type=read:pw=b3RoZXI=:name=YXBw;dGV4dC9wbGFpbg==\x1B\\");
try testing.expect(!S.last_read_granted);
// A grant is advisory: the request is still forwarded and the
// embedder may deny it.
S.responses_len = 0;
S.read_result = .denied;
s.nextSlice("\x1B]5522;type=read:id=d:pw=c2VjcmV0:name=YXBw;dGV4dC9wbGFpbg==\x1B\\");
try testing.expect(S.last_read_granted);
try testing.expectEqualStrings(
"\x1B]5522;type=read:status=EPERM:id=d\x1B\\",
S.responseSlice(),
);
// Grants are freed with the stream (the testing allocator catches
// the leak otherwise).
}
test "kitty clipboard read targets-only never consumes a one-time grant" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// A one-time read grant, as minted for a paste event.
try s.handler.kitty_clipboard_grants.grant(testing.allocator, "otp", .read, true);
// A targets-only read (payload ".") is prompt-exempt so it never
// consults, and must not burn, the one-time password.
S.read_result = .{ .success = .{} };
s.nextSlice("\x1B]5522;type=read:pw=b3Rw:name=YXBw;Lg==\x1B\\");
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expect(S.last_read_list);
try testing.expectEqual(@as(usize, 0), S.last_read_mimes_len);
try testing.expect(!S.last_read_granted);
// The follow-up data read still consumes the grant, exactly once.
s.nextSlice("\x1B]5522;type=read:pw=b3Rw:name=YXBw;dGV4dC9wbGFpbg==\x1B\\");
try testing.expect(S.last_read_granted);
s.nextSlice("\x1B]5522;type=read:pw=b3Rw:name=YXBw;dGV4dC9wbGFpbg==\x1B\\");
try testing.expect(!S.last_read_granted);
}
test "kitty clipboard write password grants" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// pw="secret", name="app": the first commit isn't granted but the
// reply may ask to remember it.
S.write_result = .{ .success = .{ .remember = true } };
s.nextSlice("\x1B]5522;type=write:pw=c2VjcmV0:name=YXBw\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqualStrings("app", S.writeName());
try testing.expect(!S.last_write_granted);
try testing.expect(S.last_write_can_remember);
// The same password is now granted; a different one is not.
S.write_result = .{ .success = .{} };
s.nextSlice("\x1B]5522;type=write:pw=c2VjcmV0:name=YXBw\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expect(S.last_write_granted);
s.nextSlice("\x1B]5522;type=write:pw=b3RoZXI=:name=YXBw\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expect(!S.last_write_granted);
try testing.expect(S.last_write_can_remember);
// Directions are independent: a write grant doesn't satisfy reads.
S.read_result = .{ .success = .{} };
s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0:name=YXBw\x1B\\");
try testing.expect(!S.last_read_granted);
// A password without a name doesn't count: it is neither granted
// nor rememberable, even if the reply asks.
S.write_result = .{ .success = .{ .remember = true } };
s.nextSlice("\x1B]5522;type=write:pw=c2VjcmV0\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqualStrings("", S.writeName());
try testing.expect(!S.last_write_granted);
try testing.expect(!S.last_write_can_remember);
// A grant is advisory: the request is still forwarded and the
// embedder may deny it.
S.responses_len = 0;
S.write_result = .denied;
s.nextSlice("\x1B]5522;type=write:id=d:pw=c2VjcmV0:name=YXBw\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expect(S.last_write_granted);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EPERM:id=d\x1B\\",
S.responseSlice(),
);
// Grants are freed with the stream (the testing allocator catches
// the leak otherwise).
}
test "kitty clipboard malformed packets are silently dropped" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Missing type, unknown type, bare metadata record, invalid mime
// base64, and orphaned transaction packets all drop silently.
s.nextSlice("\x1B]5522;loc=primary\x1B\\");
s.nextSlice("\x1B]5522;type=bobr\x1B\\");
s.nextSlice("\x1B]5522;type=read:bare\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=!!!;R2hvc3Q=\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\");
s.nextSlice("\x1B]5522;type=walias:mime=dGV4dC9wbGFpbg==;VEVYVA==\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqual(@as(usize, 0), S.responses_len);
try testing.expect(!s.handler.semantic_failure);
// The terminal is still functional afterwards.
s.nextSlice("ok");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ok", str);
}
test "kitty clipboard new write replaces in-flight transaction" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B]5522;type=write:id=old\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;b2xk\x1B\\"); // "old"
s.nextSlice("\x1B]5522;type=write:id=new\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;bmV3\x1B\\"); // "new"
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqual(@as(usize, 1), S.last_contents_len);
try testing.expectEqualStrings("new", S.dataAt(0));
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=DONE:id=new\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard invalid write packets abort with EINVAL" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
const invalid_packets = [_][]const u8{
// Alias payload decodes to a non-UTF-8 byte.
"\x1B]5522;type=walias:mime=dGV4dC9wbGFpbg==;/w==\x1B\\",
// Alias has no target MIME type.
"\x1B]5522;type=walias;VEVYVA==\x1B\\",
// Alias target MIME decodes to non-UTF-8 bytes.
"\x1B]5522;type=walias:mime=//4=;VEVYVA==\x1B\\",
// Write data MIME decodes to non-UTF-8 bytes.
"\x1B]5522;type=wdata:mime=//4=;R2hvc3Q=\x1B\\",
};
for (invalid_packets) |packet| {
S.responses_len = 0;
s.nextSlice("\x1B]5522;type=write:id=w\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\");
s.nextSlice(packet);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EINVAL:id=w\x1B\\",
S.responseSlice(),
);
try testing.expect(!s.handler.semantic_failure);
// The transaction is gone: a commit does nothing further.
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EINVAL:id=w\x1B\\",
S.responseSlice(),
);
}
}
test "kitty clipboard invalid read text does not abort a write" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
S.read_result = .{ .success = .{} };
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B]5522;type=write:id=w\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\");
// The read payload decodes to a non-UTF-8 byte. It is dropped without
// invoking the clipboard effect or disturbing the write transaction.
s.nextSlice("\x1B]5522;type=read;/w==\x1B\\");
try testing.expectEqual(@as(usize, 0), S.read_count);
try testing.expectEqual(@as(usize, 0), S.responses_len);
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=DONE:id=w\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard oversized text write aborts with EFBIG" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Shrink the limit so the test doesn't have to stream the
// default 64MiB.
s.handler.kitty_clipboard_write_max_bytes = 4;
s.nextSlice("\x1B]5522;type=write:id=w\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;SGVsbA==\x1B\\"); // "Hell"
try testing.expectEqual(@as(usize, 0), S.responses_len);
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;bw==\x1B\\"); // "o"
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EFBIG:id=w\x1B\\",
S.responseSlice(),
);
try testing.expect(!s.handler.semantic_failure);
// The transaction is gone: later data and the commit do nothing.
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;IQ==\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EFBIG:id=w\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard invalid wdata chunk aborts with EINVAL" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B]5522;type=write:id=w\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;SGVsbG8=\x1B\\"); // "Hello"
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;!!!bad!!!\x1B\\");
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EINVAL:id=w\x1B\\",
S.responseSlice(),
);
try testing.expect(!s.handler.semantic_failure);
// The transaction is gone: later data and the commit do nothing.
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;V29ybGQ=\x1B\\"); // "World"
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EINVAL:id=w\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard wdata chunks split one base64 stream" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// "some data" encoded as one stream, split at non-group
// boundaries across packets.
s.nextSlice("\x1B]5522;type=write\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;c29\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;tZSBk\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;YXRh\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqualStrings("some data", S.dataAt(0));
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=DONE\x1B\\",
S.responseSlice(),
);
}
test "kitty clipboard unpadded wdata stream aborts at commit" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// "Hello" without its final padding byte: every packet decodes,
// but the stream ends mid-group so the commit reports EINVAL.
s.nextSlice("\x1B]5522;type=write:id=w\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;SGVsbG8\x1B\\");
try testing.expectEqual(@as(usize, 0), S.responses_len);
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqualStrings(
"\x1B]5522;type=write:status=EINVAL:id=w\x1B\\",
S.responseSlice(),
);
try testing.expect(!s.handler.semantic_failure);
}
test "kitty clipboard in-flight transaction is freed on deinit" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Never committed: stream deinit must free the transaction (the
// testing allocator catches the leak otherwise).
s.nextSlice("\x1B]5522;type=write\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
}
test "kitty clipboard allocation failure is ignored" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Only transaction state uses the terminal allocator here. Swap in
// an allocator that always fails, then restore it before teardown.
{
const alloc = t.screens.active.alloc;
t.screens.active.alloc = testing.failing_allocator;
defer t.screens.active.alloc = alloc;
s.nextSlice("\x1B]5522;type=write\x1B\\");
}
// Clipboard writes are external effects, best-effort like OSC 52;
// the failed transaction never started and is not a semantic
// failure.
try testing.expect(!s.handler.semantic_failure);
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqual(@as(usize, 0), S.responses_len);
}
test "kitty clipboard without write_pty still commits writes" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = KittyClipboardCapture;
S.reset();
var handler: Handler = .init(&t);
handler.effects.clipboard_write = &S.clipboardWrite;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B]5522;type=write\x1B\\");
s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\");
s.nextSlice("\x1B]5522;type=wdata\x1B\\");
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqualStrings("Ghost", S.dataAt(0));
// Reads are dropped without a way to respond.
s.nextSlice("\x1B]5522;type=read\x1B\\");
try testing.expectEqual(@as(usize, 0), S.responses_len);
}
test "request mode DECRQM with write_pty callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
// Without callback, DECRQM should not crash
{
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// DECRQM for mode 7 (wraparound) — should be silently ignored
s.nextSlice("\x1B[?7$p");
}
t.fullReset();
// With callback, DECRQM should produce a response
{
const S = struct {
var last_response: ?[:0]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (last_response) |old| testing.allocator.free(old);
last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM");
}
};
S.last_response = null;
defer if (S.last_response) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Wraparound mode (7) is set by default
s.nextSlice("\x1B[?7$p");
try testing.expectEqualStrings("\x1B[?7;1$y", S.last_response.?);
// Disable wraparound and query again
s.nextSlice("\x1B[?7l");
s.nextSlice("\x1B[?7$p");
try testing.expectEqualStrings("\x1B[?7;2$y", S.last_response.?);
// Query an unknown mode
s.nextSlice("\x1B[?9999$p");
try testing.expectEqualStrings("\x1B[?9999;0$y", S.last_response.?);
// Query DECECM, which Ghostty recognizes but does not allow changing
s.nextSlice("\x1B[?117$p");
try testing.expectEqualStrings("\x1B[?117;4$y", S.last_response.?);
}
}
test "stream: CSI W with intermediate but no params" {
// Regression test from AFL++ crash. CSI ? W without
// parameters caused an out-of-bounds access on input.params[0].
var t: Terminal = try .init(testing.io, testing.allocator, .{
.cols = 80,
.rows = 24,
.max_scrollback_bytes = 100,
});
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
s.nextSlice("\x1b[?W");
}
test "window_title effect is called" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var title_changed_count: usize = 0;
fn titleChanged(_: *Handler) void {
title_changed_count += 1;
}
};
S.title_changed_count = 0;
var handler: Handler = .init(&t);
handler.effects.title_changed = &S.titleChanged;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Set window title via OSC 2
s.nextSlice("\x1b]2;Hello World\x1b\\");
try testing.expectEqualStrings("Hello World", t.getTitle().?);
try testing.expectEqual(@as(usize, 1), S.title_changed_count);
}
test "window_title effect not called without callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Should not crash when no callback is set
s.nextSlice("\x1b]2;Hello World\x1b\\");
// Title should still be set on terminal state
try testing.expectEqualStrings("Hello World", t.getTitle().?);
// Terminal should still be functional
s.nextSlice("Test");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Test", str);
}
test "window_title effect with empty title" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var title_changed_count: usize = 0;
fn titleChanged(_: *Handler) void {
title_changed_count += 1;
}
};
S.title_changed_count = 0;
var handler: Handler = .init(&t);
handler.effects.title_changed = &S.titleChanged;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Set empty window title
s.nextSlice("\x1b]2;\x1b\\");
try testing.expect(t.getTitle() == null);
try testing.expectEqual(@as(usize, 1), S.title_changed_count);
}
test "kitty_keyboard_query" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
var written_buf: [64]u8 = undefined;
fn writePty(_: *Handler, data: []const u8) void {
std.debug.assert(data.len <= written_buf.len);
@memcpy(written_buf[0..data.len], data);
written = written_buf[0..data.len];
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Default kitty keyboard flags should be 0
s.nextSlice("\x1b[?u");
try testing.expectEqualStrings("\x1b[?0u", S.written.?);
// Push kitty keyboard mode with flags and query again
S.written = null;
s.nextSlice("\x1b[>1u"); // push with disambiguate flag
s.nextSlice("\x1b[?u");
try testing.expectEqualStrings("\x1b[?1u", S.written.?);
}
test "xtversion default" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
var written_buf: [64]u8 = undefined;
fn writePty(_: *Handler, data: []const u8) void {
std.debug.assert(data.len <= written_buf.len);
@memcpy(written_buf[0..data.len], data);
written = written_buf[0..data.len];
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Without xtversion effect set, should report "libghostty"
s.nextSlice("\x1b[>0q");
try testing.expectEqualStrings("\x1bP>|libghostty\x1b\\", S.written.?);
}
test "xtversion with effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
var written_buf: [64]u8 = undefined;
fn writePty(_: *Handler, data: []const u8) void {
std.debug.assert(data.len <= written_buf.len);
@memcpy(written_buf[0..data.len], data);
written = written_buf[0..data.len];
}
fn xtversion(_: *Handler) []const u8 {
return "ghostty 1.2.3";
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.xtversion = &S.xtversion;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1b[>0q");
try testing.expectEqualStrings("\x1bP>|ghostty 1.2.3\x1b\\", S.written.?);
}
test "xtversion with empty string effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
var written_buf: [64]u8 = undefined;
fn writePty(_: *Handler, data: []const u8) void {
std.debug.assert(data.len <= written_buf.len);
@memcpy(written_buf[0..data.len], data);
written = written_buf[0..data.len];
}
fn xtversion(_: *Handler) []const u8 {
return "";
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.xtversion = &S.xtversion;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Empty string from effect should fall back to "libghostty"
s.nextSlice("\x1b[>0q");
try testing.expectEqualStrings("\x1bP>|libghostty\x1b\\", S.written.?);
}
test "size report csi_14_t with effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn getSize(_: *Handler) ?size_report.Size {
return .{ .rows = 24, .columns = 80, .cell_width = 9, .cell_height = 18 };
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.size = &S.getSize;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// CSI 14 t - report text area size in pixels
s.nextSlice("\x1b[14t");
defer testing.allocator.free(S.written.?);
try testing.expectEqualStrings("\x1b[4;432;720t", S.written.?);
}
test "mode 2048 enable reports current geometry and disable is silent" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var response: [128]u8 = undefined;
var response_len: usize = 0;
var calls: usize = 0;
fn writePty(_: *Handler, data: []const u8) void {
@memcpy(response[0..data.len], data);
response_len = data.len;
calls += 1;
}
fn getSize(_: *Handler) ?size_report.Size {
return .{ .rows = 24, .columns = 80, .cell_width = 8, .cell_height = 16 };
}
};
S.response_len = 0;
S.calls = 0;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.size = &S.getSize;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1b[?2048h");
s.nextSlice("\x1b[?2048h");
try testing.expectEqual(@as(usize, 2), S.calls);
try testing.expectEqualStrings("\x1b[48;24;80;384;640t", S.response[0..S.response_len]);
s.nextSlice("\x1b[?2048l");
try testing.expectEqual(@as(usize, 2), S.calls);
try testing.expect(!t.modes.get(.in_band_size_reports));
}
test "mode 2048 enable tolerates missing effects" {
const S = struct {
var calls: usize = 0;
fn writePty(_: *Handler, _: []const u8) void {
calls += 1;
}
fn getSize(_: *Handler) ?size_report.Size {
return .{ .rows = 24, .columns = 80, .cell_width = 8, .cell_height = 16 };
}
};
S.calls = 0;
var no_size_terminal: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer no_size_terminal.deinit(testing.allocator);
var no_size_handler: Handler = .init(&no_size_terminal);
no_size_handler.effects.write_pty = &S.writePty;
var no_size_stream: Stream = .init(.{
.allocator = testing.allocator,
.handler = no_size_handler,
});
defer no_size_stream.deinit();
no_size_stream.nextSlice("\x1b[?2048h");
try testing.expect(no_size_terminal.modes.get(.in_band_size_reports));
try testing.expectEqual(@as(usize, 0), S.calls);
var no_write_terminal: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer no_write_terminal.deinit(testing.allocator);
var no_write_handler: Handler = .init(&no_write_terminal);
no_write_handler.effects.size = &S.getSize;
var no_write_stream: Stream = .init(.{
.allocator = testing.allocator,
.handler = no_write_handler,
});
defer no_write_stream.deinit();
no_write_stream.nextSlice("\x1b[?2048h");
try testing.expect(no_write_terminal.modes.get(.in_band_size_reports));
try testing.expectEqual(@as(usize, 0), S.calls);
}
test "size report csi_16_t with effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn getSize(_: *Handler) ?size_report.Size {
return .{ .rows = 24, .columns = 80, .cell_width = 9, .cell_height = 18 };
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.size = &S.getSize;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// CSI 16 t - report cell size in pixels
s.nextSlice("\x1b[16t");
defer testing.allocator.free(S.written.?);
try testing.expectEqualStrings("\x1b[6;18;9t", S.written.?);
}
test "size report csi_18_t with effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn getSize(_: *Handler) ?size_report.Size {
return .{ .rows = 24, .columns = 80, .cell_width = 9, .cell_height = 18 };
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.size = &S.getSize;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// CSI 18 t - report text area size in characters
s.nextSlice("\x1b[18t");
defer testing.allocator.free(S.written.?);
try testing.expectEqualStrings("\x1b[8;24;80t", S.written.?);
}
test "size report no effect callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Without size effect, size reports should be silently ignored
s.nextSlice("\x1b[14t");
try testing.expect(S.written == null);
}
test "size report csi_21_t title disabled by default" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Set a title first
s.nextSlice("\x1b]2;My Title\x1b\\");
// CSI 21 t - report title (no size effect needed)
s.nextSlice("\x1b[21t");
try testing.expect(S.written == null);
}
test "size report csi_21_t title enabled" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.title_report = true;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Set a title first
s.nextSlice("\x1b]2;My Title\x1b\\");
// CSI 21 t - report title (no size effect needed)
s.nextSlice("\x1b[21t");
defer testing.allocator.free(S.written.?);
try testing.expectEqualStrings("\x1b]lMy Title\x1b\\", S.written.?);
}
test "enquiry no effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// ENQ without enquiry effect should not write anything
s.nextSlice("\x05");
try testing.expect(S.written == null);
}
test "enquiry with effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn enquiry(_: *Handler) []const u8 {
return "ghostty";
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.enquiry = &S.enquiry;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x05");
defer testing.allocator.free(S.written.?);
try testing.expectEqualStrings("ghostty", S.written.?);
}
test "enquiry with empty response" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn enquiry(_: *Handler) []const u8 {
return "";
}
};
S.written = null;
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.enquiry = &S.enquiry;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Empty enquiry response should not write anything
s.nextSlice("\x05");
try testing.expect(S.written == null);
}
test "device status: operating status" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// CSI 5 n — operating status report
s.nextSlice("\x1B[5n");
try testing.expectEqualStrings("\x1B[0n", S.written.?);
}
test "device status: cursor position" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Default position is 0,0 — reported as 1,1
s.nextSlice("\x1B[6n");
try testing.expectEqualStrings("\x1B[1;1R", S.written.?);
// Move cursor to row 5, col 10
s.nextSlice("\x1B[5;10H");
s.nextSlice("\x1B[6n");
try testing.expectEqualStrings("\x1B[5;10R", S.written.?);
}
test "device status: cursor position with origin mode" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Set scroll region rows 5-20
s.nextSlice("\x1B[5;20r");
// Enable origin mode
s.nextSlice("\x1B[?6h");
// Move to row 3, col 5 within the region
s.nextSlice("\x1B[3;5H");
// Query cursor position
s.nextSlice("\x1B[6n");
// Should report position relative to the scroll region
try testing.expectEqualStrings("\x1B[3;5R", S.written.?);
}
test "device status: color scheme dark" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn colorScheme(_: *Handler) ?device_status.ColorScheme {
return .dark;
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.color_scheme = &S.colorScheme;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// CSI ? 996 n — color scheme query
s.nextSlice("\x1B[?996n");
try testing.expectEqualStrings("\x1B[?997;1n", S.written.?);
}
test "device status: color scheme light" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn colorScheme(_: *Handler) ?device_status.ColorScheme {
return .light;
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.color_scheme = &S.colorScheme;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// CSI ? 996 n — color scheme query
s.nextSlice("\x1B[?996n");
try testing.expectEqualStrings("\x1B[?997;2n", S.written.?);
}
test "device status: color scheme without callback" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Without color_scheme effect, query should be silently ignored
s.nextSlice("\x1B[?996n");
try testing.expect(S.written == null);
}
test "visibility reports" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
var count: usize = 0;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
count += 1;
}
};
S.written = null;
S.count = 0;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Mode 2033 is supported and initially disabled.
s.nextSlice("\x1B[?2033$p");
try testing.expectEqualStrings("\x1B[?2033;2$y", S.written.?);
// A one-shot query reports the current state without enabling the mode.
s.nextSlice("\x1B[?998n");
try testing.expectEqualStrings("\x1B[?999;1n", S.written.?);
try testing.expect(!t.modes.get(.report_visibility));
// Enabling always sends an immediate report, even when already enabled.
t.flags.visible = false;
s.nextSlice("\x1B[?2033h");
try testing.expectEqualStrings("\x1B[?999;2n", S.written.?);
const count = S.count;
s.nextSlice("\x1B[?2033h");
try testing.expectEqual(count + 1, S.count);
// Disabling sends no report.
s.nextSlice("\x1B[?2033l");
try testing.expectEqual(count + 1, S.count);
// A terminal reset preserves the view's externally owned visibility.
s.nextSlice("\x1Bc");
s.nextSlice("\x1B[?998n");
try testing.expectEqualStrings("\x1B[?999;2n", S.written.?);
}
test "device status: readonly ignores all" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// All device status queries should be silently ignored without effects
s.nextSlice("\x1B[5n");
s.nextSlice("\x1B[6n");
s.nextSlice("\x1B[?996n");
s.nextSlice("\x1B[?998n");
// Terminal should still be functional
s.nextSlice("Test");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Test", str);
}
test "device attributes: primary DA" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn da(_: *Handler) device_attributes.Attributes {
return .{};
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.device_attributes = &S.da;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B[c");
try testing.expectEqualStrings("\x1b[?62;22c", S.written.?);
}
test "device attributes: secondary DA" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn da(_: *Handler) device_attributes.Attributes {
return .{};
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.device_attributes = &S.da;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B[>c");
try testing.expectEqualStrings("\x1b[>1;0;0c", S.written.?);
}
test "device attributes: tertiary DA" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn da(_: *Handler) device_attributes.Attributes {
return .{};
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.device_attributes = &S.da;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B[=c");
try testing.expectEqualStrings("\x1bP!|00000000\x1b\\", S.written.?);
}
test "device attributes: readonly ignores" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// All DA queries should be silently ignored without effects
s.nextSlice("\x1B[c");
s.nextSlice("\x1B[>c");
s.nextSlice("\x1B[=c");
// Terminal should still be functional
s.nextSlice("Test");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("Test", str);
}
test "device attributes: custom response" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
fn da(_: *Handler) device_attributes.Attributes {
return .{
.primary = .{
.conformance_level = .vt420,
.features = &.{ .ansi_color, .clipboard },
},
.secondary = .{
.device_type = .vt420,
.firmware_version = 100,
},
};
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.device_attributes = &S.da;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B[c");
try testing.expectEqualStrings("\x1b[?64;22;52c", S.written.?);
s.nextSlice("\x1B[>c");
try testing.expectEqualStrings("\x1b[>41;100;0c", S.written.?);
}
test "kitty graphics APC response" {
if (comptime !build_options.kitty_graphics) return error.SkipZigTest;
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
const S = struct {
var written: ?[]const u8 = null;
fn writePty(_: *Handler, data: []const u8) void {
if (written) |old| testing.allocator.free(old);
written = testing.allocator.dupe(u8, data) catch @panic("OOM");
}
};
S.written = null;
defer if (S.written) |old| testing.allocator.free(old);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Send a kitty graphics transmit command with image id 1
s.nextSlice("\x1b_Ga=t,t=d,f=24,i=1,s=1,v=2,c=10,r=1;////////\x1b\\");
// Should have written a response back
try testing.expectEqualStrings("\x1b_Gi=1;OK\x1b\\", S.written.?);
}
test "kitty graphics via APC" {
if (comptime !build_options.kitty_graphics) return error.SkipZigTest;
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 });
defer t.deinit(testing.allocator);
const handler: Handler = .init(&t);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Send a kitty graphics transmit command via APC:
// ESC _ G <payload> ESC \
// a=t,t=d,f=24,i=1,s=1,v=2,c=10,r=1;//////// (1x2 RGB direct)
s.nextSlice("\x1b_Ga=t,t=d,f=24,i=1,s=1,v=2,c=10,r=1;////////\x1b\\");
const storage = &t.screens.active.kitty_images;
const img = storage.imageById(1).?;
try testing.expectEqual(.rgb, img.format);
}
test "continuation reconstructs standard stream without duplicate effects" {
const S = struct {
var bell_count: usize = 0;
var title_count: usize = 0;
var write_count: usize = 0;
var notification_count: usize = 0;
var clipboard_count: usize = 0;
fn bell(_: *Handler) void {
bell_count += 1;
}
fn titleChanged(_: *Handler) void {
title_count += 1;
}
fn writePty(_: *Handler, _: []const u8) void {
write_count += 1;
}
fn desktopNotification(
_: *Handler,
_: Action.ShowDesktopNotification,
) void {
notification_count += 1;
}
fn clipboardWrite(
_: *Handler,
write: clipboard.Write,
) void {
clipboard_count += 1;
write.reply(.{ .success = .{} });
}
fn reset() void {
bell_count = 0;
title_count = 0;
write_count = 0;
notification_count = 0;
clipboard_count = 0;
}
};
S.reset();
const committed = "A\n\x07" ++
"\x1b]2;title\x1b\\" ++
"\x1b[5n" ++
"\x1b]9;body\x1b\\" ++
"\x1b]52;c;aA==\x1b\\";
var source_terminal: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer source_terminal.deinit(testing.allocator);
var source_handler: Handler = .init(&source_terminal);
source_handler.effects.bell = &S.bell;
source_handler.effects.title_changed = &S.titleChanged;
source_handler.effects.write_pty = &S.writePty;
source_handler.effects.desktop_notification = &S.desktopNotification;
source_handler.effects.clipboard_write = &S.clipboardWrite;
var source = Stream.init(.{
.allocator = testing.allocator,
.handler = source_handler,
.continuation_max_bytes = 1024,
});
defer source.deinit();
// Terminal mutation and all callbacks have already committed. The
// unfinished CSI is the only input needed to recreate the stream state.
source.nextSlice(committed ++ "\x1b[31");
try testing.expectEqual(@as(usize, 1), S.bell_count);
try testing.expectEqual(@as(usize, 1), S.title_count);
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqual(@as(usize, 1), S.notification_count);
try testing.expectEqual(@as(usize, 1), S.clipboard_count);
var continuation_buf: [1024]u8 = undefined;
var continuation_writer: std.Io.Writer = .fixed(&continuation_buf);
try source.writeContinuation(&continuation_writer);
try testing.expectEqualStrings("\x1b[31", continuation_writer.buffered());
var restored_terminal: Terminal = try .init(
testing.io,
testing.allocator,
.{ .cols = 80, .rows = 24 },
);
defer restored_terminal.deinit(testing.allocator);
// Stand in for restoring the already-committed terminal snapshot.
{
var snapshot_stream: Stream = .init(.{
.allocator = testing.allocator,
.handler = .init(&restored_terminal),
});
defer snapshot_stream.deinit();
snapshot_stream.nextSlice(committed);
}
const before = try restored_terminal.plainString(testing.allocator);
defer testing.allocator.free(before);
const before_x = restored_terminal.screens.active.cursor.x;
const before_y = restored_terminal.screens.active.cursor.y;
const before_style = restored_terminal.screens.active.cursor.style_id;
const before_title = restored_terminal.getTitle().?;
var restored_handler: Handler = .init(&restored_terminal);
restored_handler.effects.bell = &S.bell;
restored_handler.effects.title_changed = &S.titleChanged;
restored_handler.effects.write_pty = &S.writePty;
restored_handler.effects.desktop_notification = &S.desktopNotification;
restored_handler.effects.clipboard_write = &S.clipboardWrite;
var restored = Stream.init(.{
.allocator = testing.allocator,
.handler = restored_handler,
.continuation_max_bytes = 1024,
});
defer restored.deinit();
S.reset();
restored.nextSlice(continuation_writer.buffered());
try testing.expectEqual(@as(usize, 0), S.bell_count);
try testing.expectEqual(@as(usize, 0), S.title_count);
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqual(@as(usize, 0), S.notification_count);
try testing.expectEqual(@as(usize, 0), S.clipboard_count);
const after = try restored_terminal.plainString(testing.allocator);
defer testing.allocator.free(after);
try testing.expectEqualStrings(before, after);
try testing.expectEqual(before_x, restored_terminal.screens.active.cursor.x);
try testing.expectEqual(before_y, restored_terminal.screens.active.cursor.y);
try testing.expectEqual(before_style, restored_terminal.screens.active.cursor.style_id);
try testing.expectEqualStrings(before_title, restored_terminal.getTitle().?);
source.nextSlice("mB");
restored.nextSlice("mB");
const source_text = try source_terminal.plainString(testing.allocator);
defer testing.allocator.free(source_text);
const restored_text = try restored_terminal.plainString(testing.allocator);
defer testing.allocator.free(restored_text);
try testing.expectEqualStrings(source_text, restored_text);
try testing.expectEqual(
source_terminal.screens.active.cursor.style_id,
restored_terminal.screens.active.cursor.style_id,
);
}
test "kitty dnd: query response" {
const S = struct {
var pty: std.ArrayListUnmanaged(u8) = .empty;
fn writePty(_: *Handler, data: []const u8) void {
pty.appendSlice(testing.allocator, data) catch unreachable;
}
};
S.pty = .empty;
defer S.pty.deinit(testing.allocator);
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
s.nextSlice("\x1B]72;t=q:i=3\x1B\\");
try testing.expectEqualStrings("\x1b]72;t=q:i=3\x1b\\", S.pty.items);
}
test "kitty dnd: register, drop, and serve data" {
const S = struct {
var pty: std.ArrayListUnmanaged(u8) = .empty;
fn writePty(_: *Handler, data: []const u8) void {
pty.appendSlice(testing.allocator, data) catch unreachable;
}
};
S.pty = .empty;
defer S.pty.deinit(testing.allocator);
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Client registers to accept drops.
s.nextSlice("\x1B]72;t=a;text/plain text/uri-list\x1B\\");
try testing.expectEqualStrings("", S.pty.items);
try testing.expect(t.kitty_dnd != null);
// A native drop arrives; the embedder feeds it to the terminal
// state and delivers the produced event bytes itself.
{
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
defer aw.deinit();
try t.kitty_dnd.?.dragDrop(testing.allocator, &aw.writer, .{
.cell_x = 2,
.cell_y = 1,
.pixel_x = 20,
.pixel_y = 18,
.operations = .{ .copy = true },
}, &.{
.{ .mime = "text/plain", .data = "hello" },
});
try testing.expectEqualStrings(
"\x1b]72;t=M:x=2:y=1:X=20:Y=18:o=1:m=0;text/plain \x1b\\",
aw.written(),
);
}
// The client requests the data and concludes.
s.nextSlice("\x1B]72;t=r:x=1\x1B\\");
try testing.expectEqualStrings(
"\x1b]72;t=r:x=1:m=0;aGVsbG8=\x1b\\" ++ "\x1b]72;t=r:x=1\x1b\\",
S.pty.items,
);
S.pty.clearRetainingCapacity();
s.nextSlice("\x1B]72;t=r\x1B\\");
try testing.expectEqualStrings("", S.pty.items);
try testing.expect(t.kitty_dnd.?.drop.items == null);
}
test "kitty dnd: state updates work without write_pty effect" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) });
defer s.deinit();
// Queries produce no output (nowhere to write) but registration
// state still updates.
s.nextSlice("\x1B]72;t=q\x1B\\");
s.nextSlice("\x1B]72;t=a\x1B\\");
try testing.expect(t.kitty_dnd != null);
// The terminal remains functional.
s.nextSlice("ok");
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ok", str);
}
test "kitty dnd: registration survives terminal reset" {
const S = struct {
var pty: std.ArrayListUnmanaged(u8) = .empty;
fn writePty(_: *Handler, data: []const u8) void {
pty.appendSlice(testing.allocator, data) catch unreachable;
}
};
S.pty = .empty;
defer S.pty.deinit(testing.allocator);
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Start a chunked command, then reset mid-chunk.
s.nextSlice("\x1B]72;t=a:i=5\x1B\\");
s.nextSlice("\x1B]72;t=m:o=1:m=1;text/pl\x1B\\");
s.nextSlice("\x1Bc");
// Registration survives (matching kitty), chunking was interrupted
// so a new command is not treated as a continuation.
try testing.expect(t.kitty_dnd != null);
try testing.expect(!t.kitty_dnd.?.chunking.active);
s.nextSlice("\x1B]72;t=q\x1B\\");
try testing.expectEqualStrings("\x1b]72;t=q\x1b\\", S.pty.items);
}
test "kitty dnd: effect reports registration, acceptance, and conclusion" {
const S = struct {
var events: std.ArrayListUnmanaged(kitty_dnd.Event) = .empty;
var mimes: std.ArrayListUnmanaged(u8) = .empty;
fn clear() void {
events.deinit(testing.allocator);
events = .empty;
mimes.deinit(testing.allocator);
mimes = .empty;
}
fn dragAndDrop(handler: *Handler, ev: kitty_dnd.Event) void {
events.append(testing.allocator, ev) catch unreachable;
// Registration details are read from the terminal state.
if (ev == .registration) {
mimes.clearRetainingCapacity();
const state = handler.terminal.kitty_dnd orelse return;
var it = state.registeredMimes();
while (it.next()) |m| {
mimes.appendSlice(testing.allocator, m) catch unreachable;
mimes.append(testing.allocator, ',') catch unreachable;
}
}
}
};
S.clear();
defer S.clear();
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var handler: Handler = .init(&t);
handler.effects.drag_and_drop = &S.dragAndDrop;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// Registration with a MIME list, read back from the state.
s.nextSlice("\x1B]72;t=a;image/png text/plain\x1B\\");
try testing.expectEqual(@as(usize, 1), S.events.items.len);
try testing.expect(S.events.items[0] == .registration);
try testing.expectEqualStrings("image/png,text/plain,", S.mimes.items);
// A native drag and the client's answer.
{
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
defer aw.deinit();
try t.kitty_dnd.?.dragDrop(testing.allocator, &aw.writer, .{
.cell_x = 0,
.cell_y = 0,
.pixel_x = 0,
.pixel_y = 0,
.operations = .{ .copy = true },
}, &.{.{ .mime = "text/plain", .data = "x" }});
}
s.nextSlice("\x1B]72;t=m:o=2;text/plain\x1B\\");
try testing.expectEqual(@as(usize, 2), S.events.items.len);
try testing.expect(S.events.items[1] == .acceptance);
// Conclusion carries the performed operation.
s.nextSlice("\x1B]72;t=r:o=2\x1B\\");
try testing.expectEqual(@as(usize, 3), S.events.items.len);
try testing.expectEqual(kitty_dnd.Event.concluded_move, S.events.items[2]);
// Unregistration reports with the state gone.
s.nextSlice("\x1B]72;t=A\x1B\\");
try testing.expectEqual(@as(usize, 4), S.events.items.len);
try testing.expect(S.events.items[3] == .registration);
try testing.expect(t.kitty_dnd == null);
try testing.expectEqualStrings("", S.mimes.items);
}
/// Capture state for the Handler.paste tests below: every pty write and
/// the clipboard reads the program makes afterwards.
const PasteCapture = struct {
var written: std.ArrayList(u8) = .empty;
var write_count: usize = 0;
var read_count: usize = 0;
var read_granted_count: usize = 0;
var last_read_granted: bool = false;
var last_read_name: [64]u8 = undefined;
var last_read_name_len: usize = 0;
fn reset() void {
written.clearRetainingCapacity();
write_count = 0;
read_count = 0;
read_granted_count = 0;
last_read_granted = false;
last_read_name_len = 0;
}
fn deinit() void {
written.deinit(testing.allocator);
written = .empty;
}
fn writePty(_: *Handler, data: []const u8) void {
written.appendSlice(testing.allocator, data) catch @panic("OOM");
write_count += 1;
}
fn clipboardRead(_: *Handler, read: clipboard.Read) void {
read_count += 1;
last_read_granted = read.granted;
if (read.granted) read_granted_count += 1;
last_read_name_len = read.name.len;
@memcpy(last_read_name[0..read.name.len], read.name);
read.reply(.{ .success = .{ .contents = &.{.{
.mime = "text/plain",
.data = "Ghostty",
}} } });
}
fn readName() []const u8 {
return last_read_name[0..last_read_name_len];
}
};
test "paste: no write_pty effect is an error" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
var handler: Handler = .init(&t);
defer handler.deinit();
try testing.expectError(error.NoWritePty, handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello" }} },
}));
}
test "paste: plain text converts newlines and strips unsafe bytes" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
// Newlines are unsafe unbracketed; the embedder confirmed.
try testing.expect(try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hel\x1blo\nwor\x00ld" }} },
.allow_unsafe = true,
}));
try testing.expectEqualStrings("hel lo\rwor ld", S.written.items);
try testing.expectEqual(@as(usize, 1), S.write_count);
// The first text representation is used; others are ignored.
S.reset();
try testing.expect(try handler.paste(.{
.contents = .{ .memory = &.{
.{ .mime = "image/png", .data = "\x89PNG" },
.{ .mime = "UTF8_STRING", .data = "hi" },
.{ .mime = "text/plain", .data = "ignored" },
} },
}));
try testing.expectEqualStrings("hi", S.written.items);
try testing.expectEqual(@as(usize, 1), S.write_count);
}
test "paste: unsafe text is refused unless allowed" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
try testing.expectError(error.UnsafePaste, handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "rm -rf /\n" }} },
}));
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expect(try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "rm -rf /\n" }} },
.allow_unsafe = true,
}));
try testing.expectEqualStrings("rm -rf /\r", S.written.items);
}
test "paste: bracketed paste frames the text" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
t.modes.set(.bracketed_paste, true);
// Newlines are safe inside the frame and are preserved.
try testing.expect(try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello\nworld" }} },
}));
try testing.expectEqualStrings("\x1b[200~hello\nworld\x1b[201~", S.written.items);
try testing.expectEqual(@as(usize, 1), S.write_count);
// The frame terminator is not.
S.reset();
try testing.expectError(error.UnsafePaste, handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "he\x1b[201~llo" }} },
}));
try testing.expectEqual(@as(usize, 0), S.write_count);
// Allowed, the stripper still defuses it: ESC becomes a space.
try testing.expect(try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "he\x1b[201~llo" }} },
.allow_unsafe = true,
}));
try testing.expectEqualStrings("\x1b[200~he [201~llo\x1b[201~", S.written.items);
}
test "paste: no text representation writes nothing" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
try testing.expect(!try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "image/png", .data = "\x89PNG" }} },
}));
try testing.expect(!try handler.paste(.{
.contents = .{ .memory = &.{} },
}));
try testing.expect(!try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "" }} },
}));
try testing.expectEqual(@as(usize, 0), S.write_count);
}
test "paste: large text streams to the pty in chunks" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
t.modes.set(.bracketed_paste, true);
// Two full chunks and a partial one with the frame, never the
// whole thing at once.
const data = "x" ** 10_000;
try testing.expect(try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = data }} },
}));
const total = data.len + "\x1b[200~\x1b[201~".len;
try testing.expectEqual(
@as(usize, (total + Handler.paste_chunk_size - 1) / Handler.paste_chunk_size),
S.write_count,
);
try testing.expectEqual(total, S.written.items.len);
try testing.expect(std.mem.startsWith(u8, S.written.items, "\x1b[200~xxx"));
try testing.expect(std.mem.endsWith(u8, S.written.items, "xxx\x1b[201~"));
}
/// A paste contents reader for the tests: serves fixed data per MIME
/// type in pieces, counting the reads of each representation.
const PasteReader = struct {
mimes: []const []const u8,
data: []const []const u8,
piece: usize = 3,
reads: [4]usize = @splat(0),
/// Fail after this many bytes of a read.
fail_after: ?usize = null,
fn contents(self: *PasteReader) paste_pkg.Contents {
return .{ .reader = .{
.mimes = self.mimes,
.read = .{ .ctx = self, .read_fn = &read },
} };
}
fn read(ctx: ?*anyopaque, mime: []const u8, sink: *std.Io.Writer) clipboard.MimeReader.Error!void {
const self: *PasteReader = @ptrCast(@alignCast(ctx.?));
const index: usize = for (self.mimes, 0..) |m, i| {
if (std.mem.eql(u8, m, mime)) break i;
} else return error.ReadFailed;
self.reads[index] += 1;
const data = self.data[index];
var offset: usize = 0;
while (offset < data.len) {
if (self.fail_after) |limit| if (offset >= limit) return error.ReadFailed;
const n = @min(self.piece, data.len - offset);
try sink.writeAll(data[offset..][0..n]);
offset += n;
}
}
};
test "paste: reader contents are read on demand" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
// Unsafe text is refused from one read with nothing written; the
// image is never read.
var reader: PasteReader = .{
.mimes = &.{ "image/png", "text/plain" },
.data = &.{ "\x89PNG", "echo hi\nrm -rf /\n" },
};
try testing.expectError(error.UnsafePaste, handler.paste(.{
.contents = reader.contents(),
}));
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqual(@as(usize, 0), reader.reads[0]);
try testing.expectEqual(@as(usize, 1), reader.reads[1]);
// Allowed, the text is read once, encoded.
reader.reads = @splat(0);
try testing.expect(try handler.paste(.{
.contents = reader.contents(),
.allow_unsafe = true,
}));
try testing.expectEqualStrings("echo hi\rrm -rf /\r", S.written.items);
try testing.expectEqual(@as(usize, 0), reader.reads[0]);
try testing.expectEqual(@as(usize, 1), reader.reads[1]);
// Safe text is read once too: buffered for the check, then written.
S.reset();
reader = .{
.mimes = &.{"text/plain"},
.data = &.{"hello world"},
};
try testing.expect(try handler.paste(.{ .contents = reader.contents() }));
try testing.expectEqualStrings("hello world", S.written.items);
try testing.expectEqual(@as(usize, 1), reader.reads[0]);
// Empty text is nothing to paste, found on the one read.
S.reset();
reader = .{
.mimes = &.{"text/plain"},
.data = &.{""},
};
try testing.expect(!try handler.paste(.{ .contents = reader.contents() }));
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqual(@as(usize, 1), reader.reads[0]);
// A paste event lists the types and reads nothing at all.
S.reset();
t.modes.set(.kitty_paste_events, true);
reader = .{
.mimes = &.{ "text/plain", "image/png" },
.data = &.{ "secret", "\x89PNG" },
};
try testing.expect(try handler.paste(.{ .contents = reader.contents() }));
try testing.expect(std.mem.indexOf(u8, S.written.items, ";dGV4dC9wbGFpbiBpbWFnZS9wbmcK\x1b\\") != null);
try testing.expect(std.mem.indexOf(u8, S.written.items, "secret") == null);
try testing.expectEqual(@as(usize, 0), reader.reads[0]);
try testing.expectEqual(@as(usize, 0), reader.reads[1]);
}
test "paste: reader failure writes nothing" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
t.modes.set(.bracketed_paste, true);
// The read is buffered whole before anything is written, so a
// mid-read failure discards the buffer, checked or not.
var reader: PasteReader = .{
.mimes = &.{"text/plain"},
.data = &.{"hello world"},
.fail_after = 6,
};
try testing.expectError(error.ReadFailed, handler.paste(.{
.contents = reader.contents(),
}));
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectError(error.ReadFailed, handler.paste(.{
.contents = reader.contents(),
.allow_unsafe = true,
}));
try testing.expectEqual(@as(usize, 0), S.write_count);
}
test "paste: mode 5522 sends an event the program can read with" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
t.modes.set(.kitty_paste_events, true);
t.modes.set(.bracketed_paste, true);
// Every representation is listed, the data is never written, and
// the one-time password rides on every packet.
try testing.expect(try s.handler.paste(.{
.contents = .{ .memory = &.{
.{ .mime = "text/plain", .data = "secret" },
.{ .mime = "image/png", .data = "" },
} },
}));
try testing.expectEqual(@as(usize, 1), S.write_count);
try testing.expectEqual(@as(usize, 3), std.mem.count(u8, S.written.items, "\x1b]5522;"));
try testing.expect(std.mem.indexOf(u8, S.written.items, "secret") == null);
try testing.expect(std.mem.indexOf(u8, S.written.items, "\x1b[200~") == null);
// OK packet: parse the (base64) password out.
const ok_prefix = "\x1b]5522;type=read:status=OK:pw=";
try testing.expect(std.mem.startsWith(u8, S.written.items, ok_prefix));
const pw_end = std.mem.indexOfPos(u8, S.written.items, ok_prefix.len, "\x1b\\").?;
// Copied out since the capture buffer is reused below.
var pw_buf: [64]u8 = undefined;
const pw_b64 = pw_buf[0 .. pw_end - ok_prefix.len];
@memcpy(pw_b64, S.written.items[ok_prefix.len..pw_end]);
try testing.expectEqual(
std.base64.standard.Encoder.calcSize(kitty_clipboard.otp_len),
pw_b64.len,
);
// Listing packet: base64 of "text/plain image/png\n".
var expected_buf: [256]u8 = undefined;
const expected = try std.fmt.bufPrint(
&expected_buf,
"\x1b]5522;type=read:status=OK:pw={s}\x1b\\" ++
"\x1b]5522;type=read:status=DATA:mime=Lg==:pw={s};dGV4dC9wbGFpbiBpbWFnZS9wbmcK\x1b\\" ++
"\x1b]5522;type=read:status=DONE:pw={s}\x1b\\",
.{ pw_b64, pw_b64, pw_b64 },
);
try testing.expectEqualStrings(expected, S.written.items);
// The program reads with the password and the name "Paste event":
// the read arrives granted, exactly once.
var read_buf: [256]u8 = undefined;
const read = try std.fmt.bufPrint(
&read_buf,
"\x1b]5522;type=read:pw={s}:name=UGFzdGUgZXZlbnQ=;dGV4dC9wbGFpbg==\x1b\\",
.{pw_b64},
);
S.reset();
s.nextSlice(read);
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expect(S.last_read_granted);
try testing.expectEqualStrings("Paste event", S.readName());
try testing.expectEqualStrings(
"\x1b]5522;type=read:status=OK\x1b\\" ++
"\x1b]5522;type=read:status=DATA:mime=dGV4dC9wbGFpbg==;R2hvc3R0eQ==\x1b\\" ++
"\x1b]5522;type=read:status=DONE\x1b\\",
S.written.items,
);
// The password was one-time: a second read is not granted.
S.reset();
s.nextSlice(read);
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expect(!S.last_read_granted);
try testing.expectEqual(@as(usize, 0), S.read_granted_count);
// Every event mints a fresh password.
S.reset();
try testing.expect(try s.handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "secret" }} },
}));
try testing.expect(std.mem.indexOf(u8, S.written.items, pw_b64) == null);
}
test "paste: mode 5522 reports the selection as primary" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
t.modes.set(.kitty_paste_events, true);
for ([_]clipboard.Location{ .primary, .selection }) |location| {
S.reset();
try testing.expect(try handler.paste(.{
.source = .{ .clipboard = location },
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "x" }} },
}));
try testing.expect(std.mem.startsWith(
u8,
S.written.items,
"\x1b]5522;type=read:status=OK:loc=primary:pw=",
));
// Only on the OK packet.
try testing.expectEqual(@as(usize, 1), std.mem.count(u8, S.written.items, "loc=primary"));
}
S.reset();
try testing.expect(try handler.paste(.{
.source = .{ .clipboard = .standard },
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "x" }} },
}));
try testing.expect(std.mem.indexOf(u8, S.written.items, "loc=") == null);
}
test "paste: mode 5522 without clipboard_read pastes text" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
t.modes.set(.kitty_paste_events, true);
try testing.expect(try handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello" }} },
}));
try testing.expectEqualStrings("hello", S.written.items);
try testing.expectEqual(@as(usize, 0), handler.kitty_clipboard_grants.entries.items.len);
}
test "paste: text source never becomes an event" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
t.modes.set(.kitty_paste_events, true);
try testing.expect(try handler.paste(.{
.source = .text,
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "committed" }} },
}));
try testing.expectEqualStrings("committed", S.written.items);
try testing.expectEqual(@as(usize, 0), handler.kitty_clipboard_grants.entries.items.len);
}
test "paste: mode 5522 without entropy fails and records no grant" {
var t: Terminal = try .init(std.Io.failing, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
defer handler.deinit();
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
t.modes.set(.kitty_paste_events, true);
try testing.expectError(error.EntropyUnavailable, handler.paste(.{
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "secret" }} },
}));
try testing.expectEqual(@as(usize, 0), S.write_count);
try testing.expectEqual(@as(usize, 0), handler.kitty_clipboard_grants.entries.items.len);
// Text pastes need no entropy and still work.
try testing.expect(try handler.paste(.{
.source = .text,
.contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello" }} },
}));
try testing.expectEqualStrings("hello", S.written.items);
}
test "paste: mode 5522 DECRQM requires clipboard read effect" {
if (comptime build_options.artifact != .lib) return error.SkipZigTest;
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
// A paste event is unusable if its follow-up read can't be served, so
// the mode isn't advertised without a clipboard read effect.
s.nextSlice("\x1B[?5522$p");
try testing.expectEqualStrings("\x1B[?5522;0$y", S.written.items);
S.reset();
s.nextSlice("\x1B[?5522h");
try testing.expect(t.modes.get(.kitty_paste_events));
s.nextSlice("\x1B[?5522$p");
try testing.expectEqualStrings("\x1B[?5522;0$y", S.written.items);
// Installing the effect makes the current mode state reportable.
S.reset();
s.handler.effects.clipboard_read = &S.clipboardRead;
s.nextSlice("\x1B[?5522$p");
try testing.expectEqualStrings("\x1B[?5522;1$y", S.written.items);
S.reset();
s.nextSlice("\x1B[?5522l");
try testing.expect(!t.modes.get(.kitty_paste_events));
s.nextSlice("\x1B[?5522$p");
try testing.expectEqualStrings("\x1B[?5522;2$y", S.written.items);
// Removing the effect stops advertising the capability again.
S.reset();
s.handler.effects.clipboard_read = null;
s.nextSlice("\x1B[?5522$p");
try testing.expectEqualStrings("\x1B[?5522;0$y", S.written.items);
}
test "full reset drops kitty clipboard grants" {
var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 });
defer t.deinit(testing.allocator);
const S = PasteCapture;
S.reset();
defer S.deinit();
var handler: Handler = .init(&t);
handler.effects.write_pty = &S.writePty;
handler.effects.clipboard_read = &S.clipboardRead;
var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler });
defer s.deinit();
try s.handler.kitty_clipboard_grants.grant(testing.allocator, "pw", .read, false);
try testing.expectEqual(@as(usize, 1), s.handler.kitty_clipboard_grants.entries.items.len);
s.nextSlice("\x1Bc");
try testing.expectEqual(@as(usize, 0), s.handler.kitty_clipboard_grants.entries.items.len);
// A read with the old password is no longer granted, and the
// handler keeps working (grants can be recorded again).
S.reset();
s.nextSlice("\x1b]5522;type=read:pw=cHc=:name=YXBw;dGV4dC9wbGFpbg==\x1b\\");
try testing.expectEqual(@as(usize, 1), S.read_count);
try testing.expect(!S.last_read_granted);
try s.handler.kitty_clipboard_grants.grant(testing.allocator, "pw", .read, false);
}
|