croft-software 0.1.736

VSCode-style TUI written in Rust
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
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
use alacritty_terminal::Term;
use alacritty_terminal::event::{Event as AlacEvent, EventListener};
use alacritty_terminal::grid::{Dimensions, Scroll};
use alacritty_terminal::index::{Column, Line, Point, Side};
use alacritty_terminal::selection::{Selection as TracerSelection, SelectionType};
use alacritty_terminal::sync::FairMutex;
use alacritty_terminal::term::cell::Flags;
use alacritty_terminal::term::test::TermSize;
use alacritty_terminal::term::{Config, TermMode};
use alacritty_terminal::vte::ansi::{Color as AnsiColor, NamedColor, Processor, StdSyncHandler};
use anyhow::{Context, Result};
use portable_pty::{CommandBuilder, MasterPty, PtySize, native_pty_system};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    widgets::{Block, Borders, Widget},
};
use std::io::{Read, Write};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

const SCROLLBACK_LINES: usize = 5000;

/// A mouse button (or wheel direction) to forward to the child program
/// when it has mouse tracking enabled. Encoded as an SGR (1006) report when
/// the child selected that encoding, otherwise as a legacy X10 report.
#[derive(Clone, Copy, PartialEq)]
pub enum MouseButtonKind {
    Left,
    Middle,
    Right,
    WheelUp,
    WheelDown,
}

/// What happened to the button: a press, a release, or motion while held.
#[derive(Clone, Copy, PartialEq)]
pub enum MouseAction {
    Press,
    Release,
    Motion,
}

/// Keyboard modifiers held during a mouse event, folded into the report's
/// button byte (Shift +4, Alt +8, Ctrl +16).
#[derive(Clone, Copy, Default)]
pub struct MouseMods {
    pub shift: bool,
    pub alt: bool,
    pub ctrl: bool,
}

/// A terminal text selection, anchored to *absolute* alacritty grid lines
/// rather than viewport rows. `line` is the grid `Line` index: `0..rows`
/// is the live screen, negative values are scrollback history. Storing
/// absolute lines (instead of `row - inner.y`) is what lets a selection
/// be taller than the visible pane and survive scrolling — the highlight
/// and the extracted text both track content, not screen position.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Selection {
    pub anchor: (i32, u16),
    pub head: (i32, u16),
    /// Rectangular (column-block) selection: the highlight and the copied
    /// text cover the same column slice on every row between the endpoints
    /// (copy mode's Ctrl+V), instead of running row-major to the ends.
    pub block: bool,
}

/// What an alternate-screen selection remembers about its content so the
/// highlight can follow text that the app moves by repainting (alt-screen
/// programs own scrolling: the grid never scrolls, cells just change).
/// After every repaint the selection re-finds these rows in the grid and
/// shifts itself to them; while they are nowhere in view it goes `dormant`
/// — hidden, coordinates frozen — and reappears when the app scrolls the
/// content back.
struct AltSelAnchor {
    /// Full row text of every selected grid line at last sight, top to
    /// bottom. Rows the shift math placed outside the grid stay remembered
    /// so the block can re-anchor when they scroll back in.
    rows: Vec<String>,
    /// Grid line `rows[0]` was last seen at. Drift is measured against
    /// THIS, never against the selection's own top: mid-drag the selection
    /// top is the pointer (the head), not the anchored content, and
    /// deriving drift from it walked the anchor one row per mouse event on
    /// upward drags.
    top: i32,
    /// The extracted selection text at last sight: what copy yields while
    /// the content is scrolled out of view.
    text: String,
    /// The rows are nowhere in the grid right now: paint nothing, keep the
    /// coordinates frozen until the content reappears.
    dormant: bool,
    /// The rows (and column spans) that actually survive on screen when
    /// the block is only partially visible — the rest is off the grid or
    /// overdrawn by app chrome (Claude Code's input box, its floating
    /// "Jump to bottom" pill). Each entry is `(grid line, first column,
    /// last column)`: a row half-covered by a pill keeps its surviving
    /// prefix/suffix highlighted. Painting clips to these; `None` while
    /// the whole block is visible.
    visible: Option<Vec<(i32, u16, u16)>>,
    /// Row text just above / below the block at capture time (`None` at a
    /// grid edge). Pure tie-breakers for re-anchoring: full-screen apps
    /// repeat rows verbatim (divider rules, continuation markers), so a
    /// selection whose own fingerprint matches several shifts equally —
    /// inevitably a one-row selection on such a row — follows the copy
    /// whose neighbours also match instead of the nearest lookalike.
    ctx_above: Option<String>,
    ctx_below: Option<String>,
}

impl Selection {
    pub fn new(line: i32, col: u16) -> Self {
        Self {
            anchor: (line, col),
            head: (line, col),
            block: false,
        }
    }
    pub fn normalised(&self) -> (i32, u16, i32, u16) {
        let (a_l, a_c) = self.anchor;
        let (b_l, b_c) = self.head;
        if (a_l, a_c) <= (b_l, b_c) {
            (a_l, a_c, b_l, b_c)
        } else {
            (b_l, b_c, a_l, a_c)
        }
    }
    /// Rectangle bounds with rows and columns min/maxed independently
    /// (row-major normalisation would pair the wrong corners when the head
    /// sits below-left of the anchor): (row_lo, col_lo, row_hi, col_hi).
    pub fn block_bounds(&self) -> (i32, u16, i32, u16) {
        let (a_l, a_c) = self.anchor;
        let (b_l, b_c) = self.head;
        (a_l.min(b_l), a_c.min(b_c), a_l.max(b_l), a_c.max(b_c))
    }
    pub fn has_area(&self) -> bool {
        self.anchor != self.head
    }
}

/// Listener for events the embedded `Term` emits. Most variants (title
/// changes, cursor-blink toggles, clipboard load/store, child exit, etc.)
/// are owned by the outer croft TUI and ignored here, but two of them
/// MUST be reflected back into the shell's stdin or interactive TUIs
/// running inside the embedded terminal hang waiting for replies:
///
///   * `PtyWrite(text)` — DSR (`ESC[6n`), DA1/DA2 (`ESC[c`, `ESC[>c`),
///     keyboard modifier reports, sixel/synchronised-output queries,
///     and OSC 52 read backs all surface here. atuin times out with
///     "The cursor position could not be read within a normal duration"
///     when the DSR reply never lands on the PTY; helix, fzf, btop and
///     other Rust/Go TUIs do the same.
///   * `TextAreaSizeRequest(cb)` — `CSI 14 t` / `CSI 18 t` pixel/cell
///     size queries from TUIs that scale glyph cells (e.g. helix's
///     terminal-size detection, sixel-aware viewers). The callback
///     builds the reply string from the supplied `WindowSize`.
///
/// In tests we use `VoidListener::default()` (both channels `None`) so
/// the listener silently drops every event — the test helpers exercise
/// the parser, not the round-trip.
#[derive(Clone, Default)]
pub struct VoidListener {
    pty_response_tx: Option<std::sync::mpsc::Sender<String>>,
    size: Option<Arc<std::sync::Mutex<(u16, u16)>>>,
    /// Latched when the child rings BEL (`\a`); the app drains it via
    /// [`PtyTerminal::take_bell`] to surface the bell in the UI.
    bell: Option<Arc<AtomicBool>>,
}

impl EventListener for VoidListener {
    fn send_event(&self, event: AlacEvent) {
        match event {
            AlacEvent::Bell => {
                if let Some(bell) = self.bell.as_ref() {
                    bell.store(true, Ordering::Release);
                }
            }
            AlacEvent::PtyWrite(text) => {
                if let Some(tx) = self.pty_response_tx.as_ref() {
                    let _ = tx.send(text);
                }
            }
            AlacEvent::TextAreaSizeRequest(cb) => {
                let (Some(tx), Some(size)) = (self.pty_response_tx.as_ref(), self.size.as_ref())
                else {
                    return;
                };
                let (cols, rows) = *size.lock().unwrap();
                let ws = alacritty_terminal::event::WindowSize {
                    num_lines: rows,
                    num_cols: cols,
                    cell_width: 1,
                    cell_height: 1,
                };
                let _ = tx.send(cb(ws));
            }
            _ => {}
        }
    }
}

/// A row arriving this long after its predecessor is highlighted amber in
/// the timestamps gutter (the "where did the deploy stall" signal).
const STALL_GAP_MS: u64 = 60_000;

/// One user note pinned to a span of terminal output (iTerm2's
/// annotations), anchored like a mark so it rides the scrollback.
#[derive(Clone, Debug)]
/// The pane's monotonic scroll clock: lines the primary screen has
/// scrolled since the pane spawned, read from a one-cell tracer selection's
/// drift. Immune to the `history_size` saturation that froze selections in
/// long-lived panes (scrollback full for ages, so a history-growth delta
/// was pinned at zero while content kept rotating through the ring). Alt
/// screen freezes the clock — output goes to the alternate grid while
/// primary content holds still. A dead tracer falls back to history growth
/// since the last tick: exact for the viewport-pushing clear that killed
/// it, zero for an alt-screen round trip.
///
/// Shared (behind a mutex) between the render thread — selection,
/// copy-mode, and annotation re-anchoring — and the PTY reader thread,
/// which keys arrival timestamps on it. Lock order everywhere:
/// term → clock → line_times; the term lock is held across the whole
/// sequence on both threads, and the inner two always nest in that order.
struct ScrollClock {
    /// Monotonic reading, folded up to the last `tick`.
    base: i64,
    /// Grid line the tracer selection was planted at during the last tick;
    /// `None` before the first tick.
    planted: Option<i32>,
    /// `history_size` at the last tick: fallback delta source for the rare
    /// windows where the tracer died (screen clear, alt-screen round trip).
    hist: i64,
}

impl ScrollClock {
    fn new() -> Self {
        Self {
            base: 0,
            planted: None,
            hist: 0,
        }
    }

    /// Plant the one-cell tracer selection at `line` in alacritty's own
    /// `Term::selection` slot (croft paints selections itself, so the slot
    /// is otherwise unused). alacritty rotates it on every scroll —
    /// including ring rotation once the scrollback is full, where
    /// `history_size` saturates and stops counting — so its drift between
    /// ticks is the true lines-scrolled count.
    fn plant_tracer(term: &mut Term<VoidListener>, line: i32) {
        let point = Point::new(Line(line), Column(0));
        let mut tracer = TracerSelection::new(SelectionType::Simple, point, Side::Left);
        tracer.update(point, Side::Right);
        term.selection = Some(tracer);
    }

    /// Where the tracer sits now, when it survived since the last plant
    /// (screen clears, alt-screen swaps and content rotating fully off the
    /// scrollback kill it).
    fn tracer_line(term: &Term<VoidListener>) -> Option<i32> {
        let range = term.selection.as_ref()?.to_range(term)?;
        Some(range.start.line.0)
    }

    fn now(&self, term: &Term<VoidListener>) -> i64 {
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return self.base;
        }
        match (self.planted, Self::tracer_line(term)) {
            (Some(planted), Some(cur)) => self.base + i64::from(planted - cur),
            _ => {
                let growth = term.grid().history_size() as i64 - self.hist;
                self.base + growth.max(0)
            }
        }
    }

    /// Fold the tracer's drift into the clock, re-plant it fresh and return
    /// the folded reading. The newest history line is the parking spot:
    /// application clears only touch live-screen rows, so nothing kills it
    /// there, and it sits a full scrollback's depth from rotating off the
    /// top before the next tick (every rendered frame ticks).
    fn tick(&mut self, term: &mut Term<VoidListener>) -> i64 {
        let now = self.now(term);
        if !term.mode().contains(TermMode::ALT_SCREEN) {
            self.base = now;
            self.hist = term.grid().history_size() as i64;
            let park = if self.hist > 0 { -1 } else { 0 };
            Self::plant_tracer(term, park);
            self.planted = Some(park);
        }
        now
    }
}

/// One PTY chunk's arrival stamps for the timestamps gutter: every row the
/// cursor advanced past gets the chunk's arrival time, keyed on the scroll
/// clock (`clock reading + cursor grid line` = a stable content id) — NOT
/// on `history_size`, which treated any upward cursor motion as a
/// scrollback wipe (a `docker pull` progress redraw erased every stamp, an
/// alt-screen round trip erased them and then re-stamped the whole
/// scrollback with the exit time) and which saturates once the ring fills,
/// freezing ids to screen positions. Alternate-screen output stamps
/// nothing: it isn't scrollback rows, and the gutter is normal-screen only.
/// Destructive-clear detector over the raw PTY byte stream: ED 2
/// (`\x1b[2J`, erases the live screen), ED 3 (`\x1b[3J`, erases the
/// scrollback) and RIS (`\x1bc`, both). Keyed on the bytes rather than on
/// `history_size` movement, which is 0 → 0 when nothing has scrolled into
/// history yet. The scan is POSITIONAL: it tracks alt-screen entry/exit
/// (DECSET/DECRST 47/1047/1049) through the stream, so a wipe counts only
/// where it landed — `clear && vim` wipes the primary screen even though
/// the same chunk ends inside the alt screen. An unterminated trailing
/// sequence (bounded) carries to the next read; consumed bytes are never
/// rescanned, which keeps the alt tracking single-shot.
#[derive(Default)]
struct WipeSniffer {
    carry: Vec<u8>,
}

/// Longest carried partial sequence; a real DECSET list or ED is far
/// shorter, and the bound stops a hostile unterminated CSI from growing.
const WIPE_CARRY_MAX: usize = 64;

impl WipeSniffer {
    /// Scan one chunk. `alt` is the terminal's alt-screen state where the
    /// chunk STARTS. Returns (primary screen wiped, scrollback wiped).
    fn scan(&mut self, chunk: &[u8], mut alt: bool) -> (bool, bool) {
        let mut data = std::mem::take(&mut self.carry);
        data.extend_from_slice(chunk);
        let mut screen = false;
        let mut hist = false;
        let mut i = 0;
        while i < data.len() {
            if data[i] != 0x1b {
                i += 1;
                continue;
            }
            let Some(&next) = data.get(i + 1) else {
                self.carry = data.split_off(i);
                return (screen, hist);
            };
            match next {
                // RIS: full reset — everything is gone, back on primary.
                b'c' => {
                    screen = true;
                    hist = true;
                    alt = false;
                    i += 2;
                }
                b'[' => {
                    let mut j = i + 2;
                    while j < data.len()
                        && !(0x40..=0x7e).contains(&data[j])
                        && !matches!(data[j], 0x18 | 0x1a | 0x1b)
                    {
                        j += 1;
                    }
                    if j >= data.len() {
                        if data.len() - i <= WIPE_CARRY_MAX {
                            self.carry = data.split_off(i);
                        }
                        return (screen, hist);
                    }
                    // CAN/SUB cancel the sequence and an ESC inside an
                    // incomplete CSI begins a NEW escape (VT100) — none may
                    // be swallowed as a parameter, or whatever follows a
                    // cancelled CSI goes unseen.
                    if matches!(data[j], 0x18 | 0x1a | 0x1b) {
                        i = if data[j] == 0x1b { j } else { j + 1 };
                        continue;
                    }
                    let params = &data[i + 2..j];
                    match data[j] {
                        b'J' if params == b"2" && !alt => screen = true,
                        b'J' if params == b"3" && !alt => hist = true,
                        fin @ (b'h' | b'l') if params.first() == Some(&b'?') => {
                            for p in params[1..].split(|&c| c == b';') {
                                if p == b"47" || p == b"1047" || p == b"1049" {
                                    alt = fin == b'h';
                                }
                            }
                        }
                        _ => {}
                    }
                    i = j + 1;
                }
                _ => i += 2,
            }
        }
        (screen, hist)
    }
}

fn stamp_chunk(
    term: &mut Term<VoidListener>,
    clock: &mut ScrollClock,
    lt: &mut std::collections::BTreeMap<i64, u64>,
    prev_id: &mut i64,
    now_ms: u64,
) {
    if term.mode().contains(TermMode::ALT_SCREEN) {
        return;
    }
    let now = clock.tick(term);
    let cur_id = now + i64::from(term.grid().cursor.point.line.0);
    // Stamp from the PREVIOUS cursor row inclusive (last touch wins: the
    // row content just landed on is re-stamped as it fills). A redraw that
    // moved the cursor UP re-arrives at existing rows: stamp only the
    // cursor row, never wipe.
    let from = (*prev_id).min(cur_id);
    for a in from..=cur_id {
        lt.insert(a, now_ms);
    }
    *prev_id = cur_id;
    // Bounded by the largest configurable scrollback plus a screen;
    // oldest stamps go first.
    while lt.len() > 210_000 {
        lt.pop_first();
    }
}

struct PaneAnnotation {
    /// Grid line the span sat on when recorded, paired with the scroll
    /// clock reading at that instant. Translated with clock movement — NOT
    /// `history_size` growth, which saturates once the scrollback fills and
    /// froze annotations in exactly the long-lived panes they target.
    line_rec: i32,
    clock_rec: i64,
    start: u16,
    len: u16,
    text: String,
}

pub struct PtyTerminal {
    term: Arc<FairMutex<Term<VoidListener>>>,
    /// Set by the PTY reader thread on every chunk and by `write_input`;
    /// cleared by `take_dirty`. The main loop only redraws when set.
    pty_dirty: Arc<AtomicBool>,
    /// Bytes the reader thread has advanced into the grid since the last
    /// redraw (reset by `take_dirty`). The main loop reads this to tell an
    /// interactive echo (a few bytes: a keystroke, a shell line-rewrite,
    /// an input-box update) from a bulk stream (cat / build logs / a
    /// full-pane scroll). Small updates bypass the PTY redraw cap so echo
    /// stays native; large ones stay capped so they can't saturate the
    /// ssh pipe and starve input.
    pty_pending_bytes: Arc<AtomicUsize>,
    /// Tracks whether the inner program has enabled DECSET 2004 (bracketed
    /// paste). Sniffed off the byte stream; not all parsers expose it.
    bracketed_paste_enabled: Arc<AtomicBool>,
    /// Listening loopback ports the reader thread scraped out of the output
    /// stream (`http://localhost:PORT` banners, `listening on :PORT` lines).
    /// The app drains this each tick to feed the PORTS panel and the toast.
    port_rx: std::sync::mpsc::Receiver<crate::port_detect::PortHit>,
    master: Box<dyn MasterPty + Send>,
    /// Shared between this struct's user-input path (`write_input`,
    /// `paste_*`, `cd_into`) and the background responder thread that
    /// ships alacritty's reply bytes (`PtyWrite`, `TextAreaSizeRequest`)
    /// back to the shell's stdin. portable-pty rejects a second
    /// `take_writer` call, so there is exactly one underlying writer
    /// and both paths funnel through this mutex.
    writer: Arc<std::sync::Mutex<Box<dyn Write + Send>>>,
    _child: Box<dyn portable_pty::Child + Send + Sync>,
    /// The PTY reader thread's handle, joined in `Drop` after the child is
    /// killed so a dropped terminal never leaves a live shell + blocked
    /// reader behind. Across the test suite that leak piled up into resource
    /// pressure that broke timing-sensitive tests; production drops (closing
    /// a pane) leaked the same way.
    reader_thread: Option<std::thread::JoinHandle<()>>,
    /// Write end of the reader thread's shutdown pipe; `Drop` closes it,
    /// raising POLLHUP on the read end the reader polls alongside the pty.
    /// Without it the join above can hang forever: on Linux a background job
    /// keeps the pty slave open after the shell dies, so the reader's `read`
    /// never returns EOF (macOS revokes the tty instead).
    reader_shutdown: Option<std::io::PipeWriter>,
    /// The interactive shell's pid, captured at spawn. A shell at its
    /// prompt is its own foreground process-group leader, so this is the
    /// value `foreground_is_shell` compares `tcgetpgrp(master)` against.
    shell_pid: Option<i32>,
    /// Process-wide stable identity for this pane, assigned at spawn. Vec
    /// positions shift on close/insert/reorder; anything keying long-lived
    /// state to a pane (the build-diagnostic ownership, #119) uses this.
    uid: u64,
    /// Whether anything has ever been written toward the child's stdin
    /// (keystrokes, pastes, seeds, mouse reports). While false, no
    /// foreground application can have been launched in this pane, so a
    /// foreground group that differs from the shell can only be the
    /// shell's own rc startup — the state `cwd_seed_is_safe` treats as
    /// still seedable (#94).
    input_seen: bool,
    /// True for a `new_running` pane: the child is a launched program
    /// (a task, run-active-file, a debug attach), not an interactive
    /// shell. Such a pane is doing work the user asked for even though
    /// no input byte was ever written to it, so `is_pristine` must
    /// never mistake it for a replaceable startup default.
    run_pane: bool,
    /// Latches true the first time a non-blank manual name is set and
    /// never clears: a pane the user named is user state even after the
    /// name is cleared back, so `is_pristine` reads this latch rather
    /// than the live `manual_name` (which a blank rename empties).
    manual_name_seen: bool,
    cols: u16,
    rows: u16,
    /// Shared with the `VoidListener` so `TextAreaSizeRequest` callbacks
    /// see the live geometry without us having to thread a fresh
    /// listener instance through every `resize` call.
    size_shared: Arc<std::sync::Mutex<(u16, u16)>>,
    pub focused: bool,
    /// While broadcast input is on, an excluded pane stops receiving the
    /// mirrored keystrokes (it still gets its own input when focused).
    /// Toggled per pane with Cmd+K Shift+I; session-scoped.
    pub broadcast_excluded: bool,
    /// When focused, draw the orange→green gradient border (Black theme)
    /// instead of the solid blue one. Set by the app's focus/theme sync.
    pub focus_gradient: bool,
    pub last_area: Rect,
    pub last_inner: Rect,
    selection: Option<Selection>,
    /// Copy-mode cursor cell (absolute grid line, grid column): painted as
    /// a green modal block over the glyph so keyboard selection has a
    /// visible caret. None whenever copy mode is off.
    copy_cursor: Option<(i32, u16)>,
    /// Scroll-clock reading (see `clock_now`) when the selection /
    /// copy-cursor coordinates were recorded. Grid lines are relative to
    /// the buffer bottom, so every row the pane scrolls afterwards shifts
    /// their content up by one; subtracting the clock movement since this
    /// stamp keeps them glued to their text while output streams.
    sel_scrolled: i64,
    /// The pane's monotonic scroll clock, shared with the PTY reader
    /// thread: the render side re-anchors selections/annotations with it,
    /// the reader keys arrival timestamps on it (both under the term lock,
    /// then this mutex — always in that order).
    clock: Arc<std::sync::Mutex<ScrollClock>>,
    /// Content anchor for a selection made on the alternate screen, where
    /// apps (Claude Code, vim) never scroll the grid — they repaint it in
    /// place, so no scroll clock can see the text move. `None` while the
    /// selection lives on the primary screen.
    alt_sel: Option<AltSelAnchor>,
    /// A mouse drag-selection is in progress (button still held). While
    /// dragging, the selection follows the pointer over whatever the grid
    /// shows — it must never hide itself because the content anchor lost
    /// its rows (the drag may have started on a blank row or on an app's
    /// animated status line). The definitive anchor is captured on
    /// release (`end_drag`).
    drag_selecting: bool,
    /// User-given pane name (via rename), overriding the auto label. `None`
    /// until the user renames the pane.
    manual_name: Option<String>,
    /// Live foreground-process label (`zsh`, `vim`, `node`…), refreshed off the
    /// event loop on a cadence. Empty until the first refresh resolves.
    auto_label: String,
    /// Terminal find state: the needle currently highlighted across the grid
    /// and the options it matches under. `None` when no find bar is open.
    /// Set by the app's terminal find bar; the render loop paints every
    /// occurrence on each visible row.
    search_needle: Option<String>,
    search_opts: crate::widgets::search::SearchOpts,
    /// The active match painted in the brighter accent, versus the muted
    /// highlight on every other occurrence (VS Code's current-vs-other match
    /// colours). Stored as `(clock_rec, line_rec, col, len)` — the line is
    /// anchored on the scroll clock like annotations and hints, so streaming
    /// output moves the bright cell with its text instead of leaving it
    /// parked on a viewport row.
    current_match: Option<(i64, i32, usize, usize)>,
    /// Latched by the event listener when the child rings BEL; drained by
    /// [`Self::take_bell`].
    bell: Arc<AtomicBool>,
    /// OSC 133 semantic prompt marks recorded by the reader thread, in
    /// arrival order. Positions are stored as `(grid line, history size)`
    /// at record time; [`Self::command_marks`] translates to current grid
    /// lines (content scrolls into history as output arrives, so a stored
    /// line drifts by exactly the history growth since recording).
    marks: Arc<std::sync::Mutex<Vec<StoredMark>>>,
    /// Latest OSC 7 cwd report from the shell, when integration is active.
    osc7_cwd: Arc<std::sync::Mutex<Option<std::path::PathBuf>>>,
    /// Latest OSC 7 reporting host (SSH inside the pane moves it to the
    /// remote's hostname); feeds the per-host accent rules.
    osc7_host: Arc<std::sync::Mutex<Option<String>>>,
    /// Host-accent dressing resolved by the app against the prefs rules:
    /// border / pill color and the watermark badge.
    pub accent: Option<(u8, u8, u8)>,
    pub accent_badge: Option<String>,
    /// OSC 9 notification payloads awaiting the app's drain.
    notifications: Arc<std::sync::Mutex<Vec<String>>>,
    /// Latest OSC 9;4 progress report `(state, percent)`: 1 normal,
    /// 2 error, 3 indeterminate, 4 warning. `None` when idle (state 0,
    /// command end, or never reported). Drives the bottom-border gauge.
    progress: Arc<std::sync::Mutex<Option<(u8, u8)>>>,
    /// Arrival time (epoch millis) per grid row, keyed by the row's stable
    /// absolute id (`current line + history size`, constant as content
    /// scrolls). Stamped by the reader thread at chunk granularity; cleared
    /// wholesale when the scrollback is wiped (the ids restart).
    line_times: Arc<std::sync::Mutex<std::collections::BTreeMap<i64, u64>>>,
    /// Paint the right-edge HH:MM:SS gutter (the "Terminal: Toggle
    /// Timestamps" palette command flips this on every pane).
    pub show_timestamps: bool,
    /// User notes pinned to output spans (Cmd+K N on a selection).
    /// Session-scoped, like the scrollback they describe.
    annotations: Vec<PaneAnnotation>,
    /// Finished commands (exit, duration) from the reader thread, awaiting
    /// the app's drain for long-command notifications.
    finished_rx: std::sync::mpsc::Receiver<FinishedCommand>,
    /// Quick-select hint spans pushed down by the app while hint mode is
    /// active, paired with the scroll-clock reading their lines were
    /// captured at: the render loop re-bases each span every frame so the
    /// labels follow their matches through streaming output instead of
    /// squatting on fixed viewport rows. `None` when quick-select is off.
    hints: Option<(i64, Vec<HintSpan>)>,
    /// The user's trigger set, shared with the reader thread (which scans
    /// completed lines for notify/bell firings) and read by the render loop
    /// (which paints highlight-trigger matches). The inner Arc is swapped by
    /// [`Self::set_triggers`] on startup and config reload.
    triggers: Arc<std::sync::Mutex<std::sync::Arc<crate::triggers::TriggerSet>>>,
    /// Notify/bell trigger firings from the reader thread, awaiting the
    /// app's drain into the status bar.
    trigger_rx: std::sync::mpsc::Receiver<crate::triggers::TriggerHit>,
    /// The theme's 16 ANSI colors; Named and Indexed 0-15 cell colors render
    /// through it so panes look the same on every host terminal (VS Code
    /// owns its terminal palette the same way). Synced by the app's theme
    /// pass via [`Self::set_palette`].
    palette: [(u8, u8, u8); 16],
    /// Inline images captured from the pane's output (iTerm2 OSC 1337
    /// `inline=1`, the imgcat protocol), anchored like marks. Capped at
    /// [`IMAGES_MAX`]; the app overlays the newest visible one.
    images: Arc<std::sync::Mutex<Vec<StoredImage>>>,
    /// Test-only capture of every byte written toward the child's stdin.
    #[cfg(test)]
    written_for_test: Arc<std::sync::Mutex<Vec<u8>>>,
}

/// One captured inline image at its recording-time anchor, keyed on the
/// scroll clock like annotations (current line =
/// `line_rec - (clock_now - clock_rec)`), NOT on `history_size`, whose
/// saturation froze the picture onto a viewport row in long-lived panes.
struct StoredImage {
    seq: u64,
    data: std::sync::Arc<Vec<u8>>,
    line_rec: i32,
    clock_rec: i64,
}

/// A pane inline image surfaced to the app: its per-pane id, the raw image
/// bytes, and the anchor's CURRENT grid line (negative = scrolled into
/// history).
#[derive(Clone)]
pub struct PaneImage {
    pub seq: u64,
    pub data: std::sync::Arc<Vec<u8>>,
    pub line: i32,
}

/// Most inline images kept per pane; older ones scroll away like text.
const IMAGES_MAX: usize = 4;

/// One quick-select hint for the render loop: the match span on absolute
/// grid line `line` (char indices into the spacer-skipped row text; the
/// colmap translates back to grid columns), the label overlaid at its start,
/// and how many label chars the user has already typed (those are consumed,
/// only the remainder renders).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HintSpan {
    pub line: i32,
    pub start: usize,
    pub len: usize,
    pub label: String,
    pub typed: usize,
}

/// One OSC 133 mark at its recording-time anchor, keyed on the scroll
/// clock like annotations and images (current position =
/// `line_rec - (clock_now - clock_rec)`), NOT on `history_size`, whose
/// saturation froze surviving marks onto viewport rows and whose zeroing
/// on a scrollback wipe inverted the math outright. Marks past the
/// scrollback floor are GC'd in `marks_snapshot`.
struct StoredMark {
    kind: crate::shell_integration::OscEvent,
    line_rec: i32,
    clock_rec: i64,
    /// Cursor column when the mark landed (`PromptEnd`'s column is where the
    /// typed command starts on its prompt line).
    col_rec: usize,
    /// For `CommandEnd` marks: how long the command ran (CommandStart →
    /// CommandEnd, measured in the reader thread). `None` elsewhere.
    dur: Option<std::time::Duration>,
}

/// A finished command as reported by the reader thread at its `133;D`
/// mark: exit + duration (for the long-command notice) plus the typed
/// command text (extracted from the B→C mark span while the term lock is
/// held) and the pane's OSC 7 cwd at finish time — everything the durable
/// command history records.
#[derive(Clone, Debug)]
pub struct FinishedCommand {
    pub exit: Option<i32>,
    pub dur: std::time::Duration,
    pub cmd: String,
    pub cwd: Option<std::path::PathBuf>,
    /// The OSC 7 reporting hostname AT COMPLETION time. Captured with the
    /// 133;D mark, not read later at drain: an in-pane `ssh`/`exit` between
    /// the two would attribute the command to the wrong machine.
    pub host: Option<String>,
    /// The command's output (the 133;C→133;D span), escape-free, captured at
    /// the D mark while the rows are guaranteed still present. Capped to the
    /// LAST [`FINISHED_OUTPUT_CAP_LINES`] lines — the tail keeps a compiler's
    /// error summary; a build long enough to overflow the cap loses its
    /// earliest lines. Feeds the build problem matchers (#119).
    pub output: String,
}

/// Tail cap on [`FinishedCommand::output`].
pub const FINISHED_OUTPUT_CAP_LINES: usize = 5000;

/// A finished command derived from the OSC 133 marks: the grid line of the
/// prompt it was typed at (current coords, negative = scrollback), its exit
/// code (`None` when the shell omitted it), how long it ran, where its typed
/// text starts (`PromptEnd` line + column), and its output span
/// (`CommandStart` line up to but excluding the `CommandEnd` line).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CommandDecoration {
    pub line: i32,
    pub exit: Option<i32>,
    pub duration: Option<std::time::Duration>,
    pub input: Option<(i32, usize)>,
    pub output_start: i32,
    pub output_end: i32,
}

/// Pair the mark stream (oldest first, each with its current grid line and,
/// for `CommandEnd`, the measured duration) into one record per *finished*
/// command: the last `PromptStart` line before a `CommandStart` names the
/// row, the following `CommandEnd` supplies exit + duration. A `CommandEnd`
/// with no pending `CommandStart` is dropped — that's how a second
/// integration layer's duplicate marks (Ghostty's hooks chained behind
/// croft's) stay out of the record.
pub fn pair_decorations(
    marks: &[(
        crate::shell_integration::OscEvent,
        i32,
        usize,
        Option<std::time::Duration>,
    )],
) -> Vec<CommandDecoration> {
    use crate::shell_integration::OscEvent as E;
    let mut out = Vec::new();
    let mut prompt: Option<i32> = None;
    let mut input: Option<(i32, usize)> = None;
    let mut started: Option<i32> = None;
    for (kind, line, col, dur) in marks {
        match kind {
            E::PromptStart => {
                prompt = Some(*line);
                input = None;
            }
            E::PromptEnd => input = Some((*line, *col)),
            E::CommandStart => started = Some(*line),
            E::CommandEnd(exit) => {
                if let Some(output_start) = started.take() {
                    out.push(CommandDecoration {
                        line: prompt.unwrap_or(output_start),
                        exit: *exit,
                        duration: *dur,
                        input,
                        output_start,
                        output_end: *line,
                    });
                }
            }
            _ => {}
        }
    }
    out
}

/// Compact human form of a command duration: "480ms", "3.4s", "2m 05s".
pub fn human_duration(d: std::time::Duration) -> String {
    let secs = d.as_secs_f64();
    if secs < 1.0 {
        format!("{}ms", d.as_millis())
    } else if secs < 60.0 {
        format!("{secs:.1}s")
    } else {
        format!("{}m {:02}s", d.as_secs() / 60, d.as_secs() % 60)
    }
}

/// Cap on retained marks per pane (a mark per prompt/command boundary;
/// thousands would mean a very long session — drop the oldest).
const MARKS_MAX: usize = 2000;

/// The nearest prompt line strictly above (`forward == false`) or below
/// (`forward == true`) the viewport-top grid line `current_top`. Feeds
/// Cmd+Up / Cmd+Down command navigation.
pub fn pick_prompt_jump(prompt_lines: &[i32], current_top: i32, forward: bool) -> Option<i32> {
    if forward {
        prompt_lines
            .iter()
            .copied()
            .filter(|&l| l > current_top)
            .min()
    } else {
        prompt_lines
            .iter()
            .copied()
            .filter(|&l| l < current_top)
            .max()
    }
}

/// Inject croft's shell-integration environment for supported shells, and
/// return any argv to prepend before the login flag.
/// zsh: point `ZDOTDIR` at croft's shim. bash: the kitty/Ghostty trick —
/// `--posix` plus `ENV` pointing at croft's shim (posix-mode bash reads
/// only `$ENV`; the shim backs out and replays the real startup files).
/// fish: prepend a `vendor_conf.d` data dir to `XDG_DATA_DIRS`; the script
/// defers to fish 4's native marks. Every shim sources the user's real
/// dotfiles unchanged and emits OSC 133 prompt marks + the OSC 7 cwd
/// report. Opt out with `CROFT_SHELL_INTEGRATION=0`. Failures are
/// non-fatal — the shell still spawns, just without marks.
fn apply_shell_integration_env(
    cmd: &mut CommandBuilder,
    shell_path: &str,
    config_dir: &std::path::Path,
) -> Vec<String> {
    if std::env::var("CROFT_SHELL_INTEGRATION").as_deref() == Ok("0") {
        return Vec::new();
    }
    let base = std::path::Path::new(shell_path)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("");
    match base {
        "zsh" => {
            let Ok(shim) = crate::shell_integration::ensure_zsh_shim(config_dir) else {
                return Vec::new();
            };
            // An inherited ZDOTDIR pointing at croft's own shim (croft
            // launched from a croft pane) is poisoned — the user's dotfiles
            // live in HOME.
            let inherited = std::env::var_os("ZDOTDIR").map(std::path::PathBuf::from);
            let home = std::env::var_os("HOME")
                .map(std::path::PathBuf::from)
                .unwrap_or_default();
            if home.as_os_str().is_empty() {
                return Vec::new();
            }
            let user_zdotdir = crate::shell_integration::resolve_user_zdotdir(
                inherited.as_deref(),
                &config_dir.join("shell-integration"),
                &home,
            );
            cmd.env("CROFT_USER_ZDOTDIR", user_zdotdir);
            cmd.env("ZDOTDIR", &shim);
            Vec::new()
        }
        "bash" => {
            if !bash_env_injection_supported(shell_path) {
                return Vec::new();
            }
            let Ok(shim) = crate::shell_integration::ensure_bash_shim(config_dir) else {
                return Vec::new();
            };
            // Preserve a user $ENV (rare for bash) for the shim to restore.
            if let Ok(env) = std::env::var("ENV") {
                cmd.env("CROFT_BASH_ENV", env);
            }
            cmd.env("ENV", &shim);
            cmd.env("CROFT_BASH_INJECT", "1");
            // Posix mode defaults HISTFILE to ~/.sh_history; pre-seed the
            // bash default and let the shim unexport it.
            if std::env::var_os("HISTFILE").is_none()
                && let Some(home) = std::env::var_os("HOME")
            {
                cmd.env(
                    "HISTFILE",
                    std::path::Path::new(&home).join(".bash_history"),
                );
                cmd.env("CROFT_BASH_UNEXPORT_HISTFILE", "1");
            }
            vec!["--posix".to_string()]
        }
        "fish" => {
            let Ok(dir) = crate::shell_integration::ensure_fish_integration(config_dir) else {
                return Vec::new();
            };
            let inherited = std::env::var("XDG_DATA_DIRS").ok();
            cmd.env(
                "XDG_DATA_DIRS",
                crate::shell_integration::fish_xdg_data_dirs(&dir, inherited.as_deref()),
            );
            cmd.env("CROFT_FISH_XDG_DATA_DIR", &dir);
            Vec::new()
        }
        _ => Vec::new(),
    }
}

/// Whether this bash reads `$ENV` in posix mode, i.e. the injection works
/// at all. Verified empirically: macOS's system bash 3.2 never reads $ENV
/// under `--posix`, so injecting would strip its startup files for
/// nothing; Homebrew/Linux 5.x does. 4.4 is the ecosystem floor (kitty,
/// Ghostty). One short probe subprocess per pane spawn — pane creation
/// already forks a shell, so this is noise.
fn bash_env_injection_supported(shell_path: &str) -> bool {
    let Ok(out) = std::process::Command::new(shell_path)
        .args(["-c", "echo \"${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}\""])
        .output()
    else {
        return false;
    };
    parse_bash_version_supported(&String::from_utf8_lossy(&out.stdout))
}

/// `major.minor` >= 4.4, tolerant of anything a non-bash prints.
fn parse_bash_version_supported(s: &str) -> bool {
    let mut it = s.trim().split('.');
    let (Some(maj), Some(min)) = (
        it.next().and_then(|v| v.parse::<u32>().ok()),
        it.next().and_then(|v| v.parse::<u32>().ok()),
    ) else {
        return false;
    };
    maj > 4 || (maj == 4 && min >= 4)
}

/// Pick the program + args to spawn the user's interactive shell so it
/// behaves like the one iTerm2 / Terminal.app launches. Both run the
/// shell as a *login* shell (`Login shell` is iTerm2's default Command
/// setting), which sources `~/.zprofile` / `~/.profile` in addition to
/// the interactive rc file. Without that, anything the user puts in
/// `.zprofile` — PATH munging, framework loaders, plugin managers like
/// zinit/oh-my-zsh that install keybindings, vi-mode toggles — is
/// silently absent inside croft. For mainstream POSIX shells we pass
/// `-l`; for anything exotic we leave argv empty rather than risk
/// breaking the spawn with an unknown flag.
pub fn interactive_shell_invocation(shell_path: &str) -> (String, Vec<String>) {
    let basename = std::path::Path::new(shell_path)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or(shell_path);
    let supports_login_flag = matches!(
        basename,
        "zsh" | "bash" | "fish" | "ksh" | "mksh" | "dash" | "tcsh" | "sh"
    );
    let args = if supports_login_flag {
        vec!["-l".to_string()]
    } else {
        Vec::new()
    };
    (shell_path.to_string(), args)
}

/// Parse `/etc/shells` into `(path, basename)` terminal profiles: skip comments
/// and blanks, keep absolute paths only, and dedupe by basename (first wins, so
/// `/bin/bash` shadows `/usr/local/bin/bash`). The basename is the label shown.
pub fn parse_shells(contents: &str) -> Vec<(String, String)> {
    let mut out: Vec<(String, String)> = Vec::new();
    for line in contents.lines() {
        let line = line.trim();
        if line.is_empty() || !line.starts_with('/') {
            continue;
        }
        let base = std::path::Path::new(line)
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or(line)
            .to_string();
        if out.iter().any(|(_, b)| *b == base) {
            continue;
        }
        out.push((line.to_string(), base));
    }
    out
}

/// Resolve a pid to its command name (basename), cross-platform, via `sysinfo`
/// (no subprocess). Used to label a terminal pane with its foreground process.
/// Refreshes only the one pid, so it's cheap, but still runs off the event loop.
pub fn process_name(pid: i32) -> Option<String> {
    use sysinfo::{Pid, ProcessesToUpdate, System};
    if pid <= 0 {
        return None;
    }
    let p = Pid::from(pid as usize);
    let mut sys = System::new();
    sys.refresh_processes(ProcessesToUpdate::Some(&[p]), true);
    sys.process(p)
        .map(|pr| pr.name().to_string_lossy().to_string())
        .filter(|n| !n.is_empty())
}

/// Live cwd of a running process by PID, or None when the platform doesn't
/// expose one. Used by `split_terminal` so a new pane lands wherever the
/// user has `cd`'d the active shell, and by [`PtyTerminal::local_shell_cwd`]
/// as the ground truth an OSC 7 claim must match to be trusted.
#[cfg(target_os = "linux")]
pub fn cwd_of_pid(pid: u32) -> Option<std::path::PathBuf> {
    std::fs::read_link(format!("/proc/{pid}/cwd")).ok()
}

/// macOS: call `proc_pidinfo` with `PROC_PIDVNODEPATHINFO` directly via the
/// libSystem FFI instead of shelling out to `lsof -d cwd`. Two reasons:
///   1. `lsof` on Sonoma+ tickles the "App Management" / "App Data" TCC
///      privacy class (the OS sees the responsible parent process — iTerm
///      — inspecting another process's open files and prompts the user).
///      `proc_pidinfo` against our own child PID needs no TCC entitlement.
///   2. No fork/exec on the hot path of every terminal split.
///
/// Struct layout matches `<sys/proc_info.h>`. We read the path field of
/// `pvi_cdir` (the cwd vnode) at the documented offset.
#[cfg(target_os = "macos")]
pub fn cwd_of_pid(pid: u32) -> Option<std::path::PathBuf> {
    use std::ffi::OsString;
    use std::os::raw::{c_int, c_void};
    use std::os::unix::ffi::OsStringExt;

    const PROC_PIDVNODEPATHINFO: c_int = 9;
    const MAXPATHLEN: usize = 1024;

    #[repr(C)]
    #[derive(Default, Clone, Copy)]
    struct VinfoStat {
        vst_dev: u32,
        vst_mode: u16,
        vst_nlink: u16,
        vst_ino: u64,
        vst_uid: u32,
        vst_gid: u32,
        vst_atime: i64,
        vst_atimensec: i64,
        vst_mtime: i64,
        vst_mtimensec: i64,
        vst_ctime: i64,
        vst_ctimensec: i64,
        vst_birthtime: i64,
        vst_birthtimensec: i64,
        vst_size: i64,
        vst_blocks: i64,
        vst_blksize: i32,
        vst_flags: u32,
        vst_gen: u32,
        vst_rdev: u32,
        vst_qspare: [i64; 2],
    }

    #[repr(C)]
    #[derive(Clone, Copy)]
    struct VnodeInfo {
        vi_stat: VinfoStat,
        vi_type: i32,
        vi_pad: i32,
        vi_fsid: [i32; 2],
    }

    #[repr(C)]
    #[derive(Clone, Copy)]
    struct VnodeInfoPath {
        vip_vi: VnodeInfo,
        vip_path: [u8; MAXPATHLEN],
    }

    #[repr(C)]
    #[derive(Clone, Copy)]
    struct ProcVnodePathInfo {
        pvi_cdir: VnodeInfoPath,
        pvi_rdir: VnodeInfoPath,
    }

    unsafe extern "C" {
        fn proc_pidinfo(
            pid: c_int,
            flavor: c_int,
            arg: u64,
            buffer: *mut c_void,
            buffersize: c_int,
        ) -> c_int;
    }

    let mut info: ProcVnodePathInfo = unsafe { std::mem::zeroed() };
    let size = std::mem::size_of::<ProcVnodePathInfo>() as c_int;
    let ret = unsafe {
        proc_pidinfo(
            pid as c_int,
            PROC_PIDVNODEPATHINFO,
            0,
            &mut info as *mut _ as *mut c_void,
            size,
        )
    };
    if ret <= 0 {
        return None;
    }
    let path = &info.pvi_cdir.vip_path;
    let len = path.iter().position(|&b| b == 0).unwrap_or(0);
    if len == 0 {
        return None;
    }
    Some(std::path::PathBuf::from(OsString::from_vec(
        path[..len].to_vec(),
    )))
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
pub fn cwd_of_pid(_pid: u32) -> Option<std::path::PathBuf> {
    None
}

/// The label to show for a pane: a manual name wins, else the live foreground
/// process name.
pub fn pick_pane_label<'a>(manual: Option<&'a str>, auto: &'a str) -> &'a str {
    manual.unwrap_or(auto)
}

impl PtyTerminal {
    pub fn new(cwd: &std::path::Path) -> Result<Self> {
        let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/bash".to_string());
        Self::new_shell(&shell, cwd)
    }

    /// Spawn an interactive login session for a specific shell (a terminal
    /// profile), rather than `$SHELL`. The same login-flag handling as [`new`].
    pub fn new_shell(shell_path: &str, cwd: &std::path::Path) -> Result<Self> {
        let (program, args) = interactive_shell_invocation(shell_path);
        let mut cmd = CommandBuilder::new(&program);
        // Integration args go first: bash only recognises long options
        // (--posix) when they precede single-character ones (-l).
        let pre = apply_shell_integration_env(&mut cmd, shell_path, &crate::prefs::config_dir());
        for a in pre.iter().chain(args.iter()) {
            cmd.arg(a);
        }
        cmd.cwd(cwd);
        Self::spawn_with(cmd, None)
    }

    pub fn new_running(program: &str, args: &[String], cwd: &std::path::Path) -> Result<Self> {
        let mut cmd = CommandBuilder::new(program);
        for a in args {
            cmd.arg(a);
        }
        cmd.cwd(cwd);
        let label = if args.is_empty() {
            program.to_string()
        } else {
            format!("{program} {}", args.join(" "))
        };
        Self::spawn_with(cmd, Some(label))
    }

    pub fn visible_text(&self) -> String {
        let term = self.term.lock();
        let rows = term.screen_lines();
        let cols = term.columns();
        if rows == 0 || cols == 0 {
            return String::new();
        }
        let off = term.grid().display_offset() as i32;
        extract_selection_text(&term, -off, 0, rows as i32 - 1 - off, cols - 1)
    }

    /// Every readable grid line as plain text — oldest scrollback first, then
    /// the live screen — paired with the absolute alacritty `Line` index of
    /// row 0. Terminal find searches these lines; `top + row` maps a hit's
    /// row back to its grid line so it can be scrolled into view and
    /// highlighted. Wide-char spacer cells are skipped (see
    /// [`row_text_and_cols`]), so match positions are char indices, not grid
    /// columns.
    pub fn grid_lines(&self) -> (Vec<String>, i32) {
        let term = self.term.lock();
        if term.columns() == 0 {
            return (Vec::new(), 0);
        }
        let top = term.grid().topmost_line().0;
        let bottom = term.screen_lines() as i32 - 1;
        let mut lines = Vec::new();
        let mut l = top;
        while l <= bottom {
            let (s, _cols) = row_text_and_cols(&term, l);
            lines.push(s.trim_end().to_string());
            l += 1;
        }
        (lines, top)
    }

    /// [`Self::grid_lines`] plus the scroll-clock reading, captured under
    /// ONE term lock. Terminal find stamps its match anchor with the clock;
    /// separate reads would let output land between them and mis-pair a
    /// line with a reading it wasn't captured at.
    pub fn grid_lines_and_clock(&self) -> (Vec<String>, i32, i64) {
        let term = self.term.lock();
        if term.columns() == 0 {
            return (Vec::new(), 0, self.clock_now(&term));
        }
        let top = term.grid().topmost_line().0;
        let bottom = term.screen_lines() as i32 - 1;
        let mut lines = Vec::new();
        let mut l = top;
        while l <= bottom {
            let (s, _cols) = row_text_and_cols(&term, l);
            lines.push(s.trim_end().to_string());
            l += 1;
        }
        (lines, top, self.clock_now(&term))
    }

    /// The OSC 8 hyperlink URI stored under viewport cell `(row, col)`, if
    /// any. Hyperlinked cells carry the URI invisibly; the app's
    /// Cmd/Ctrl+click handler checks this before the plain-text URL regex.
    pub fn hyperlink_at(&self, row: usize, col: usize) -> Option<String> {
        let term = self.term.lock();
        if col >= term.columns() {
            return None;
        }
        let line_idx = row as i32 - term.grid().display_offset() as i32;
        if line_idx < term.grid().topmost_line().0 || line_idx >= term.screen_lines() as i32 {
            return None;
        }
        term.grid()[Point::new(Line(line_idx), Column(col))]
            .hyperlink()
            .map(|h| h.uri().to_string())
    }

    /// Set (or clear) the find highlight. Every occurrence of `needle` is
    /// painted across the visible grid on the next render.
    pub fn set_search(&mut self, needle: Option<String>, opts: crate::widgets::search::SearchOpts) {
        self.search_needle = needle.filter(|s| !s.is_empty());
        self.search_opts = opts;
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// Mark which occurrence is the active match `(abs_line, col, len)` so the
    /// render loop paints it in the brighter accent. `clock` is the
    /// scroll-clock reading the line was captured against (the
    /// [`Self::grid_lines_and_clock`] snapshot); the render re-bases per
    /// frame so the highlight follows its text through streaming output.
    pub fn set_current_match(&mut self, m: Option<(i32, usize, usize)>, clock: i64) {
        self.current_match = m.map(|(line, col, len)| (clock, line, col, len));
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// Set (or clear) the quick-select hint overlay. The app owns hint-mode
    /// state and re-pushes the filtered set as the user types label chars;
    /// `clock` is the scroll-clock reading the span lines were captured at
    /// (from [`Self::visible_lines_and_clock`]), reused verbatim on every
    /// re-push so the render-time translation stays anchored to the open.
    pub fn set_hints(&mut self, hints: Option<Vec<HintSpan>>, clock: i64) {
        self.hints = hints.map(|h| (clock, h));
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// The visible viewport rows as spacer-skipped plain text, paired with
    /// the absolute grid line of the first row (`-display_offset`).
    /// Quick-select scans exactly what is on screen — labels the user cannot
    /// see cannot be typed.
    pub fn visible_lines(&self) -> (Vec<String>, i32) {
        let term = self.term.lock();
        if term.columns() == 0 {
            return (Vec::new(), 0);
        }
        let off = term.grid().display_offset() as i32;
        let rows = term.screen_lines() as i32;
        let mut lines = Vec::new();
        for l in -off..rows - off {
            let (s, _cols) = row_text_and_cols(&term, l);
            lines.push(s.trim_end().to_string());
        }
        (lines, -off)
    }

    /// [`Self::visible_lines`] plus per-row soft-wrap flags and the scroll
    /// clock, all from ONE term lock: coordinates and clock captured
    /// together so output landing between two separate reads can never
    /// mis-pair them (quick-select anchors its labels on this snapshot).
    /// `wraps[i]` says row `i` continues into row `i + 1` (WRAPLINE on its
    /// last cell); a wrapping row keeps its trailing spaces — they are real
    /// cells mid-line, and trimming them would glue the next row's first
    /// word onto this one when the rows are stitched for scanning (#64).
    pub fn visible_lines_and_clock(&mut self) -> (Vec<String>, Vec<bool>, i32, i64) {
        let mut term = self.term.lock();
        let clock = self.clock.lock().unwrap().tick(&mut term);
        if term.columns() == 0 {
            return (Vec::new(), Vec::new(), 0, clock);
        }
        let off = term.grid().display_offset() as i32;
        let rows = term.screen_lines() as i32;
        let cols = term.columns();
        let mut lines = Vec::new();
        let mut wraps = Vec::new();
        for l in -off..rows - off {
            let (s, _cols) = row_text_and_cols(&term, l);
            let wrapped = term.grid()[Point::new(Line(l), Column(cols - 1))]
                .flags
                .contains(Flags::WRAPLINE);
            lines.push(if wrapped { s } else { s.trim_end().to_string() });
            wraps.push(wrapped);
        }
        (lines, wraps, -off, clock)
    }

    /// Swap in a new trigger set. The app calls this for every pane on every
    /// drain tick (so a pane is covered no matter where it was created); the
    /// ptr-eq check makes the steady state a cheap no-op that never dirties
    /// the pane. The reader thread picks a new set up on its next chunk; the
    /// render loop on its next frame.
    pub fn set_triggers(&self, set: std::sync::Arc<crate::triggers::TriggerSet>) {
        let mut cur = self.triggers.lock().unwrap();
        if std::sync::Arc::ptr_eq(&cur, &set) {
            return;
        }
        *cur = set;
        drop(cur);
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// Notify/bell trigger firings recorded by the reader thread since the
    /// last drain.
    pub fn drain_trigger_hits(&self) -> Vec<crate::triggers::TriggerHit> {
        self.trigger_rx.try_iter().collect()
    }

    /// Swap the ANSI palette the render loop maps Named/Indexed 0-15 cell
    /// colors through (the theme sync calls this every pass; unchanged
    /// palettes are a no-op so the pane never dirties spuriously).
    pub fn set_palette(&mut self, palette: [(u8, u8, u8); 16]) {
        if self.palette != palette {
            self.palette = palette;
            self.pty_dirty.store(true, Ordering::Release);
        }
    }

    /// Inline images captured from the pane's output, oldest first, each
    /// with its anchor's CURRENT grid line (drift model identical to command
    /// marks). Images whose anchor scrolled past the scrollback floor are
    /// garbage-collected here.
    pub fn pane_images(&self) -> Vec<PaneImage> {
        let mut term = self.term.lock();
        let now = self.clock.lock().unwrap().tick(&mut term);
        let floor = term.grid().topmost_line().0;
        drop(term);
        let mut imgs = self.images.lock().unwrap();
        imgs.retain(|m| m.line_rec - (now - m.clock_rec) as i32 >= floor);
        imgs.iter()
            .map(|m| PaneImage {
                seq: m.seq,
                data: m.data.clone(),
                line: m.line_rec - (now - m.clock_rec) as i32,
            })
            .collect()
    }

    /// Whether the pane is in the alternate screen (a full-screen app owns
    /// the viewport; anchored overlays make no sense there).
    pub fn alt_screen(&self) -> bool {
        self.term.lock().mode().contains(TermMode::ALT_SCREEN)
    }

    /// Whether the child enabled application cursor keys (DECCKM, `\e[?1h`).
    /// Arrows and Home/End must then arrive as SS3 (`\eOA`…) — the form
    /// terminfo advertises, and the only one apps that bind keys from
    /// terminfo (Python's REPL, less) recognize.
    pub fn app_cursor_keys(&self) -> bool {
        self.term.lock().mode().contains(TermMode::APP_CURSOR)
    }

    /// The viewport's scroll offset into history (0 = live bottom). Public
    /// so the app can map an anchor's grid line to a viewport row.
    pub fn scroll_display_offset(&self) -> i32 {
        self.display_offset()
    }

    /// Scroll the viewport so absolute grid line `abs_line` sits near the
    /// middle of the pane, clamped to the scrollback range. No-op in
    /// alternate screen, where there is no scrollback to move through and the
    /// whole grid is already on screen.
    pub fn scroll_to_line(&mut self, abs_line: i32) {
        let mut term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return;
        }
        let rows = term.screen_lines() as i32;
        let max_off = (-term.grid().topmost_line().0).max(0);
        let cur = term.grid().display_offset() as i32;
        let desired = scroll_offset_for_line(rows, max_off, abs_line);
        let delta = desired - cur;
        if delta != 0 {
            term.scroll_display(Scroll::Delta(delta));
        }
        drop(term);
        self.pty_dirty.store(true, Ordering::Release);
    }

    fn spawn_with(mut cmd: CommandBuilder, run_label: Option<String>) -> Result<Self> {
        let pty_system = native_pty_system();
        let cols = 80u16;
        let rows = 24u16;
        let pair = pty_system
            .openpty(PtySize {
                cols,
                rows,
                pixel_width: 0,
                pixel_height: 0,
            })
            .context("openpty")?;

        cmd.env("TERM", "xterm-256color");
        cmd.env("COLORTERM", "truecolor");
        let child = pair.slave.spawn_command(cmd).context("spawn child")?;
        let shell_pid = child.process_id().map(|p| p as i32);
        drop(pair.slave);

        let writer: Arc<std::sync::Mutex<Box<dyn Write + Send>>> = Arc::new(std::sync::Mutex::new(
            pair.master.take_writer().context("take writer")?,
        ));
        let mut reader = pair.master.try_clone_reader().context("clone reader")?;

        let term_size = TermSize::new(cols as usize, rows as usize);
        let cfg = Config {
            // The user's `terminal_scrollback` (config.json) when set,
            // else the built-in default. Read per spawn so a settings edit
            // applies to the next pane without a relaunch.
            scrolling_history: crate::prefs::terminal_scrollback_lines(
                crate::prefs::Prefs::load_or_default().terminal_scrollback,
                SCROLLBACK_LINES,
            ),
            ..Config::default()
        };
        let size_shared = Arc::new(std::sync::Mutex::new((cols, rows)));
        let (response_tx, response_rx) = std::sync::mpsc::channel::<String>();
        let bell = Arc::new(AtomicBool::new(false));
        let listener = VoidListener {
            pty_response_tx: Some(response_tx),
            size: Some(size_shared.clone()),
            bell: Some(bell.clone()),
        };
        let term = Term::new(cfg, &term_size, listener);
        let term = Arc::new(FairMutex::new(term));
        let term_for_thread = term.clone();
        let writer_for_responder = writer.clone();

        std::thread::spawn(move || {
            while let Ok(text) = response_rx.recv() {
                let Ok(mut w) = writer_for_responder.lock() else {
                    break;
                };
                if w.write_all(text.as_bytes()).is_err() {
                    break;
                }
                if w.flush().is_err() {
                    break;
                }
            }
        });

        let pty_dirty = Arc::new(AtomicBool::new(true));
        let pty_dirty_for_thread = pty_dirty.clone();
        let pty_pending_bytes = Arc::new(AtomicUsize::new(0));
        let pty_pending_bytes_for_thread = pty_pending_bytes.clone();
        let bracketed_paste_enabled = Arc::new(AtomicBool::new(false));
        let bracketed_paste_for_thread = bracketed_paste_enabled.clone();
        let (port_tx, port_rx) = std::sync::mpsc::channel::<crate::port_detect::PortHit>();

        if let Some(label) = run_label.as_deref() {
            let header = format!("\x1b[2m▶ {label}\x1b[22m\r\n");
            let mut p = Processor::<StdSyncHandler>::new();
            let mut t = term.lock();
            p.advance(&mut *t, header.as_bytes());
        }

        let script_mode = run_label.is_some();

        let marks = Arc::new(std::sync::Mutex::new(Vec::<StoredMark>::new()));
        let marks_for_thread = marks.clone();
        let images = Arc::new(std::sync::Mutex::new(Vec::<StoredImage>::new()));
        let images_for_thread = images.clone();
        let (finished_tx, finished_rx) = std::sync::mpsc::channel::<FinishedCommand>();
        let osc7_cwd = Arc::new(std::sync::Mutex::new(Option::<std::path::PathBuf>::None));
        let osc7_for_thread = osc7_cwd.clone();
        let notifications = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let notifications_for_thread = notifications.clone();
        let progress = Arc::new(std::sync::Mutex::new(None::<(u8, u8)>));
        let progress_for_thread = progress.clone();
        let line_times = Arc::new(std::sync::Mutex::new(
            std::collections::BTreeMap::<i64, u64>::new(),
        ));
        let line_times_for_thread = line_times.clone();
        let clock = Arc::new(std::sync::Mutex::new(ScrollClock::new()));
        let clock_for_thread = clock.clone();
        let osc7_host = Arc::new(std::sync::Mutex::new(None::<String>));
        let osc7_host_for_thread = osc7_host.clone();
        // Trigger set shared with the reader thread; the inner Arc is swapped
        // by `set_triggers` on startup / config reload, picked up per chunk.
        let triggers = Arc::new(std::sync::Mutex::new(std::sync::Arc::new(
            crate::triggers::TriggerSet::default(),
        )));
        let triggers_for_thread = triggers.clone();
        let (trigger_tx, trigger_rx) = std::sync::mpsc::channel::<crate::triggers::TriggerHit>();

        // Shutdown pipe + master fd for the reader's poll gate: the reader
        // must be wakeable without depending on the pty ever reaching EOF.
        let (shutdown_r, shutdown_w) = std::io::pipe().context("shutdown pipe")?;
        let pty_fd = pair
            .master
            .as_raw_fd()
            .context("pty master has no raw fd")?;
        let reader_thread = std::thread::spawn(move || {
            let mut processor = Processor::<StdSyncHandler>::new();
            let mut port_sniffer = crate::port_detect::PortSniffer::new();
            let mut osc_sniffer = crate::shell_integration::OscSniffer::default();
            let mut wipe_sniffer = WipeSniffer::default();
            let mut trigger_scanner = crate::triggers::TriggerScanner::new();
            let mut trigger_hits = Vec::new();
            // Per-pane monotonic id for captured inline images; the overlay
            // layout key uses it to tell a new picture from a moved one.
            let mut image_seq = 0u64;
            // Command timing: armed by 133;C, consumed by the next 133;D.
            // Content id of the cursor row after the previous chunk: the
            // rows this chunk touched run from there to the cursor's new
            // id, and each takes the chunk's arrival time (last touch wins,
            // so a row is stamped when its content actually landed, not
            // when the cursor first parked on it).
            let mut prev_stamp_id: i64 = 0;
            let mut cmd_start: Option<std::time::Instant> = None;
            let mut buf = [0u8; 65536];
            loop {
                if !wait_pty_readable(pty_fd, &shutdown_r) {
                    break;
                }
                match reader.read(&mut buf) {
                    Ok(0) => break,
                    Ok(n) => {
                        sniff_bracketed_paste_mode(&buf[..n], &bracketed_paste_for_thread);
                        port_sniffer.sniff(&buf[..n], &port_tx);
                        // Shell-integration marks: split the advance at each
                        // OSC 133 so the cursor can be sampled exactly where
                        // the mark landed (alacritty drops the sequences
                        // themselves as unknown OSC).
                        let osc_events = osc_sniffer.scan(&buf[..n]);
                        let mut t = term_for_thread.lock();
                        let (screen_wiped, hist_wiped) =
                            wipe_sniffer.scan(&buf[..n], t.mode().contains(TermMode::ALT_SCREEN));
                        let mut done = 0usize;
                        for (end, ev) in osc_events {
                            processor.advance(&mut *t, &buf[done..end]);
                            done = end;
                            use crate::shell_integration::OscEvent as E;
                            match ev {
                                E::Cwd(p, host) => {
                                    *osc7_for_thread.lock().unwrap() = Some(p);
                                    *osc7_host_for_thread.lock().unwrap() = host;
                                }
                                E::Notify(msg) => {
                                    notifications_for_thread.lock().unwrap().push(msg);
                                }
                                E::Progress(state, pct) => {
                                    // State 0 clears; the rest hold the latest
                                    // (state, percent) for the border gauge.
                                    *progress_for_thread.lock().unwrap() =
                                        (state != 0).then_some((state, pct));
                                }
                                E::InlineImage(data) => {
                                    // Anchor the picture at the cursor's grid
                                    // position, keyed on the scroll clock like
                                    // an annotation (lock order term → clock).
                                    let line_rec = t.grid().cursor.point.line.0;
                                    let clock_rec = clock_for_thread.lock().unwrap().tick(&mut t);
                                    let mut imgs = images_for_thread.lock().unwrap();
                                    if imgs.len() >= IMAGES_MAX {
                                        let drop_n = imgs.len() + 1 - IMAGES_MAX;
                                        imgs.drain(..drop_n);
                                    }
                                    image_seq += 1;
                                    imgs.push(StoredImage {
                                        seq: image_seq,
                                        data: std::sync::Arc::new(data),
                                        line_rec,
                                        clock_rec,
                                    });
                                }
                                kind @ (E::PromptStart
                                | E::PromptEnd
                                | E::CommandStart
                                | E::CommandEnd(_)) => {
                                    let dur = match &kind {
                                        E::CommandStart => {
                                            cmd_start = Some(std::time::Instant::now());
                                            None
                                        }
                                        E::CommandEnd(exit) => {
                                            // A finished command's gauge is
                                            // stale even if the program never
                                            // sent the state-0 clear.
                                            *progress_for_thread.lock().unwrap() = None;
                                            let dur = cmd_start.take().map(|s| s.elapsed());
                                            if let Some(d) = dur {
                                                // The typed command text and the
                                                // shell's cwd travel with the
                                                // completion so the durable
                                                // history records context.
                                                let (cmd, output) = {
                                                    let now = clock_for_thread
                                                        .lock()
                                                        .unwrap()
                                                        .tick(&mut t);
                                                    let ms = marks_for_thread.lock().unwrap();
                                                    (
                                                        last_command_input_text(&t, &ms, now),
                                                        last_command_output_text(&t, &ms, now),
                                                    )
                                                };
                                                let cwd = osc7_for_thread.lock().unwrap().clone();
                                                let host =
                                                    osc7_host_for_thread.lock().unwrap().clone();
                                                let _ = finished_tx.send(FinishedCommand {
                                                    exit: *exit,
                                                    dur: d,
                                                    cmd,
                                                    cwd,
                                                    host,
                                                    output,
                                                });
                                            }
                                            dur
                                        }
                                        _ => None,
                                    };
                                    let line_rec = t.grid().cursor.point.line.0;
                                    let clock_rec = clock_for_thread.lock().unwrap().tick(&mut t);
                                    let col_rec = t.grid().cursor.point.column.0;
                                    let mut ms = marks_for_thread.lock().unwrap();
                                    if ms.len() >= MARKS_MAX {
                                        let drop_n = ms.len() + 1 - MARKS_MAX;
                                        ms.drain(..drop_n);
                                    }
                                    ms.push(StoredMark {
                                        kind,
                                        line_rec,
                                        clock_rec,
                                        col_rec,
                                        dur,
                                    });
                                }
                            }
                        }
                        processor.advance(&mut *t, &buf[done..n]);
                        // A destructive clear erases the content the pane's
                        // captured images anchored to: ED 2 the live screen
                        // rows, ED 3 the scrollback ones, RIS both. The
                        // sniffer already excluded wipes that landed inside
                        // the alt screen (positional tracking), so no mode
                        // check here — the chunk's FINAL mode says nothing
                        // about where a wipe fell (`clear && vim`).
                        if screen_wiped || hist_wiped {
                            let now = clock_for_thread.lock().unwrap().tick(&mut t);
                            let erased = |line_rec: i32, clock_rec: i64| {
                                let line = line_rec - (now - clock_rec) as i32;
                                if line >= 0 { screen_wiped } else { hist_wiped }
                            };
                            images_for_thread
                                .lock()
                                .unwrap()
                                .retain(|m| !erased(m.line_rec, m.clock_rec));
                            // Marks anchored to erased content go the same
                            // way: a stale decoration dot's menu copies or
                            // re-runs whatever later lands on its row.
                            marks_for_thread
                                .lock()
                                .unwrap()
                                .retain(|m| !erased(m.line_rec, m.clock_rec));
                        }
                        // Stamp newly-arrived rows for the timestamps gutter:
                        // every row the cursor moved past in this chunk gets
                        // the chunk's arrival time (one read of output is
                        // sub-millisecond, so chunk granularity is honest).
                        {
                            let now_ms = std::time::SystemTime::now()
                                .duration_since(std::time::UNIX_EPOCH)
                                .map(|d| d.as_millis() as u64)
                                .unwrap_or(0);
                            let mut clock = clock_for_thread.lock().unwrap();
                            let mut lt = line_times_for_thread.lock().unwrap();
                            stamp_chunk(&mut t, &mut clock, &mut lt, &mut prev_stamp_id, now_ms);
                        }
                        // Notify/bell triggers match completed output lines,
                        // never inside a full-screen app (the alt screen owns
                        // the bytes; iTerm2 skips those too). The scanner
                        // tracks the alt boundary POSITIONALLY from the
                        // stream — the chunk's final mode says nothing about
                        // where a line fell — so it must see EVERY chunk, or
                        // its alt tracking and string state machine desync
                        // and skipped-gap output splices into phantom lines.
                        let trig = triggers_for_thread.lock().unwrap().clone();
                        trigger_scanner.scan(&buf[..n], &trig, &mut trigger_hits);
                        for h in trigger_hits.drain(..) {
                            let _ = trigger_tx.send(h);
                        }
                        drop(t);
                        pty_pending_bytes_for_thread.fetch_add(n, Ordering::Relaxed);
                        pty_dirty_for_thread.store(true, Ordering::Release);
                    }
                    Err(_) => break,
                }
            }
            if script_mode {
                let footer = b"\r\n\x1b[2m[Process exited]\x1b[22m\r\n";
                let mut t = term_for_thread.lock();
                processor.advance(&mut *t, footer);
                drop(t);
                pty_dirty_for_thread.store(true, Ordering::Release);
            }
        });

        Ok(Self {
            term,
            pty_dirty,
            pty_pending_bytes,
            bracketed_paste_enabled,
            port_rx,
            master: pair.master,
            writer,
            _child: child,
            reader_thread: Some(reader_thread),
            reader_shutdown: Some(shutdown_w),
            shell_pid,
            uid: {
                static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            },
            input_seen: false,
            run_pane: script_mode,
            manual_name_seen: false,
            cols,
            rows,
            size_shared,
            focused: false,
            broadcast_excluded: false,
            focus_gradient: false,
            last_area: Rect::default(),
            last_inner: Rect::default(),
            selection: None,
            copy_cursor: None,
            sel_scrolled: 0,
            clock,
            alt_sel: None,
            drag_selecting: false,
            manual_name: None,
            auto_label: String::new(),
            search_needle: None,
            search_opts: crate::widgets::search::SearchOpts::default(),
            current_match: None,
            bell,
            marks,
            osc7_cwd,
            osc7_host,
            accent: None,
            accent_badge: None,
            notifications,
            progress,
            line_times,
            show_timestamps: false,
            annotations: Vec::new(),
            finished_rx,
            hints: None,
            triggers,
            trigger_rx,
            palette: crate::theme::VSCODE_ANSI,
            images,
            #[cfg(test)]
            written_for_test: Arc::new(std::sync::Mutex::new(Vec::new())),
        })
    }

    /// OSC 133 marks with their *current* grid line (negative = scrollback),
    /// oldest first. Marks whose content scrolled past the scrollback floor
    /// are garbage-collected here.
    pub fn command_marks(&self) -> Vec<(crate::shell_integration::OscEvent, i32)> {
        self.marks_snapshot()
            .into_iter()
            .map(|(kind, line, _, _)| (kind, line))
            .collect()
    }

    /// The marks with current grid lines and, for `CommandEnd`, the measured
    /// command duration. GC + drift adjustment as in [`Self::command_marks`].
    fn marks_snapshot(
        &self,
    ) -> Vec<(
        crate::shell_integration::OscEvent,
        i32,
        usize,
        Option<std::time::Duration>,
    )> {
        let mut term = self.term.lock();
        let now = self.clock.lock().unwrap().tick(&mut term);
        let floor = term.grid().topmost_line().0;
        drop(term);
        let mut marks = self.marks.lock().unwrap();
        marks.retain(|m| m.line_rec - (now - m.clock_rec) as i32 >= floor);
        marks
            .iter()
            .map(|m| {
                (
                    m.kind.clone(),
                    m.line_rec - (now - m.clock_rec) as i32,
                    m.col_rec,
                    m.dur,
                )
            })
            .collect()
    }

    /// One record per finished command, for the gutter decorations: prompt
    /// line (current grid coords), exit code, duration.
    pub fn command_decorations(&self) -> Vec<CommandDecoration> {
        pair_decorations(&self.marks_snapshot())
    }

    /// Finished commands since the last drain: (exit code, duration). Feeds
    /// the status-bar notification for long commands in unfocused panes.
    pub fn drain_finished_commands(&self) -> Vec<FinishedCommand> {
        self.finished_rx.try_iter().collect()
    }

    /// The text a finished command printed: its output span (`output_start`
    /// up to but excluding `output_end`), trailing blank lines dropped.
    pub fn command_output_text(&self, d: &CommandDecoration) -> String {
        if d.output_end <= d.output_start {
            return String::new();
        }
        let term = self.term.lock();
        extract_selection_text(
            &term,
            d.output_start,
            0,
            d.output_end - 1,
            term.columns().saturating_sub(1),
        )
    }

    /// The command as it was typed at its prompt: from the `PromptEnd`
    /// mark's cell to the end of the input rows (the row before the output
    /// starts), soft-wrapped rows joined.
    pub fn command_input_text(&self, d: &CommandDecoration) -> String {
        let Some((line, col)) = d.input else {
            return String::new();
        };
        if d.output_start <= line {
            return String::new();
        }
        let term = self.term.lock();
        extract_selection_text(
            &term,
            line,
            col,
            d.output_start - 1,
            term.columns().saturating_sub(1),
        )
    }

    /// Select the command's output span (and snap the view to it), so the
    /// user sees exactly what Copy Output grabbed. No-op for empty output.
    pub fn select_command_output(&mut self, d: &CommandDecoration) {
        if d.output_end <= d.output_start {
            return;
        }
        self.selection = Some(Selection {
            anchor: (d.output_start, 0),
            head: (d.output_end - 1, self.cols.saturating_sub(1)),
            block: false,
        });
        self.stamp_selection_clock();
        self.scroll_to_line(d.output_start);
    }

    /// The decoration whose gutter dot sits at screen cell (col, row), if
    /// any: col must be the pane's left border column and row a viewport
    /// row whose grid line carries a finished command.
    pub fn decoration_at_screen(&self, col: u16, row: u16) -> Option<CommandDecoration> {
        let inner = self.last_inner;
        if col != self.last_area.x || row < inner.y || row >= inner.y + inner.height {
            return None;
        }
        let term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return None;
        }
        let display_offset = term.grid().display_offset() as i32;
        drop(term);
        let line = i32::from(row - inner.y) - display_offset;
        self.command_decorations()
            .into_iter()
            .find(|d| d.line == line)
    }

    /// Current grid lines of the PromptStart marks — the Cmd+Up/Cmd+Down
    /// navigation targets.
    pub fn prompt_lines(&self) -> Vec<i32> {
        self.command_marks()
            .into_iter()
            .filter(|(kind, _)| *kind == crate::shell_integration::OscEvent::PromptStart)
            .map(|(_, line)| line)
            .collect()
    }

    /// The shell's live cwd per its latest OSC 7 report, when shell
    /// integration is active. Fresher than sampling `cwd_of_pid`.
    pub fn shell_cwd(&self) -> Option<std::path::PathBuf> {
        self.osc7_cwd.lock().unwrap().clone()
    }

    /// The kernel-reported cwd of the pane's shell process. Unlike
    /// [`Self::shell_cwd`] it needs no shell integration, so it answers for
    /// any local pane; `None` when the pid or the kernel query is
    /// unavailable (android, a dead shell, a remote pane's ssh process).
    pub fn kernel_shell_cwd(&self) -> Option<std::path::PathBuf> {
        cwd_of_pid(u32::try_from(self.shell_pid?).ok()?)
    }

    /// OSC 9 notification payloads since the last drain.
    pub fn drain_notifications(&self) -> Vec<String> {
        std::mem::take(&mut *self.notifications.lock().unwrap())
    }

    /// The viewport-top grid line: what `pick_prompt_jump` navigates from.
    pub fn viewport_top_line(&self) -> i32 {
        -(self.term.lock().grid().display_offset() as i32)
    }

    /// Scroll so absolute grid line `abs_line` sits at the top of the pane
    /// (VS Code parks the jumped-to command at the top). No-op in alternate
    /// screen, like the other scrollback moves.
    pub fn scroll_line_to_top(&mut self, abs_line: i32) {
        let mut term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return;
        }
        let max_off = (-term.grid().topmost_line().0).max(0);
        let desired = (-abs_line).clamp(0, max_off);
        let delta = desired - term.grid().display_offset() as i32;
        if delta != 0 {
            term.scroll_display(Scroll::Delta(delta));
        }
        drop(term);
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// True once if the child rang BEL since the last call (drains the latch).
    pub fn take_bell(&self) -> bool {
        self.bell.swap(false, Ordering::AcqRel)
    }

    /// The pane's foreground process group leader pid (what owns the tty now):
    /// the shell at a prompt, or a running command. `None` if unavailable.
    pub fn foreground_pid(&self) -> Option<i32> {
        self.master.process_group_leader()
    }

    /// The shell's own pid, stable for this pane's lifetime (the key the app
    /// uses to match async label lookups back to the right pane).
    pub fn shell_pid(&self) -> Option<i32> {
        self.shell_pid
    }

    /// The label to show for this pane: manual name if set, else the live
    /// foreground-process label.
    pub fn label(&self) -> &str {
        pick_pane_label(self.manual_name.as_deref(), &self.auto_label)
    }

    /// Set the foreground-process label (from the off-loop refresh).
    pub fn set_auto_label(&mut self, label: String) {
        self.auto_label = label;
    }

    /// The user's manual pane name, when one was set via rename (what the
    /// session snapshot persists; `label()` mixes in the auto label).
    pub fn manual_name(&self) -> Option<&str> {
        self.manual_name.as_deref()
    }

    /// Set or clear the user's manual pane name (a blank name clears it).
    pub fn set_manual_name(&mut self, name: Option<String>) {
        self.manual_name = name.filter(|n| !n.trim().is_empty());
        if self.manual_name.is_some() {
            self.manual_name_seen = true;
        }
    }

    pub fn take_dirty(&self) -> bool {
        self.pty_pending_bytes.store(0, Ordering::Relaxed);
        self.pty_dirty.swap(false, Ordering::AcqRel)
    }

    /// The pane's live OSC 9;4 progress `(state, percent)`, if a program is
    /// reporting one.
    pub fn progress(&self) -> Option<(u8, u8)> {
        *self.progress.lock().unwrap()
    }

    /// The arrow-key bytes a plain click at screen cell (col, row) should
    /// send to walk the shell cursor to the clicked column (Ghostty's
    /// click-to-move-cursor). `Some` only when the shell is sitting at a
    /// prompt (the newest OSC 133 mark is B, on this same row), the click
    /// lands on the cursor's own row, and the target cell differs from the
    /// cursor. The target clamps to the typed span: never left of where
    /// input starts, never past one cell after the row's last glyph. The
    /// line itself is untouched — only cursor motion is synthesized, so it
    /// works identically in zsh, bash, and fish line editors.
    pub fn prompt_click_arrows(&self, col: u16, row: u16) -> Option<Vec<u8>> {
        let (vr, vc) = self.cell_at(col, row)?;
        let term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return None;
        }
        let off = term.grid().display_offset() as i32;
        let line = vr as i32 - off;
        let cursor = term.grid().cursor.point;
        if line != cursor.line.0 {
            return None;
        }
        let now = self.clock_now(&term);
        let ms = self.marks.lock().unwrap();
        let last = ms.last()?;
        if !matches!(last.kind, crate::shell_integration::OscEvent::PromptEnd) {
            return None;
        }
        if last.line_rec - (now - last.clock_rec) as i32 != line {
            return None;
        }
        let b_col = last.col_rec as u16;
        let (text, colmap) = row_text_and_cols(&term, line);
        let chars = text.trim_end().chars().count();
        let end_col = if chars == 0 {
            b_col
        } else {
            colmap
                .get(chars - 1)
                .map(|&c| c as u16 + 1)
                .unwrap_or(b_col)
        };
        let cur = cursor.column.0 as u16;
        // The row may have been rewritten SHORTER than the recorded 133;B
        // column (a backgrounded progress writer, a shrunk pane): the live
        // bounds then sit left of `b_col`, and `clamp` with min > max
        // panics. Bound the lower edge by the upper so the gesture
        // degrades instead of taking the app down.
        let hi = end_col.max(cur);
        let target = vc.clamp(b_col.min(hi), hi);
        if target == cur {
            return None;
        }
        let (seq, n) = if target > cur {
            (b"\x1b[C".as_slice(), target - cur)
        } else {
            (b"\x1b[D".as_slice(), cur - target)
        };
        Some(seq.repeat(n as usize))
    }

    /// The hostname the shell last reported over OSC 7 (an in-pane SSH
    /// session with integration moves it to the remote host).
    pub fn shell_host(&self) -> Option<String> {
        self.osc7_host.lock().unwrap().clone()
    }

    /// [`Self::shell_cwd`], trusted only when the report provably applies
    /// to the LOCAL shell: the reporting host must be this machine (empty,
    /// "localhost", or the local hostname — RFC 8089 semantics via the
    /// command history's canonicalizer) AND the claimed path must name the
    /// directory the kernel says the shell is in ([`cwd_of_pid`]). PTY
    /// bytes carry no author — any foreground job can print a
    /// local-looking claim and die before the bytes are even parsed — so
    /// no tty-ownership sample at any moment establishes provenance.
    /// Matching the kernel's ground truth does: a forged claim equal to
    /// the truth grants nothing, and one that differs is rejected. The
    /// cost is that platforms without [`cwd_of_pid`] never trust a claim.
    pub fn local_shell_cwd(&self) -> Option<std::path::PathBuf> {
        let host = self.shell_host().unwrap_or_default();
        if !crate::command_history::is_local_host(&host) {
            return None;
        }
        let claim = self.shell_cwd()?;
        let real = cwd_of_pid(self.pid()?)?;
        // Symlink-tolerant equality (macOS reports /private/tmp for a
        // shell sitting in /tmp): equal canonical paths name the same
        // directory, so a claim passing this check cannot mislead.
        (claim.canonicalize().ok()? == real.canonicalize().ok()?).then_some(claim)
    }

    /// The pane's monotonic scroll-clock reading (see `clock_now`): the
    /// content anchor the app pairs with grid coordinates it captures now
    /// (annotation prompts, copy mode), so the pair survives streaming
    /// output and scrollback saturation alike.
    pub fn scroll_clock(&mut self) -> i64 {
        self.tick_clock()
    }

    /// Pin a note to the span starting at grid `line`, columns
    /// `start..start+len` (Cmd+K N's commit). `clock` is the scroll-clock
    /// reading `line` was captured against ([`Self::scroll_clock`] at the
    /// prompt's OPEN) — sampling it here instead would mis-anchor the note
    /// by every row that streamed while the user typed it.
    pub fn add_annotation(&mut self, line: i32, clock: i64, start: u16, len: u16, text: String) {
        self.annotations.push(PaneAnnotation {
            line_rec: line,
            clock_rec: clock,
            start,
            len: len.max(1),
            text,
        });
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// Every annotation with its span translated to current grid lines:
    /// `(line, start, len, text)`. Spans whose content fell off the
    /// scrollback are dropped for good.
    pub fn annotations_current(&mut self) -> Vec<(i32, u16, u16, String)> {
        let now = self.tick_clock();
        let top = self.term.lock().grid().topmost_line().0;
        self.annotations
            .retain(|a| a.line_rec - (now - a.clock_rec) as i32 >= top);
        self.annotations
            .iter()
            .map(|a| {
                (
                    a.line_rec - (now - a.clock_rec) as i32,
                    a.start,
                    a.len,
                    a.text.clone(),
                )
            })
            .collect()
    }

    /// The newest grid line where a captured line starts, or `None`. The
    /// straight case is a row carrying `needle` as a prefix; a pane
    /// narrower than the needle instead shows the line's FIRST WRAPPED ROW,
    /// whose text is a prefix OF the needle. That reverse arm is gated on
    /// real wrap metadata, not row length: the row must itself soft-wrap
    /// (WRAPLINE on its last cell) — otherwise any short unrelated row
    /// sharing the prefix would win — and continuation rows (the PREVIOUS
    /// row wraps) never match either arm, or a later continuation sharing
    /// the prefix would beat the true start under the newest-first scan.
    pub fn find_captured_line(&self, needle: &str) -> Option<i32> {
        if needle.is_empty() {
            return None;
        }
        let term = self.term.lock();
        let cols = term.columns();
        let wraps = |line: i32| {
            cols > 0
                && term.grid()[Point::new(Line(line), Column(cols - 1))]
                    .flags
                    .contains(Flags::WRAPLINE)
        };
        let top = term.grid().topmost_line().0;
        let bottom = term.screen_lines() as i32 - 1;
        for line in (top..=bottom).rev() {
            if line > top && wraps(line - 1) {
                continue;
            }
            let (text, _) = row_text_and_cols(&term, line);
            let t = text.trim_end();
            if t.is_empty() {
                continue;
            }
            if t.starts_with(needle) || (wraps(line) && needle.starts_with(t)) {
                return Some(line);
            }
        }
        None
    }

    /// Every annotation translated against an EXPLICIT clock reading — the
    /// caller's own snapshot — as `(line, start, len, text)`. The
    /// existing-note lookup in the annotate prompt compares these lines
    /// against a selection captured WITH that reading; translating with a
    /// fresh (later) clock instead let output that scrolled in between
    /// shift the annotations away from the still-fixed selection line, so
    /// annotating the same span opened an empty prompt and duplicated the
    /// note. Pure translation: the scrolled-off GC stays in
    /// [`Self::annotations_current`].
    pub fn annotations_at_clock(&self, clock: i64) -> Vec<(i32, u16, u16, String)> {
        self.annotations
            .iter()
            .map(|a| {
                (
                    a.line_rec - (clock - a.clock_rec) as i32,
                    a.start,
                    a.len,
                    a.text.clone(),
                )
            })
            .collect()
    }

    /// The annotation index + note under screen cell (col, row), if any.
    /// Annotations live on the primary screen; while an alt-screen app owns
    /// the viewport their lines describe rows the user cannot see, so no
    /// cell resolves.
    pub fn annotation_at(&self, col: u16, row: u16) -> Option<(usize, String)> {
        let (vr, vc) = self.cell_at(col, row)?;
        let term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return None;
        }
        let off = term.grid().display_offset() as i32;
        let now = self.clock_now(&term);
        drop(term);
        let line = vr as i32 - off;
        self.annotations
            .iter()
            .enumerate()
            .find(|(_, a)| {
                a.line_rec - (now - a.clock_rec) as i32 == line
                    && vc >= a.start
                    && vc < a.start + a.len
            })
            .map(|(i, a)| (i, a.text.clone()))
    }

    pub fn remove_annotation(&mut self, idx: usize) {
        if idx < self.annotations.len() {
            self.annotations.remove(idx);
            self.pty_dirty.store(true, Ordering::Release);
        }
    }

    pub fn set_annotation_text(&mut self, idx: usize, text: String) {
        if let Some(a) = self.annotations.get_mut(idx) {
            a.text = text;
            self.pty_dirty.store(true, Ordering::Release);
        }
    }

    /// When the row at current grid `line` arrived (epoch millis), if the
    /// reader thread stamped it.
    pub fn row_time(&self, line: i32) -> Option<u64> {
        let term = self.term.lock();
        let now = self.clock_now(&term);
        drop(term);
        self.line_times
            .lock()
            .unwrap()
            .get(&(i64::from(line) + now))
            .copied()
    }

    /// Drain the loopback ports the reader thread scraped since the last call.
    /// Each port is reported at most once over the terminal's lifetime, so this
    /// is empty on most ticks.
    pub fn drain_ports(&self) -> Vec<crate::port_detect::PortHit> {
        self.port_rx.try_iter().collect()
    }

    /// Bytes advanced into the grid since the last redraw, without
    /// clearing. The main loop uses this to classify a PTY-only redraw as
    /// interactive echo (small, redraw now) or bulk stream (large, stay
    /// capped). Reset on the next `take_dirty`.
    pub fn peek_pending_bytes(&self) -> usize {
        self.pty_pending_bytes.load(Ordering::Relaxed)
    }

    /// Process ID of the shell running inside this terminal, when the
    /// platform exposes one. Used to look up the live cwd so a new split
    /// inherits the directory the user has `cd`'d into.
    pub fn pid(&self) -> Option<u32> {
        self._child.process_id()
    }

    /// Read the dirty flag without clearing it. Lets the main loop decide
    /// whether to redraw now or coalesce, without losing the signal if we
    /// choose to skip this iteration.
    pub fn peek_dirty(&self) -> bool {
        self.pty_dirty.load(Ordering::Acquire)
    }

    pub fn cell_at(&self, col: u16, row: u16) -> Option<(u16, u16)> {
        let inner = self.last_inner;
        if inner.width == 0 || inner.height == 0 {
            return None;
        }
        if col < inner.x || col >= inner.x + inner.width {
            return None;
        }
        if row < inner.y || row >= inner.y + inner.height {
            return None;
        }
        Some((row - inner.y, col - inner.x))
    }

    /// Current scrollback offset: how many rows the viewport is scrolled
    /// up from the live bottom. Viewport row `r` maps to absolute grid
    /// line `r - display_offset`.
    fn display_offset(&self) -> i32 {
        self.term.lock().grid().display_offset() as i32
    }

    /// Text of the grid row under screen cell `(col, row)`, plus the 0-based
    /// column within that text the click landed on. Drives Cmd/Ctrl+click URL
    /// detection. `None` when the cell is outside the content area.
    pub fn line_text_at(&self, col: u16, row: u16) -> Option<(String, usize)> {
        let (r, c) = self.cell_at(col, row)?;
        let term = self.term.lock();
        let line = r as i32 - term.grid().display_offset() as i32;
        let (text, cols_map) = row_text_and_cols(&term, line);
        // Grid column → char index in the spacer-skipped text: the last
        // produced char at-or-before the clicked column, so clicking a wide
        // char's spacer cell resolves to the wide char itself.
        let idx = cols_map.iter().rposition(|&gc| gc <= c as usize)?;
        Some((text, idx))
    }

    /// The OSC 8 hyperlink under a screen position (host cell coords), if
    /// any — the screen-coord twin of [`Self::hyperlink_at`].
    pub fn hyperlink_at_screen(&self, col: u16, row: u16) -> Option<String> {
        let (r, c) = self.cell_at(col, row)?;
        self.hyperlink_at(r as usize, c as usize)
    }

    /// The current scroll-clock reading against an already-locked term.
    fn clock_now(&self, term: &Term<VoidListener>) -> i64 {
        self.clock.lock().unwrap().now(term)
    }

    /// Fold the tracer's drift into the clock, re-plant it fresh and return
    /// the folded reading (see [`ScrollClock::tick`]).
    fn tick_clock(&mut self) -> i64 {
        let mut term = self.term.lock();
        self.clock.lock().unwrap().tick(&mut term)
    }

    /// Stamp freshly-set selection / copy-cursor coordinates against a
    /// fresh clock tick; a selection set on the alternate screen also
    /// captures its content anchor (see `AltSelAnchor`).
    fn stamp_selection_clock(&mut self) {
        self.sel_scrolled = self.tick_clock();
        let term = self.term.lock();
        self.alt_sel = if term.mode().contains(TermMode::ALT_SCREEN) {
            self.selection.and_then(|s| capture_alt_anchor(&term, s))
        } else {
            None
        };
    }

    /// Re-anchor an alternate-screen selection to its content after the
    /// app repainted (see `AltSelAnchor`). Finds the vertical shift whose
    /// on-grid rows all match the remembered ones — requiring at least one
    /// non-blank matched row so a blank fingerprint can't latch anywhere —
    /// preferring the smallest movement; no qualifying shift parks the
    /// selection dormant until its content scrolls back into view. A
    /// selection created on the primary screen has no anchor and simply
    /// stays frozen for the duration of the alt-screen trip.
    fn rebase_alt_selection(&mut self) {
        if self.selection.is_none() {
            self.alt_sel = None;
            return;
        }
        let Some(mut anchor) = self.alt_sel.take() else {
            return;
        };
        let term = self.term.lock();
        let rows_vis = term.screen_lines() as i32;
        let old_top = anchor.top;
        let k = anchor.rows.len() as i32;
        // One grid snapshot up front (text + char→column maps): the
        // matcher probes every candidate shift, so per-candidate row reads
        // would re-extract (and re-allocate) the same rows dozens of times
        // a frame.
        let grid_rows: Vec<(String, Vec<usize>)> =
            (0..rows_vis).map(|l| row_text_and_cols(&term, l)).collect();
        // Per candidate shift, how many block rows the grid matches
        // exactly at that offset. Rows can go missing not just off-grid:
        // app chrome (Claude Code's input box, its floating pills, an
        // animated status row) overdraws rows anywhere in the block, so
        // partial survival anchors by however many exact rows remain — at
        // least two (when the block has two) including a non-blank one,
        // so a lone repeated divider row can't latch the highlight onto an
        // unrelated copy of itself. More exact rows win; ties go to the
        // shift whose captured neighbour rows also match (a ONE-row block
        // has a one-row fingerprint, and full-screen apps repeat rows
        // verbatim — the neighbours tell its copy from the lookalikes),
        // then to the smallest movement.
        let k_rows = anchor.rows.len();
        let min_exact = k_rows.min(2);
        // (top, exact-row count, matched-neighbour count)
        let mut best: Option<(i32, usize, usize)> = None;
        for top in 1 - k..rows_vis {
            let (mut exact, mut nonblank) = (0usize, false);
            for (i, want) in anchor.rows.iter().enumerate() {
                let line = top + i as i32;
                if (0..rows_vis).contains(&line) && grid_rows[line as usize].0 == *want {
                    exact += 1;
                    nonblank |= !want.trim().is_empty();
                }
            }
            let mut ctx = 0usize;
            for (want, line) in [
                (anchor.ctx_above.as_ref(), top - 1),
                (anchor.ctx_below.as_ref(), top + k),
            ] {
                if let Some(want) = want
                    && (0..rows_vis).contains(&line)
                    && grid_rows[line as usize].0 == *want
                {
                    ctx += 1;
                }
            }
            if exact >= min_exact
                && nonblank
                && best.is_none_or(|(btop, bexact, bctx)| {
                    exact > bexact
                        || (exact == bexact && ctx > bctx)
                        || (exact == bexact
                            && ctx == bctx
                            && (top - old_top).abs() < (btop - old_top).abs())
                })
            {
                best = Some((top, exact, ctx));
            }
        }
        match best {
            Some((top, _, _)) => {
                let d = top - old_top;
                anchor.top = top;
                if d != 0
                    && let Some(s) = self.selection.as_mut()
                {
                    s.anchor.0 += d;
                    s.head.0 += d;
                }
                anchor.dormant = false;
                // The clip: every block row still on screen, whole when its
                // text matches exactly, or just the surviving prefix/suffix
                // columns when app chrome (a floating pill) overdraws part
                // of it. The shift is already pinned by the exact run, so
                // partial credit here can't drag the highlight elsewhere.
                let mut clips: Vec<(i32, u16, u16)> = Vec::new();
                let mut all_exact = true;
                for (i, want) in anchor.rows.iter().enumerate() {
                    let line = top + i as i32;
                    if !(0..rows_vis).contains(&line) {
                        all_exact = false;
                        continue;
                    }
                    let (got, colmap) = &grid_rows[line as usize];
                    if got == want {
                        clips.push((line, 0, u16::MAX));
                    } else {
                        all_exact = false;
                        for (lo, hi) in partial_overlap_cols(want, got, colmap) {
                            clips.push((line, lo, hi));
                        }
                    }
                }
                anchor.visible = (!all_exact).then_some(clips);
                // Refresh the anchor — this is also what folds in a user
                // extension of the selection — but only when every row is
                // in view and intact, so the remembered block never loses
                // a covered or off-screen part mid-scroll.
                if all_exact && let Some(fresh) = capture_alt_anchor(&term, self.selection.unwrap())
                {
                    anchor = fresh;
                }
            }
            // Nothing matched anywhere. Mid-drag that must NOT hide the
            // selection — the drag may have started on a blank row or an
            // animated status line, and the user is pointing at what they
            // see right now; the real anchor is captured on release.
            None if self.drag_selecting => {
                anchor.dormant = false;
                anchor.visible = None;
                if let Some(fresh) = capture_alt_anchor(&term, self.selection.unwrap()) {
                    anchor = fresh;
                }
            }
            None => anchor.dormant = true,
        }
        drop(term);
        self.alt_sel = Some(anchor);
    }

    /// Shift the stored selection and copy-cursor down-anchored coordinates
    /// by the scroll-clock movement since they were recorded, so they keep
    /// naming the same content while output streams. Endpoints whose
    /// content fell off the scrollback pin to the oldest line; a selection
    /// entirely off the buffer is dropped for good. No-op (and no lock)
    /// when neither exists — setters stamp `sel_scrolled` themselves.
    fn rebase_selection(&mut self) {
        if self.selection.is_none() && self.copy_cursor.is_none() {
            return;
        }
        if self.term.lock().mode().contains(TermMode::ALT_SCREEN) {
            self.rebase_alt_selection();
            return;
        }
        if self.alt_sel.take().is_some() {
            // The selection was made on the alternate screen and the app
            // has left it: the alternate grid — and the content the
            // coordinates named — is gone with it.
            self.selection = None;
            self.copy_cursor = None;
            return;
        }
        let now = self.tick_clock();
        let top = self.term.lock().grid().topmost_line().0;
        let delta = (now - self.sel_scrolled) as i32;
        self.sel_scrolled = now;
        if delta != 0 {
            if let Some(sel) = self.selection.as_mut() {
                sel.anchor.0 -= delta;
                sel.head.0 -= delta;
            }
            if let Some(cur) = self.copy_cursor.as_mut() {
                cur.0 -= delta;
            }
        }
        if self
            .selection
            .is_some_and(|s| s.anchor.0 < top && s.head.0 < top)
        {
            self.selection = None;
        } else if let Some(sel) = self.selection.as_mut() {
            sel.anchor.0 = sel.anchor.0.max(top);
            sel.head.0 = sel.head.0.max(top);
        }
        if let Some(cur) = self.copy_cursor.as_mut() {
            cur.0 = cur.0.max(top);
        }
    }

    pub fn start_selection_at(&mut self, col: u16, row: u16) {
        if let Some((r, c)) = self.cell_at(col, row) {
            let line = r as i32 - self.display_offset();
            self.selection = Some(Selection::new(line, c));
            self.drag_selecting = true;
            self.stamp_selection_clock();
        }
    }

    /// Test-only: parse `bytes` straight into this pane's grid, as if the
    /// child had printed them — app-level tests drive alt-screen repaint
    /// scenarios deterministically through this.
    #[cfg(test)]
    pub fn feed_bytes_for_test(&self, bytes: &[u8]) {
        let mut p = Processor::<StdSyncHandler>::new();
        let mut term = self.term.lock();
        p.advance(&mut *term, bytes);
    }

    /// Test-only: run one reader-thread timestamp pass over the current
    /// grid, exactly as a real chunk arrival would.
    #[cfg(test)]
    pub fn stamp_chunk_for_test(&self, prev_id: &mut i64, now_ms: u64) {
        let mut term = self.term.lock();
        let mut clock = self.clock.lock().unwrap();
        let mut lt = self.line_times.lock().unwrap();
        stamp_chunk(&mut term, &mut clock, &mut lt, prev_id, now_ms);
    }

    /// Test-only: the stamped (content id, arrival ms) pairs.
    #[cfg(test)]
    pub fn line_time_entries_for_test(&self) -> Vec<(i64, u64)> {
        self.line_times
            .lock()
            .unwrap()
            .iter()
            .map(|(&k, &v)| (k, v))
            .collect()
    }

    /// Test-only: record an OSC 133 mark as the reader thread would,
    /// stamped against the grid's CURRENT cursor/history state, so tests
    /// can stage prompt-dependent gestures without a live shell.
    #[cfg(test)]
    pub fn push_mark_for_test(&self, kind: crate::shell_integration::OscEvent, col_rec: usize) {
        let mut term = self.term.lock();
        let line_rec = term.grid().cursor.point.line.0;
        let clock_rec = self.clock.lock().unwrap().tick(&mut term);
        drop(term);
        self.marks.lock().unwrap().push(StoredMark {
            kind,
            line_rec,
            clock_rec,
            col_rec,
            dur: None,
        });
    }

    /// Test-only: record an inline image as the reader thread would,
    /// anchored at the grid's CURRENT cursor row.
    #[cfg(test)]
    pub fn push_image_for_test(&self, data: Vec<u8>) {
        let mut term = self.term.lock();
        let line_rec = term.grid().cursor.point.line.0;
        let clock_rec = self.clock.lock().unwrap().tick(&mut term);
        drop(term);
        let mut imgs = self.images.lock().unwrap();
        let seq = imgs.last().map_or(0, |m| m.seq) + 1;
        imgs.push(StoredImage {
            seq,
            data: std::sync::Arc::new(data),
            line_rec,
            clock_rec,
        });
    }

    /// Test-only: whether a quick-select hint overlay is currently set.
    #[cfg(test)]
    pub fn has_hints_for_test(&self) -> bool {
        self.hints.is_some()
    }

    /// Test-only: whether a find highlight needle is currently set.
    #[cfg(test)]
    pub fn has_search_for_test(&self) -> bool {
        self.search_needle.is_some()
    }

    /// Test-only: the raw state behind the corrected selection accessors —
    /// (stored selection, sel_scrolled, clock_base, alt anchor top, alt
    /// mode) — for diagnosing drift math from app-level tests.
    #[cfg(test)]
    pub fn debug_sel_state(&self) -> (Option<Selection>, i64, i64, Option<i32>, bool) {
        (
            self.selection,
            self.sel_scrolled,
            self.clock.lock().unwrap().base,
            self.alt_sel.as_ref().map(|a| a.top),
            self.term.lock().mode().contains(TermMode::ALT_SCREEN),
        )
    }

    /// The mouse button came up: the drag-selection is final. Capture the
    /// definitive content anchor now — during the drag the selection
    /// followed the pointer over whatever the grid showed.
    pub fn end_drag(&mut self) {
        if self.drag_selecting {
            self.drag_selecting = false;
            self.stamp_selection_clock();
        }
    }

    pub fn extend_selection_to(&mut self, col: u16, row: u16) {
        self.drag_selecting = true;
        self.rebase_selection();
        let cell = self.cell_at(col, row);
        let off = self.display_offset();
        if let (Some(sel), Some((r, c))) = (self.selection.as_mut(), cell) {
            sel.head = (r as i32 - off, c);
        }
    }

    /// Extend a drag-selection toward a pointer that may have left the
    /// pane. Columns and rows are clamped to the inner content rect; the
    /// returned value is the auto-scroll direction the caller should keep
    /// applying while the pointer stays past an edge: `-1` (pointer above
    /// the top, scroll into history), `+1` (below the bottom, scroll
    /// toward live), or `0` (inside the pane, no auto-scroll). Mirrors
    /// the click-drag-past-the-edge selection growth in iTerm2/VS Code.
    pub fn drag_select_to(&mut self, col: u16, row: u16) -> i32 {
        let inner = self.last_inner;
        if inner.width == 0 || inner.height == 0 {
            return 0;
        }
        self.drag_selecting = true;
        let top = inner.y;
        let bottom = inner.y + inner.height - 1;
        let (vp_row, dir) = if row < top {
            (0u16, -1)
        } else if row > bottom {
            (inner.height - 1, 1)
        } else {
            (row - inner.y, 0)
        };
        let max_x = inner.x + inner.width - 1;
        let c = col.clamp(inner.x, max_x) - inner.x;
        self.rebase_selection();
        let off = self.display_offset();
        if let Some(sel) = self.selection.as_mut() {
            sel.head = (vp_row as i32 - off, c);
        }
        dir
    }

    /// One step of edge auto-scroll while a drag-selection is held past
    /// the top (`dir < 0`) or bottom (`dir > 0`) edge. Scrolls the
    /// viewport by one row and re-pins the selection head to the new edge
    /// line at the last-known drag column. On the alternate screen the
    /// inner program owns scrolling, so a mouse-tracking app (Claude Code,
    /// a pager) gets a wheel report at the edge cell instead — it scrolls
    /// its own content, the content anchor drags the selection along, and
    /// the head re-pins each tick.
    pub fn autoscroll_select(&mut self, dir: i32, col: u16) {
        if dir == 0 {
            return;
        }
        if self.term.lock().mode().contains(TermMode::ALT_SCREEN) {
            let inner = self.last_inner;
            if inner.width == 0 || inner.height == 0 || !self.mouse_reporting() {
                return;
            }
            let edge_row = if dir < 0 {
                inner.y
            } else {
                inner.y + inner.height - 1
            };
            let ccol = col.clamp(inner.x, inner.x + inner.width - 1);
            let button = if dir < 0 {
                MouseButtonKind::WheelUp
            } else {
                MouseButtonKind::WheelDown
            };
            self.report_mouse(
                button,
                MouseAction::Press,
                ccol,
                edge_row,
                MouseMods::default(),
            );
            self.rebase_selection();
            if let Some(sel) = self.selection.as_mut() {
                sel.head = (i32::from(edge_row - inner.y), ccol - inner.x);
            }
            return;
        }
        let scrolled = if dir < 0 {
            self.scroll_up(1)
        } else {
            self.scroll_down(1)
        };
        if !scrolled {
            return;
        }
        let inner = self.last_inner;
        if inner.width == 0 || inner.height == 0 {
            return;
        }
        let max_x = inner.x + inner.width - 1;
        let c = col.clamp(inner.x, max_x) - inner.x;
        let vp_row = if dir < 0 { 0u16 } else { inner.height - 1 };
        self.rebase_selection();
        let off = self.display_offset();
        if let Some(sel) = self.selection.as_mut() {
            sel.head = (vp_row as i32 - off, c);
        }
    }

    pub fn clear_selection(&mut self) {
        self.selection = None;
        self.alt_sel = None;
    }

    /// Double-click word-select: expand the selection to cover the word
    /// under the screen coordinate `(col, row)`. No-op when the click
    /// lands on whitespace / punctuation or outside the live area.
    pub fn select_word_at(&mut self, col: u16, row: u16) {
        let Some((r, c)) = self.cell_at(col, row) else {
            return;
        };
        let term = self.term.lock();
        let display_offset = term.grid().display_offset();
        let Some((anchor, head)) =
            select_word_at_in_term(&term, display_offset, r as usize, c as usize)
        else {
            return;
        };
        drop(term);
        // `select_word_at_in_term` reports viewport rows; anchor them to
        // absolute grid lines so the selection stays put when scrolled.
        let off = display_offset as i32;
        let to_abs = |(vr, vc): (u16, u16)| (vr as i32 - off, vc);
        self.selection = Some(Selection {
            anchor: to_abs(anchor),
            head: to_abs(head),
            block: false,
        });
        self.stamp_selection_clock();
    }

    /// The selection with its endpoints corrected for the scroll-clock
    /// movement since they were recorded, so callers always see coordinates
    /// that name the content the user selected (see `rebase_selection`).
    pub fn selection(&self) -> Option<Selection> {
        let mut sel = self.selection?;
        let delta = (self.clock_now(&self.term.lock()) - self.sel_scrolled) as i32;
        sel.anchor.0 -= delta;
        sel.head.0 -= delta;
        Some(sel)
    }

    /// Replace (or clear) the selection wholesale. Copy mode drives the
    /// selection from the keyboard through this, bypassing the mouse path.
    pub fn set_selection(&mut self, sel: Option<Selection>) {
        self.rebase_selection();
        self.selection = sel;
        self.stamp_selection_clock();
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// Show / move / hide the copy-mode cursor block.
    pub fn set_copy_cursor(&mut self, cur: Option<(i32, u16)>) {
        self.rebase_selection();
        self.copy_cursor = cur;
        self.stamp_selection_clock();
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// The shell cursor's cell in absolute grid coords (line can be
    /// negative only transiently; the live screen is 0-based): where copy
    /// mode starts.
    pub fn cursor_line_col(&self) -> (i32, u16) {
        let term = self.term.lock();
        let p = term.grid().cursor.point;
        (p.line.0, p.column.0 as u16)
    }

    /// The drift-corrected selection together with the scroll-clock reading
    /// it is valid against, from ONE terminal snapshot. Callers that pair a
    /// grid coordinate with a clock (the annotation prompt) must use this:
    /// reading them through separate locks lets PTY output land in between,
    /// pairing a pre-output line with a post-output clock and anchoring to
    /// the wrong content row.
    pub fn selection_and_clock(&mut self) -> (Option<Selection>, i64) {
        let mut term = self.term.lock();
        let now = self.clock.lock().unwrap().tick(&mut term);
        let sel = self.selection.map(|mut s| {
            let delta = (now - self.sel_scrolled) as i32;
            s.anchor.0 -= delta;
            s.head.0 -= delta;
            s
        });
        (sel, now)
    }

    /// Cursor cell, grid bounds, and the scroll-clock reading from ONE
    /// terminal snapshot (copy mode's open — same atomicity argument as
    /// [`Self::selection_and_clock`]).
    #[allow(clippy::type_complexity)]
    pub fn cursor_bounds_and_clock(&mut self) -> ((i32, u16), (i32, i32, u16, u16), i64) {
        let mut term = self.term.lock();
        let now = self.clock.lock().unwrap().tick(&mut term);
        let p = term.grid().cursor.point;
        let top = term.grid().topmost_line().0;
        let bottom = term.screen_lines() as i32 - 1;
        (
            (p.line.0, p.column.0 as u16),
            (top, bottom, self.cols, self.rows),
            now,
        )
    }

    /// One grid row as spacer-skipped text plus its char-index → grid-column
    /// map (the free `row_text_and_cols` behind the term lock).
    pub fn row_text(&self, line: i32) -> (String, Vec<usize>) {
        let term = self.term.lock();
        row_text_and_cols(&term, line)
    }

    /// The readable grid range and viewport size for keyboard navigation:
    /// (oldest line, newest line, columns, viewport rows).
    pub fn grid_bounds(&self) -> (i32, i32, u16, u16) {
        let term = self.term.lock();
        let top = term.grid().topmost_line().0;
        let bottom = term.screen_lines() as i32 - 1;
        (top, bottom, self.cols, self.rows)
    }

    pub fn selection_text(&self) -> String {
        // An alt-screen selection scrolled (partly) out of the app's view
        // names content the grid no longer fully holds — dormant, or
        // clipped by app chrome. The anchor remembered the text, so copy
        // still yields the whole highlighted block.
        if let Some(anchor) = self
            .alt_sel
            .as_ref()
            .filter(|a| a.dormant || a.visible.is_some())
        {
            return anchor.text.clone();
        }
        let Some(mut sel) = self.selection else {
            return String::new();
        };
        let term = self.term.lock();
        // Same scroll-clock correction as `selection()`, computed under
        // the lock already held for extraction.
        let delta = (self.clock_now(&term) - self.sel_scrolled) as i32;
        sel.anchor.0 -= delta;
        sel.head.0 -= delta;
        if sel.block {
            let (rl, cl, rh, ch) = sel.block_bounds();
            return block_selection_text(&term, rl, cl as usize, rh, ch as usize);
        }
        let (sr, sc, er, ec) = sel.normalised();
        extract_selection_text(&term, sr, sc as usize, er, ec as usize)
    }

    /// The pane's stable identity (see the `uid` field).
    pub fn uid(&self) -> u64 {
        self.uid
    }

    pub fn write_input(&mut self, data: &[u8]) {
        self.reset_scrollback();
        self.input_seen = true;
        #[cfg(test)]
        self.written_for_test
            .lock()
            .unwrap()
            .extend_from_slice(data);
        if let Ok(mut w) = self.writer.lock() {
            let _ = w.write_all(data);
            let _ = w.flush();
        }
        self.pty_dirty.store(true, Ordering::Release);
    }

    /// Test-only: every byte the app has written toward the child's stdin.
    #[cfg(test)]
    pub fn written_bytes_for_test(&self) -> Vec<u8> {
        self.written_for_test.lock().unwrap().clone()
    }

    /// True when the child program has enabled any mouse-tracking mode
    /// (DECSET 1000 click / 1002 button-drag / 1003 any-motion). When it has,
    /// wheel events should be forwarded to it via `report_mouse` so it scrolls
    /// its own buffer rather than croft synthesising arrow keys.
    pub fn mouse_reporting(&self) -> bool {
        self.term.lock().mode().intersects(TermMode::MOUSE_MODE)
    }

    /// Encode a mouse gesture at host-screen cell `(col, row)` as a mouse
    /// report and send it to the child. Returns false without writing when
    /// the cell is outside the pane, the child isn't tracking the mouse, or
    /// the event is motion the child didn't ask for (no 1002/1003). The report
    /// is SGR (1006) when the child selected it, otherwise legacy X10.
    pub fn report_mouse(
        &mut self,
        button: MouseButtonKind,
        action: MouseAction,
        col: u16,
        row: u16,
        mods: MouseMods,
    ) -> bool {
        let Some((r, c)) = self.cell_at(col, row) else {
            return false;
        };
        let mode = *self.term.lock().mode();
        if !mode.intersects(TermMode::MOUSE_MODE) {
            return false;
        }
        // Motion (a held-button drag) is only wanted under 1002/1003.
        if action == MouseAction::Motion
            && !mode.intersects(TermMode::MOUSE_DRAG | TermMode::MOUSE_MOTION)
        {
            return false;
        }
        let report = encode_mouse_report(
            mode.contains(TermMode::SGR_MOUSE),
            button,
            action,
            c,
            r,
            mods,
        );
        if let Ok(mut w) = self.writer.lock() {
            let _ = w.write_all(&report);
            let _ = w.flush();
        }
        self.input_seen = true;
        self.pty_dirty.store(true, Ordering::Release);
        true
    }

    /// Seed a `cd <path>` into the embedded shell so the terminal
    /// follows an Explorer-side workspace-root change. Sent verbatim to
    /// the PTY input - the shell parses it like a typed command. See
    /// `format_cd_command` for the line-clearing prefix and the path
    /// quoting strategy.
    pub fn change_cwd(&mut self, path: &std::path::Path) {
        self.write_input(&format_cd_command(path));
    }

    /// Paste a payload into the embedded shell. Bracketed-paste markers
    /// are added only if the inner program asked for them; otherwise the
    /// payload is sent raw so simple shells don't see literal `\e[200~`.
    pub fn paste_input(&mut self, payload: &[u8]) {
        if self.bracketed_paste_enabled.load(Ordering::Acquire) {
            self.write_input(b"\x1b[200~");
            self.write_input(payload);
            self.write_input(b"\x1b[201~");
        } else {
            self.write_input(payload);
        }
    }

    /// True when the embedded shell is sitting at its prompt rather than
    /// running a foreground application (an editor, a pager, or an agent
    /// like Claude Code). Compares the PTY's foreground process group
    /// against the shell's own pid: a shell at its prompt is its own
    /// process-group leader, so `tcgetpgrp(master) == shell_pid`. Once
    /// the shell forks a command, that command owns a new foreground
    /// group and the two diverge. The only case that must suppress a
    /// `cd` seed is a foreground group that exists AND is not the shell,
    /// because that is an app reading the tty in raw mode; a missing
    /// foreground group (the brief window after spawn, before the shell
    /// claims the tty) means nothing has grabbed input, so a `cd` is
    /// still safe. `tcgetpgrp` is identical on macOS and Linux.
    pub fn foreground_is_shell(&self) -> bool {
        match (self.master.process_group_leader(), self.shell_pid) {
            (Some(fg), Some(pid)) => fg == pid,
            _ if self.input_seen => {
                // A pane that has received input CAN be running a launched
                // app, so an UNRESOLVABLE sample (failed tcgetpgrp, or a
                // spawn that never reported a shell pid) must not read as
                // "shell owns it" — under scheduler load the sample
                // transiently fails while a foreground command runs, and
                // the old blanket `true` let a cd seed reach that app
                // (#155). Retry once (the failure window is brief), then
                // answer FALSE: every consumer fails safe on false (seed
                // suppressed, task pane not reused, no prompt arrows).
                std::thread::sleep(std::time::Duration::from_millis(5));
                match (self.master.process_group_leader(), self.shell_pid) {
                    (Some(fg), Some(pid)) => fg == pid,
                    _ => false,
                }
            }
            // Startup window: no input has ever been written, so nothing
            // user-facing can own the tty yet — a missing group is the
            // shell's own rc still claiming it, and a seed queues as
            // type-ahead (#94).
            _ => true,
        }
    }

    /// True when seeding a `cd` into the PTY cannot reach a user-facing
    /// foreground application. Either the shell owns the foreground group
    /// (`foreground_is_shell`), or the pane is still in its startup
    /// window: nothing has ever been written toward the child's stdin and
    /// shell integration has recorded no prompt mark, so a foreground
    /// group that differs from the shell can only be the shell's own rc
    /// startup. A seed written then is ordinary type-ahead — the bytes
    /// wait in the tty input queue for the first prompt — because a pane
    /// that has never received input cannot be running a launched app.
    /// Without the carve-out, a Make Root landing inside that window
    /// silently skipped the seed and left the shell behind the Explorer
    /// (#94; under full-suite load the window stretches past the call).
    pub fn cwd_seed_is_safe(&self) -> bool {
        self.foreground_is_shell() || (!self.input_seen && self.marks.lock().unwrap().is_empty())
    }

    /// Test-only: the RESOLVED foreground sample — `Some(fg == shell)`
    /// when both sides are known, `None` otherwise. `foreground_is_shell`
    /// folds policy fallbacks (the startup carve-out, the fail-closed
    /// retry) into its answer, so a test precondition polling it can be
    /// satisfied by a fallback and race the state it believes it proved
    /// (#186); gate preconditions on `Some(..)` instead.
    #[cfg(test)]
    pub fn foreground_resolved_for_test(&self) -> Option<bool> {
        match (self.master.process_group_leader(), self.shell_pid) {
            (Some(fg), Some(pid)) => Some(fg == pid),
            _ => None,
        }
    }

    /// True while the pane is exactly as croft created it: an interactive
    /// shell (never a `new_running` program pane, whose child is doing
    /// launched work with no input byte ever written) that has received
    /// no input and carries no manual name — nothing the user could miss
    /// if the pane were replaced. The re-root restore (#137) may swap
    /// only such panes for the incoming workspace's saved layout; any
    /// input (a keystroke, a paste, a seeded `cd`) or a rename clears
    /// this permanently.
    pub fn is_pristine(&self) -> bool {
        !self.run_pane && !self.input_seen && !self.manual_name_seen
    }

    /// Scroll the visible viewport up by `n` rows into scrollback.
    /// Returns false if the inner program is in alternate-screen mode
    /// (apps like vim/htop/less manage their own scrolling).
    pub fn scroll_up(&mut self, n: usize) -> bool {
        let mut term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return false;
        }
        term.scroll_display(Scroll::Delta(n as i32));
        self.pty_dirty.store(true, Ordering::Release);
        true
    }

    pub fn scroll_down(&mut self, n: usize) -> bool {
        let mut term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return false;
        }
        term.scroll_display(Scroll::Delta(-(n as i32)));
        self.pty_dirty.store(true, Ordering::Release);
        true
    }

    /// Jump the viewport to the oldest scrollback line (Shift+Home). No-op
    /// in the alternate screen, which has no scrollback.
    pub fn scroll_to_top(&mut self) -> bool {
        let mut term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return false;
        }
        term.scroll_display(Scroll::Top);
        drop(term);
        self.pty_dirty.store(true, Ordering::Release);
        true
    }

    /// Jump the viewport back to the live bottom (Shift+End). Returns false
    /// in the alternate screen — no scrollback there, so the chord belongs
    /// to the program, exactly like its three scrolling siblings.
    pub fn reset_scrollback(&mut self) -> bool {
        let mut term = self.term.lock();
        if term.mode().contains(TermMode::ALT_SCREEN) {
            return false;
        }
        term.scroll_display(Scroll::Bottom);
        self.pty_dirty.store(true, Ordering::Release);
        true
    }

    /// Clear the visible screen and scrollback history (VS Code's terminal
    /// "Clear"), homing the cursor. Feeds the standard erase sequences into the
    /// grid (`ED 3` wipes scrollback, `ED 2` the screen); the shell redraws its
    /// prompt on the next keystroke. Does not touch the running program.
    pub fn clear_screen_and_scrollback(&mut self) {
        let mut processor = Processor::<StdSyncHandler>::new();
        {
            let mut term = self.term.lock();
            // ED 2 BEFORE ED 3 (xterm.js / VS Code's clear() order):
            // alacritty's primary-screen ED 2 scrolls the viewport INTO
            // history rather than blanking in place, so erasing history
            // first would leave the "cleared" rows alive in scrollback.
            processor.advance(&mut *term, b"\x1b[2J\x1b[3J\x1b[H");
            term.scroll_display(Scroll::Bottom);
        }
        // The content the images and OSC 133 marks anchored to is gone;
        // keeping either leaves overlays (a floating picture, phantom
        // decoration dots whose menus copy or re-run the wrong rows) over
        // the fresh prompt.
        self.images.lock().unwrap().clear();
        self.marks.lock().unwrap().clear();
        self.pty_dirty.store(true, Ordering::Release);
    }

    pub fn resize(&mut self, cols: u16, rows: u16) {
        // A degenerate pane (a host window shrunk to a sliver leaves the
        // layout no room) must not reach alacritty: Term::resize panics on
        // a zero/one-cell grid mid-reflow. Keep the last sane size; the
        // grid simply clips until the window grows back.
        let (cols, rows) = (cols.max(2), rows.max(2));
        if cols == self.cols && rows == self.rows {
            return;
        }
        self.cols = cols;
        self.rows = rows;
        *self.size_shared.lock().unwrap() = (cols, rows);
        let _ = self.master.resize(PtySize {
            cols,
            rows,
            pixel_width: 0,
            pixel_height: 0,
        });
        let mut term = self.term.lock();
        let size = TermSize::new(cols as usize, rows as usize);
        term.resize(size);
        self.pty_dirty.store(true, Ordering::Release);
    }
}

/// Block until the pty has data (or EOF/error — the following `read` then
/// observes it), or until the shutdown pipe signals (its write end closed in
/// `Drop`). Returns false on shutdown. A bare blocking `read` is not enough
/// to ever exit: on Linux a background job keeps the pty slave open after
/// the shell dies, so the read never returns and joining the reader would
/// freeze the UI for as long as the job runs.
fn wait_pty_readable(pty_fd: std::os::fd::RawFd, shutdown: &std::io::PipeReader) -> bool {
    use std::os::fd::AsRawFd;
    let mut fds = [
        libc::pollfd {
            fd: pty_fd,
            events: libc::POLLIN,
            revents: 0,
        },
        libc::pollfd {
            fd: shutdown.as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        },
    ];
    loop {
        let n = unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) };
        if n < 0 {
            if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            return false;
        }
        if fds[1].revents != 0 {
            return false;
        }
        if fds[0].revents != 0 {
            return true;
        }
    }
}

impl Drop for PtyTerminal {
    fn drop(&mut self) {
        // Kill the shell AND reap it: `kill()` reaps only when the shell dies
        // inside its SIGHUP grace loop; the SIGKILL escalation (a shell that
        // traps HUP) never waits, and `Child`'s own drop doesn't either, so
        // without the `wait` every such closed pane left a zombie for the
        // life of the process. The responder thread ends on its own once
        // `self.term` (holding its channel sender) drops with this struct.
        let _ = self._child.kill();
        let _ = self._child.wait();
        // Wake the reader (POLLHUP on its shutdown fd) and join it. EOF alone
        // is not a reliable wake: see `wait_pty_readable`.
        drop(self.reader_shutdown.take());
        if let Some(handle) = self.reader_thread.take() {
            let _ = handle.join();
        }
    }
}

/// Walk the visible grid from (sr, sc) to (er, ec) inclusive, joining
/// cell contents row-by-row, trimming trailing whitespace per row, and
/// inserting `\n` between rows. Coordinates are viewport-relative;
/// `display_offset` is the alacritty grid's current scrollback offset
/// (number of rows the viewport has been scrolled back from the live
/// bottom). The grid line we read is `row - display_offset`, which is
/// negative when the user has scrolled into history — alacritty's
/// `Grid::index<Point>` accepts those negative `Line` values and
/// returns the scrollback cell, so the extracted text matches exactly
/// what the user sees highlighted on screen. Without the subtraction
/// the function silently re-reads the live grid at the viewport row,
/// which is a different cell entirely once `display_offset > 0` and
/// the user gets the wrong line on the clipboard.
/// One grid row as text plus a char-index → grid-column map. A wide char
/// (CJK, emoji) occupies two grid columns — the `WIDE_CHAR` cell and a
/// spacer cell — so the spacers are skipped, making the text read
/// contiguously (`"日本語"`, never `"日 本 語"`). `cols[i]` is the grid
/// column the i-th char starts at, so highlight painters can map a match's
/// char range back onto grid cells.
pub fn row_text_and_cols(term: &Term<VoidListener>, line_idx: i32) -> (String, Vec<usize>) {
    let ncols = term.columns();
    let mut s = String::with_capacity(ncols);
    let mut cols = Vec::with_capacity(ncols);
    for c in 0..ncols {
        let cell = &term.grid()[Point::new(Line(line_idx), Column(c))];
        if cell
            .flags
            .intersects(Flags::WIDE_CHAR_SPACER | Flags::LEADING_WIDE_CHAR_SPACER)
        {
            continue;
        }
        s.push(if cell.c == '\0' { ' ' } else { cell.c });
        cols.push(c);
    }
    (s, cols)
}

/// The typed text of the command now ending: from the newest PromptEnd
/// (`133;B`) cell to the line before the newest CommandStart (`133;C`) —
/// the same span `pair_decorations` derives after the fact. Runs in the
/// reader thread at the `133;D` mark with the term lock held; empty when
/// the B/C marks are missing (no shell integration on this pane).
fn last_command_input_text(
    term: &Term<VoidListener>,
    marks: &[StoredMark],
    clock_now: i64,
) -> String {
    use crate::shell_integration::OscEvent as E;
    let cur = |m: &StoredMark| m.line_rec - (clock_now - m.clock_rec) as i32;
    let Some(ci) = marks
        .iter()
        .rposition(|m| matches!(m.kind, E::CommandStart))
    else {
        return String::new();
    };
    let Some(b) = marks[..ci]
        .iter()
        .rev()
        .find(|m| matches!(m.kind, E::PromptEnd))
    else {
        return String::new();
    };
    let bl = cur(b);
    let cl = cur(&marks[ci]);
    if cl <= bl {
        return String::new();
    }
    extract_selection_text(
        term,
        bl,
        b.col_rec,
        cl - 1,
        term.columns().saturating_sub(1),
    )
}

/// The finished command's OUTPUT: the rows between the newest `133;C` mark
/// and the row the cursor sits on as its `133;D` arrives (exclusive — that
/// row is the D mark's own). Escape-free (the grid never stores escapes),
/// tail-capped at [`FINISHED_OUTPUT_CAP_LINES`].
fn last_command_output_text(
    term: &Term<VoidListener>,
    marks: &[StoredMark],
    clock_now: i64,
) -> String {
    use crate::shell_integration::OscEvent as E;
    let Some(c) = marks
        .iter()
        .rev()
        .find(|m| matches!(m.kind, E::CommandStart))
    else {
        return String::new();
    };
    // The C mark was recorded when the command STARTED: everything the
    // command printed has scrolled the grid since, so its recorded line is
    // re-based through the scroll clock exactly like the input extractor.
    // The mark's own row is the FIRST output row — `133;C` is emitted right
    // after the command's newline, before the program prints anything.
    let start = c.line_rec - (clock_now - c.clock_rec) as i32;
    // The cursor row is INCLUDED: `133;D` moves no cursor, so a diagnostic
    // printed without a trailing newline leaves the mark ON that row — the
    // old `- 1` dropped exactly the line the matchers needed. With a
    // trailing newline the cursor row is a fresh blank, trimmed below.
    let end = term.grid().cursor.point.line.0;
    if end < start {
        return String::new();
    }
    let text = extract_selection_text(term, start, 0, end, term.columns().saturating_sub(1));
    let text = text.trim_end_matches('\n');
    let lines: Vec<&str> = text.lines().collect();
    if lines.len() > FINISHED_OUTPUT_CAP_LINES {
        lines[lines.len() - FINISHED_OUTPUT_CAP_LINES..].join("\n")
    } else {
        text.to_string()
    }
}

/// Epoch millis → local wall-clock `HH:MM:SS` (libc localtime, the same
/// no-date-crate route the trash metadata writer takes).
fn hhmmss(millis: u64) -> String {
    // `localtime_r` takes a `*const time_t`, so the binding's own definition is
    // the only correct type here. musl's is mid-migration to 64-bit and libc
    // marks the alias deprecated ahead of the change, which `-D warnings`
    // turns into a build error on the Linux targets; naming the field type
    // through `tm` is not possible (it has no `time_t` member), so the alias
    // stays and the deprecation is acknowledged at this one call.
    #[allow(deprecated)]
    let secs = (millis / 1000) as libc::time_t;
    let mut tm: libc::tm = unsafe { std::mem::zeroed() };
    if unsafe { libc::localtime_r(&secs, &mut tm) }.is_null() {
        return String::from("--:--:--");
    }
    format!("{:02}:{:02}:{:02}", tm.tm_hour, tm.tm_min, tm.tm_sec)
}

/// The surviving column spans of a block row that app chrome partially
/// overdrew: the longest common prefix and suffix between the remembered
/// row and what the grid shows. Returns the grid-column ranges to keep
/// highlighted — a pill floating over the middle of a row leaves BOTH
/// intact ends lit.
fn partial_overlap_cols(want: &str, got: &str, colmap: &[usize]) -> Vec<(u16, u16)> {
    // Row text is full grid width: strip the trailing blank run or it
    // counts as a huge shared suffix between any two rows.
    let want = want.trim_end();
    let got = got.trim_end();
    let want_n = want.chars().count();
    let got_n = got.chars().count();
    if want_n == 0 || got_n == 0 {
        return Vec::new();
    }
    let prefix = want
        .chars()
        .zip(got.chars())
        .take_while(|(a, b)| a == b)
        .count();
    let suffix = want
        .chars()
        .rev()
        .zip(got.chars().rev())
        .take_while(|(a, b)| a == b)
        .count();
    // Each surviving end must carry real content, not just indentation.
    // No minimum share of the row: a pill over the MIDDLE leaves both
    // ends under half, and this credit is paint-only — the shift was
    // already anchored by exact rows, so it can't move the highlight.
    let solid =
        |chars: &mut dyn Iterator<Item = char>| chars.filter(|c| !c.is_whitespace()).count() >= 4;
    let good_prefix = prefix > 0 && solid(&mut want.chars().take(prefix));
    let good_suffix = suffix > 0 && solid(&mut want.chars().rev().take(suffix));
    let mut spans = Vec::new();
    if good_prefix && good_suffix && prefix + suffix >= got_n {
        // The ends overlap: the row differs by a few mid-row cells (an
        // animated counter ticking inside the selection) — keep it whole.
        spans.push((colmap[0] as u16, colmap[got_n - 1] as u16));
    } else {
        if good_prefix {
            spans.push((colmap[0] as u16, colmap[prefix - 1] as u16));
        }
        if good_suffix {
            spans.push((colmap[got_n - suffix] as u16, colmap[got_n - 1] as u16));
        }
    }
    spans
}

/// Capture the content anchor for an alternate-screen selection: its row
/// text and extracted text as the grid shows them now. `None` when any
/// selected line is outside the grid (a partially scrolled-out block keeps
/// its previous, fuller anchor instead).
fn capture_alt_anchor(term: &Term<VoidListener>, sel: Selection) -> Option<AltSelAnchor> {
    let rows_vis = term.screen_lines() as i32;
    let (lo, hi) = (sel.anchor.0.min(sel.head.0), sel.anchor.0.max(sel.head.0));
    if lo < 0 || hi >= rows_vis {
        return None;
    }
    let rows = (lo..=hi).map(|l| row_text_and_cols(term, l).0).collect();
    let ctx_above = (lo > 0).then(|| row_text_and_cols(term, lo - 1).0);
    let ctx_below = (hi + 1 < rows_vis).then(|| row_text_and_cols(term, hi + 1).0);
    let text = if sel.block {
        let (rl, cl, rh, ch) = sel.block_bounds();
        block_selection_text(term, rl, cl as usize, rh, ch as usize)
    } else {
        let (sr, sc, er, ec) = sel.normalised();
        extract_selection_text(term, sr, sc as usize, er, ec as usize)
    };
    Some(AltSelAnchor {
        rows,
        top: lo,
        text,
        dormant: false,
        visible: None,
        ctx_above,
        ctx_below,
    })
}

pub fn extract_selection_text(
    term: &Term<VoidListener>,
    sr: i32,
    sc: usize,
    er: i32,
    ec: usize,
) -> String {
    let cols = term.columns();
    // Clamp the range to the grid: the newest live line is
    // `screen_lines - 1`; the oldest readable line is the grid's topmost
    // (scrollback floor). Reading outside that panics alacritty's index.
    let max_line = term.screen_lines() as i32 - 1;
    let min_line = term.grid().topmost_line().0;
    let sr = sr.max(min_line);
    let er = er.min(max_line);
    let mut out = String::new();
    let mut line_idx = sr;
    while line_idx <= er {
        let row_start = if line_idx == sr { sc } else { 0 };
        let row_end = if line_idx == er {
            ec.min(cols.saturating_sub(1))
        } else {
            cols.saturating_sub(1)
        };
        let mut line = String::new();
        for col in row_start..=row_end {
            let p = Point::new(Line(line_idx), Column(col));
            let cell = &term.grid()[p];
            // A wide char's spacer cell holds no glyph of its own — skip it
            // so copied CJK/emoji text comes out contiguous.
            if cell
                .flags
                .intersects(Flags::WIDE_CHAR_SPACER | Flags::LEADING_WIDE_CHAR_SPACER)
            {
                continue;
            }
            let c = cell.c;
            if c == '\0' {
                line.push(' ');
            } else {
                line.push(c);
            }
        }
        let trimmed = line.trim_end();
        out.push_str(trimmed);
        if line_idx != er {
            // A soft-wrapped row (WRAPLINE on its last cell) continues the
            // same logical line: no separator, so copied text, the durable
            // command history, and the sticky header all see the line the
            // user actually typed — a '\n' here corrupted stored commands
            // and re-ran only their first fragment on paste. A hard row
            // break keeps the newline.
            let wrapped = cols > 0
                && term.grid()[Point::new(Line(line_idx), Column(cols - 1))]
                    .flags
                    .contains(Flags::WRAPLINE);
            if !wrapped {
                out.push('\n');
            }
        }
        line_idx += 1;
    }
    out
}

/// Inspect a row of `term` around `(row, col)` (both viewport-relative)
/// and return the anchor/head pair that brackets the contiguous run of
/// word characters covering the pivot. `display_offset` is the
/// alacritty grid's current scroll-back offset so the lookup
/// `Line(row - display_offset)` resolves to the scrollback cell when
/// the user has scrolled into history. Without this, a double-click
/// on text the user sees on screen but lives in scrollback would read
/// the live grid at the same viewport y, which is usually blank — so
/// `is_terminal_word_char` returns false and the function returns
/// `None`, giving the user-visible symptom "double-click doesn't
/// auto-select."
///
/// Returns `None` when the pivot sits on a non-word character
/// (whitespace, punctuation), so a double click between words is a
/// no-op rather than a spurious selection. Word semantics match
/// `widgets::editor::is_word_char`: alphanumeric + underscore. Pure
/// function so the test suite can exercise it without spawning a PTY.
pub fn select_word_at_in_term(
    term: &Term<VoidListener>,
    display_offset: usize,
    row: usize,
    col: usize,
) -> Option<((u16, u16), (u16, u16))> {
    let cols = term.columns();
    if col >= cols {
        return None;
    }
    let row_idx = row as i32 - display_offset as i32;
    let cell_char = |c: usize| -> char {
        let p = Point::new(Line(row_idx), Column(c));
        let ch = term.grid()[p].c;
        if ch == '\0' { ' ' } else { ch }
    };
    if !is_terminal_word_char(cell_char(col)) {
        return None;
    }
    let mut start = col;
    while start > 0 && is_terminal_word_char(cell_char(start - 1)) {
        start -= 1;
    }
    let mut end = col;
    while end + 1 < cols && is_terminal_word_char(cell_char(end + 1)) {
        end += 1;
    }
    Some(((row as u16, start as u16), (row as u16, end as u16)))
}

pub fn is_terminal_word_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}

/// Encode a mouse event as the byte sequence a child program expects.
/// `sgr` selects SGR (1006) encoding over legacy X10. `local_col`/`local_row`
/// are 0-based cells within the pane; the wire protocol is 1-based, so both
/// are offset by one. Button/modifier bit layout follows xterm: Left/Middle/
/// Right = 0/1/2, wheel up/down = 64/65, +32 for motion, +4/+8/+16 for
/// Shift/Alt/Ctrl. Under X10 a release reports button code 3 and coordinates
/// clamp to 223 (the largest a single 0x20-based byte can carry).
pub fn encode_mouse_report(
    sgr: bool,
    button: MouseButtonKind,
    action: MouseAction,
    local_col: u16,
    local_row: u16,
    mods: MouseMods,
) -> Vec<u8> {
    let mut cb: u8 = match button {
        MouseButtonKind::Left => 0,
        MouseButtonKind::Middle => 1,
        MouseButtonKind::Right => 2,
        MouseButtonKind::WheelUp => 64,
        MouseButtonKind::WheelDown => 65,
    };
    if action == MouseAction::Motion {
        cb += 32;
    }
    if mods.shift {
        cb += 4;
    }
    if mods.alt {
        cb += 8;
    }
    if mods.ctrl {
        cb += 16;
    }
    let cx = local_col + 1;
    let cy = local_row + 1;
    if sgr {
        let terminator = if action == MouseAction::Release {
            'm'
        } else {
            'M'
        };
        format!("\x1b[<{cb};{cx};{cy}{terminator}").into_bytes()
    } else {
        if action == MouseAction::Release {
            cb = (cb & !0b11) | 0b11;
        }
        let enc = |v: u16| (v.min(223) as u8).wrapping_add(32);
        vec![0x1b, b'[', b'M', cb.wrapping_add(32), enc(cx), enc(cy)]
    }
}

/// Build the byte sequence that retargets a POSIX shell's working
/// directory to `path`. Prefixed with `\x05\x15` so cursor moves to end
/// of line and the existing line is killed backward before the `cd` is
/// typed - that way a half-typed command at the prompt does not
/// concatenate with our `cd`. Path is single-quoted with embedded
/// quotes escaped via the standard `'\''` sleight-of-hand so paths
/// containing spaces or apostrophes round-trip safely.
pub fn format_cd_command(path: &std::path::Path) -> Vec<u8> {
    let s = path.to_string_lossy();
    let mut out = Vec::with_capacity(s.len() + 8);
    out.extend_from_slice(b"\x05\x15cd '");
    for c in s.chars() {
        if c == '\'' {
            out.extend_from_slice(b"'\\''");
        } else {
            let mut buf = [0u8; 4];
            out.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
        }
    }
    out.extend_from_slice(b"'\n");
    out
}

/// Walk a chunk of PTY output and toggle the bracketed-paste flag when
/// we see `\e[?2004h` (set) / `\e[?2004l` (reset).
pub fn sniff_bracketed_paste_mode(chunk: &[u8], flag: &AtomicBool) {
    let needle_set: &[u8] = b"\x1b[?2004h";
    let needle_reset: &[u8] = b"\x1b[?2004l";
    let mut i = 0;
    while i < chunk.len() {
        if chunk[i..].starts_with(needle_set) {
            flag.store(true, Ordering::Release);
            i += needle_set.len();
        } else if chunk[i..].starts_with(needle_reset) {
            flag.store(false, Ordering::Release);
            i += needle_reset.len();
        } else {
            i += 1;
        }
    }
}

/// Build the OSC 52 escape sequence that asks the host terminal to put
/// `text` on the system clipboard.
pub fn osc52_copy_seq(text: &str) -> Vec<u8> {
    use base64::Engine;
    let encoded = base64::engine::general_purpose::STANDARD.encode(text.as_bytes());
    let mut out = Vec::with_capacity(encoded.len() + 8);
    out.extend_from_slice(b"\x1b]52;c;");
    out.extend_from_slice(encoded.as_bytes());
    out.push(0x07);
    out
}

/// Map an alacritty cell color to ratatui through the theme's 16-color ANSI
/// palette: Named colors and Indexed 0-15 resolve to the palette's RGB (so a
/// pane renders the same on every host terminal, the way VS Code owns its
/// terminal palette); true-color cells pass through; higher indexes keep the
/// standard 256-color cube the host renders.
fn ansi_to_ratatui(c: AnsiColor, palette: &[(u8, u8, u8); 16]) -> Option<Color> {
    match c {
        AnsiColor::Spec(rgb) => Some(Color::Rgb(rgb.r, rgb.g, rgb.b)),
        AnsiColor::Indexed(i) if i < 16 => Some(pal(palette, i as usize)),
        AnsiColor::Indexed(i) => Some(Color::Indexed(i)),
        AnsiColor::Named(named) => named_to_ratatui(named, palette),
    }
}

fn pal(palette: &[(u8, u8, u8); 16], i: usize) -> Color {
    let (r, g, b) = palette[i];
    Color::Rgb(r, g, b)
}

fn named_to_ratatui(n: NamedColor, palette: &[(u8, u8, u8); 16]) -> Option<Color> {
    use NamedColor::*;
    match n {
        Foreground | Background | Cursor | DimForeground => None,
        Black | DimBlack => Some(pal(palette, 0)),
        Red | DimRed => Some(pal(palette, 1)),
        Green | DimGreen => Some(pal(palette, 2)),
        Yellow | DimYellow => Some(pal(palette, 3)),
        Blue | DimBlue => Some(pal(palette, 4)),
        Magenta | DimMagenta => Some(pal(palette, 5)),
        Cyan | DimCyan => Some(pal(palette, 6)),
        White | DimWhite => Some(pal(palette, 7)),
        BrightBlack => Some(pal(palette, 8)),
        BrightRed => Some(pal(palette, 9)),
        BrightGreen => Some(pal(palette, 10)),
        BrightYellow => Some(pal(palette, 11)),
        BrightBlue => Some(pal(palette, 12)),
        BrightMagenta => Some(pal(palette, 13)),
        BrightCyan => Some(pal(palette, 14)),
        BrightWhite => Some(pal(palette, 15)),
        BrightForeground => None,
    }
}

impl Widget for &mut PtyTerminal {
    fn render(self, area: Rect, buf: &mut Buffer) {
        // A host-accent rule outranks the focus color: a production pane
        // must read as dangerous whether or not it holds focus.
        let block_style = if let Some((r, g, b)) = self.accent {
            Style::default().fg(Color::Rgb(r, g, b))
        } else if self.focused {
            Style::default().fg(Color::Rgb(0x4e, 0x9a, 0xff))
        } else {
            Style::default().fg(Color::DarkGray)
        };
        // The panel group's tab strip already labels this region "TERMINAL", so
        // each pane stays titleless — a bare bordered box, matching VS Code's
        // unlabelled split terminal panes. The border alone frames the pane.
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(block_style);
        let inner = block.inner(area);
        block.render(area, buf);
        // Black theme: replace the solid focus border with the orange→green
        // gradient (matching the welcome activity box).
        if self.focused && self.focus_gradient && self.accent.is_none() {
            crate::gradient::paint_gradient_box(buf, area);
        }
        self.last_area = area;
        self.last_inner = inner;
        // Re-anchor the selection / copy-cursor to the content they were
        // made on before painting, so the highlight scrolls WITH streaming
        // output instead of sitting at fixed screen rows while text slides
        // underneath (the Claude Code drag-select bug).
        self.rebase_selection();
        // (is_block, bounds): block selections rectangle-test each cell,
        // linear ones use the row-major span. A dormant alt-screen
        // selection paints nothing — its content is scrolled out of the
        // app's view and the frozen coordinates sit over unrelated text; a
        // partially covered one paints only its surviving rows.
        let dormant = self.alt_sel.as_ref().is_some_and(|a| a.dormant);
        let sel_clip = self.alt_sel.as_ref().and_then(|a| a.visible.clone());
        let sel_paint = self.selection.filter(|_| !dormant).map(|s| {
            if s.block {
                (true, s.block_bounds())
            } else {
                (false, s.normalised())
            }
        });

        let cols = inner.width;
        let rows = inner.height;
        self.resize(cols, rows);

        // Snapshot before taking the term lock: command_decorations locks
        // the term itself and FairMutex is not reentrant.
        let decorations = self.command_decorations();
        // Trigger set snapshot for the highlight pass (cheap Arc clone).
        let trigger_set = self.triggers.lock().unwrap().clone();

        let term = self.term.lock();
        // Annotation spans at their current grid lines, translated by the
        // scroll clock (same anchor as selections — history growth
        // saturates and froze these in long-lived panes). None while an
        // alt-screen app owns the viewport: the spans name primary-screen
        // rows, and painting them over vim/htop content is chart junk.
        let ann_spans: Vec<(i32, u16, u16)> = if term.mode().contains(TermMode::ALT_SCREEN) {
            Vec::new()
        } else {
            let ann_now = self.clock_now(&term);
            self.annotations
                .iter()
                .map(|a| (a.line_rec - (ann_now - a.clock_rec) as i32, a.start, a.len))
                .collect()
        };
        let display_offset = term.grid().display_offset();
        let cursor_visible = term.mode().contains(TermMode::SHOW_CURSOR) && self.focused;
        let alt_screen = term.mode().contains(TermMode::ALT_SCREEN);
        let cursor_point = term.grid().cursor.point;
        // The cursor is in absolute grid coords (Line is 0..rows in alt
        // screen, can wander into negative scrollback in normal screen).
        // Convert to viewport row by adding display_offset (>0 when the
        // user has scrolled up into history).
        let cursor_row_in_viewport = if alt_screen {
            cursor_point.line.0
        } else {
            cursor_point.line.0 + display_offset as i32
        };
        let cursor_col_in_viewport = cursor_point.column.0 as i32;

        // Quick-select hint drift: lines were captured at the set's clock
        // reading; re-base them to now so labels follow their matches
        // through streaming output (the annotation translation, one read
        // per frame).
        let hint_drift: i32 = self
            .hints
            .as_ref()
            .map_or(0, |(c0, _)| (self.clock_now(&term) - c0) as i32);

        // The active find match, re-based to the current clock the same way
        // (its stored line drifts by exactly the rows scrolled since it was
        // anchored, so the bright cell stays glued to its text).
        let active_match: Option<(i32, usize, usize)> = self
            .current_match
            .map(|(c0, l, c, n)| (l - (self.clock_now(&term) - c0) as i32, c, n));

        for y in 0..rows {
            // Find matches on this row once, then paint them per cell below.
            // Match positions are char indices in the spacer-skipped row text
            // (the same text the find bar searched); the colmap translates
            // them back to grid columns. 0 = no match, 1 = match, 2 = active.
            let row_line_idx = (y as i32) - (display_offset as i32);
            let row_paint: Option<Vec<u8>> = self.search_needle.as_deref().map(|needle| {
                let (text, colmap) = row_text_and_cols(&term, row_line_idx);
                let mut paint = vec![0u8; cols as usize];
                for (mc, ml) in
                    crate::widgets::editor_find::line_matches(&text, self.search_opts, needle)
                {
                    let active = active_match == Some((row_line_idx, mc, ml));
                    for k in mc..mc + ml {
                        if let Some(&col) = colmap.get(k) {
                            paint[col] = if active { 2 } else { 1 };
                        }
                    }
                }
                paint
            });
            // Quick-select hints on this row: 1 = match span, 2 = label cell
            // (the char to draw rides in the parallel vec). Char indices go
            // through the same colmap as the find highlight.
            let hint_paint: Option<(Vec<u8>, Vec<char>)> =
                self.hints.as_ref().and_then(|(_, hints)| {
                    let row_hints: Vec<&HintSpan> = hints
                        .iter()
                        .filter(|h| h.line - hint_drift == row_line_idx)
                        .collect();
                    if row_hints.is_empty() {
                        return None;
                    }
                    let (_text, colmap) = row_text_and_cols(&term, row_line_idx);
                    let mut span = vec![0u8; cols as usize];
                    let mut label = vec!['\0'; cols as usize];
                    for h in row_hints {
                        for k in h.start..h.start + h.len {
                            if let Some(&col) = colmap.get(k) {
                                span[col] = 1;
                            }
                        }
                        for (j, c) in h.label.chars().skip(h.typed).enumerate() {
                            if let Some(&col) = colmap.get(h.start + j) {
                                span[col] = 2;
                                label[col] = c;
                            }
                        }
                    }
                    Some((span, label))
                });
            // Trigger highlights on this row: per-cell fg/bg from the first
            // matching highlight trigger. Painted under the find highlight
            // and quick-select labels, which both win.
            let trig_paint: Option<TrigRowPaint> = trigger_set.has_highlights().then(|| {
                let (text, colmap) = row_text_and_cols(&term, row_line_idx);
                let mut paint: TrigRowPaint = vec![None; cols as usize];
                for s in crate::triggers::highlight_spans(&text, &trigger_set) {
                    let fg = s.fg.map(|(r, g, b)| Color::Rgb(r, g, b));
                    let bg = s.bg.map(|(r, g, b)| Color::Rgb(r, g, b));
                    for k in s.start..s.start + s.len {
                        if let Some(&col) = colmap.get(k) {
                            paint[col] = Some((fg, bg));
                        }
                    }
                }
                paint
            });
            for x in 0..cols {
                let line_idx = (y as i32) - (display_offset as i32);
                let p = Point::new(Line(line_idx), Column(x as usize));
                let cell = &term.grid()[p];
                let mut display_char = if cell.c == '\0' { ' ' } else { cell.c };
                let mut style = Style::default();
                if let Some(c) = ansi_to_ratatui(cell.fg, &self.palette) {
                    style = style.fg(c);
                }
                if let Some(c) = ansi_to_ratatui(cell.bg, &self.palette) {
                    style = style.bg(c);
                }
                let flags = cell.flags;
                if flags.contains(Flags::BOLD) {
                    style = style.add_modifier(Modifier::BOLD);
                }
                if flags.contains(Flags::ITALIC) {
                    style = style.add_modifier(Modifier::ITALIC);
                }
                // SGR 2 (faint): Claude Code's ghost suggestions and other
                // TUIs de-emphasise text with this; dropping it makes the
                // suggestion read as typed input.
                if flags.contains(Flags::DIM) {
                    style = style.add_modifier(Modifier::DIM);
                }
                if flags.intersects(Flags::ALL_UNDERLINES) {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                if flags.contains(Flags::INVERSE) {
                    style = style.add_modifier(Modifier::REVERSED);
                }
                // Trigger highlight: the user's per-trigger colours over the
                // matched span. Cursor / selection / find / quick-select all
                // paint after this, so they stay visible on top.
                if let Some(paint) = trig_paint.as_ref()
                    && let Some(Some((fg, bg))) = paint.get(x as usize)
                {
                    if let Some(c) = fg {
                        style = style.fg(*c);
                    }
                    if let Some(c) = bg {
                        style = style.bg(*c);
                    }
                }
                // Annotated spans: amber + underline, under the cursor /
                // selection / find layers so those still win.
                if ann_spans
                    .iter()
                    .any(|&(l, s0, ln)| l == line_idx && x >= s0 && x < s0 + ln)
                {
                    style = style
                        .fg(Color::Rgb(0xe5, 0xc0, 0x7b))
                        .add_modifier(Modifier::UNDERLINED);
                }
                if cursor_visible
                    && (y as i32) == cursor_row_in_viewport
                    && (x as i32) == cursor_col_in_viewport
                {
                    style = style.add_modifier(Modifier::REVERSED);
                }
                if let Some((block, (sr, sc, er, ec))) = sel_paint
                    && sel_clip.as_ref().is_none_or(|clips| {
                        clips
                            .iter()
                            .any(|&(l, lo, hi)| l == line_idx && x >= lo && x <= hi)
                    })
                    && if block {
                        cell_in_block_selection(line_idx, x, sr, sc, er, ec)
                    } else {
                        cell_in_selection(line_idx, x, sr, sc, er, ec)
                    }
                {
                    style = style.bg(Color::Rgb(0x26, 0x4f, 0x78));
                }
                // Find highlight: muted amber on every occurrence, bright
                // orange on the active match (VS Code's find colours).
                if let Some(paint) = row_paint.as_ref() {
                    match paint.get(x as usize) {
                        Some(1) => {
                            style = style
                                .fg(Color::Black)
                                .bg(Color::Rgb(0xff, 0xd7, 0x4a))
                                .add_modifier(Modifier::BOLD);
                        }
                        Some(2) => {
                            style = style
                                .fg(Color::Black)
                                .bg(Color::Rgb(0xff, 0x8c, 0x2a))
                                .add_modifier(Modifier::BOLD);
                        }
                        _ => {}
                    }
                }
                // Quick-select: matched spans turn green, label cells overlay
                // black-on-gold and replace the glyph underneath (WezTerm's
                // quick-select colours).
                if let Some((span, labels)) = hint_paint.as_ref() {
                    match span.get(x as usize) {
                        Some(1) => {
                            style = style
                                .fg(Color::Rgb(0x66, 0xcc, 0x66))
                                .add_modifier(Modifier::BOLD);
                        }
                        Some(2) => {
                            if let Some(&lc) = labels.get(x as usize)
                                && lc != '\0'
                            {
                                display_char = lc;
                            }
                            style = Style::default()
                                .fg(Color::Black)
                                .bg(Color::Rgb(0xff, 0xd7, 0x4a))
                                .add_modifier(Modifier::BOLD);
                        }
                        _ => {}
                    }
                }
                // Copy-mode cursor: a green modal block, painted last so the
                // caret stays visible over selection / find / trigger paint.
                if let Some((cl, cc)) = self.copy_cursor
                    && line_idx == cl
                    && x == cc
                {
                    style = Style::default()
                        .fg(Color::Black)
                        .bg(Color::Rgb(0x66, 0xcc, 0x66))
                        .add_modifier(Modifier::BOLD);
                }
                let target_x = inner.x + x;
                let target_y = inner.y + y;
                let target = &mut buf[(target_x, target_y)];
                let mut tmp = [0u8; 4];
                target.set_symbol(display_char.encode_utf8(&mut tmp));
                target.set_style(style);
            }
        }
        // OSC 9;4 progress gauge along the bottom border (Ghostty/WezTerm
        // parity): a fill in the state's colour over the border glyphs —
        // blue normal, red error, yellow warning — and a sweeping segment
        // while indeterminate. Border cells are croft chrome, so no program
        // content is ever covered (same contract as the decoration dots).
        if let Some((state, pct)) = *self.progress.lock().unwrap()
            && area.height >= 2
            && inner.width > 0
        {
            let w = inner.width as u32;
            let (fill_from, fill_len) = if state == 3 {
                // Indeterminate: a ~fifth-width segment sweeping left to
                // right, phased off the wall clock so each frame advances.
                let seg = (w / 5).max(1);
                let span = (w - seg).max(1);
                let ms = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_millis() as u32)
                    .unwrap_or(0);
                ((ms / 80) % span, seg)
            } else {
                (0, w * u32::from(pct.min(100)) / 100)
            };
            let color = match state {
                2 => Color::Rgb(0xf1, 0x4c, 0x4c),
                4 => Color::Rgb(0xe5, 0xc0, 0x7b),
                _ => Color::Rgb(0x1b, 0x81, 0xa8),
            };
            let by = area.y + area.height - 1;
            for i in 0..fill_len.min(w) {
                let x = inner.x + (fill_from + i) as u16;
                let cell = &mut buf[(x, by)];
                cell.set_symbol("━");
                cell.set_style(Style::default().fg(color));
            }
        }
        // Timestamps gutter: the arrival clock of each stamped row hugs the
        // right edge (iTerm2's Show Timestamps), amber with a warning mark
        // when the row landed a long stall after its predecessor. Chrome
        // over content, painted only while the palette toggle is on.
        if self.show_timestamps && !alt_screen && inner.width > 14 {
            // Row ids are `clock + grid line` — content-stable through
            // scrollback saturation, matching the reader thread's stamps.
            // Clock before line_times: the shared lock order is
            // term → clock → line_times (see [`ScrollClock`]).
            let clock_now = self.clock_now(&term);
            let lt = self.line_times.lock().unwrap();
            for y in 0..rows {
                let abs = clock_now + (y as i32 - display_offset as i32) as i64;
                let Some(&ms) = lt.get(&abs) else { continue };
                let prev = lt.range(..abs).next_back().map(|(_, &v)| v);
                let stalled = prev.is_some_and(|p| ms.saturating_sub(p) >= STALL_GAP_MS);
                let mut text = hhmmss(ms);
                if stalled {
                    text = format!("\u{26a0} {text}");
                }
                let tw = text.chars().count() as u16;
                if tw >= inner.width {
                    continue;
                }
                let x0 = inner.x + inner.width - tw;
                let fg = if stalled {
                    Color::Rgb(0xe5, 0xc0, 0x7b)
                } else {
                    Color::Rgb(0x5b, 0x64, 0x72)
                };
                for (j, c) in text.chars().enumerate() {
                    let cell = &mut buf[(x0 + j as u16, inner.y + y)];
                    let mut tmpc = [0u8; 4];
                    cell.set_symbol(c.encode_utf8(&mut tmpc));
                    cell.set_style(Style::default().fg(fg));
                }
            }
        }
        // Host-accent badge watermark: the rule's badge text, dim in the
        // accent color, parked at the pane's top-right (under the sticky
        // header, over content — it is a warning, that is the point).
        if let (Some((r, g, b)), Some(badge)) = (self.accent, self.accent_badge.as_deref())
            && !badge.is_empty()
        {
            let bw = badge.chars().count() as u16;
            if bw + 2 < inner.width {
                let x0 = inner.x + inner.width - bw - 1;
                for (j, c) in badge.chars().enumerate() {
                    let cell = &mut buf[(x0 + j as u16, inner.y)];
                    let mut tmpc = [0u8; 4];
                    cell.set_symbol(c.encode_utf8(&mut tmpc));
                    cell.set_style(
                        Style::default()
                            .fg(Color::Rgb(r, g, b))
                            .add_modifier(Modifier::DIM | Modifier::BOLD),
                    );
                }
            }
        }
        // Sticky command header (Warp): when the viewport is scrolled so its
        // top row falls inside one command's output while that command's
        // prompt sits above the view, the typed command pins to the pane's
        // top row with the scroll depth. Finished commands resolve through
        // the decoration spans; the still-running one through the newest
        // CommandStart mark. Unpins the instant the top row leaves the span.
        if display_offset > 0 && !alt_screen && rows > 0 && inner.width > 4 {
            let top_row = -(display_offset as i32);
            let cols_last = term.columns().saturating_sub(1);
            let header = decorations
                .iter()
                .find(|d| d.line < top_row && d.output_start <= top_row && top_row < d.output_end)
                .and_then(|d| {
                    let (l, c) = d.input?;
                    (d.output_start > l)
                        .then(|| extract_selection_text(&term, l, c, d.output_start - 1, cols_last))
                })
                .or_else(|| {
                    let ms = self.marks.lock().unwrap();
                    let running = ms.last().is_some_and(|m| {
                        matches!(m.kind, crate::shell_integration::OscEvent::CommandStart)
                    });
                    if !running {
                        return None;
                    }
                    let now = self.clock_now(&term);
                    let last = ms.last().unwrap();
                    let c_line = last.line_rec - (now - last.clock_rec) as i32;
                    (c_line < top_row).then(|| last_command_input_text(&term, &ms, now))
                })
                .filter(|t| !t.trim().is_empty());
            if let Some(text) = header {
                let bg = Color::Rgb(0x25, 0x2b, 0x36);
                for x in 0..inner.width {
                    let cell = &mut buf[(inner.x + x, inner.y)];
                    cell.set_symbol(" ");
                    cell.set_style(Style::default().bg(bg));
                }
                // A multi-row command (soft-wrapped or a quoted/heredoc
                // newline) arrives with its rows joined by '\n'; a cell must
                // never hold ANY control char, so the whole class maps to
                // spaces, not just the row join.
                let label: String = format!("\u{25b6} {}", text.trim())
                    .chars()
                    .map(|c| if c.is_control() { ' ' } else { c })
                    .collect();
                let mut xw = inner.x + 1;
                for ch in label.chars() {
                    if xw + 1 >= inner.x + inner.width {
                        break;
                    }
                    let cell = &mut buf[(xw, inner.y)];
                    let mut tmpc = [0u8; 4];
                    cell.set_symbol(ch.encode_utf8(&mut tmpc));
                    cell.set_style(
                        Style::default()
                            .fg(Color::Rgb(0xec, 0xf0, 0xf4))
                            .bg(bg)
                            .add_modifier(Modifier::BOLD),
                    );
                    xw += 1;
                }
                let right = format!(" \u{2191} {display_offset} ");
                let rw = right.chars().count() as u16;
                if xw + rw < inner.x + inner.width {
                    let x0 = inner.x + inner.width - rw;
                    for (j, ch) in right.chars().enumerate() {
                        let cell = &mut buf[(x0 + j as u16, inner.y)];
                        let mut tmpc = [0u8; 4];
                        cell.set_symbol(ch.encode_utf8(&mut tmpc));
                        cell.set_style(Style::default().fg(Color::Rgb(0x8b, 0x93, 0xa1)).bg(bg));
                    }
                }
            }
        }
        // Command decorations: a VS Code-style dot on the left border at
        // each finished command's prompt row — blue for success, red for a
        // non-zero exit. Normal screen only; an alt-screen app owns the
        // viewport and has no prompts.
        if !alt_screen {
            for d in &decorations {
                let vp = d.line + display_offset as i32;
                if (0..rows as i32).contains(&vp) {
                    let ok = d.exit.unwrap_or(0) == 0;
                    let cell = &mut buf[(area.x, inner.y + vp as u16)];
                    cell.set_symbol("●");
                    cell.set_style(Style::default().fg(if ok {
                        Color::Rgb(0x1b, 0x81, 0xa8)
                    } else {
                        Color::Rgb(0xf1, 0x4c, 0x4c)
                    }));
                }
            }
        }
    }
}

/// Per-cell trigger-highlight paint for one row: `None` = cell untouched,
/// `Some((fg, bg))` = the matching trigger's colours (either side optional,
/// leaving that half of the cell style alone).
type TrigRowPaint = Vec<Option<(Option<Color>, Option<Color>)>>;

/// The display offset that brings absolute grid line `abs_line` to the
/// vertical middle of a `rows`-tall viewport, clamped to `[0, max_off]` (0 =
/// live bottom, `max_off` = oldest scrollback). The render loop shows the
/// grid line `y - display_offset` at viewport row `y`, so centering the
/// target means `display_offset = rows/2 - abs_line`. Pure for testing.
pub fn scroll_offset_for_line(rows: i32, max_off: i32, abs_line: i32) -> i32 {
    (rows / 2 - abs_line).clamp(0, max_off.max(0))
}

/// The text a rectangular selection covers: the same inclusive column
/// slice `[cl..=ch]` from every row `[rl..=rh]`, one line per row (what
/// vim's Ctrl+V yank produces). Rows are clamped to the readable grid the
/// same way `extract_selection_text` clamps.
pub fn block_selection_text(
    term: &Term<VoidListener>,
    rl: i32,
    cl: usize,
    rh: i32,
    ch: usize,
) -> String {
    let max_line = term.screen_lines() as i32 - 1;
    let min_line = term.grid().topmost_line().0;
    let rl = rl.max(min_line);
    let rh = rh.min(max_line);
    let mut rows = Vec::new();
    for line in rl..=rh {
        rows.push(extract_selection_text(term, line, cl, line, ch));
    }
    rows.join("\n")
}

/// True iff (row, col) is inside the inclusive rectangle whose corners are
/// (rl, cl) and (rh, ch) — [`Selection::block_bounds`] output. Public for
/// unit testing.
pub fn cell_in_block_selection(row: i32, col: u16, rl: i32, cl: u16, rh: i32, ch: u16) -> bool {
    row >= rl && row <= rh && col >= cl && col <= ch
}

/// True iff (row, col) is inside the inclusive row-major range
/// [(sr,sc)..=(er,ec)]. Public for unit testing.
pub fn cell_in_selection(row: i32, col: u16, sr: i32, sc: u16, er: i32, ec: u16) -> bool {
    if row < sr || row > er {
        return false;
    }
    if row == sr && col < sc {
        return false;
    }
    if row == er && col > ec {
        return false;
    }
    true
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The zsh the shim tests drive, or an ANNOUNCED skip on a machine
    /// without one (#52). CI provisions zsh, so the gate holds where it
    /// matters; on a zsh-less dev box these tests must skip with a reason
    /// — a hard spawn failure (or a silent `return`) makes "suite green"
    /// ambiguous. Same convention as the cross-compile and PDF gates.
    fn zsh_or_skip() -> Option<&'static str> {
        let zsh = "/bin/zsh";
        if std::path::Path::new(zsh).exists() {
            Some(zsh)
        } else {
            eprintln!("SKIPPED: {zsh} not installed on this machine");
            None
        }
    }

    fn fresh_term(cols: usize, rows: usize) -> Term<VoidListener> {
        let cfg = Config {
            scrolling_history: 1000,
            ..Config::default()
        };
        let size = TermSize::new(cols, rows);
        Term::new(cfg, &size, VoidListener::default())
    }

    fn feed(term: &mut Term<VoidListener>, bytes: &[u8]) {
        let mut p = Processor::<StdSyncHandler>::new();
        p.advance(term, bytes);
    }

    #[test]
    fn block_selection_highlights_and_extracts_a_rectangle() {
        let mut term = fresh_term(20, 5);
        feed(&mut term, b"alpha1\r\nbravo2\r\ncharlie3\r\n");
        // A rectangle over rows 0..=2, cols 1..=3, anchored bottom-right so
        // the bounds must come from independent min/max, not the row-major
        // normalisation linear selections use.
        let sel = Selection {
            anchor: (2, 3),
            head: (0, 1),
            block: true,
        };
        let (rl, cl, rh, ch) = sel.block_bounds();
        assert_eq!((rl, cl, rh, ch), (0, 1, 2, 3));
        assert!(cell_in_block_selection(1, 2, rl, cl, rh, ch));
        assert!(!cell_in_block_selection(1, 0, rl, cl, rh, ch));
        assert!(!cell_in_block_selection(1, 4, rl, cl, rh, ch));
        assert!(!cell_in_block_selection(3, 2, rl, cl, rh, ch));
        assert_eq!(
            block_selection_text(&term, rl, cl as usize, rh, ch as usize),
            "lph\nrav\nhar",
            "a block selection copies the same column slice from every row"
        );
    }

    #[test]
    fn annotations_anchor_to_content_and_ride_the_scrollback() {
        let tmp = tempfile::tempdir().unwrap();
        let script = "n=note; echo ${n}-worthy-line; read x; i=0; while [ $i -lt 30 ]; do echo fill-$i; i=$((i+1)); done; sleep 30";
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        term.last_inner = Rect {
            x: 0,
            y: 0,
            width: 80,
            height: 24,
        };
        let mut waited = 0u32;
        while !term
            .grid_lines()
            .0
            .iter()
            .any(|l| l.starts_with("note-worthy-line"))
        {
            assert!(waited < 8000, "line never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        let (lines, top) = term.grid_lines();
        let line = top
            + lines
                .iter()
                .position(|l| l.starts_with("note-worthy-line"))
                .unwrap() as i32;
        let clock = term.scroll_clock();
        term.add_annotation(line, clock, 0, 16, String::from("this is where it broke"));
        // The screen cell over the span resolves to the note.
        let (idx, text) = term
            .annotation_at(3, line as u16)
            .expect("the annotated cell must resolve");
        assert_eq!(idx, 0);
        assert_eq!(text, "this is where it broke");
        assert!(
            term.annotation_at(30, line as u16).is_none(),
            "cells past the span carry no note"
        );

        // 30 more lines push the annotated row toward (or into) history;
        // the anchor drifts with the content.
        term.write_input(b"\n");
        let mut waited = 0u32;
        while !term.grid_lines().0.iter().any(|l| l.starts_with("fill-29")) {
            assert!(waited < 8000, "filler never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        let (lines, top) = term.grid_lines();
        let expect_line = top
            + lines
                .iter()
                .position(|l| l.starts_with("note-worthy-line"))
                .unwrap() as i32;
        let cur = term.annotations_current();
        assert_eq!(cur.len(), 1);
        assert_eq!(
            cur[0].0, expect_line,
            "the annotation must still sit on its content line"
        );
        assert_eq!(cur[0].3, "this is where it broke");
    }

    #[test]
    fn prompt_click_arrows_map_a_click_to_cursor_motion() {
        let tmp = tempfile::tempdir().unwrap();
        // A prompt with 133;A/B marks and typed text, cursor parked after
        // the text (no newline). `read` keeps the shell at the prompt.
        let script = "h=hello; printf '\\033]133;A\\007$ \\033]133;B\\007'; printf \"${h}-world\"; read x; sleep 30";
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        term.last_inner = Rect {
            x: 1,
            y: 1,
            width: 80,
            height: 24,
        };
        let mut waited = 0u32;
        while !term
            .grid_lines()
            .0
            .iter()
            .any(|l| l.contains("hello-world"))
        {
            assert!(waited < 8000, "prompt text never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        let (lines, top) = term.grid_lines();
        let vrow = (lines
            .iter()
            .position(|l| l.contains("hello-world"))
            .unwrap() as i32
            + top) as u16; // display_offset 0: grid line == viewport row
        // Click on the 'h' (grid col 2): the cursor sits after 'd' (col 13),
        // so 11 left-arrows bring it there.
        let bytes = term
            .prompt_click_arrows(1 + 2, 1 + vrow)
            .expect("a prompt-row click must produce motion");
        assert_eq!(bytes, b"\x1b[D".repeat(11));
        // Clamped left of the input start: col 0 is the "$ " prompt glyphs.
        let bytes = term.prompt_click_arrows(1, 1 + vrow).unwrap();
        assert_eq!(bytes, b"\x1b[D".repeat(11), "clamps to the 133;B column");
        // A click on the cursor cell itself is a no-op.
        assert!(term.prompt_click_arrows(1 + 13, 1 + vrow).is_none());
        // A click on another row does nothing.
        assert!(term.prompt_click_arrows(1 + 2, 1 + vrow + 1).is_none());
    }

    #[test]
    fn row_times_record_when_each_line_arrived() {
        // Deterministic (#170): a loaded scheduler can hold the reader
        // past the writer's sleep, coalescing both echoes into ONE read
        // that stamp_chunk marks with a single now_ms — so no real-PTY
        // timing assertion can pin per-chunk stamps. Drive the stamping
        // path directly instead, one chunk per arrival.
        let (_tmp, t) = quiet_pty();
        let mut prev = 0i64;
        feed_pty(&t, b"first-marker\r\n");
        t.stamp_chunk_for_test(&mut prev, 1000);
        feed_pty(&t, b"second-marker\r\n");
        t.stamp_chunk_for_test(&mut prev, 1400);
        let (lines, top) = t.grid_lines();
        let a = lines
            .iter()
            .position(|l| l.starts_with("first-marker"))
            .expect("first line on the grid");
        let b = lines
            .iter()
            .position(|l| l.starts_with("second-marker"))
            .expect("second line on the grid");
        let ta = t
            .row_time(top + a as i32)
            .expect("the first line must be stamped");
        let tb = t
            .row_time(top + b as i32)
            .expect("the second line must be stamped");
        assert_eq!(ta, 1000, "first row keeps its own chunk's arrival time");
        assert_eq!(tb, 1400, "second row gets the later chunk's arrival time");
    }

    #[test]
    fn reader_thread_stamps_arriving_rows() {
        // End-to-end: the real reader loop must feed stamp_chunk. Only
        // presence and ordering are asserted — any gap assertion races
        // the scheduler (#170).
        let tmp = tempfile::tempdir().unwrap();
        let script = "a=first; echo ${a}-marker; sleep 30";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let mut waited = 0u32;
        loop {
            let (lines, top) = term.grid_lines();
            if let Some(a) = lines.iter().position(|l| l.starts_with("first-marker")) {
                assert!(
                    term.row_time(top + a as i32).is_some(),
                    "the reader thread must stamp rows it delivers"
                );
                break;
            }
            assert!(waited < 8000, "marker never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
    }

    #[test]
    fn resize_reflows_long_lines_instead_of_truncating() {
        // Scrollback soft-reflow is an engine property (alacritty_terminal's
        // Term::resize reflows the normal screen); this pins it so a future
        // engine upgrade can't silently lose it.
        let mut term = fresh_term(30, 5);
        feed(&mut term, b"abcdefghijklmnopqrstuvwxyz1234\r\n");
        term.resize(TermSize::new(10, 5));
        let top = term.grid().topmost_line().0;
        let bottom = term.screen_lines() as i32 - 1;
        let mut all = String::new();
        for l in top..=bottom {
            let (s, _) = row_text_and_cols(&term, l);
            all.push_str(s.trim_end());
        }
        assert!(
            all.contains("abcdefghijklmnopqrstuvwxyz1234"),
            "text must survive a narrowing resize by reflowing: {all:?}"
        );
        // Widening back re-joins the soft-wrapped rows onto one line.
        term.resize(TermSize::new(40, 5));
        let joined: Vec<String> = (term.grid().topmost_line().0..term.screen_lines() as i32)
            .map(|l| row_text_and_cols(&term, l).0.trim_end().to_string())
            .collect();
        assert!(
            joined
                .iter()
                .any(|r| r.contains("abcdefghijklmnopqrstuvwxyz1234")),
            "widening must re-join the reflowed rows: {joined:?}"
        );
    }

    #[test]
    fn scroll_offset_centers_a_scrollback_match_and_clamps_to_the_range() {
        // 24-row pane, 100 lines of scrollback (max_off 100). A match on an
        // old line (-80) centers it: 12 - (-80) = 92, within [0, 100].
        assert_eq!(scroll_offset_for_line(24, 100, -80), 92);
        // A match on the live screen (line 20) wants a negative offset but is
        // clamped to 0 — you cannot scroll below the live bottom.
        assert_eq!(scroll_offset_for_line(24, 100, 20), 0);
        // A match older than the deepest scrollback is clamped to max_off.
        assert_eq!(scroll_offset_for_line(24, 100, -200), 100);
        // No scrollback (alt screen / fresh grid): always pinned to bottom.
        assert_eq!(scroll_offset_for_line(24, 0, -5), 0);
    }

    #[test]
    fn mouse_report_encoding_matches_the_xterm_wire_format() {
        let none = MouseMods::default();
        let shift = MouseMods {
            shift: true,
            ..MouseMods::default()
        };
        // SGR: 1-based coords, 'M' press / 'm' release, wheel = 64/65.
        assert_eq!(
            encode_mouse_report(true, MouseButtonKind::Left, MouseAction::Press, 0, 0, none),
            b"\x1b[<0;1;1M"
        );
        assert_eq!(
            encode_mouse_report(
                true,
                MouseButtonKind::Left,
                MouseAction::Release,
                4,
                2,
                none
            ),
            b"\x1b[<0;5;3m"
        );
        assert_eq!(
            encode_mouse_report(
                true,
                MouseButtonKind::WheelDown,
                MouseAction::Press,
                9,
                9,
                none
            ),
            b"\x1b[<65;10;10M"
        );
        // Motion adds 32; Shift adds 4.
        assert_eq!(
            encode_mouse_report(
                true,
                MouseButtonKind::Left,
                MouseAction::Motion,
                0,
                0,
                shift
            ),
            b"\x1b[<36;1;1M"
        );
        // Legacy X10: 0x20-based bytes, release collapses the button to code 3.
        assert_eq!(
            encode_mouse_report(false, MouseButtonKind::Left, MouseAction::Press, 0, 0, none),
            &[0x1b, b'[', b'M', 32, 33, 33]
        );
        assert_eq!(
            encode_mouse_report(
                false,
                MouseButtonKind::Left,
                MouseAction::Release,
                0,
                0,
                none
            ),
            &[0x1b, b'[', b'M', 35, 33, 33]
        );
    }

    #[test]
    fn dsr_cursor_position_query_round_trips_into_the_pty_response_channel() {
        // Regression for atuin reverse-search (Ctrl+R) panicking with
        // "Error: The cursor position could not be read within a normal
        // duration": atuin sends DSR (`ESC[6n`) and waits for the
        // terminal to write back `ESC[<row>;<col>R` on stdin. Before
        // this fix, croft's `VoidListener` discarded every event
        // alacritty emitted, so the reply never reached the shell and
        // atuin timed out. With the responder wiring in place, the
        // listener must publish the reply on the channel that the
        // background thread forwards to the PTY master.
        let (tx, rx) = std::sync::mpsc::channel::<String>();
        let listener = VoidListener {
            pty_response_tx: Some(tx),
            size: Some(Arc::new(std::sync::Mutex::new((80, 24)))),
            bell: None,
        };
        let cfg = Config::default();
        let size = TermSize::new(80, 24);
        let mut term = Term::new(cfg, &size, listener);
        let mut processor = Processor::<StdSyncHandler>::new();
        processor.advance(&mut term, b"\x1b[6n");
        let response = rx
            .recv_timeout(std::time::Duration::from_millis(500))
            .expect("DSR cursor-position query must produce a PtyWrite event");
        assert_eq!(
            response, "\x1b[1;1R",
            "a fresh terminal's cursor is at row 1, column 1 (1-based), so the reply must be ESC[1;1R"
        );
    }

    #[test]
    fn text_area_size_request_replies_with_the_current_grid_geometry() {
        // CSI 18 t ("report text area size in characters") is what
        // helix, neovim, and other modern TUIs use to learn the
        // viewport size beyond what TIOCGWINSZ surfaces. The reply
        // must echo the live cols/rows held in `size_shared` so a
        // resize inside croft propagates to the embedded program
        // without a fresh listener instance.
        let (tx, rx) = std::sync::mpsc::channel::<String>();
        let size = Arc::new(std::sync::Mutex::new((120u16, 40u16)));
        let listener = VoidListener {
            pty_response_tx: Some(tx),
            size: Some(size),
            bell: None,
        };
        let cfg = Config::default();
        let term_size = TermSize::new(120, 40);
        let mut term = Term::new(cfg, &term_size, listener);
        let mut processor = Processor::<StdSyncHandler>::new();
        processor.advance(&mut term, b"\x1b[18t");
        let response = rx
            .recv_timeout(std::time::Duration::from_millis(500))
            .expect("CSI 18 t must produce a TextAreaSizeRequest reply on the channel");
        assert_eq!(
            response, "\x1b[8;40;120t",
            "CSI 18 t reply format is ESC[8;<rows>;<cols>t per xterm convention"
        );
    }

    #[test]
    fn interactive_shell_invocation_passes_login_flag_for_zsh() {
        let (program, args) = interactive_shell_invocation("/bin/zsh");
        assert_eq!(program, "/bin/zsh");
        assert_eq!(
            args,
            vec!["-l".to_string()],
            "zsh must be spawned as a login shell so ~/.zprofile is sourced and the embedded terminal inherits the same env / keybindings as the user's native iTerm2 shell"
        );
    }

    #[test]
    fn interactive_shell_invocation_passes_login_flag_for_bash_fish_ksh() {
        for path in [
            "/bin/bash",
            "/usr/local/bin/fish",
            "/bin/ksh",
            "/usr/bin/tcsh",
        ] {
            let (_, args) = interactive_shell_invocation(path);
            assert_eq!(
                args,
                vec!["-l".to_string()],
                "{} must be spawned with -l so its login-shell rc files are sourced",
                path
            );
        }
    }

    #[test]
    fn interactive_shell_invocation_skips_login_flag_for_unknown_shells() {
        let (_, args) = interactive_shell_invocation("/opt/exotic/myshell");
        assert!(
            args.is_empty(),
            "an unknown shell must be spawned without -l in case the flag means something else there"
        );
    }

    #[test]
    fn sniff_bracketed_paste_mode_toggles_on_set_and_reset() {
        let flag = AtomicBool::new(false);
        sniff_bracketed_paste_mode(b"prompt> \x1b[?2004h", &flag);
        assert!(flag.load(Ordering::Acquire));
        sniff_bracketed_paste_mode(b"\x1b[?2004l\nbye", &flag);
        assert!(!flag.load(Ordering::Acquire));
    }

    #[test]
    fn sniff_bracketed_paste_mode_ignores_unrelated_dec_modes() {
        let flag = AtomicBool::new(false);
        sniff_bracketed_paste_mode(b"\x1b[?25h\x1b[?1049h", &flag);
        assert!(!flag.load(Ordering::Acquire));
    }

    #[test]
    fn pty_starts_dirty_so_first_frame_renders() {
        let tmp = tempfile::tempdir().unwrap();
        let term = PtyTerminal::new(tmp.path()).unwrap();
        assert!(
            term.take_dirty(),
            "first take_dirty must be true so we draw the initial state"
        );
    }

    #[test]
    fn take_dirty_clears_the_flag() {
        let tmp = tempfile::tempdir().unwrap();
        let term = PtyTerminal::new(tmp.path()).unwrap();
        let _ = term.take_dirty();
        assert!(
            !term.take_dirty(),
            "second take_dirty without new bytes must be false"
        );
    }

    #[test]
    fn write_input_marks_dirty() {
        let tmp = tempfile::tempdir().unwrap();
        let mut term = PtyTerminal::new(tmp.path()).unwrap();
        let _ = term.take_dirty();
        term.write_input(b"echo hi\r");
        assert!(
            term.take_dirty(),
            "write_input must mark the terminal dirty"
        );
    }

    /// Drop must REAP the shell on every path. portable-pty's `kill()` reaps
    /// only when the shell dies inside its SIGHUP grace loop; a shell that
    /// ignores HUP gets the SIGKILL escalation, which never waits — so every
    /// such closed pane left a zombie for the life of the process.
    #[test]
    fn dropping_a_terminal_reaps_a_hup_ignoring_shell() {
        let tmp = tempfile::tempdir().unwrap();
        let mut term = PtyTerminal::new(tmp.path()).unwrap();
        let pid = term.pid().expect("spawned shell has a pid") as i32;
        // The needle only ever appears as command OUTPUT ($(..) is unexpanded
        // in the input echo), so matching it proves the trap line executed.
        term.write_input(b"trap '' HUP; echo TRAP_$(echo armed)\n");
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        while !term.visible_text().contains("TRAP_armed") {
            assert!(
                std::time::Instant::now() < deadline,
                "staging: the shell never confirmed the HUP trap"
            );
            std::thread::sleep(std::time::Duration::from_millis(20));
        }
        drop(term);
        let mut status = 0i32;
        let r = unsafe { libc::waitpid(pid, &mut status, libc::WNOHANG) };
        assert!(
            r < 0,
            "the shell was left as an unreaped zombie (waitpid still found it: r={r})"
        );
    }

    /// Drop must return promptly even when a background job keeps the pty
    /// slave open: on Linux the reader's blocked `read` never returns then
    /// (no tty revoke like macOS), so joining it would freeze the UI until
    /// the job exits. The shutdown pipe is what wakes the reader.
    #[test]
    fn a_background_job_cannot_stall_a_dropped_pane() {
        let tmp = tempfile::tempdir().unwrap();
        let mut term = PtyTerminal::new(tmp.path()).unwrap();
        term.write_input(b"sleep 30 &\n");
        std::thread::sleep(std::time::Duration::from_millis(1500));
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            drop(term);
            let _ = tx.send(());
        });
        assert!(
            rx.recv_timeout(std::time::Duration::from_secs(10)).is_ok(),
            "drop hung: the reader never woke while a background job held the pty slave"
        );
    }

    #[test]
    fn format_cd_command_single_quotes_plain_path() {
        let bytes = format_cd_command(std::path::Path::new("/tmp/foo"));
        assert_eq!(
            bytes,
            b"\x05\x15cd '/tmp/foo'\n".to_vec(),
            "Ctrl-E + Ctrl-U must precede the cd so a half-typed prompt line is cleared first, then the path is single-quoted and newline-terminated to fire immediately"
        );
    }

    #[test]
    fn format_cd_command_escapes_embedded_apostrophe() {
        let bytes = format_cd_command(std::path::Path::new("/tmp/it's a dir"));
        assert_eq!(
            bytes,
            b"\x05\x15cd '/tmp/it'\\''s a dir'\n".to_vec(),
            "embedded ' must be escaped as '\\'' so the cd does not break out of single quotes when the path contains an apostrophe (POSIX-standard quoting)"
        );
    }

    #[test]
    fn format_cd_command_quotes_paths_with_spaces() {
        let bytes = format_cd_command(std::path::Path::new("/tmp/a b/c d"));
        assert_eq!(
            bytes,
            b"\x05\x15cd '/tmp/a b/c d'\n".to_vec(),
            "single-quoting must preserve spaces verbatim so cd lands at the right directory"
        );
    }

    #[test]
    fn foreground_is_shell_flips_false_while_a_foreground_command_runs() {
        let tmp = tempfile::tempdir().unwrap();
        let mut term = PtyTerminal::new(tmp.path()).unwrap();

        let mut waited = 0u32;
        while waited < 4000 && !term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert!(
            term.foreground_is_shell(),
            "a shell at its prompt is the tty's foreground process group, so a cd is safe to inject"
        );

        term.write_input(b"sleep 10\n");
        let mut waited = 0u32;
        while waited < 4000 && term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert!(
            !term.foreground_is_shell(),
            "while a foreground command owns the tty, the foreground group is the command not the shell, so a cd must not be injected"
        );
    }

    /// Spawn a pane whose "shell" is a script holding the tty foreground
    /// group with a job-control child — the rc-startup state the #94 flake
    /// caught `change_workspace_root` in. Returns the pane after the child
    /// has taken the foreground (bounded wait, asserted).
    fn startup_owned_pane(tmp: &tempfile::TempDir) -> PtyTerminal {
        let script = tmp.path().join("slow-startup.sh");
        std::fs::write(&script, "#!/bin/bash\nset -m\nsleep 30\n").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        let term = PtyTerminal::new_running(script.to_str().unwrap(), &[], tmp.path()).unwrap();
        let mut waited = 0u32;
        while waited < 4000 && term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert!(
            !term.foreground_is_shell(),
            "precondition: the startup child must own the foreground group"
        );
        term
    }

    #[test]
    fn cwd_seed_stays_safe_through_startup_until_a_prompt_mark_arrives() {
        let tmp = tempfile::tempdir().unwrap();
        let term = startup_owned_pane(&tmp);
        assert!(
            term.cwd_seed_is_safe(),
            "an untouched pane whose foreground group is shell startup must stay seedable: the seed queues as type-ahead for the first prompt"
        );
        // Once shell integration reports a prompt, startup is over — a
        // non-shell foreground group is a real app from here on.
        term.push_mark_for_test(crate::shell_integration::OscEvent::PromptStart, 0);
        assert!(
            !term.cwd_seed_is_safe(),
            "after the first prompt mark, a non-shell foreground group must suppress the seed"
        );
    }

    #[test]
    fn cwd_seed_turns_unsafe_once_the_pane_has_received_input() {
        let tmp = tempfile::tempdir().unwrap();
        let mut term = startup_owned_pane(&tmp);
        // Any input could have launched the app that owns the tty, so the
        // startup carve-out must die with the first written byte.
        term.write_input(b"q");
        assert!(
            !term.cwd_seed_is_safe(),
            "after input has been written, a non-shell foreground group must suppress the seed"
        );
    }

    /// A foreground process that owns the tty (an ssh client, exactly)
    /// controls everything the pane prints — including an OSC 7 report
    /// impersonating this machine's own hostname. The self-reported host
    /// is no proof of locality then: `local_shell_cwd` must refuse while
    /// the shell is not the foreground group, whatever the claim says.
    #[test]
    fn a_foreground_process_cannot_impersonate_a_local_cwd_report() {
        let out = std::process::Command::new("hostname").output().unwrap();
        let local = String::from_utf8_lossy(&out.stdout).trim().to_string();
        if local.is_empty() {
            return;
        }
        let tmp = tempfile::tempdir().unwrap();
        let mut term = PtyTerminal::new(tmp.path()).unwrap();
        let mut waited = 0u32;
        while waited < 4000 && !term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        // Stage the decoy: the shim's own prompt-time OSC 7 reports the
        // shell's real cwd (the tempdir), so `shell_cwd()` is `Some` long
        // before the forged claim exists — an `is_some()` stage exits on
        // this report and reads the wrong value (#69).
        let mut waited = 0u32;
        while waited < 4000 && term.shell_cwd().is_none() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        // A foreground job (the ssh stand-in) prints an OSC 7 claiming
        // this machine's own hostname. It sleeps BEFORE printing, so the
        // tty is lost to the job well before the claim can be parsed —
        // deterministically the ordering that flaked under suite load.
        term.write_input(
            format!("sleep 0.5; printf '\\033]7;file://{local}/tmp\\007'; sleep 10\n").as_bytes(),
        );
        let mut waited = 0u32;
        while waited < 4000 && term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert!(!term.foreground_is_shell(), "staging: a job owns the tty");
        // Wait for the claim ITSELF: any earlier `Some` is the decoy.
        let claim = std::path::PathBuf::from("/tmp");
        let mut waited = 0u32;
        while waited < 6000 && term.shell_cwd().as_deref() != Some(claim.as_path()) {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert_eq!(
            term.shell_cwd(),
            Some(claim),
            "staging: the claim was captured"
        );
        assert_eq!(
            term.local_shell_cwd(),
            None,
            "a tty-owning foreground process must not place local splits by claim"
        );
    }

    /// The buffered claim outliving its forger: a foreground job prints a
    /// local-looking OSC 7 and dies, but the reader thread only PARSES
    /// those bytes later (here: the render side held the term lock, the
    /// production delay). Any tty-ownership sample taken at parse time
    /// sees the shell back at its prompt — PTY bytes carry no author, so
    /// timing can never establish provenance. The claim must be rejected
    /// on content: it names a directory the shell is not actually in.
    #[test]
    fn a_claim_parsed_only_after_its_job_died_is_still_untrusted() {
        let Some(zsh) = zsh_or_skip() else { return };
        let tmp = tempfile::tempdir().unwrap();
        // zsh -f: job control (the job gets its own pgroup) but no
        // integration, so nothing overwrites the forged claim.
        let mut term =
            PtyTerminal::new_running(zsh, &[String::from("-f"), String::from("-i")], tmp.path())
                .unwrap();
        let mut waited = 0u32;
        while waited < 4000 && !term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        // The job sleeps before printing, leaving time to stall the
        // reader below; then it prints the claim and lingers briefly so
        // the bytes enter the kernel buffer while it owns the tty.
        let term_arc = term.term.clone();
        term.write_input(
            b"/bin/sh -c 'sleep 1; printf \"\\033]7;file://localhost/tmp\\007\"; sleep 1'\n",
        );
        // Wait for the job to take the tty and the reader to drain the
        // command echo, THEN stall parsing by taking the term lock. The
        // lock cannot be held from the start: with the reader parked,
        // zsh's startup burst fills the kernel pty queue and blocks the
        // shell before it ever runs the line. The claim plus one prompt
        // redraw are small enough that nothing blocks during the stall.
        let mut waited = 0u32;
        while waited < 4000 && term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert!(!term.foreground_is_shell(), "staging: the job took the tty");
        std::thread::sleep(std::time::Duration::from_millis(150));
        let guard = term_arc.lock();
        // With parsing stalled, wait out the job's remaining life: claim
        // printed, job dead, shell back at its prompt. tcgetpgrp is
        // kernel state, untouched by our lock.
        let mut waited = 0u32;
        while waited < 8000 && !term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert!(
            term.foreground_is_shell(),
            "staging: the job is gone and the shell owns the tty again"
        );
        std::thread::sleep(std::time::Duration::from_millis(300));
        drop(guard);
        let mut waited = 0u32;
        while waited < 4000 && term.shell_cwd() != Some(std::path::PathBuf::from("/tmp")) {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert_eq!(
            term.shell_cwd(),
            Some(std::path::PathBuf::from("/tmp")),
            "staging: the claim was captured after the job died"
        );
        assert_eq!(
            term.local_shell_cwd(),
            None,
            "a claim whose bytes were parsed after its job died must not be trusted"
        );
    }

    /// The forged claim outliving its forger: a foreground job prints an
    /// OSC 7 with a local-looking host and EXITS; the shell retakes the
    /// tty without re-reporting (no integration), so a foreground check at
    /// consultation time passes on the stale cache. A report that arrived
    /// from a job never becomes trusted, however long ago the job died.
    #[test]
    fn a_stale_claim_from_a_dead_foreground_job_stays_untrusted() {
        let Some(zsh) = zsh_or_skip() else { return };
        let tmp = tempfile::tempdir().unwrap();
        // zsh -f: job control (the claim's job gets its own pgroup, like a
        // real ssh client) but NO integration — the shell never overwrites
        // the stale claim with a fresh trusted report of its own.
        let mut term =
            PtyTerminal::new_running(zsh, &[String::from("-f"), String::from("-i")], tmp.path())
                .unwrap();
        let mut waited = 0u32;
        while waited < 4000 && !term.foreground_is_shell() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        // An EXTERNAL job (its own pgroup, like an ssh client — a bare
        // printf would be a zsh builtin printing from the shell's own
        // pgroup) settles as the tty owner, prints the local-looking
        // claim, lingers so the capture happens while it owns the tty,
        // and dies.
        term.write_input(
            b"/bin/sh -c 'sleep 1; printf \"\\033]7;file://localhost/tmp\\007\"; sleep 1'\n",
        );
        let mut waited = 0u32;
        while waited < 8000
            && !(term.foreground_is_shell()
                && term.shell_cwd() == Some(std::path::PathBuf::from("/tmp")))
        {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert_eq!(
            term.shell_cwd(),
            Some(std::path::PathBuf::from("/tmp")),
            "staging: the claim was captured"
        );
        assert!(
            term.foreground_is_shell(),
            "staging: the job is gone and the shell owns the tty again"
        );
        assert_eq!(
            term.local_shell_cwd(),
            None,
            "a claim captured while a job owned the tty must never become trusted"
        );
    }

    #[test]
    fn clear_screen_and_scrollback_wipes_the_grid() {
        fn dump(term: &PtyTerminal) -> String {
            let t = term.term.lock();
            let rows = t.screen_lines();
            let cols = t.columns();
            let off = t.grid().display_offset() as i32;
            extract_selection_text(&t, -off, 0, rows as i32 - 1 - off, cols.saturating_sub(1))
        }
        let tmp = tempfile::tempdir().unwrap();
        // The spawn banner prints the command's argv, so the probe must never
        // appear verbatim in argv: under load the banner lands before the
        // child's output, the wait below matches the BANNER, and the clear
        // fires before the probe exists - which the assert then finds alive.
        // The shell concatenation keeps argv and output distinct.
        let mut term = PtyTerminal::new_running(
            "/bin/sh",
            &[String::from("-c"), String::from("echo CLEAR-PROBE-\"\"XYZ")],
            tmp.path(),
        )
        .unwrap();
        let mut waited = 0u32;
        while waited < 4000 && !dump(&term).contains("CLEAR-PROBE-XYZ") {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert!(
            dump(&term).contains("CLEAR-PROBE-XYZ"),
            "probe present before clear"
        );
        term.clear_screen_and_scrollback();
        assert!(
            !dump(&term).contains("CLEAR-PROBE-XYZ"),
            "clear wipes the screen and scrollback"
        );
    }

    #[test]
    fn change_cwd_writes_cd_command_to_pty() {
        let tmp = tempfile::tempdir().unwrap();
        let mut term = PtyTerminal::new(tmp.path()).unwrap();
        let _ = term.take_dirty();
        let target = tempfile::tempdir().unwrap();
        term.change_cwd(target.path());
        assert!(
            term.take_dirty(),
            "change_cwd must mark the terminal dirty so the next frame paints the new prompt"
        );
    }

    #[test]
    fn new_running_spawns_program_directly_and_produces_output() {
        let tmp = tempfile::tempdir().unwrap();
        let term = PtyTerminal::new_running(
            "/bin/echo",
            &[String::from("croft-direct-spawn")],
            tmp.path(),
        )
        .unwrap();
        let mut waited_ms = 0u32;
        while waited_ms < 4000 && !term.peek_dirty() {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited_ms += 20;
        }
        assert!(
            term.peek_dirty(),
            "direct-spawned /bin/echo must produce output without any write_input"
        );
    }

    #[test]
    fn pristine_covers_only_an_untouched_interactive_shell() {
        let tmp = tempfile::tempdir().unwrap();
        let mut shell = PtyTerminal::new(tmp.path()).unwrap();
        assert!(
            shell.is_pristine(),
            "a freshly spawned shell with no input and no name is pristine"
        );
        shell.write_input(b"echo touched\n");
        assert!(
            !shell.is_pristine(),
            "any input byte permanently clears pristineness"
        );

        let mut named = PtyTerminal::new(tmp.path()).unwrap();
        named.set_manual_name(Some(String::from("srv")));
        assert!(
            !named.is_pristine(),
            "a manual name is user state; the pane must not be replaceable"
        );
        named.set_manual_name(None);
        assert!(
            !named.is_pristine(),
            "a rename latches: clearing the name back must not make the pane replaceable"
        );

        let run = PtyTerminal::new_running(
            "/bin/sh",
            &[String::from("-c"), String::from("sleep 1")],
            tmp.path(),
        )
        .unwrap();
        assert!(
            !run.is_pristine(),
            "a launched-program pane is doing user work despite having seen no input"
        );
    }

    /// Poll `grid_lines` until a predicate matches or 4s elapse.
    fn wait_for_grid<F: Fn(&[String]) -> bool>(term: &PtyTerminal, pred: F) -> Vec<String> {
        let mut waited_ms = 0u32;
        loop {
            let (lines, _top) = term.grid_lines();
            if pred(&lines) {
                return lines;
            }
            assert!(
                waited_ms < 4000,
                "expected output never reached the grid; grid was: {lines:?}"
            );
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited_ms += 20;
        }
    }

    #[test]
    fn ansi_named_colors_render_through_the_theme_palette() {
        // SGR 31 red must come out as the theme palette's red (VS Code's
        // #cd3131 by default), not the host terminal's Color::Red — croft
        // owns its terminal palette the way VS Code does. A palette swap
        // repaints with the new colors.
        let tmp = tempfile::tempdir().unwrap();
        // The sentinel is assembled at runtime (`${s}RED`) so the pane's
        // `▶ …` run-label header — which contains the raw command text —
        // can never match the scan below.
        let mut term = PtyTerminal::new_running(
            "/bin/sh",
            &[
                String::from("-c"),
                String::from("s=QQ; printf \"\\033[31m${s}RED\\033[0m\\n\"; sleep 30"),
            ],
            tmp.path(),
        )
        .unwrap();
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.contains("QQRED")));
        let area = Rect::new(0, 0, 60, 10);
        let find_q_fg = |term: &mut PtyTerminal| -> Option<Color> {
            let mut buf = Buffer::empty(area);
            Widget::render(term, area, &mut buf);
            for y in 0..area.height {
                for x in 1..area.width - 2 {
                    if buf[(x, y)].symbol() == "Q"
                        && buf[(x + 1, y)].symbol() == "Q"
                        && buf[(x + 2, y)].symbol() == "R"
                    {
                        return Some(buf[(x, y)].fg);
                    }
                }
            }
            None
        };
        assert_eq!(
            find_q_fg(&mut term),
            Some(Color::Rgb(0xcd, 0x31, 0x31)),
            "SGR red must map through the default palette"
        );
        let mut palette = crate::theme::VSCODE_ANSI;
        palette[1] = (0x12, 0x34, 0x56);
        term.set_palette(palette);
        assert_eq!(
            find_q_fg(&mut term),
            Some(Color::Rgb(0x12, 0x34, 0x56)),
            "a palette swap must repaint SGR red with the new color"
        );
    }

    #[test]
    fn faint_cells_render_with_the_dim_modifier() {
        // Claude Code paints its inline ghost suggestion as default-foreground
        // text under SGR 2 (faint). The pane must forward the flag as
        // Modifier::DIM, or the suggestion renders at full brightness and is
        // indistinguishable from text the user typed.
        let tmp = tempfile::tempdir().unwrap();
        let mut term = PtyTerminal::new_running(
            "/bin/sh",
            &[
                String::from("-c"),
                String::from("s=QQ; printf \"\\033[2m${s}GHOST\\033[0m\\n\"; sleep 30"),
            ],
            tmp.path(),
        )
        .unwrap();
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.contains("QQGHOST")));
        let area = Rect::new(0, 0, 60, 10);
        let mut buf = Buffer::empty(area);
        Widget::render(&mut term, area, &mut buf);
        for y in 0..area.height {
            for x in 1..area.width - 2 {
                if buf[(x, y)].symbol() == "Q"
                    && buf[(x + 1, y)].symbol() == "Q"
                    && buf[(x + 2, y)].symbol() == "G"
                {
                    assert!(
                        buf[(x, y)].modifier.contains(Modifier::DIM),
                        "SGR 2 (faint) cells must carry Modifier::DIM"
                    );
                    return;
                }
            }
        }
        panic!("the faint sentinel never appeared in the rendered buffer");
    }

    #[test]
    fn imgcat_style_inline_images_are_captured_with_a_grid_anchor() {
        use base64::Engine;
        // A 1x1 red PNG; what `imgcat tiny.png` would emit.
        let mut png_buf = Vec::new();
        image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255]))
            .write_to(
                &mut std::io::Cursor::new(&mut png_buf),
                image::ImageFormat::Png,
            )
            .expect("encode test png");
        let png: &[u8] = &png_buf;
        let b64 = base64::engine::general_purpose::STANDARD.encode(png);
        let tmp = tempfile::tempdir().unwrap();
        let script = format!(
            "printf 'before\\n'; printf '\\033]1337;File=inline=1:{b64}\\007\\n'; printf 'after\\n'; sleep 30"
        );
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script], tmp.path()).unwrap();
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.contains("after")));
        let mut waited = 0u32;
        let imgs = loop {
            let imgs = term.pane_images();
            if !imgs.is_empty() {
                break imgs;
            }
            assert!(waited < 8000, "inline image never captured");
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        };
        assert_eq!(imgs.len(), 1);
        assert_eq!(imgs[0].data.as_slice(), png, "payload must round-trip");
        // Anchor: the row right after the `before` line (the run-label
        // header above it wraps, so absolute row numbers are not stable).
        let (lines, top) = term.grid_lines();
        let before_row = lines
            .iter()
            .rposition(|l| l.trim_end() == "before")
            .expect("the before line must be on the grid") as i32
            + top;
        assert_eq!(
            imgs[0].line,
            before_row + 1,
            "anchor must sit on the image's grid row; grid: {lines:?}"
        );
    }

    #[test]
    fn wide_chars_read_back_without_phantom_spacer_spaces() {
        // A CJK char occupies two grid columns: the WIDE_CHAR cell plus a
        // WIDE_CHAR_SPACER cell whose `c` is ' '. Text extraction must skip
        // the spacer, or copied/searched text comes out as "日 本 語" and a
        // find for "日本語" can never match.
        let tmp = tempfile::tempdir().unwrap();
        let term = PtyTerminal::new_running("/bin/echo", &[String::from("日本語 ok")], tmp.path())
            .unwrap();
        let lines = wait_for_grid(&term, |ls| ls.iter().any(|l| l.contains('日')));
        assert!(
            lines.iter().any(|l| l.contains("日本語 ok")),
            "wide chars must read back contiguously (no spacer-cell spaces); grid was: {lines:?}"
        );
    }

    #[test]
    fn wide_chars_render_the_glyph_once_with_a_blank_spacer_cell() {
        // The render loop writes the double-width glyph into its WIDE_CHAR
        // cell and must leave the following spacer cell blank — ratatui skips
        // the cell after a width-2 symbol when diffing, so anything else
        // there would corrupt column alignment.
        let tmp = tempfile::tempdir().unwrap();
        let mut term =
            PtyTerminal::new_running("/bin/echo", &[String::from("日X")], tmp.path()).unwrap();
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.contains('日')));
        let area = Rect::new(0, 0, 40, 10);
        let mut buf = Buffer::empty(area);
        Widget::render(&mut term, area, &mut buf);
        let mut found = false;
        for y in 0..area.height {
            for x in 0..area.width.saturating_sub(2) {
                if buf[(x, y)].symbol() == "日" {
                    assert_eq!(
                        buf[(x + 1, y)].symbol(),
                        " ",
                        "the spacer cell after a wide glyph must stay blank"
                    );
                    assert_eq!(
                        buf[(x + 2, y)].symbol(),
                        "X",
                        "the next glyph must land two columns after the wide char"
                    );
                    found = true;
                }
            }
        }
        assert!(found, "the wide glyph must appear in the rendered buffer");
    }

    #[test]
    fn hyperlink_at_returns_the_osc8_uri_under_the_cell() {
        // OSC 8 hyperlinks live in the cell, invisible in the text. The app's
        // Cmd/Ctrl+click handler asks `hyperlink_at` before falling back to
        // the plain-text URL regex.
        let tmp = tempfile::tempdir().unwrap();
        let script =
            "printf '\\033]8;;https://example.com/doc\\033\\\\CLICK-ME\\033]8;;\\033\\\\\\n'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        // The run header echoes the command text, so "CLICK-ME" appears there
        // too — as plain text without a link. Wait for the child's actual
        // output row (the one without "printf").
        let is_output = |l: &String| l.contains("CLICK-ME") && !l.contains("printf");
        let lines = wait_for_grid(&term, |ls| ls.iter().any(is_output));
        let row = lines.iter().position(is_output).unwrap();
        let col = lines[row].find("CLICK-ME").unwrap();
        assert_eq!(
            term.hyperlink_at(row, col).as_deref(),
            Some("https://example.com/doc"),
            "the cell under the linked text must expose the OSC 8 URI"
        );
        assert_eq!(
            term.hyperlink_at(0, 0),
            None,
            "cells outside the link (the header row) carry no URI"
        );
    }

    #[test]
    fn osc133_marks_record_kind_exit_code_and_grid_line() {
        // Emit a prompt mark before "PROMPT", a command-start mark, and a
        // finished mark carrying exit 3. Marks must land on the grid lines
        // where the cursor sat when they arrived.
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf 'before\\n\\033]133;A\\007PROMPT-LINE\\n\\033]133;C\\007out\\n\\033]133;D;3\\007'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let is_output = |l: &String| l.contains("PROMPT-LINE") && !l.contains("printf");
        let lines = wait_for_grid(&term, |ls| {
            ls.iter().any(is_output) && !term.command_marks().is_empty()
        });
        let (_, top) = term.grid_lines();
        let prompt_row = lines.iter().position(is_output).unwrap();
        let marks = term.command_marks();
        let prompt_line = marks
            .iter()
            .find(|(kind, _)| *kind == crate::shell_integration::OscEvent::PromptStart)
            .map(|(_, line)| *line)
            .expect("a PromptStart mark must be recorded");
        assert_eq!(
            prompt_line,
            top + prompt_row as i32,
            "the prompt mark must sit on the line where PROMPT-LINE was printed"
        );
        assert!(
            marks
                .iter()
                .any(|(kind, _)| *kind == crate::shell_integration::OscEvent::CommandEnd(Some(3))),
            "the finished mark must carry exit code 3; marks: {marks:?}"
        );
    }

    #[test]
    fn marks_follow_their_content_into_scrollback() {
        // A mark recorded on the live screen must keep pointing at the same
        // content after later output scrolls that content into history.
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]133;A\\007MARKED-PROMPT\\n'; i=0; while [ $i -lt 40 ]; do echo filler-$i; i=$((i+1)); done";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.contains("filler-39")));
        // The script's tail (trailing newline, shell exit) can scroll another
        // row or two AFTER filler-39 becomes visible. `lines` and `top` must
        // come from ONE settled snapshot, or the row index and the grid top
        // describe different moments and the compare is off by exactly the
        // late scroll (#110). Settle = two consecutive identical reads.
        let (lines, top) = {
            let mut prev = term.grid_lines();
            let mut waited = 0u32;
            loop {
                std::thread::sleep(std::time::Duration::from_millis(40));
                let cur = term.grid_lines();
                if cur == prev || waited >= 4000 {
                    break cur;
                }
                prev = cur;
                waited += 40;
            }
        };
        let content_row = lines
            .iter()
            .position(|l| l.contains("MARKED-PROMPT") && !l.contains("printf"))
            .expect("the marked line must still be in scrollback");
        let marks = term.command_marks();
        let prompt_line = marks
            .iter()
            .find(|(kind, _)| *kind == crate::shell_integration::OscEvent::PromptStart)
            .map(|(_, line)| *line)
            .expect("the mark must survive the scroll");
        assert_eq!(
            prompt_line,
            top + content_row as i32,
            "the mark must move with its content into history (negative grid lines)"
        );
        assert!(
            prompt_line < 0,
            "after 40 filler lines the mark is in scrollback"
        );
    }

    #[test]
    fn osc7_updates_the_shell_cwd() {
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]7;file://anyhost/tmp/croft%%20dir\\007ok\\n'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        wait_for_grid(&term, |ls| {
            ls.iter().any(|l| l.contains("ok") && !l.contains("printf"))
        });
        assert_eq!(
            term.shell_cwd(),
            Some(std::path::PathBuf::from("/tmp/croft dir")),
            "OSC 7 must update the pane's live cwd"
        );
    }

    /// A remote shell's OSC 7 (an in-pane ssh with integration) names a
    /// path on the REMOTE machine: consumers that act on the local
    /// filesystem (split-in-cwd, session restore) must not trust it just
    /// because the same path happens to exist locally.
    #[test]
    fn a_remote_reported_cwd_is_not_trusted_for_local_use() {
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]7;file://prod-db-1/tmp\\007ok\\n'; sleep 30";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        wait_for_grid(&term, |ls| {
            ls.iter().any(|l| l.contains("ok") && !l.contains("printf"))
        });
        assert_eq!(
            term.shell_cwd(),
            Some(std::path::PathBuf::from("/tmp")),
            "staging: the raw report is visible"
        );
        assert_eq!(
            term.local_shell_cwd(),
            None,
            "a path reported by another machine must not be reused locally"
        );
        // The truthful counterpart: the reporter really sits in the
        // directory it claims (spawned in /tmp), so the claim matches the
        // kernel's cwd for the pane's shell and localhost IS this machine.
        let script = "printf '\\033]7;file://localhost/tmp\\007ok\\n'; sleep 30";
        let term2 = PtyTerminal::new_running(
            "/bin/sh",
            &[String::from("-c"), script.into()],
            std::path::Path::new("/tmp"),
        )
        .unwrap();
        wait_for_grid(&term2, |ls| {
            ls.iter().any(|l| l.contains("ok") && !l.contains("printf"))
        });
        assert_eq!(
            term2.local_shell_cwd(),
            Some(std::path::PathBuf::from("/tmp")),
            "a localhost report IS this machine (RFC 8089)"
        );
    }

    #[test]
    fn osc9_notifications_drain_once() {
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]9;build done\\007ok\\n'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        wait_for_grid(&term, |ls| {
            ls.iter().any(|l| l.contains("ok") && !l.contains("printf"))
        });
        assert_eq!(term.drain_notifications(), vec![String::from("build done")]);
        assert!(
            term.drain_notifications().is_empty(),
            "a second drain returns nothing"
        );
    }

    #[test]
    fn notify_triggers_fire_on_completed_output_lines() {
        let tmp = tempfile::tempdir().unwrap();
        // The shell waits for a line of input, so the trigger set is in
        // place before the matching output is ever produced.
        let script = "read x; echo deploy failed; sleep 30";
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let set = crate::triggers::TriggerSet::from_json(
            r#"[ { "regex": "deploy (\\w+)", "action": "notify", "message": "deploy went \\1" } ]"#,
        );
        assert!(set.has_events());
        term.set_triggers(std::sync::Arc::new(set));
        term.write_input(b"\n");
        let mut waited = 0u32;
        let hits = loop {
            let hits = term.drain_trigger_hits();
            if !hits.is_empty() {
                break hits;
            }
            assert!(waited < 8000, "trigger never fired");
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        };
        assert_eq!(hits.len(), 1, "{hits:?}");
        assert_eq!(hits[0].action, crate::triggers::TriggerAction::Notify);
        assert_eq!(hits[0].message, "deploy went failed");
    }

    /// A banner printed just before handing off to a full-screen app lands
    /// in the same PTY chunk as the alt-screen entry: gating the trigger
    /// scan on the chunk's FINAL mode dropped that whole chunk (and let a
    /// chunk that merely ENDED on the primary screen fire on alt content).
    /// The scanner tracks the boundary positionally, so the reader must
    /// feed it every chunk unconditionally.
    #[test]
    fn a_trigger_fires_for_primary_text_that_shares_a_chunk_with_alt_entry() {
        let tmp = tempfile::tempdir().unwrap();
        let script = "read x; printf 'deploy failed\\n\\033[?1049h'; sleep 1; printf '\\033[?1049lok\\n'; sleep 30";
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let set = crate::triggers::TriggerSet::from_json(
            r#"[ { "regex": "deploy (\\w+)", "action": "notify", "message": "deploy went \\1" } ]"#,
        );
        assert!(set.has_events());
        term.set_triggers(std::sync::Arc::new(set));
        term.write_input(b"\n");
        let mut waited = 0u32;
        let hits = loop {
            let hits = term.drain_trigger_hits();
            if !hits.is_empty() {
                break hits;
            }
            assert!(
                waited < 8000,
                "the primary-screen line before the alt entry never fired"
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        };
        assert_eq!(hits.len(), 1, "{hits:?}");
        assert_eq!(hits[0].message, "deploy went failed");
    }

    #[test]
    fn poisoned_zdotdir_from_a_nested_croft_still_loads_the_user_rc() {
        // Regression: every pane exports ZDOTDIR=<shim>, so a croft launched
        // FROM a croft pane inherited it and treated the shim as the user's
        // dotfile dir — the shim then sourced itself in a recursion loop and
        // the user's real .zshrc (their theme, aliases) never ran. A
        // poisoned CROFT_USER_ZDOTDIR pointing at the shim must be ignored
        // in favour of $HOME.
        let Some(zsh) = zsh_or_skip() else { return };
        let home = tempfile::tempdir().unwrap();
        std::fs::write(
            home.path().join(".zshrc"),
            "USER_RC_SENTINEL=loaded\nexport USER_RC_SENTINEL\n",
        )
        .unwrap();
        let cfg_dir = tempfile::tempdir().unwrap();
        let shim = crate::shell_integration::ensure_zsh_shim(cfg_dir.path()).unwrap();
        let mut cmd = CommandBuilder::new(zsh);
        cmd.arg("-i");
        cmd.cwd(home.path());
        cmd.env("HOME", home.path());
        // The poison: both vars point at the shim itself, exactly what a
        // pane's environment hands a nested croft.
        cmd.env("ZDOTDIR", &shim);
        cmd.env("CROFT_USER_ZDOTDIR", &shim);
        let mut term = PtyTerminal::spawn_with(cmd, None).unwrap();
        let mut waited = 0u32;
        while term.prompt_lines().is_empty() {
            assert!(
                waited < 8000,
                "no prompt mark; grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        term.write_input(b"echo SENTINEL_IS=$USER_RC_SENTINEL\r");
        let mut waited = 0u32;
        loop {
            let (lines, _) = term.grid_lines();
            assert!(
                !lines.iter().any(|l| l.contains("recursion limit")),
                "the shim must never source itself; grid: {lines:?}"
            );
            if lines
                .iter()
                .any(|l| l.contains("SENTINEL_IS=loaded") && !l.contains("echo"))
            {
                break;
            }
            assert!(
                waited < 8000,
                "user rc never ran under a poisoned ZDOTDIR; grid: {lines:?}"
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
    }

    #[test]
    fn pair_decorations_matches_prompts_with_exits() {
        use crate::shell_integration::OscEvent as E;
        let ms = std::time::Duration::from_millis;
        // Two full cycles (ok then failing), then a bare prompt with no
        // command: two records, the bare prompt contributes nothing. The
        // PromptEnd cell names where the typed command starts; the output
        // span runs from the CommandStart line up to the CommandEnd line.
        let marks = [
            (E::PromptStart, -10, 0, None),
            (E::PromptEnd, -10, 5, None),
            (E::CommandStart, -9, 0, None),
            (E::CommandEnd(Some(0)), -7, 0, Some(ms(2400))),
            (E::PromptStart, -7, 0, None),
            (E::PromptEnd, -7, 5, None),
            (E::CommandStart, -6, 0, None),
            (E::CommandEnd(Some(3)), -2, 0, Some(ms(150))),
            (E::PromptStart, -2, 0, None),
        ];
        assert_eq!(
            pair_decorations(&marks),
            vec![
                CommandDecoration {
                    line: -10,
                    exit: Some(0),
                    duration: Some(ms(2400)),
                    input: Some((-10, 5)),
                    output_start: -9,
                    output_end: -7,
                },
                CommandDecoration {
                    line: -7,
                    exit: Some(3),
                    duration: Some(ms(150)),
                    input: Some((-7, 5)),
                    output_start: -6,
                    output_end: -2,
                },
            ]
        );
        // A duplicate CommandEnd (a second integration layer echoing the
        // marks) has no pending CommandStart and must be dropped.
        let dup = [
            (E::PromptStart, -5, 0, None),
            (E::CommandStart, -5, 0, None),
            (E::CommandEnd(Some(0)), -4, 0, Some(ms(90))),
            (E::CommandEnd(Some(0)), -4, 0, None),
        ];
        assert_eq!(pair_decorations(&dup).len(), 1);
        // A command still running (no CommandEnd yet) has no record.
        let running = [(E::PromptStart, 0, 0, None), (E::CommandStart, 0, 0, None)];
        assert!(pair_decorations(&running).is_empty());
    }

    #[test]
    fn command_output_and_input_extract_and_select() {
        // Copy Output / Re-run Command need the exact spans back out of the
        // grid: the typed command (from the PromptEnd cell) and the output
        // rows between CommandStart and CommandEnd. Selecting the output
        // highlights precisely those rows.
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]133;A\\007$ \\033]133;B\\007cmd --flag\\n\\033]133;C\\007out1\\nout2\\n\\033]133;D;0\\007'";
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let mut waited = 0u32;
        while term.command_decorations().is_empty() {
            assert!(
                waited < 8000,
                "no decoration; marks: {:?}",
                term.command_marks()
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        let deco = term.command_decorations()[0];
        assert_eq!(term.command_input_text(&deco), "cmd --flag");
        assert_eq!(term.command_output_text(&deco), "out1\nout2");
        term.select_command_output(&deco);
        let sel = term.selection().expect("output must be selected");
        let (sr, _, er, _) = sel.normalised();
        assert_eq!(
            (sr, er),
            (deco.output_start, deco.output_end - 1),
            "selection covers exactly the output rows"
        );
        assert_eq!(term.selection_text(), "out1\nout2");
    }

    #[test]
    fn human_duration_formats_ms_seconds_minutes() {
        let d = std::time::Duration::from_millis;
        assert_eq!(human_duration(d(480)), "480ms");
        assert_eq!(human_duration(d(3400)), "3.4s");
        assert_eq!(human_duration(d(59_940)), "59.9s");
        assert_eq!(human_duration(d(125_000)), "2m 05s");
    }

    #[test]
    fn osc133_decorations_carry_exit_and_duration_end_to_end() {
        // A real PTY run emitting the mark protocol: the finished command
        // must surface as one decoration with its exit code and a duration
        // covering the sleep between CommandStart and CommandEnd, and the
        // same completion must arrive once through the notification drain.
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]133;A\\007$ cmd\\n\\033]133;B\\007\\033]133;C\\007'; sleep 1; printf 'out\\n\\033]133;D;2\\007'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let mut waited = 0u32;
        loop {
            let decos = term.command_decorations();
            if let Some(d) = decos.first() {
                assert_eq!(d.exit, Some(2), "exit code from 133;D;2");
                let dur = d.duration.expect("duration must be measured");
                // Marks are arrival-stamped by the reader thread, so a late
                // pickup of the pre-sleep chunk shrinks the measured gap
                // under suite load (#65): the floor sits at half the sleep.
                assert!(
                    dur >= std::time::Duration::from_millis(500),
                    "duration must cover the sleep; got {dur:?}"
                );
                break;
            }
            assert!(
                waited < 8000,
                "no decoration appeared; marks: {:?}",
                term.command_marks()
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        let finished = term.drain_finished_commands();
        assert_eq!(finished.len(), 1, "one completion in the drain");
        assert_eq!(finished[0].exit, Some(2));
        assert!(finished[0].dur >= std::time::Duration::from_millis(500));
        assert!(
            term.drain_finished_commands().is_empty(),
            "a second drain returns nothing"
        );
    }

    #[test]
    fn decoration_dot_paints_on_the_left_border_and_hit_tests() {
        // The finished command's prompt row gets a VS Code-style dot on the
        // pane's left border: red here (exit 2). Clicking the dot resolves
        // back to the record via decoration_at_screen.
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]133;A\\007$ cmd\\n\\033]133;B\\007\\033]133;C\\007out\\n\\033]133;D;2\\007'";
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let mut waited = 0u32;
        while term.command_decorations().is_empty() {
            assert!(
                waited < 8000,
                "no decoration; marks: {:?}",
                term.command_marks()
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        let area = Rect::new(0, 0, 40, 12);
        let mut buf = Buffer::empty(area);
        (&mut term).render(area, &mut buf);
        let deco = term.command_decorations()[0];
        // Live view: display_offset 0, viewport row = grid line; +1 for the
        // top border.
        let y = 1 + u16::try_from(deco.line).unwrap();
        let cell = &buf[(0, y)];
        assert_eq!(
            cell.symbol(),
            "●",
            "dot on the left border at the prompt row"
        );
        assert_eq!(
            cell.style().fg,
            Some(Color::Rgb(0xf1, 0x4c, 0x4c)),
            "non-zero exit paints the error red"
        );
        // The border above the dot is untouched.
        assert_eq!(buf[(0, y - 1)].symbol(), "│");
        assert_eq!(
            term.decoration_at_screen(0, y),
            Some(deco),
            "clicking the dot resolves the record"
        );
        assert_eq!(
            term.decoration_at_screen(0, y - 1),
            None,
            "no record on a bare border row"
        );
    }

    #[test]
    fn foreign_terminal_shim_zdotdir_chains_to_the_user_rc() {
        // Regression: Ghostty and kitty inject their own shell integration by
        // pointing ZDOTDIR at their shim dir before launching their child.
        // When that child is croft itself (ghostty --initial-command=croft),
        // croft inherited the foreign shim as the "user" dotfile dir — a dir
        // with no .zshrc — so the user's real rc (theme, aliases) never ran.
        // The wrapper contract (kitty invented it, Ghostty copied it) is that
        // the foreign .zshenv restores the REAL ZDOTDIR when sourced; croft's
        // shim must re-read ZDOTDIR after chaining and source the user's rc
        // from wherever it landed.
        let Some(zsh) = zsh_or_skip() else { return };
        let home = tempfile::tempdir().unwrap();
        std::fs::write(
            home.path().join(".zshrc"),
            "USER_RC_SENTINEL=loaded\nexport USER_RC_SENTINEL\n",
        )
        .unwrap();
        // A Ghostty-style wrapper shim: only a .zshenv, which restores the
        // real ZDOTDIR (here: unset, meaning HOME) and chains onward.
        let foreign = tempfile::tempdir().unwrap();
        std::fs::write(
            foreign.path().join(".zshenv"),
            "builtin unset ZDOTDIR\n[[ -r \"$HOME/.zshenv\" ]] && builtin source \"$HOME/.zshenv\"\n",
        )
        .unwrap();
        let cfg_dir = tempfile::tempdir().unwrap();
        let shim = crate::shell_integration::ensure_zsh_shim(cfg_dir.path()).unwrap();
        let mut cmd = CommandBuilder::new(zsh);
        cmd.arg("-i");
        cmd.cwd(home.path());
        cmd.env("HOME", home.path());
        cmd.env("ZDOTDIR", &shim);
        // What resolve_user_zdotdir hands the pane: the inherited foreign dir.
        cmd.env("CROFT_USER_ZDOTDIR", foreign.path());
        let mut term = PtyTerminal::spawn_with(cmd, None).unwrap();
        let mut waited = 0u32;
        while term.prompt_lines().is_empty() {
            assert!(
                waited < 8000,
                "no prompt mark; grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        term.write_input(b"echo SENTINEL_IS=$USER_RC_SENTINEL\r");
        let mut waited = 0u32;
        loop {
            let (lines, _) = term.grid_lines();
            if lines
                .iter()
                .any(|l| l.contains("SENTINEL_IS=loaded") && !l.contains("echo"))
            {
                break;
            }
            assert!(
                waited < 8000,
                "user rc never ran behind a foreign terminal shim; grid: {lines:?}"
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
    }

    #[test]
    fn zsh_shim_emits_prompt_marks_end_to_end() {
        // The real proof: an interactive zsh reading croft's ZDOTDIR shim
        // must emit OSC 133 prompt marks and an OSC 7 cwd report at its
        // first prompt, with the user's own (here: empty) dotfiles sourced.
        let Some(zsh) = zsh_or_skip() else { return };
        let user_dir = tempfile::tempdir().unwrap();
        let cfg_dir = tempfile::tempdir().unwrap();
        let shim = crate::shell_integration::ensure_zsh_shim(cfg_dir.path()).unwrap();
        let mut cmd = CommandBuilder::new(zsh);
        cmd.arg("-i");
        cmd.cwd(user_dir.path());
        cmd.env("ZDOTDIR", &shim);
        cmd.env("CROFT_USER_ZDOTDIR", user_dir.path());
        let term = PtyTerminal::spawn_with(cmd, None).unwrap();
        let mut waited_ms = 0u32;
        while term.prompt_lines().is_empty() {
            assert!(
                waited_ms < 8000,
                "zsh never emitted a prompt mark; grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited_ms += 40;
        }
        assert!(
            term.shell_cwd().is_some(),
            "the shim's precmd must also report the cwd via OSC 7"
        );
    }

    /// The zsh shim must emit `$PWD` percent-escaped (fish already does):
    /// the sniffer unconditionally percent-DECODES OSC 7 paths, so a real
    /// directory containing `%41` used to be reported as `A`, corrupting
    /// the cwd every consumer sees.
    #[test]
    fn a_percent_directory_round_trips_through_the_zsh_shim() {
        let Some(zsh) = zsh_or_skip() else { return };
        let user_dir = tempfile::tempdir().unwrap();
        let odd = user_dir.path().join("a%41b");
        std::fs::create_dir(&odd).unwrap();
        let cfg_dir = tempfile::tempdir().unwrap();
        let shim = crate::shell_integration::ensure_zsh_shim(cfg_dir.path()).unwrap();
        let mut cmd = CommandBuilder::new(zsh);
        cmd.arg("-i");
        cmd.cwd(&odd);
        cmd.env("ZDOTDIR", &shim);
        cmd.env("CROFT_USER_ZDOTDIR", user_dir.path());
        let term = PtyTerminal::spawn_with(cmd, None).unwrap();
        // Two state-gated stages instead of one wall-clock guess: under full-
        // suite load, interactive zsh startup alone can eat a fixed budget and
        // the timeout measures the machine, not the shim (#109). First wait
        // for the prompt mark (proof the precmd hook ran), THEN the OSC 7
        // report — emitted by that same precmd — must follow almost at once.
        let mut waited = 0u32;
        while term.prompt_lines().is_empty() {
            assert!(
                waited < 20_000,
                "zsh never emitted a prompt mark; grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        let mut waited = 0u32;
        let cwd = loop {
            if let Some(c) = term.shell_cwd() {
                break c;
            }
            assert!(
                waited < 4000,
                "the shim's precmd ran (prompt mark present) but never reported a cwd; grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        };
        assert!(
            cwd.ends_with("a%41b"),
            "the literal directory name must survive the OSC 7 round trip, got {cwd:?}"
        );
    }

    /// A bash new enough for `$ENV` + `--posix` injection (>= 4.4), for the
    /// e2e tests: Homebrew/Linux bash qualifies, macOS's system 3.2 not.
    fn modern_bash() -> Option<&'static str> {
        ["/opt/homebrew/bin/bash", "/usr/local/bin/bash", "/bin/bash"]
            .into_iter()
            .find(|b| bash_env_injection_supported(b))
    }

    /// An empty Enter in a bash whose user set PROMPT_COMMAND as an ARRAY
    /// (bash 5.1+, ble.sh and prompt frameworks do) must not fabricate a
    /// command: the DEBUG-trap guard joined the array with a SPACE
    /// (`${arr[*]}` under default IFS), so its own precmd entries never
    /// matched and every blank prompt emitted a spurious 133;C + 133;D —
    /// a phantom decoration that hijacked Cmd+K Shift+R/C/S.
    #[test]
    fn an_empty_enter_with_an_array_prompt_command_fabricates_no_command() {
        let Some(bash) = modern_bash() else {
            return;
        };
        // Array PROMPT_COMMAND execution needs bash >= 5.1.
        let out = std::process::Command::new(bash)
            .args(["-c", "echo ${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}"])
            .output()
            .unwrap();
        let v: u32 = String::from_utf8_lossy(&out.stdout)
            .trim()
            .parse()
            .unwrap_or(0);
        if v < 51 {
            return;
        }
        let home = tempfile::tempdir().unwrap();
        std::fs::write(
            home.path().join(".bashrc"),
            "mytheme() { :; }\nPROMPT_COMMAND=(mytheme)\n",
        )
        .unwrap();
        let cfg = tempfile::tempdir().unwrap();
        let mut cmd = CommandBuilder::new(bash);
        cmd.cwd(home.path());
        cmd.env("HOME", home.path());
        let args = apply_shell_integration_env(&mut cmd, bash, cfg.path());
        for a in &args {
            cmd.arg(a);
        }
        cmd.arg("-i");
        let mut term = PtyTerminal::spawn_with(cmd, None).unwrap();
        let mut waited = 0u32;
        while term.prompt_lines().is_empty() {
            assert!(waited < 8000, "bash never emitted a prompt mark");
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        let prompts_before = term.prompt_lines().len();
        term.write_input(b"\n");
        let mut waited = 0u32;
        while term.prompt_lines().len() <= prompts_before {
            assert!(waited < 8000, "the empty Enter never produced a new prompt");
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        assert!(
            pair_decorations(&term.marks_snapshot()).is_empty(),
            "an empty Enter must not fabricate a finished command"
        );
    }

    #[test]
    fn shell_integration_env_prepends_posix_for_modern_bash_only() {
        // bash is injected by inserting `--posix` before `-l` and pointing
        // $ENV at croft's shim — but only from 4.4 (the kitty/Ghostty
        // floor): macOS's system bash 3.2 ignores $ENV in posix mode, so
        // injecting would strip its startup files for nothing. zsh
        // (ZDOTDIR) and fish (XDG_DATA_DIRS) are env-only and prepend
        // nothing.
        let cfg = tempfile::tempdir().unwrap();
        if let Some(bash) = modern_bash() {
            let mut cmd = CommandBuilder::new(bash);
            assert_eq!(
                apply_shell_integration_env(&mut cmd, bash, cfg.path()),
                vec!["--posix".to_string()]
            );
        }
        assert!(!parse_bash_version_supported("3.2"));
        assert!(!parse_bash_version_supported("4.3"));
        assert!(!parse_bash_version_supported("garbage"));
        assert!(parse_bash_version_supported("4.4"));
        assert!(parse_bash_version_supported("5.2"));
        assert!(parse_bash_version_supported("10.0"));
        if std::path::Path::new("/bin/bash").exists() && !bash_env_injection_supported("/bin/bash")
        {
            // On a macOS box with system bash 3.2: no injection, no args.
            let mut cmd = CommandBuilder::new("/bin/bash");
            assert!(
                apply_shell_integration_env(&mut cmd, "/bin/bash", cfg.path()).is_empty(),
                "bash 3.2 must spawn clean, without posix injection"
            );
        }
        for shell in ["/bin/zsh", "/usr/bin/fish", "/bin/dash"] {
            let mut cmd = CommandBuilder::new(shell);
            assert!(
                apply_shell_integration_env(&mut cmd, shell, cfg.path()).is_empty(),
                "{shell} must not gain args"
            );
        }
    }

    #[test]
    fn zsh_shim_marks_the_prompt_end_for_input_extraction() {
        // Copy Output / Re-run Command read the typed command from the
        // PromptEnd (133;B) mark. The shim must emit it itself — relying on
        // a chained foreign integration (Ghostty's) leaves remote Linux
        // panes with no input span at all.
        let Some(zsh) = zsh_or_skip() else { return };
        let user_dir = tempfile::tempdir().unwrap();
        let cfg_dir = tempfile::tempdir().unwrap();
        let shim = crate::shell_integration::ensure_zsh_shim(cfg_dir.path()).unwrap();
        let mut cmd = CommandBuilder::new(zsh);
        cmd.arg("-i");
        cmd.cwd(user_dir.path());
        cmd.env("ZDOTDIR", &shim);
        cmd.env("CROFT_USER_ZDOTDIR", user_dir.path());
        let term = PtyTerminal::spawn_with(cmd, None).unwrap();
        let mut waited = 0u32;
        loop {
            let marks = term.command_marks();
            if marks
                .iter()
                .any(|(k, _)| *k == crate::shell_integration::OscEvent::PromptEnd)
            {
                break;
            }
            assert!(
                waited < 8000,
                "zsh never emitted a PromptEnd mark; marks: {marks:?}, grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
    }

    #[test]
    fn bash_shim_via_posix_env_emits_marks_and_sources_the_user_profile() {
        // The kitty/Ghostty bash injection end-to-end, exactly as croft
        // spawns it: `bash --posix -l` with ENV pointing at croft's shim.
        // In posix mode bash reads ONLY $ENV, so the shim must back out of
        // posix mode, replay the login startup files (the user's
        // .bash_profile here), and emit the full mark cycle with exit codes.
        // Needs a bash >= 4.4 (macOS system 3.2 ignores $ENV in posix mode
        // and is never injected).
        let Some(bash) = modern_bash() else {
            return;
        };
        let home = tempfile::tempdir().unwrap();
        std::fs::write(
            home.path().join(".bash_profile"),
            "USER_RC_SENTINEL=loaded\nexport USER_RC_SENTINEL\n",
        )
        .unwrap();
        let cfg_dir = tempfile::tempdir().unwrap();
        let shim = crate::shell_integration::ensure_bash_shim(cfg_dir.path()).unwrap();
        let mut cmd = CommandBuilder::new(bash);
        cmd.arg("--posix");
        cmd.arg("-l");
        cmd.cwd(home.path());
        cmd.env("HOME", home.path());
        cmd.env("ENV", &shim);
        cmd.env("CROFT_BASH_INJECT", "1");
        let mut term = PtyTerminal::spawn_with(cmd, None).unwrap();
        let mut waited = 0u32;
        while term.prompt_lines().is_empty() {
            assert!(
                waited < 8000,
                "bash never emitted a prompt mark; grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        assert!(
            term.shell_cwd().is_some(),
            "the shim's precmd must report cwd via OSC 7"
        );
        term.write_input(b"echo SENTINEL_IS=$USER_RC_SENTINEL\r");
        let mut waited = 0u32;
        loop {
            let (lines, _) = term.grid_lines();
            if lines
                .iter()
                .any(|l| l.contains("SENTINEL_IS=loaded") && !l.contains("echo"))
            {
                break;
            }
            assert!(
                waited < 8000,
                "user .bash_profile never ran through the shim; grid: {lines:?}"
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        // A failing command must round-trip its exit code through 133;D and
        // the shim must mark the prompt end for input extraction.
        term.write_input(b"false\r");
        let mut waited = 0u32;
        loop {
            use crate::shell_integration::OscEvent as E;
            let marks = term.command_marks();
            if marks.iter().any(|(k, _)| *k == E::CommandEnd(Some(1))) {
                assert!(
                    marks.iter().any(|(k, _)| *k == E::PromptEnd),
                    "bash PS1 must carry the 133;B input-start mark; marks: {marks:?}"
                );
                break;
            }
            assert!(
                waited < 8000,
                "no CommandEnd(1) after `false`; marks: {marks:?}, grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
    }

    /// A fish binary for the e2e: common install paths, then `$PATH`.
    /// Absent on plain macOS (skips), present on Linux boxes and via nix.
    fn find_fish() -> Option<String> {
        let fixed = [
            "/opt/homebrew/bin/fish",
            "/usr/local/bin/fish",
            "/usr/bin/fish",
        ];
        if let Some(f) = fixed.iter().find(|f| std::path::Path::new(f).exists()) {
            return Some((*f).to_string());
        }
        let path = std::env::var("PATH").ok()?;
        path.split(':')
            .map(|d| std::path::Path::new(d).join("fish"))
            .find(|p| p.is_file())
            .map(|p| p.display().to_string())
    }

    #[test]
    fn fish_integration_restores_xdg_dirs_and_yields_single_native_marks() {
        // fish end-to-end through croft's injection env: fish 4 must emit
        // the marks natively (croft's vendor script installs NO second
        // emitter, unlike Ghostty's which double-marks), the exit code must
        // round-trip, and the script must scrub the injection from the
        // session env so children never see it.
        let Some(fish) = find_fish() else {
            return;
        };
        let home = tempfile::tempdir().unwrap();
        let cfg_dir = tempfile::tempdir().unwrap();
        let dir = crate::shell_integration::ensure_fish_integration(cfg_dir.path()).unwrap();
        let mut cmd = CommandBuilder::new(&fish);
        cmd.arg("-i");
        cmd.arg("-l");
        cmd.cwd(home.path());
        cmd.env("HOME", home.path());
        cmd.env(
            "XDG_DATA_DIRS",
            crate::shell_integration::fish_xdg_data_dirs(&dir, None),
        );
        cmd.env("CROFT_FISH_XDG_DATA_DIR", &dir);
        let mut term = PtyTerminal::spawn_with(cmd, None).unwrap();
        let mut waited = 0u32;
        while term.prompt_lines().is_empty() {
            assert!(
                waited < 8000,
                "fish never emitted a prompt mark; grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        term.write_input(b"false\r");
        let mut waited = 0u32;
        loop {
            use crate::shell_integration::OscEvent as E;
            let marks = term.command_marks();
            if marks.iter().any(|(k, _)| *k == E::CommandEnd(Some(1))) {
                break;
            }
            assert!(
                waited < 8000,
                "no CommandEnd(1) after `false`; marks: {marks:?}, grid: {:?}",
                term.grid_lines().0
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        // Exactly one finished record for `false`: a second emitter would
        // double-decorate every command.
        let failing: Vec<_> = term
            .command_decorations()
            .into_iter()
            .filter(|d| d.exit == Some(1))
            .collect();
        assert_eq!(failing.len(), 1, "duplicate decorations: {failing:?}");
        // The injection env must be gone inside the session.
        term.write_input(b"echo DIRS=$XDG_DATA_DIRS; set -q CROFT_FISH_XDG_DATA_DIR; and echo SI_LEAKED; or echo SI_CLEAN\r");
        let needle = format!("{}", dir.display());
        let mut waited = 0u32;
        loop {
            let (lines, _) = term.grid_lines();
            if lines
                .iter()
                .any(|l| l.contains("SI_CLEAN") && !l.contains("echo"))
            {
                assert!(
                    !lines
                        .iter()
                        .any(|l| l.contains("SI_LEAKED") && !l.contains("echo")),
                    "grid: {lines:?}"
                );
                let dirs_line = lines
                    .iter()
                    .find(|l| l.contains("DIRS=") && !l.contains("echo"))
                    .cloned()
                    .unwrap_or_default();
                assert!(
                    !dirs_line.contains(&needle),
                    "XDG_DATA_DIRS still carries the injection dir: {dirs_line}"
                );
                break;
            }
            assert!(
                waited < 8000,
                "env scrub probe never answered; grid: {lines:?}"
            );
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
    }

    #[test]
    fn prompt_jump_targets_walk_previous_and_next() {
        // Pure chooser used by Cmd+Up / Cmd+Down: pick the nearest prompt
        // line above / below the current viewport top, if any.
        let prompts = [-30, -12, 0];
        assert_eq!(pick_prompt_jump(&prompts, 0, false), Some(-12));
        assert_eq!(pick_prompt_jump(&prompts, -12, false), Some(-30));
        assert_eq!(pick_prompt_jump(&prompts, -30, false), None);
        assert_eq!(pick_prompt_jump(&prompts, -30, true), Some(-12));
        assert_eq!(pick_prompt_jump(&prompts, -12, true), Some(0));
        assert_eq!(pick_prompt_jump(&prompts, 0, true), None);
    }

    #[test]
    fn bell_sets_a_flag_the_app_can_drain() {
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf 'DONE\\007\\n'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        // Wait for the child's output row, not the run header echoing the
        // command text — the BEL byte arrives with the output.
        wait_for_grid(&term, |ls| {
            ls.iter()
                .any(|l| l.contains("DONE") && !l.contains("printf"))
        });
        assert!(
            term.take_bell(),
            "BEL from the child must set the bell flag for the app to drain"
        );
        assert!(
            !term.take_bell(),
            "take_bell drains the flag — a second read is false"
        );
    }

    #[test]
    fn grid_lines_exposes_output_for_terminal_find() {
        // The find bar searches the grid text this returns. Spawn a program
        // that prints a known needle, wait for it, and confirm the search
        // helpers locate it on the mapped grid line.
        let tmp = tempfile::tempdir().unwrap();
        let needle = "croft-find-needle-42";
        let term =
            PtyTerminal::new_running("/bin/echo", &[String::from(needle)], tmp.path()).unwrap();
        let mut waited_ms = 0u32;
        loop {
            let (lines, top) = term.grid_lines();
            if let Some(row) = lines.iter().position(|l| l.contains(needle)) {
                let hit = crate::widgets::editor_find::find_next_match(
                    &lines,
                    needle,
                    crate::widgets::search::SearchOpts::default(),
                    0,
                    0,
                    false,
                )
                .expect("search must find the printed needle");
                assert_eq!(hit.row, row, "match row must line up with the grid row");
                // `top + row` maps the hit back to an absolute grid line the
                // viewport can scroll to; for the live screen that is >= 0.
                assert!(top + hit.row as i32 >= top);
                break;
            }
            assert!(waited_ms < 4000, "echo output never reached the grid");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited_ms += 20;
        }
    }

    #[test]
    fn pending_bytes_counts_advanced_output_and_resets_on_take_dirty() {
        let tmp = tempfile::tempdir().unwrap();
        let term = PtyTerminal::new_running(
            "/bin/echo",
            &[String::from("croft-pending-bytes-probe")],
            tmp.path(),
        )
        .unwrap();
        let mut waited_ms = 0u32;
        while waited_ms < 4000 && term.peek_pending_bytes() == 0 {
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited_ms += 20;
        }
        assert!(
            term.peek_pending_bytes() > 0,
            "the reader thread must accumulate the bytes it advanced so the main loop can tell echo from a bulk stream"
        );
        assert!(
            term.peek_pending_bytes() <= 4096,
            "a one-line echo must stay under the small-update threshold so it bypasses the redraw cap"
        );
        let _ = term.take_dirty();
        assert_eq!(
            term.peek_pending_bytes(),
            0,
            "take_dirty must reset the byte accumulator so the next window starts from zero"
        );
    }

    #[test]
    fn new_running_renders_running_header_in_term_immediately() {
        let tmp = tempfile::tempdir().unwrap();
        let term = PtyTerminal::new_running(
            "/bin/echo",
            &[String::from("croft-header-probe")],
            tmp.path(),
        )
        .unwrap();
        let snapshot = term.visible_text();
        assert!(
            snapshot.contains("/bin/echo croft-header-probe"),
            "expected the run command in the header line; got:\n{snapshot}"
        );
        assert!(
            snapshot.starts_with('▶') || snapshot.contains("▶ "),
            "expected a ▶ marker on the header line; got:\n{snapshot}"
        );
    }

    #[test]
    fn new_running_appends_exit_footer_when_child_finishes() {
        let tmp = tempfile::tempdir().unwrap();
        let term =
            PtyTerminal::new_running("/bin/echo", &[String::from("croft-exit-probe")], tmp.path())
                .unwrap();
        let mut waited_ms = 0u32;
        let footer_needle = "[Process exited]";
        loop {
            if term.visible_text().contains(footer_needle) {
                break;
            }
            if waited_ms >= 4000 {
                let snap = term.visible_text();
                panic!(
                    "expected '{footer_needle}' to appear within 4s of the child exiting; got:\n{snap}"
                );
            }
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited_ms += 20;
        }
    }

    #[test]
    fn new_for_interactive_shell_does_not_render_running_header() {
        let tmp = tempfile::tempdir().unwrap();
        let term = PtyTerminal::new(tmp.path()).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(50));
        let snapshot = term.visible_text();
        assert!(
            !snapshot.contains("▶ "),
            "interactive shell must not render a one-shot script header; got:\n{snapshot}"
        );
        assert!(
            !snapshot.contains("[Process exited]"),
            "interactive shell must not render an exit footer; got:\n{snapshot}"
        );
    }

    #[test]
    fn selection_normalised_handles_anchor_after_head() {
        let s = Selection {
            anchor: (5, 4),
            head: (2, 1),
            block: false,
        };
        assert_eq!(s.normalised(), (2, 1, 5, 4));
    }

    #[test]
    fn selection_normalised_handles_same_row() {
        let s = Selection {
            anchor: (3, 9),
            head: (3, 2),
            block: false,
        };
        assert_eq!(s.normalised(), (3, 2, 3, 9));
    }

    #[test]
    fn selection_has_area_only_when_endpoints_differ() {
        let s = Selection::new(2, 5);
        assert!(!s.has_area());
        let s2 = Selection {
            anchor: (2, 5),
            head: (2, 6),
            block: false,
        };
        assert!(s2.has_area());
    }

    #[test]
    fn cell_in_selection_within_single_row() {
        assert!(cell_in_selection(2, 5, 2, 3, 2, 7));
        assert!(!cell_in_selection(2, 2, 2, 3, 2, 7));
        assert!(!cell_in_selection(2, 8, 2, 3, 2, 7));
        assert!(!cell_in_selection(1, 5, 2, 3, 2, 7));
        assert!(!cell_in_selection(3, 5, 2, 3, 2, 7));
    }

    #[test]
    fn cell_in_selection_spans_multiple_rows() {
        assert!(!cell_in_selection(1, 4, 1, 5, 3, 2));
        assert!(cell_in_selection(1, 5, 1, 5, 3, 2));
        assert!(cell_in_selection(1, 9, 1, 5, 3, 2));
        assert!(cell_in_selection(2, 0, 1, 5, 3, 2));
        assert!(cell_in_selection(2, 9, 1, 5, 3, 2));
        assert!(cell_in_selection(3, 2, 1, 5, 3, 2));
        assert!(!cell_in_selection(3, 3, 1, 5, 3, 2));
        assert!(!cell_in_selection(4, 0, 1, 5, 3, 2));
    }

    #[test]
    fn osc52_copy_seq_wraps_with_correct_envelope() {
        let bytes = osc52_copy_seq("hello");
        assert_eq!(bytes, b"\x1b]52;c;aGVsbG8=\x07");
    }

    #[test]
    fn osc52_copy_seq_handles_empty() {
        assert_eq!(osc52_copy_seq(""), b"\x1b]52;c;\x07");
    }

    #[test]
    fn osc52_copy_seq_handles_unicode() {
        let bytes = osc52_copy_seq("héllo");
        assert!(bytes.starts_with(b"\x1b]52;c;"));
        assert_eq!(*bytes.last().unwrap(), 0x07);
        let body = &bytes[7..bytes.len() - 1];
        use base64::Engine;
        let decoded = base64::engine::general_purpose::STANDARD
            .decode(body)
            .unwrap();
        assert_eq!(decoded, "héllo".as_bytes());
    }

    #[test]
    fn select_word_at_in_term_brackets_the_alphanumeric_run_around_the_pivot() {
        let mut t = fresh_term(40, 5);
        feed(&mut t, b"hello world from croft");
        let got = select_word_at_in_term(&t, 0, 0, 8);
        assert_eq!(
            got,
            Some(((0, 6), (0, 10))),
            "pivot inside 'world' must select cols 6..=10"
        );
    }

    #[test]
    fn select_word_at_in_term_returns_none_when_pivot_is_on_whitespace() {
        let mut t = fresh_term(40, 5);
        feed(&mut t, b"hello world");
        assert_eq!(
            select_word_at_in_term(&t, 0, 0, 5),
            None,
            "pivot on the space between 'hello' and 'world' must yield no selection"
        );
    }

    #[test]
    fn select_word_at_in_term_treats_underscores_as_word_chars_and_dots_as_boundaries() {
        let mut t = fresh_term(40, 5);
        feed(&mut t, b"foo_bar.baz");
        assert_eq!(
            select_word_at_in_term(&t, 0, 0, 2),
            Some(((0, 0), (0, 6))),
            "'foo_bar' is one word (underscore is a word char), '.baz' is separate"
        );
        assert_eq!(
            select_word_at_in_term(&t, 0, 0, 9),
            Some(((0, 8), (0, 10))),
            "pivot inside 'baz' must select cols 8..=10"
        );
    }

    /// Double-click on a word that lives in scrollback must still
    /// word-select. Pre-fix, `select_word_at_in_term` ignored the
    /// scrollback offset and read the live grid at the same viewport
    /// row, which was usually blank — the lookup returned None and
    /// the user saw "double-click doesn't auto-select."
    #[test]
    fn select_word_at_in_term_word_selects_in_scrollback_when_display_offset_is_nonzero() {
        let mut t = fresh_term(20, 3);
        // Push three lines into scrollback by feeding six rows into a
        // three-row viewport.
        feed(
            &mut t,
            b"scroll-A row\r\nscroll-B row\r\nscroll-C row\r\nlive-D row\r\nlive-E row\r\nlive-F row",
        );
        t.scroll_display(alacritty_terminal::grid::Scroll::Delta(2));
        let display_offset = t.grid().display_offset();
        assert_eq!(
            display_offset, 2,
            "precondition: Scroll::Delta(2) must move the viewport into scrollback"
        );
        // Viewport row 0 with display_offset=2 = grid Line(-2) =
        // "scroll-B row". Pivot at column 2 sits inside "scroll-B".
        let got = select_word_at_in_term(&t, display_offset, 0, 2);
        assert!(
            got.is_some(),
            "double-click on a scrolled-back row must return a real anchor/head, not None - if this is None the user sees the double-click 'do nothing'"
        );
        let (anchor, head) = got.unwrap();
        // "scroll-B" spans cols 0..=7 (s=0, c=1, r=2, o=3, l=4, l=5,
        // -=… actually '-' is not a word char, so the word is
        // "scroll" at cols 0..=5). The pivot is at col 2, which
        // lives inside "scroll". The selection must cover that run.
        assert_eq!(
            anchor,
            (0, 0),
            "anchor must mark the start of the word run on viewport row 0"
        );
        assert_eq!(
            head,
            (0, 5),
            "head must mark the last char of 'scroll' before the hyphen breaks the word run"
        );
    }

    #[test]
    fn extract_selection_text_single_line() {
        let mut t = fresh_term(20, 5);
        feed(&mut t, b"hello world");
        let txt = extract_selection_text(&t, 0, 6, 0, 10);
        assert_eq!(txt, "world");
    }

    #[test]
    fn extract_selection_text_multi_line_trims_trailing_spaces() {
        let mut t = fresh_term(20, 5);
        feed(&mut t, b"first line\r\nsecond line");
        let txt = extract_selection_text(&t, 0, 6, 1, 5);
        assert_eq!(txt, "line\nsecond");
    }

    /// `extract_selection_text` now addresses absolute grid lines, so a
    /// negative line index reads scrollback directly and a given line
    /// returns the same content no matter how the viewport is scrolled.
    /// This is the property that lets a selection survive scrolling: the
    /// endpoints name content, not screen rows.
    #[test]
    fn extract_selection_text_addresses_absolute_grid_lines() {
        let mut t = fresh_term(20, 3);
        // Six lines into a 3-row viewport. Each `\r\n` past the bottom
        // pushes the topmost row into scrollback. End-state:
        //   scrollback: scroll-A = Line(-3), scroll-B = Line(-2), scroll-C = Line(-1)
        //   live:       live-D = Line(0), live-E = Line(1), live-F = Line(2)
        feed(
            &mut t,
            b"scroll-A row\r\nscroll-B row\r\nscroll-C row\r\nlive-D row\r\nlive-E row\r\nlive-F row",
        );

        assert_eq!(
            extract_selection_text(&t, 0, 0, 0, 19),
            "live-D row",
            "Line(0) is the first live row"
        );
        assert_eq!(
            extract_selection_text(&t, -2, 0, -2, 19),
            "scroll-B row",
            "Line(-2) reads the scrollback row directly, no display_offset needed"
        );

        // Scrolling the viewport must NOT change what an absolute line
        // returns - that invariant is why a selection no longer drifts
        // when the user scrolls mid-drag.
        t.scroll_display(alacritty_terminal::grid::Scroll::Delta(2));
        assert_eq!(t.grid().display_offset(), 2);
        assert_eq!(
            extract_selection_text(&t, -2, 0, -2, 19),
            "scroll-B row",
            "Line(-2) still reads 'scroll-B row' after scrolling - absolute lines are scroll-invariant"
        );
    }

    /// The whole point of the fix: a selection that spans from scrollback
    /// history into the live screen extracts every line, even though it
    /// is far taller than the 3-row viewport. Before the absolute-
    /// coordinate model the selection was capped at the visible pane
    /// height and the off-screen lines were simply unreachable.
    #[test]
    fn selection_stays_glued_to_its_content_while_output_streams() {
        // A selection made while a program keeps printing (Claude Code, any
        // streaming TUI) must track its content into scrollback, not stay at
        // fixed grid lines while the text slides underneath.
        let tmp = tempfile::tempdir().unwrap();
        let go = tmp.path().join("go");
        // Sentinel assembled at runtime so the `▶ sh -c …` run-label header
        // (which echoes the script text) can never match it.
        let script = format!(
            "t=TAR; echo \"${{t}}GET-line\"; until [ -e {} ]; do sleep 0.05; done; seq 1 60; sleep 30",
            go.display()
        );
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script], tmp.path()).unwrap();
        let mut waited = 0u32;
        let line = loop {
            let (lines, top) = term.grid_lines();
            if let Some(idx) = lines.iter().position(|l| l.starts_with("TARGET-line")) {
                break top + idx as i32;
            }
            assert!(waited < 8000, "sentinel never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        };
        term.set_selection(Some(Selection {
            anchor: (line, 0),
            head: (line, 30),
            block: false,
        }));
        assert_eq!(term.selection_text(), "TARGET-line");

        // Release the second phase: 60 more lines scroll the buffer.
        std::fs::File::create(&go).unwrap();
        let mut waited = 0u32;
        while !term.grid_lines().0.iter().any(|l| l == "60") {
            assert!(waited < 8000, "streamed output never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        assert_eq!(
            term.selection_text(),
            "TARGET-line",
            "the selection must follow its content as output streams past it"
        );
        let sel = term.selection().expect("selection survives streaming");
        assert!(
            sel.anchor.0 < line,
            "reported endpoints re-anchor upward as lines scroll into history"
        );
    }

    #[test]
    fn selection_survives_streaming_past_a_saturated_scrollback() {
        // Once a pane has scrolled more than SCROLLBACK_LINES, history_size
        // saturates at the cap while content keeps rotating through the
        // ring. A selection made in that state must still track its content
        // as more output streams (the live Claude Code pane: long-running,
        // scrollback full since forever).
        let tmp = tempfile::tempdir().unwrap();
        let go = tmp.path().join("go");
        let script = format!(
            "seq 1 5200; t=TAR; echo \"${{t}}GET-line\"; until [ -e {} ]; do sleep 0.05; done; seq 1 60; sleep 30",
            go.display()
        );
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script], tmp.path()).unwrap();
        let mut waited = 0u32;
        let line = loop {
            let (lines, top) = term.grid_lines();
            if let Some(idx) = lines.iter().position(|l| l.starts_with("TARGET-line")) {
                break top + idx as i32;
            }
            assert!(waited < 8000, "sentinel never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        };
        let hist_at_select = term.term.lock().grid().history_size();
        term.set_selection(Some(Selection {
            anchor: (line, 0),
            head: (line, 30),
            block: false,
        }));
        assert_eq!(term.selection_text(), "TARGET-line");

        std::fs::File::create(&go).unwrap();
        let mut waited = 0u32;
        while !term.grid_lines().0.iter().any(|l| l == "60") {
            assert!(waited < 8000, "streamed output never arrived");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
        // Premise of this test: the scrollback was already full, so
        // history_size never moved even though 60 lines scrolled past.
        assert_eq!(
            term.term.lock().grid().history_size(),
            hist_at_select,
            "test setup must saturate the scrollback before streaming"
        );
        assert_eq!(
            term.selection_text(),
            "TARGET-line",
            "the selection must follow its content even with history_size saturated"
        );
    }

    #[test]
    fn alt_screen_selection_stays_partially_visible_when_app_chrome_covers_its_edge() {
        // Scrolling Claude Code slides the selected block toward its input
        // box; the rows that reach it are overdrawn by the box while the
        // rest stay visible. The surviving rows must keep their highlight
        // (matched as a partial block), and copy must still yield the whole
        // remembered selection.
        let (_tmp, mut t) = quiet_pty();
        feed_pty(
            &t,
            b"\x1b[?1049h\x1b[H\x1b[2Jalpha one\r\nbravo two\r\ncharlie three\r\ndelta four\r\necho five",
        );
        t.set_selection(Some(Selection {
            anchor: (1, 0),
            head: (3, 9),
            block: false,
        }));
        assert_eq!(t.selection_text(), "bravo two\ncharlie three\ndelta four");

        // The app scrolls one line; the block's last row slides under the
        // input box, which repaints over it. Rows 0-1 of the block survive
        // at lines 0-1; line 2 onward is app chrome.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jbravo two\r\ncharlie three\r\n> INPUT BOX\r\n> more chrome\r\n> even more",
        );
        t.rebase_selection();
        let anchor = t.alt_sel.as_ref().expect("anchor survives");
        assert!(
            !anchor.dormant,
            "two of three selected rows are still on screen: not dormant"
        );
        assert_eq!(
            anchor.visible,
            Some(vec![(0, 0, u16::MAX), (1, 0, u16::MAX)]),
            "only the surviving rows keep their highlight"
        );
        assert_eq!(
            t.selection_text(),
            "bravo two\ncharlie three\ndelta four",
            "copy yields the whole remembered selection while partially covered"
        );

        // Scrolled back: the whole block is visible again, fully live.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jalpha one\r\nbravo two\r\ncharlie three\r\ndelta four\r\necho five",
        );
        t.rebase_selection();
        let anchor = t.alt_sel.as_ref().expect("anchor survives");
        assert!(!anchor.dormant);
        assert_eq!(anchor.visible, None, "fully visible again: no clip");
        assert_eq!(t.selection_text(), "bravo two\ncharlie three\ndelta four");
    }

    #[test]
    fn a_single_row_selection_on_a_repeated_row_reanchors_to_its_own_copy() {
        // A one-row selection has a one-row fingerprint, and full-screen
        // apps repeat rows verbatim (divider rules, continuation markers).
        // After a repaint the nearest identical copy may be a DIFFERENT
        // copy: the neighbours captured with the anchor must break the tie
        // so the highlight follows its own row, not the closest lookalike.
        let (_tmp, mut t) = quiet_pty();
        feed_pty(
            &t,
            b"\x1b[?1049h\x1b[H\x1b[2Jalpha\r\n-----\r\nbravo\r\n-----\r\ncharlie",
        );
        // Select the SECOND divider (row 3), between bravo and charlie.
        t.set_selection(Some(Selection {
            anchor: (3, 0),
            head: (3, 4),
            block: false,
        }));
        assert_eq!(t.selection_text(), "-----");

        // The app scrolls two rows: the selected divider moves to row 1
        // (still between bravo and charlie), while a new divider appears
        // at row 4 — NEARER to the old position than the real one.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jbravo\r\n-----\r\ncharlie\r\ndelta\r\n-----",
        );
        t.rebase_selection();
        let anchor = t.alt_sel.as_ref().expect("anchor survives");
        assert_eq!(
            anchor.top, 1,
            "the highlight must follow its own copy (neighbours bravo/charlie), not the nearest lookalike"
        );
        assert_eq!(
            t.selection.map(|s| (s.anchor, s.head)),
            Some(((1, 0), (1, 4))),
            "the selection coordinates must move with the content"
        );
        assert_eq!(t.selection_text(), "-----");
    }

    /// Drive a PtyTerminal's grid directly: parse `bytes` into its term as
    /// if the child had printed them. The child (`sleep`) stays silent, so
    /// the grid is exactly what the test painted.
    fn feed_pty(t: &PtyTerminal, bytes: &[u8]) {
        let mut p = Processor::<StdSyncHandler>::new();
        let mut term = t.term.lock();
        p.advance(&mut *term, bytes);
    }

    fn quiet_pty() -> (tempfile::TempDir, PtyTerminal) {
        let tmp = tempfile::tempdir().unwrap();
        let t = PtyTerminal::new_running("/bin/sleep", &[String::from("30")], tmp.path()).unwrap();
        (tmp, t)
    }

    /// An alt-screen round trip (`git log`, vim, htop) used to wipe every
    /// arrival stamp (the alternate grid has no history, so the cursor's
    /// absolute id collapsed and looked like an ED 3 wipe) and then
    /// re-stamp the whole scrollback with the exit time. Stamps are keyed
    /// on the scroll clock now, which the alternate screen freezes.
    #[test]
    fn timestamps_survive_an_alt_screen_round_trip() {
        let (_tmp, t) = quiet_pty();
        let mut prev = 0i64;
        feed_pty(&t, b"one\r\ntwo\r\nthree\r\n");
        t.stamp_chunk_for_test(&mut prev, 1000);
        let before = t.line_time_entries_for_test();
        assert!(!before.is_empty(), "rows got stamped");
        let (first_id, first_ms) = before[0];
        assert_eq!(first_ms, 1000);
        feed_pty(&t, b"\x1b[?1049h\x1b[Hpager-screen");
        t.stamp_chunk_for_test(&mut prev, 2000);
        feed_pty(&t, b"\x1b[?1049l");
        t.stamp_chunk_for_test(&mut prev, 3000);
        let after = t.line_time_entries_for_test();
        assert_eq!(
            before.iter().map(|&(k, _)| k).collect::<Vec<_>>(),
            after.iter().map(|&(k, _)| k).collect::<Vec<_>>(),
            "an alt-screen trip must not wipe or invent stamped rows"
        );
        let kept = after.iter().find(|&&(k, _)| k == first_id).unwrap().1;
        assert_eq!(
            kept, 1000,
            "an old row must keep its true arrival time, not the pager's exit time"
        );
    }

    /// Once the scrollback ring saturates, `history_size` stops moving, so
    /// history-keyed row ids froze to screen positions: new rows overwrote
    /// the same ids and the gutter looked up unrelated rows' times. Clock
    /// ids keep advancing one per scrolled line forever.
    #[test]
    fn timestamps_keep_advancing_past_a_saturated_scrollback() {
        let (_tmp, t) = quiet_pty();
        let mut prev = 0i64;
        let mut chunk = String::new();
        for i in 0..5300 {
            chunk.push_str(&format!("s-{i}\r\n"));
        }
        feed_pty(&t, chunk.as_bytes());
        t.stamp_chunk_for_test(&mut prev, 1000);
        let max1 = t
            .line_time_entries_for_test()
            .last()
            .map(|&(k, _)| k)
            .unwrap();
        let mut more = String::new();
        for i in 0..100 {
            more.push_str(&format!("t-{i}\r\n"));
        }
        feed_pty(&t, more.as_bytes());
        t.stamp_chunk_for_test(&mut prev, 2000);
        let max2 = t
            .line_time_entries_for_test()
            .last()
            .map(|&(k, _)| k)
            .unwrap();
        assert!(
            max2 >= max1 + 100,
            "row ids must keep advancing after saturation (got {max1} then {max2})"
        );
    }

    /// An inline image captured after the scrollback saturated must ride
    /// the ring rotation like the content it anchors: `history_size` stops
    /// growing there, so a history-delta anchor freezes onto a viewport
    /// row and the picture sits on top of every later command's output.
    #[test]
    fn an_inline_image_rides_scrollback_past_saturation() {
        let (_tmp, t) = quiet_pty();
        let mut chunk = String::new();
        for i in 0..5200 {
            chunk.push_str(&format!("pre-{i}\r\n"));
        }
        feed_pty(&t, chunk.as_bytes());
        feed_pty(&t, b"image-anchor-row");
        t.push_image_for_test(vec![9, 9, 9]);
        feed_pty(&t, b"\r\n");
        let find_anchor = |t: &PtyTerminal| {
            let (lines, top) = t.grid_lines();
            top + lines
                .iter()
                .position(|l| l.starts_with("image-anchor-row"))
                .unwrap() as i32
        };
        assert_eq!(
            t.pane_images().pop().unwrap().line,
            find_anchor(&t),
            "the anchor starts on its row"
        );
        let mut more = String::new();
        for i in 0..60 {
            more.push_str(&format!("post-{i}\r\n"));
        }
        feed_pty(&t, more.as_bytes());
        assert_eq!(
            t.pane_images().pop().unwrap().line,
            find_anchor(&t),
            "the anchor must follow its content through ring rotation"
        );
    }

    /// Typing `clear` reaches the grid as ED 3 through the PTY reader
    /// thread (modern terminfo wipes scrollback), never through the pane's
    /// own Clear method: the reader must drop captured images too, or the
    /// picture floats over the fresh prompt.
    #[test]
    fn a_program_emitted_scrollback_wipe_drops_captured_images() {
        use base64::Engine;
        let mut png_buf = Vec::new();
        image::RgbaImage::from_pixel(1, 1, image::Rgba([0, 255, 0, 255]))
            .write_to(
                &mut std::io::Cursor::new(&mut png_buf),
                image::ImageFormat::Png,
            )
            .expect("encode test png");
        let b64 = base64::engine::general_purpose::STANDARD.encode(&png_buf);
        let tmp = tempfile::tempdir().unwrap();
        // Fill lines push real content into scrollback first (ED 3 can only
        // shrink a nonempty history); the wipe then waits on stdin so the
        // test can observe the capture before it lands.
        let script = format!(
            "i=0; while [ $i -lt 60 ]; do echo fill-$i; i=$((i+1)); done; printf '\\033]1337;File=inline=1:{b64}\\007\\n'; read x; printf '\\033[3J\\033[2J'; printf 'wiped\\n'; sleep 30"
        );
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script], tmp.path()).unwrap();
        let mut waited = 0u32;
        while term.pane_images().is_empty() {
            assert!(waited < 8000, "inline image never captured");
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        term.write_input(b"\n");
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.starts_with("wiped")));
        assert!(
            term.pane_images().is_empty(),
            "an ED 3 scrollback wipe from the program must drop the pane's images"
        );
    }

    /// A wipe with EMPTY scrollback (picture captured on the first screen,
    /// nothing scrolled into history yet) must drop the image too: the
    /// history-shrink heuristic saw 0 → 0 and kept the stale overlay.
    #[test]
    fn a_zero_history_screen_wipe_drops_captured_images() {
        use base64::Engine;
        let mut png_buf = Vec::new();
        image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255]))
            .write_to(
                &mut std::io::Cursor::new(&mut png_buf),
                image::ImageFormat::Png,
            )
            .expect("encode test png");
        let b64 = base64::engine::general_purpose::STANDARD.encode(&png_buf);
        let tmp = tempfile::tempdir().unwrap();
        let script = format!(
            "printf '\\033]1337;File=inline=1:{b64}\\007\\n'; read x; printf '\\033[2J\\033[3J\\033[H'; printf 'wiped\\n'; sleep 30"
        );
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script], tmp.path()).unwrap();
        let mut waited = 0u32;
        while term.pane_images().is_empty() {
            assert!(waited < 8000, "inline image never captured");
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        term.write_input(b"\n");
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.starts_with("wiped")));
        assert!(
            term.pane_images().is_empty(),
            "a screen wipe with empty history must still drop the pane's images"
        );
    }

    /// CAN/SUB cancel an incomplete CSI and an ESC inside one begins a new
    /// escape (VT100): the wipe sniffer's CSI walk must not swallow the
    /// sequence that follows a cancelled CSI, or a wipe (or alt entry)
    /// right after one goes unseen.
    #[test]
    fn a_cancelled_csi_does_not_hide_a_wipe_from_the_sniffer() {
        let mut w = WipeSniffer::default();
        // ESC inside an incomplete CSI: the ED 3 after it must be seen.
        let (_s, h) = w.scan(b"\x1b[12\x1b[3J", false);
        assert!(h, "ESC-in-CSI swallowed the scrollback wipe");
        // CAN aborts; the alt entry after it must be seen, so the ED 2
        // inside the alt screen is correctly ignored.
        let mut w = WipeSniffer::default();
        let (s, _h) = w.scan(b"\x1b[\x18\x1b[?1049h\x1b[2J", false);
        assert!(!s, "CAN-cancelled CSI hid the alt entry from the sniffer");
    }

    /// `clear && vim`: the wipe and the alt-screen entry land in ONE PTY
    /// chunk. Judging the wipe by the chunk's FINAL mode attributed it to
    /// the alt screen and kept the invalidated primary images, which then
    /// reappeared over unrelated content when the app exited.
    #[test]
    fn a_wipe_followed_by_alt_entry_in_one_chunk_still_drops_images() {
        use base64::Engine;
        let mut png_buf = Vec::new();
        image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 255, 0, 255]))
            .write_to(
                &mut std::io::Cursor::new(&mut png_buf),
                image::ImageFormat::Png,
            )
            .expect("encode test png");
        let b64 = base64::engine::general_purpose::STANDARD.encode(&png_buf);
        let tmp = tempfile::tempdir().unwrap();
        // The wipe and the alt ENTRY share one write (one PTY chunk, the
        // pane still in the alt screen when it ends); the exit comes later.
        let script = format!(
            "printf '\\033]1337;File=inline=1:{b64}\\007\\n'; read x; printf '\\033[2J\\033[3J\\033[?1049halt-owns-this'; sleep 1; printf '\\033[?1049l'; printf 'back\\n'; sleep 30"
        );
        let mut term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script], tmp.path()).unwrap();
        let mut waited = 0u32;
        while term.pane_images().is_empty() {
            assert!(waited < 8000, "inline image never captured");
            std::thread::sleep(std::time::Duration::from_millis(40));
            waited += 40;
        }
        term.write_input(b"\n");
        wait_for_grid(&term, |ls| ls.iter().any(|l| l.starts_with("back")));
        assert!(
            term.pane_images().is_empty(),
            "the wipe preceded the alt entry, so the primary images are gone"
        );
    }

    /// Clearing the pane erases the content the OSC 133 marks anchored to:
    /// keeping them used to invert their drift math (history_size zeroed)
    /// so stale decoration dots later resurfaced on unrelated output rows,
    /// where clicking one copied or re-ran the wrong text.
    #[test]
    fn clearing_the_terminal_drops_its_marks() {
        use crate::shell_integration::OscEvent;
        let (_tmp, mut t) = quiet_pty();
        feed_pty(&t, b"\r\n$ ");
        t.push_mark_for_test(OscEvent::PromptStart, 0);
        t.push_mark_for_test(OscEvent::PromptEnd, 2);
        feed_pty(&t, b"cmd\r\nout\r\n");
        t.push_mark_for_test(OscEvent::CommandStart, 0);
        t.push_mark_for_test(OscEvent::CommandEnd(Some(0)), 0);
        assert_eq!(
            pair_decorations(&t.marks_snapshot()).len(),
            1,
            "staging: one finished command"
        );
        t.clear_screen_and_scrollback();
        assert!(
            pair_decorations(&t.marks_snapshot()).is_empty(),
            "cleared content keeps no command decorations"
        );
    }

    /// A prompt mark recorded after the scrollback saturated must ride the
    /// ring rotation like its content: under the history-size model the
    /// delta froze, so the mark (and its decoration dot) drifted off its
    /// row as output streamed.
    #[test]
    fn a_prompt_mark_rides_scrollback_past_saturation() {
        use crate::shell_integration::OscEvent;
        let (_tmp, mut t) = quiet_pty();
        let mut chunk = String::new();
        for i in 0..5200 {
            chunk.push_str(&format!("pre-{i}\r\n"));
        }
        feed_pty(&t, chunk.as_bytes());
        feed_pty(&t, b"$ marker-prompt");
        t.push_mark_for_test(OscEvent::PromptStart, 0);
        feed_pty(&t, b"\r\n");
        let find = |t: &mut PtyTerminal| {
            let (lines, top) = t.grid_lines();
            top + lines
                .iter()
                .position(|l| l.starts_with("$ marker-prompt"))
                .unwrap() as i32
        };
        assert_eq!(
            t.marks_snapshot()[0].1,
            find(&mut t),
            "the mark starts on its prompt row"
        );
        let mut more = String::new();
        for i in 0..60 {
            more.push_str(&format!("post-{i}\r\n"));
        }
        feed_pty(&t, more.as_bytes());
        assert_eq!(
            t.marks_snapshot()[0].1,
            find(&mut t),
            "the mark must follow its prompt through ring rotation"
        );
    }

    /// The pane's Clear wipes screen AND scrollback: the content an inline
    /// image anchored to is gone, so the picture must go with it instead of
    /// floating over the fresh prompt at a now-meaningless row.
    #[test]
    fn clearing_the_terminal_drops_its_captured_images() {
        let (_tmp, mut t) = quiet_pty();
        feed_pty(&t, b"\r\nsome output");
        t.push_image_for_test(vec![1, 2, 3]);
        feed_pty(&t, b"\r\n");
        assert_eq!(t.pane_images().len(), 1);
        t.clear_screen_and_scrollback();
        assert!(
            t.pane_images().is_empty(),
            "a cleared pane keeps no image anchored to erased content"
        );
    }

    /// A captured line wider than the pane appears in the grid as a WRAPPED
    /// first row (a prefix of the needle): the jump must find it, must not
    /// be fooled by an unrelated short row sharing the prefix (only rows
    /// that really soft-wrap qualify for the reverse arm), and must skip
    /// continuation rows so the newest-first scan can't land mid-line.
    #[test]
    fn capture_jump_matches_wrapped_lines_without_false_prefixes() {
        let (_tmp, mut t) = quiet_pty();
        let area = Rect::new(0, 0, 40, 12);
        let mut buf = Buffer::empty(area);
        (&mut t).render(area, &mut buf);
        let needle: String = format!("error[E0308]: {}", "x".repeat(60))
            .chars()
            .take(60)
            .collect();
        // An unrelated NON-wrapped row that happens to share the prefix.
        feed_pty(&t, b"\r\nerror[E0308]: unrelated short\r\n");
        assert_eq!(
            t.find_captured_line(&needle),
            None,
            "a short non-wrapped lookalike must not match"
        );
        // The real captured line, wrapping across two grid rows.
        let full = format!("error[E0308]: {}", "x".repeat(60));
        feed_pty(&t, full.as_bytes());
        let start = {
            let term = t.term.lock();
            term.grid().cursor.point.line.0 - 1
        };
        assert_eq!(
            t.find_captured_line(&needle),
            Some(start),
            "the wrapped line's FIRST row is the jump target, not its continuation"
        );
    }

    /// A soft-wrapped logical line copies as ONE line: the WRAPLINE flag on
    /// the wrapping row's last cell tells a continuation from a real row
    /// break, so selection copy, the durable command history, and the
    /// sticky header all see the text the user actually typed. A '\n' at
    /// the wrap corrupted stored commands and re-ran only the first
    /// fragment on paste. Hard breaks keep their newline.
    #[test]
    fn selection_over_a_soft_wrapped_line_copies_one_logical_line() {
        let (_tmp, mut t) = quiet_pty();
        let area = Rect::new(0, 0, 40, 10);
        let mut buf = Buffer::empty(area);
        (&mut t).render(area, &mut buf);
        let cols = t.term.lock().columns();
        // A fresh row below any spawn banner, then a line long enough to wrap.
        feed_pty(&t, b"\r\n");
        let start = t.term.lock().grid().cursor.point.line.0;
        let long: String = "abcdefghij".repeat(6); // 60 chars > pane width
        feed_pty(&t, long.as_bytes());
        t.set_selection(Some(Selection {
            anchor: (start, 0),
            head: (start + 1, cols.saturating_sub(1) as u16),
            block: false,
        }));
        assert_eq!(
            t.selection_text(),
            long,
            "the wrapped rows must rejoin without a newline"
        );
        // A hard row break keeps its newline.
        feed_pty(&t, b"\r\nsecond");
        t.set_selection(Some(Selection {
            anchor: (start + 1, 0),
            head: (start + 2, 6),
            block: false,
        }));
        assert!(
            t.selection_text().contains('\n'),
            "a real row break still copies as two lines: {:?}",
            t.selection_text()
        );
    }

    /// The annotate prompt's existing-note lookup compares the selection
    /// line captured at snapshot time against annotation lines; both must
    /// translate at the SAME clock reading, or output scrolling between the
    /// capture and the lookup shifts the annotations away from the fixed
    /// selection line and the same span duplicates instead of editing.
    #[test]
    fn annotation_lookup_translates_at_the_callers_clock() {
        let (_tmp, mut t) = quiet_pty();
        feed_pty(&t, b"existing span\r\n");
        let clock = t.scroll_clock();
        t.add_annotation(0, clock, 0, 13, String::from("existing note"));
        // The prompt captures selection + clock in one snapshot...
        let (_, snap_clock) = t.selection_and_clock();
        // ...then 30 rows land before the existing-note lookup runs.
        let mut fill = String::new();
        for i in 0..30 {
            fill.push_str(&format!("late-{i}\r\n"));
        }
        feed_pty(&t, fill.as_bytes());
        let cur = t.annotations_at_clock(snap_clock);
        assert_eq!(
            cur[0].0, 0,
            "translated at the captured clock, the note still names the captured line"
        );
    }

    /// Annotations name PRIMARY-screen rows; while vim/htop own the
    /// viewport the same line numbers describe unrelated alternate-grid
    /// content, so neither the amber paint nor click hit-testing may
    /// resolve there — and both come back when the app exits.
    #[test]
    fn annotations_hide_while_an_alt_screen_app_owns_the_viewport() {
        let (_tmp, mut t) = quiet_pty();
        t.last_inner = Rect {
            x: 0,
            y: 0,
            width: 80,
            height: 24,
        };
        feed_pty(&t, b"annotated content row\r\n");
        let clock = t.scroll_clock();
        t.add_annotation(0, clock, 0, 15, String::from("note"));
        assert!(
            t.annotation_at(3, 0).is_some(),
            "the note resolves on the primary screen"
        );
        feed_pty(&t, b"\x1b[?1049h\x1b[Hvim-owns-this-screen");
        assert!(
            t.annotation_at(3, 0).is_none(),
            "no hit while the alternate screen is up"
        );
        feed_pty(&t, b"\x1b[?1049l");
        assert!(
            t.annotation_at(3, 0).is_some(),
            "back on the primary screen the note resolves again"
        );
    }

    /// The Annotate prompt captures its span when it OPENS; rows that
    /// stream while the user types the note must not shift the pin. The
    /// commit anchors with the open-time (line, clock) pair — sampling a
    /// fresh baseline at commit pinned the note N rows below its text
    /// after N rows of output.
    #[test]
    fn an_annotation_committed_after_output_streamed_pins_where_it_was_captured() {
        let (_tmp, mut t) = quiet_pty();
        feed_pty(&t, b"target-line alpha\r\n");
        let (lines, top) = t.grid_lines();
        let line = top
            + lines
                .iter()
                .position(|l| l.starts_with("target-line"))
                .unwrap() as i32;
        let clock = t.scroll_clock();
        // 40 rows stream while the note is being typed.
        let mut more = String::new();
        for i in 0..40 {
            more.push_str(&format!("while-typing-{i}\r\n"));
        }
        feed_pty(&t, more.as_bytes());
        t.add_annotation(line, clock, 0, 11, String::from("note"));
        let cur = t.annotations_current();
        assert_eq!(cur.len(), 1, "the annotation survives");
        let term = t.term.lock();
        let text = extract_selection_text(&term, cur[0].0, 0, cur[0].0, 20);
        assert!(
            text.starts_with("target-line alpha"),
            "the note must pin to the text selected at open, got row: {text:?}"
        );
    }

    /// Past `SCROLLBACK_LINES` the ring rotates while `history_size` stops
    /// moving, so a history-growth anchor freezes to a screen position.
    /// Annotations ride the scroll clock (like selections) and must keep
    /// following their content in exactly the long-lived panes (Claude
    /// Code, `tail -f`) the feature targets.
    #[test]
    fn an_annotation_survives_streaming_past_a_saturated_scrollback() {
        let (_tmp, mut t) = quiet_pty();
        let mut chunk = String::new();
        for i in 0..5200 {
            chunk.push_str(&format!("pre-{i}\r\n"));
        }
        feed_pty(&t, chunk.as_bytes());
        feed_pty(&t, b"note-worthy-marker here\r\n");
        let (lines, top) = t.grid_lines();
        let line = top
            + lines
                .iter()
                .position(|l| l.starts_with("note-worthy-marker"))
                .unwrap() as i32;
        let clock = t.scroll_clock();
        t.add_annotation(line, clock, 0, 18, String::from("pinned"));
        let mut more = String::new();
        for i in 0..60 {
            more.push_str(&format!("post-{i}\r\n"));
        }
        feed_pty(&t, more.as_bytes());
        let cur = t.annotations_current();
        assert_eq!(cur.len(), 1, "the annotation survives the rotation");
        let term = t.term.lock();
        let text = extract_selection_text(&term, cur[0].0, 0, cur[0].0, 25);
        assert!(
            text.starts_with("note-worthy-marker"),
            "the note must follow its content through ring rotation, got row: {text:?}"
        );
    }

    /// A command spanning several grid rows (a quoted/heredoc newline, or a
    /// soft wrap shown in a later-widened pane) joins its rows with '\n' in
    /// `extract_selection_text`; the sticky header used to print that byte
    /// into a cell verbatim, corrupting the frame on the host terminal (the
    /// screen-corruption class the pdftoppm/DAP capture fixes exist to stop).
    #[test]
    fn sticky_header_never_paints_a_control_char_for_a_multi_row_command() {
        use crate::shell_integration::OscEvent;
        let (_tmp, mut t) = quiet_pty();
        // First render sizes the grid to the pane (78 cols), so the long
        // command genuinely wraps.
        let area = Rect::new(0, 0, 80, 20);
        let mut buf = Buffer::empty(area);
        (&mut t).render(area, &mut buf);
        feed_pty(&t, b"$ ");
        t.push_mark_for_test(OscEvent::PromptEnd, 2);
        // A multi-line typed command (heredoc / quoted newline): the input
        // span covers two HARD rows, so the joined text carries a newline
        // well inside the label's painted width.
        feed_pty(&t, b"echo \"build-it-99\r\nsecond-line\"");
        feed_pty(&t, b"\r\n");
        t.push_mark_for_test(OscEvent::CommandStart, 0);
        for i in 0..40 {
            feed_pty(&t, format!("out-{i}\r\n").as_bytes());
        }
        t.push_mark_for_test(OscEvent::CommandEnd(Some(0)), 0);
        assert!(
            !t.command_decorations().is_empty(),
            "the marks must pair into a decoration"
        );
        t.scroll_to_top();
        for _ in 0..5 {
            t.scroll_down(1);
        }
        let mut buf = Buffer::empty(area);
        (&mut t).render(area, &mut buf);
        let inner = t.last_inner;
        let top: String = (inner.x..inner.x + inner.width)
            .map(|x| buf[(x, inner.y)].symbol().chars().next().unwrap_or(' '))
            .collect();
        assert!(
            top.contains("build-it-99"),
            "the wrapped command must still pin: {top:?}"
        );
        for y in 0..area.height {
            for x in 0..area.width {
                let sym = buf[(x, y)].symbol();
                assert!(
                    !sym.chars().any(|c| c.is_control()),
                    "cell ({x},{y}) holds a control char: {sym:?}"
                );
            }
        }
    }

    /// A prompt row rewritten SHORTER than the recorded 133;B column (a
    /// backgrounded `\r\x1b[K` progress writer, or a pane shrunk under the
    /// prompt) used to panic click-to-move: the clamp bounds inverted
    /// (`min > max`) and `u16::clamp` asserts. A click is never allowed to
    /// take the app down; the degraded gesture just clamps sanely.
    #[test]
    fn a_prompt_click_after_the_row_was_rewritten_shorter_does_not_panic() {
        let (_tmp, mut t) = quiet_pty();
        t.last_inner = Rect {
            x: 0,
            y: 0,
            width: 80,
            height: 24,
        };
        feed_pty(&t, b"user@host ~/Documents/croft % ");
        t.push_mark_for_test(crate::shell_integration::OscEvent::PromptEnd, 30);
        // The rewrite leaves 7 columns of text and the cursor at column 7,
        // both left of the recorded prompt-end column 30.
        feed_pty(&t, b"\r\x1b[Kworking");
        let row = t.term.lock().grid().cursor.point.line.0 as u16;
        // Live bounds: the text ends at column 7 and the cursor sits at
        // column 7, both left of the recorded 133;B column 30. The click at
        // column 5 clamps into [30.min(7), 7] = 7, which equals the cursor,
        // so the gesture degrades to no motion instead of panicking.
        assert_eq!(
            t.prompt_click_arrows(5, row),
            None,
            "a click on a shortened prompt row must degrade, not panic"
        );
    }

    #[test]
    fn alt_screen_selection_follows_content_across_repaints() {
        // Alt-screen apps (Claude Code) never scroll the grid — they
        // repaint it in place. The selection must re-find its text after
        // each repaint, hide while it is scrolled out of the app's view,
        // and come back when the content does.
        let (_tmp, mut t) = quiet_pty();
        feed_pty(
            &t,
            b"\x1b[?1049h\x1b[H\x1b[2Jalpha one\r\nbravo two\r\ncharlie three\r\ndelta four",
        );
        t.set_selection(Some(Selection {
            anchor: (1, 0),
            head: (1, 8),
            block: false,
        }));
        assert_eq!(t.selection_text(), "bravo two");

        // The app scrolls its transcript up one line and repaints.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jbravo two\r\ncharlie three\r\ndelta four\r\necho five",
        );
        t.rebase_selection();
        assert_eq!(t.selection_text(), "bravo two");
        assert_eq!(
            t.selection().unwrap().anchor.0,
            0,
            "the highlight must move to where the app repainted its text"
        );

        // The selected content scrolls out of the app's view entirely:
        // highlight goes dormant but copy still yields the remembered text.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jfoxtrot six\r\ngolf seven\r\nhotel eight\r\nindia nine",
        );
        t.rebase_selection();
        assert!(
            t.alt_sel.as_ref().is_some_and(|a| a.dormant),
            "no matching content anywhere in the grid: dormant"
        );
        assert_eq!(t.selection_text(), "bravo two");

        // The app scrolls the content back into view at a new position.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jalpha one\r\nbravo two\r\ncharlie three\r\ndelta four",
        );
        t.rebase_selection();
        assert!(t.alt_sel.as_ref().is_some_and(|a| !a.dormant));
        assert_eq!(t.selection().unwrap().anchor.0, 1);
        assert_eq!(t.selection_text(), "bravo two");
    }

    #[test]
    fn alt_screen_row_half_covered_by_a_floating_pill_keeps_its_surviving_prefix() {
        // Claude Code floats a "Jump to bottom" pill ON TOP of a content
        // row. That row's text no longer equals the remembered row, but
        // its left part is untouched — it must keep a highlight clipped to
        // the surviving columns instead of dropping out entirely.
        let (_tmp, mut t) = quiet_pty();
        feed_pty(
            &t,
            b"\x1b[?1049h\x1b[H\x1b[2Jalpha one\r\nbravo two words here\r\ncharlie three words here\r\ndelta four words here\r\necho five",
        );
        t.set_selection(Some(Selection {
            anchor: (1, 0),
            head: (3, 20),
            block: false,
        }));
        // The app repaints the same content but overlays a pill over the
        // right half of the block's middle row.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jalpha one\r\nbravo two words here\r\ncharlie three[ PILL ]here\r\ndelta four words here\r\necho five",
        );
        t.rebase_selection();
        let anchor = t.alt_sel.as_ref().expect("anchor survives");
        assert!(!anchor.dormant);
        let clips = anchor.visible.as_ref().expect("partially covered");
        assert!(
            clips.contains(&(1, 0, u16::MAX)) && clips.contains(&(3, 0, u16::MAX)),
            "intact rows keep their full highlight: {clips:?}"
        );
        assert!(
            clips
                .iter()
                .any(|&(l, lo, hi)| l == 2 && lo == 0 && (12..14).contains(&hi)),
            "the covered row keeps its surviving prefix ('charlie three'): {clips:?}"
        );
        assert!(
            clips
                .iter()
                .any(|&(l, lo, hi)| l == 2 && lo == 21 && hi == 24),
            "the surviving suffix ('here') stays lit too, even under half the row: {clips:?}"
        );
    }

    #[test]
    fn alt_screen_bottom_up_drag_from_a_blank_row_selects_and_survives_release() {
        // Bottom-up selections usually start on a blank row or an animated
        // status row: the anchor captured at mouse-down mismatches on the
        // next frame, and before drag-awareness that turned the whole drag
        // dormant (invisible). While the button is held the selection must
        // follow the pointer over whatever is on screen; release captures
        // the definitive anchor.
        let (_tmp, mut t) = quiet_pty();
        feed_pty(
            &t,
            b"\x1b[?1049h\x1b[H\x1b[2Jalpha one\r\nbravo two\r\ncharlie three\r\n\r\ntokens 42",
        );
        t.last_inner = Rect::new(0, 0, 40, 10);
        // Mouse-down on the blank row (row 3), then an app repaint changes
        // the animated status row so the blank-row anchor can't match as a
        // block, then drag upward.
        t.start_selection_at(5, 3);
        feed_pty(&t, b"\x1b[5;1Htokens 43\x1b[K");
        t.extend_selection_to(0, 1);
        let anchor = t.alt_sel.as_ref().expect("anchor present");
        assert!(
            !anchor.dormant,
            "a held drag never hides itself, whatever the anchor matched"
        );
        assert_eq!(t.selection_text(), "bravo two\ncharlie three\n");
        t.end_drag();
        // After release the anchor is definitive: the app scrolls its
        // content down one row and the highlight follows.
        feed_pty(
            &t,
            b"\x1b[H\x1b[2Jnew line\r\nalpha one\r\nbravo two\r\ncharlie three\r\n\r\ntokens 44",
        );
        t.rebase_selection();
        assert_eq!(t.selection().expect("survives").normalised().0, 2);
        assert_eq!(t.selection_text(), "bravo two\ncharlie three\n");
    }

    #[test]
    fn alt_screen_edge_autoscroll_forwards_wheel_to_the_tracking_app() {
        // On the alternate screen a drag held past the pane edge cannot
        // scroll croft scrollback (there is none); a mouse-tracking app
        // gets a wheel report at the edge cell so IT scrolls, and the
        // selection head re-pins to the edge row.
        let tmp = tempfile::tempdir().unwrap();
        // `cat -v` echoes whatever the pty receives back as printable
        // text, so the wheel report's arrival is observable in the grid.
        let mut t =
            PtyTerminal::new_running("/bin/cat", &[String::from("-v")], tmp.path()).unwrap();
        feed_pty(&t, b"\x1b[?1049h\x1b[?1002h\x1b[?1006h\x1b[H\x1b[2J");
        t.last_inner = Rect::new(0, 0, 40, 10);
        t.set_selection(Some(Selection {
            anchor: (2, 0),
            head: (2, 5),
            block: false,
        }));
        t.autoscroll_select(1, 5);
        assert_eq!(
            t.selection().unwrap().head.0,
            9,
            "the head re-pins to the bottom edge row"
        );
        let mut waited = 0u32;
        // SGR wheel-down at 1-based cell (6, 10): `ESC[<65;6;10M`.
        while !t.grid_lines().0.iter().any(|l| l.contains("[<65;6;10M")) {
            assert!(waited < 8000, "the app never received the wheel report");
            std::thread::sleep(std::time::Duration::from_millis(20));
            waited += 20;
        }
    }

    #[test]
    fn primary_selection_survives_alt_round_trip_but_alt_selection_dies_on_exit() {
        let (_tmp, mut t) = quiet_pty();
        feed_pty(&t, b"hello world\r\n");
        // The `▶ /bin/sleep 30` run-label header owns the top row(s), so
        // find where the fed line actually landed.
        let (lines, top) = t.grid_lines();
        let line = top
            + lines
                .iter()
                .position(|l| l.starts_with("hello"))
                .expect("fed line present") as i32;
        t.set_selection(Some(Selection {
            anchor: (line, 0),
            head: (line, 4),
            block: false,
        }));
        assert_eq!(t.selection_text(), "hello");
        // vim-style round trip: enter and leave the alternate screen.
        feed_pty(&t, b"\x1b[?1049h\x1b[H\x1b[2Jvim vim vim");
        t.rebase_selection();
        feed_pty(&t, b"\x1b[?1049l");
        t.rebase_selection();
        assert_eq!(
            t.selection_text(),
            "hello",
            "a primary-screen selection is frozen across an alt-screen trip"
        );
        // A selection made ON the alt screen names alternate-grid content;
        // leaving the alt screen destroys that content and the selection.
        feed_pty(&t, b"\x1b[?1049h\x1b[H\x1b[2Jquick brown fox");
        t.set_selection(Some(Selection {
            anchor: (0, 0),
            head: (0, 4),
            block: false,
        }));
        assert_eq!(t.selection_text(), "quick");
        feed_pty(&t, b"\x1b[?1049l");
        t.rebase_selection();
        assert!(
            t.selection().is_none(),
            "an alt-screen selection dies with the alternate grid"
        );
    }

    #[test]
    fn selection_spanning_scrollback_into_live_extracts_every_line() {
        let mut t = fresh_term(20, 3);
        feed(&mut t, b"AAAA\r\nBBBB\r\nCCCC\r\nDDDD\r\nEEEE\r\nFFFF");
        // Select from the oldest scrollback line (AAAA = Line(-3)) all
        // the way to the last live line (FFFF = Line(2)).
        let all = extract_selection_text(&t, -3, 0, 2, 19);
        assert_eq!(all, "AAAA\nBBBB\nCCCC\nDDDD\nEEEE\nFFFF");
    }

    #[test]
    fn process_name_resolves_a_live_pid() {
        let me = std::process::id() as i32;
        let name = process_name(me);
        assert!(
            name.as_deref().is_some_and(|n| !n.is_empty()),
            "the test process resolves to a non-empty name: {name:?}"
        );
    }

    #[test]
    fn pick_pane_label_prefers_a_manual_name() {
        assert_eq!(pick_pane_label(Some("server"), "zsh"), "server");
        assert_eq!(pick_pane_label(None, "vim"), "vim");
        assert_eq!(pick_pane_label(None, ""), "");
    }

    #[test]
    fn parse_shells_skips_comments_and_dedupes_by_basename() {
        let s = "# /etc/shells\n/bin/zsh\n/bin/bash\n/usr/local/bin/bash\n\nnot-a-path\n/usr/bin/fish\n";
        let p = parse_shells(s);
        let bases: Vec<&str> = p.iter().map(|(_, b)| b.as_str()).collect();
        assert_eq!(
            bases,
            vec!["zsh", "bash", "fish"],
            "comments/blanks/non-paths skipped, basenames deduped"
        );
        assert_eq!(p[0].0, "/bin/zsh", "full path retained");
    }
    #[test]
    fn finished_command_carries_its_output_block() {
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]133;A\\007$ \\033]133;B\\007make\\n\\033]133;C\\007'; printf 'main.c:7:3: error: boom\\nsecond line\\n'; printf '\\033]133;D;1\\007'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        let mut finished = Vec::new();
        while std::time::Instant::now() < deadline {
            finished.extend(term.drain_finished_commands());
            if !finished.is_empty() {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(40));
        }
        let f = finished
            .first()
            .expect("the 133;D must yield a FinishedCommand");
        assert!(
            f.output.contains("main.c:7:3: error: boom") && f.output.contains("second line"),
            "the output block must ride the completion; got {:?}",
            f.output
        );
    }

    #[test]
    fn finished_output_keeps_a_final_line_without_trailing_newline() {
        // `133;D` moves no cursor: a diagnostic printed WITHOUT a trailing
        // newline leaves the mark on that very row, and excluding the
        // cursor row dropped exactly the line the matchers needed (#120
        // review).
        let tmp = tempfile::tempdir().unwrap();
        let script = "printf '\\033]133;A\\007$ \\033]133;B\\007cc\\n\\033]133;C\\007'; printf 'lib.c:2:1: error: no newline here'; printf '\\033]133;D;1\\007'";
        let term =
            PtyTerminal::new_running("/bin/sh", &[String::from("-c"), script.into()], tmp.path())
                .unwrap();
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        let mut finished = Vec::new();
        while std::time::Instant::now() < deadline {
            finished.extend(term.drain_finished_commands());
            if !finished.is_empty() {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(40));
        }
        let f = finished.first().expect("completion expected");
        assert!(
            f.output.contains("lib.c:2:1: error: no newline here"),
            "the unterminated final row must be captured; got {:?}",
            f.output
        );
    }
}