ftui-extras 0.4.0

Feature-gated extras for FrankenTUI (markdown, charts, clipboard, themes).
Documentation
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
//! Terminal state machine for tracking terminal content and cursor.
//!
//! This module provides a grid-based terminal state that can be updated
//! by parsing ANSI escape sequences via the [`AnsiHandler`] trait.
//!
//! # Invariants
//!
//! 1. **Cursor bounds**: Cursor position is always within grid bounds (0..width, 0..height).
//! 2. **Grid consistency**: Grid size always matches (width × height) cells.
//! 3. **Scrollback limit**: Scrollback never exceeds `max_scrollback` lines.
//!
//! # Failure Modes
//!
//! | Failure | Cause | Behavior |
//! |---------|-------|----------|
//! | Out of bounds | Invalid coordinates | Clamped to valid range |
//! | Zero size | Resize to 0x0 | Minimum 1x1 enforced |
//! | Scrollback overflow | Too many lines | Oldest lines dropped |

use std::collections::VecDeque;

use ftui_style::Color;

/// Sentinel character stored in the right-hand cells of a wide (double-width)
/// character.  During copy extraction these cells are skipped so the character
/// is emitted only once.
pub const WIDE_CONTINUATION: char = '\u{E000}';

/// Per-row flag tracking how the row terminates.
///
/// When text wraps because it exceeds the terminal width (auto-wrap / DECAWM),
/// the row is flagged [`SoftWrap`](LineFlag::SoftWrap).  Explicit newlines
/// (LF / CR+LF) produce [`HardNewline`](LineFlag::HardNewline).
///
/// Copy extraction uses these flags to decide whether to join adjacent rows
/// or insert a newline between them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineFlag {
    /// Row ends with a hard newline (explicit LF/CR+LF, or a fresh row).
    #[default]
    HardNewline,
    /// Row is soft-wrapped (content exceeded terminal width and auto-wrapped).
    SoftWrap,
}

/// Terminal cell attributes (bitflags).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CellAttrs(u8);

impl CellAttrs {
    /// No attributes set.
    pub const NONE: Self = Self(0);
    /// Bold/bright.
    pub const BOLD: Self = Self(0b0000_0001);
    /// Dim/faint.
    pub const DIM: Self = Self(0b0000_0010);
    /// Italic.
    pub const ITALIC: Self = Self(0b0000_0100);
    /// Underline.
    pub const UNDERLINE: Self = Self(0b0000_1000);
    /// Blink.
    pub const BLINK: Self = Self(0b0001_0000);
    /// Reverse video.
    pub const REVERSE: Self = Self(0b0010_0000);
    /// Hidden/invisible.
    pub const HIDDEN: Self = Self(0b0100_0000);
    /// Strikethrough.
    pub const STRIKETHROUGH: Self = Self(0b1000_0000);

    /// Check if an attribute is set.
    #[must_use]
    pub const fn contains(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }

    /// Set an attribute.
    #[must_use]
    pub const fn with(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Clear an attribute.
    #[must_use]
    pub const fn without(self, other: Self) -> Self {
        Self(self.0 & !other.0)
    }

    /// Set or clear an attribute based on a boolean.
    #[must_use]
    pub const fn set(self, attr: Self, enabled: bool) -> Self {
        if enabled {
            self.with(attr)
        } else {
            self.without(attr)
        }
    }
}

/// A single terminal cell.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Cell {
    /// The character in this cell (space if empty).
    pub ch: char,
    /// Foreground color (None = default).
    pub fg: Option<Color>,
    /// Background color (None = default).
    pub bg: Option<Color>,
    /// Text attributes.
    pub attrs: CellAttrs,
}

impl Default for Cell {
    fn default() -> Self {
        Self {
            ch: ' ',
            fg: None,
            bg: None,
            attrs: CellAttrs::NONE,
        }
    }
}

impl Cell {
    /// Create a new cell with the given character.
    #[must_use]
    pub const fn new(ch: char) -> Self {
        Self {
            ch,
            fg: None,
            bg: None,
            attrs: CellAttrs::NONE,
        }
    }

    /// Check if this cell is "empty" (space with default colors and no attrs).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.ch == ' ' && self.fg.is_none() && self.bg.is_none() && self.attrs.0 == 0
    }

    /// Check if this cell is the right-hand continuation of a wide character.
    #[must_use]
    pub fn is_wide_continuation(&self) -> bool {
        self.ch == WIDE_CONTINUATION
    }
}

/// Cursor shape for rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CursorShape {
    /// Block cursor (default).
    #[default]
    Block,
    /// Underline cursor.
    Underline,
    /// Bar/beam cursor.
    Bar,
}

/// Cursor state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cursor {
    /// Column (0-indexed).
    pub x: u16,
    /// Row (0-indexed).
    pub y: u16,
    /// Whether cursor is visible.
    pub visible: bool,
    /// Cursor shape.
    pub shape: CursorShape,
    /// Saved cursor position (DECSC/DECRC).
    pub saved: Option<(u16, u16)>,
}

impl Default for Cursor {
    fn default() -> Self {
        Self {
            x: 0,
            y: 0,
            visible: true,
            shape: CursorShape::Block,
            saved: None,
        }
    }
}

impl Cursor {
    /// Create a new cursor at the origin.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            x: 0,
            y: 0,
            visible: true,
            shape: CursorShape::Block,
            saved: None,
        }
    }
}

/// Terminal mode flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TerminalModes(u32);

impl TerminalModes {
    /// Auto-wrap mode (DECAWM).
    pub const WRAP: Self = Self(0b0000_0001);
    /// Origin mode (DECOM).
    pub const ORIGIN: Self = Self(0b0000_0010);
    /// Insert mode (IRM).
    pub const INSERT: Self = Self(0b0000_0100);
    /// Cursor visible (DECTCEM).
    pub const CURSOR_VISIBLE: Self = Self(0b0000_1000);
    /// Alternate screen buffer.
    pub const ALT_SCREEN: Self = Self(0b0001_0000);
    /// Bracketed paste mode.
    pub const BRACKETED_PASTE: Self = Self(0b0010_0000);
    /// Mouse tracking enabled.
    pub const MOUSE_TRACKING: Self = Self(0b0100_0000);
    /// Focus events enabled.
    pub const FOCUS_EVENTS: Self = Self(0b1000_0000);

    /// Check if a mode is set.
    #[must_use]
    pub const fn contains(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }

    /// Set a mode.
    #[must_use]
    pub const fn with(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Clear a mode.
    #[must_use]
    pub const fn without(self, other: Self) -> Self {
        Self(self.0 & !other.0)
    }

    /// Set or clear a mode based on a boolean.
    #[must_use]
    pub const fn set(self, mode: Self, enabled: bool) -> Self {
        if enabled {
            self.with(mode)
        } else {
            self.without(mode)
        }
    }
}

/// Region to clear.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClearRegion {
    /// Clear from cursor to end of screen.
    CursorToEnd,
    /// Clear from start of screen to cursor.
    StartToCursor,
    /// Clear entire screen.
    All,
    /// Clear from cursor to end of line.
    LineFromCursor,
    /// Clear from start of line to cursor.
    LineToCursor,
    /// Clear entire line.
    Line,
}

/// Dirty region tracking.
///
/// Uses a bitmap for efficient tracking of which cells have changed.
#[derive(Debug, Clone)]
pub struct DirtyRegion {
    /// Bitmap: 1 bit per cell, row-major order.
    bits: Vec<u64>,
    /// Width of the grid.
    width: u16,
    /// Height of the grid.
    height: u16,
    /// Whether any cell is dirty.
    any_dirty: bool,
}

impl DirtyRegion {
    /// Create a new dirty region for the given dimensions.
    #[must_use]
    pub fn new(width: u16, height: u16) -> Self {
        let total_cells = (width as usize) * (height as usize);
        let num_words = total_cells.div_ceil(64);
        Self {
            bits: vec![0; num_words],
            width,
            height,
            any_dirty: false,
        }
    }

    /// Mark a cell as dirty.
    pub fn mark(&mut self, x: u16, y: u16) {
        if x < self.width && y < self.height {
            let idx = (y as usize) * (self.width as usize) + (x as usize);
            let word = idx / 64;
            let bit = idx % 64;
            self.bits[word] |= 1 << bit;
            self.any_dirty = true;
        }
    }

    /// Mark a rectangular region as dirty.
    pub fn mark_rect(&mut self, x: u16, y: u16, w: u16, h: u16) {
        for row in y..y.saturating_add(h).min(self.height) {
            for col in x..x.saturating_add(w).min(self.width) {
                self.mark(col, row);
            }
        }
    }

    /// Mark the entire grid as dirty.
    pub fn mark_all(&mut self) {
        self.bits.fill(u64::MAX);
        self.any_dirty = true;
    }

    /// Check if a cell is dirty.
    #[must_use]
    pub fn is_dirty(&self, x: u16, y: u16) -> bool {
        if x < self.width && y < self.height {
            let idx = (y as usize) * (self.width as usize) + (x as usize);
            let word = idx / 64;
            let bit = idx % 64;
            (self.bits[word] >> bit) & 1 == 1
        } else {
            false
        }
    }

    /// Check if any cell is dirty.
    #[must_use]
    pub fn has_dirty(&self) -> bool {
        self.any_dirty
    }

    /// Clear all dirty flags.
    pub fn clear(&mut self) {
        self.bits.fill(0);
        self.any_dirty = false;
    }

    /// Resize the dirty region (clears all flags).
    pub fn resize(&mut self, width: u16, height: u16) {
        let total_cells = (width as usize) * (height as usize);
        let num_words = total_cells.div_ceil(64);
        self.bits.resize(num_words, 0);
        self.bits.fill(0);
        self.width = width;
        self.height = height;
        self.any_dirty = false;
    }
}

/// Terminal grid (visible area).
#[derive(Debug, Clone)]
pub struct Grid {
    /// Cells in row-major order.
    cells: Vec<Cell>,
    /// Width in columns.
    width: u16,
    /// Height in rows.
    height: u16,
    /// Per-row line flags (one per row).
    line_flags: Vec<LineFlag>,
}

impl Grid {
    /// Create a new grid with the given dimensions.
    #[must_use]
    pub fn new(width: u16, height: u16) -> Self {
        let width = width.max(1);
        let height = height.max(1);
        let size = (width as usize) * (height as usize);
        Self {
            cells: vec![Cell::default(); size],
            width,
            height,
            line_flags: vec![LineFlag::default(); height as usize],
        }
    }

    /// Get grid width.
    #[must_use]
    pub const fn width(&self) -> u16 {
        self.width
    }

    /// Get grid height.
    #[must_use]
    pub const fn height(&self) -> u16 {
        self.height
    }

    /// Get a reference to a cell.
    #[must_use]
    pub fn cell(&self, x: u16, y: u16) -> Option<&Cell> {
        if x < self.width && y < self.height {
            let idx = (y as usize) * (self.width as usize) + (x as usize);
            self.cells.get(idx)
        } else {
            None
        }
    }

    /// Get a mutable reference to a cell.
    pub fn cell_mut(&mut self, x: u16, y: u16) -> Option<&mut Cell> {
        if x < self.width && y < self.height {
            let idx = (y as usize) * (self.width as usize) + (x as usize);
            self.cells.get_mut(idx)
        } else {
            None
        }
    }

    /// Get the line flag for a row.
    #[must_use]
    pub fn line_flag(&self, y: u16) -> LineFlag {
        self.line_flags
            .get(y as usize)
            .copied()
            .unwrap_or(LineFlag::HardNewline)
    }

    /// Set the line flag for a row.
    pub fn set_line_flag(&mut self, y: u16, flag: LineFlag) {
        if let Some(f) = self.line_flags.get_mut(y as usize) {
            *f = flag;
        }
    }

    /// Get a slice of all line flags.
    #[must_use]
    pub fn line_flags(&self) -> &[LineFlag] {
        &self.line_flags
    }

    /// Clear a row to default cells.
    pub fn clear_row(&mut self, y: u16) {
        if y < self.height {
            let start = (y as usize) * (self.width as usize);
            let end = start + (self.width as usize);
            for cell in &mut self.cells[start..end] {
                *cell = Cell::default();
            }
            self.set_line_flag(y, LineFlag::HardNewline);
        }
    }

    /// Resize the grid, preserving content where possible.
    pub fn resize(&mut self, new_width: u16, new_height: u16) {
        let new_width = new_width.max(1);
        let new_height = new_height.max(1);

        if new_width == self.width && new_height == self.height {
            return;
        }

        let mut new_cells = vec![Cell::default(); (new_width as usize) * (new_height as usize)];

        // Copy existing content
        let copy_width = self.width.min(new_width) as usize;
        let copy_height = self.height.min(new_height) as usize;

        for y in 0..copy_height {
            let old_start = y * (self.width as usize);
            let new_start = y * (new_width as usize);
            new_cells[new_start..new_start + copy_width]
                .copy_from_slice(&self.cells[old_start..old_start + copy_width]);
        }

        self.cells = new_cells;
        self.width = new_width;

        // Resize line flags, preserving existing values
        self.line_flags
            .resize(new_height as usize, LineFlag::HardNewline);
        self.height = new_height;
    }

    /// Scroll the grid up by n lines, filling bottom with empty lines.
    /// Returns the lines that scrolled off the top along with their line flags.
    pub fn scroll_up(&mut self, n: u16) -> Vec<(Vec<Cell>, LineFlag)> {
        let n = n.min(self.height) as usize;
        if n == 0 {
            return Vec::new();
        }

        let mut scrolled_off = Vec::with_capacity(n);

        // Collect lines and their flags that will scroll off
        for y in 0..n {
            let start = y * (self.width as usize);
            let end = start + (self.width as usize);
            let flag = self.line_flags.get(y).copied().unwrap_or_default();
            scrolled_off.push((self.cells[start..end].to_vec(), flag));
        }

        // Shift remaining lines up
        let shift_count = (self.height as usize - n) * (self.width as usize);
        self.cells.copy_within(n * (self.width as usize).., 0);

        // Shift line flags up
        self.line_flags.drain(..n);
        self.line_flags
            .resize(self.height as usize, LineFlag::HardNewline);

        // Clear bottom lines
        for cell in &mut self.cells[shift_count..] {
            *cell = Cell::default();
        }

        scrolled_off
    }

    /// Scroll the grid down by n lines, filling top with empty lines.
    pub fn scroll_down(&mut self, n: u16) {
        let n = n.min(self.height) as usize;
        if n == 0 {
            return;
        }

        let width = self.width as usize;
        let height = self.height as usize;

        // Shift lines down
        self.cells.copy_within(0..(height - n) * width, n * width);

        // Clear top lines
        for cell in &mut self.cells[0..n * width] {
            *cell = Cell::default();
        }

        // Shift line flags down: truncate bottom, insert default at top
        self.line_flags.truncate(height.saturating_sub(n));
        for _ in 0..n {
            self.line_flags.insert(0, LineFlag::HardNewline);
        }
    }
}

/// Scrollback buffer.
#[derive(Debug, Clone)]
pub struct Scrollback {
    /// Lines in the scrollback buffer, paired with their line flags.
    lines: VecDeque<(Vec<Cell>, LineFlag)>,
    /// Maximum number of lines to keep.
    max_lines: usize,
}

impl Scrollback {
    /// Create a new scrollback buffer.
    #[must_use]
    pub fn new(max_lines: usize) -> Self {
        Self {
            lines: VecDeque::new(),
            max_lines,
        }
    }

    /// Add a line to the scrollback (defaults to [`LineFlag::HardNewline`]).
    pub fn push(&mut self, line: Vec<Cell>) {
        self.push_with_flag(line, LineFlag::HardNewline);
    }

    /// Add a line with an explicit line flag to the scrollback.
    pub fn push_with_flag(&mut self, line: Vec<Cell>, flag: LineFlag) {
        if self.max_lines == 0 {
            return;
        }

        while self.lines.len() >= self.max_lines {
            self.lines.pop_front();
        }
        self.lines.push_back((line, flag));
    }

    /// Add multiple lines to the scrollback.
    pub fn push_many(&mut self, lines: impl IntoIterator<Item = Vec<Cell>>) {
        for line in lines {
            self.push(line);
        }
    }

    /// Add multiple lines with their flags to the scrollback.
    pub fn push_many_with_flags(&mut self, lines: impl IntoIterator<Item = (Vec<Cell>, LineFlag)>) {
        for (line, flag) in lines {
            self.push_with_flag(line, flag);
        }
    }

    /// Get the number of lines in scrollback.
    #[must_use]
    pub fn len(&self) -> usize {
        self.lines.len()
    }

    /// Check if scrollback is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.lines.is_empty()
    }

    /// Get a line from scrollback (0 = most recent).
    #[must_use]
    pub fn line(&self, index: usize) -> Option<&[Cell]> {
        if index < self.lines.len() {
            self.lines
                .get(self.lines.len() - 1 - index)
                .map(|(cells, _)| cells.as_slice())
        } else {
            None
        }
    }

    /// Get a line and its flag from scrollback (0 = most recent).
    #[must_use]
    pub fn line_with_flag(&self, index: usize) -> Option<(&[Cell], LineFlag)> {
        if index < self.lines.len() {
            self.lines
                .get(self.lines.len() - 1 - index)
                .map(|(cells, flag)| (cells.as_slice(), *flag))
        } else {
            None
        }
    }

    /// Clear the scrollback.
    pub fn clear(&mut self) {
        self.lines.clear();
    }
}

/// Current pen state for writing characters.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Pen {
    /// Foreground color.
    pub fg: Option<Color>,
    /// Background color.
    pub bg: Option<Color>,
    /// Text attributes.
    pub attrs: CellAttrs,
}

impl Pen {
    /// Reset pen to defaults.
    pub fn reset(&mut self) {
        self.fg = None;
        self.bg = None;
        self.attrs = CellAttrs::NONE;
    }
}

/// Complete terminal state.
#[derive(Debug, Clone)]
pub struct TerminalState {
    /// The visible grid.
    grid: Grid,
    /// Cursor state.
    cursor: Cursor,
    /// Scrollback buffer.
    scrollback: Scrollback,
    /// Terminal modes.
    modes: TerminalModes,
    /// Dirty region tracking.
    dirty: DirtyRegion,
    /// Current pen for new characters.
    pen: Pen,
    /// Scroll region (top, bottom) - 0-indexed, inclusive.
    scroll_region: (u16, u16),
    /// Window title (from OSC sequences).
    title: String,
}

impl TerminalState {
    /// Create a new terminal state with the given dimensions.
    #[must_use]
    pub fn new(width: u16, height: u16) -> Self {
        let width = width.max(1);
        let height = height.max(1);
        Self {
            grid: Grid::new(width, height),
            cursor: Cursor::new(),
            scrollback: Scrollback::new(1000), // Default 1000 lines
            modes: TerminalModes::WRAP.with(TerminalModes::CURSOR_VISIBLE),
            dirty: DirtyRegion::new(width, height),
            pen: Pen::default(),
            scroll_region: (0, height.saturating_sub(1)),
            title: String::new(),
        }
    }

    /// Create with custom scrollback limit.
    #[must_use]
    pub fn with_scrollback(width: u16, height: u16, max_scrollback: usize) -> Self {
        let mut state = Self::new(width, height);
        state.scrollback = Scrollback::new(max_scrollback);
        state
    }

    /// Get grid width.
    #[must_use]
    pub fn width(&self) -> u16 {
        self.grid.width()
    }

    /// Get grid height.
    #[must_use]
    pub fn height(&self) -> u16 {
        self.grid.height()
    }

    /// Get a reference to the grid.
    #[must_use]
    pub const fn grid(&self) -> &Grid {
        &self.grid
    }

    /// Get a reference to a cell.
    #[must_use]
    pub fn cell(&self, x: u16, y: u16) -> Option<&Cell> {
        self.grid.cell(x, y)
    }

    /// Get a mutable reference to a cell (marks as dirty).
    pub fn cell_mut(&mut self, x: u16, y: u16) -> Option<&mut Cell> {
        self.dirty.mark(x, y);
        self.grid.cell_mut(x, y)
    }

    /// Get cursor state.
    #[must_use]
    pub const fn cursor(&self) -> &Cursor {
        &self.cursor
    }

    /// Get terminal modes.
    #[must_use]
    pub const fn modes(&self) -> TerminalModes {
        self.modes
    }

    /// Get the dirty region.
    #[must_use]
    pub const fn dirty(&self) -> &DirtyRegion {
        &self.dirty
    }

    /// Get the scrollback buffer.
    #[must_use]
    pub const fn scrollback(&self) -> &Scrollback {
        &self.scrollback
    }

    /// Get the current pen.
    #[must_use]
    pub const fn pen(&self) -> &Pen {
        &self.pen
    }

    /// Get the window title.
    #[must_use]
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Mark all cells as clean.
    pub fn mark_clean(&mut self) {
        self.dirty.clear();
    }

    /// Move cursor to absolute position (clamped to bounds).
    pub fn move_cursor(&mut self, x: u16, y: u16) {
        self.cursor.x = x.min(self.grid.width().saturating_sub(1));
        self.cursor.y = y.min(self.grid.height().saturating_sub(1));
    }

    /// Move cursor relative to current position.
    pub fn move_cursor_relative(&mut self, dx: i16, dy: i16) {
        let new_x = (self.cursor.x as i32 + dx as i32)
            .max(0)
            .min(self.grid.width() as i32 - 1) as u16;
        let new_y = (self.cursor.y as i32 + dy as i32)
            .max(0)
            .min(self.grid.height() as i32 - 1) as u16;
        self.cursor.x = new_x;
        self.cursor.y = new_y;
    }

    /// Set cursor visibility.
    pub fn set_cursor_visible(&mut self, visible: bool) {
        self.cursor.visible = visible;
        self.modes = self.modes.set(TerminalModes::CURSOR_VISIBLE, visible);
    }

    /// Save cursor position.
    pub fn save_cursor(&mut self) {
        self.cursor.saved = Some((self.cursor.x, self.cursor.y));
    }

    /// Restore cursor position.
    pub fn restore_cursor(&mut self) {
        if let Some((x, y)) = self.cursor.saved {
            self.move_cursor(x, y);
        }
    }

    /// Write a character at the cursor position.
    pub fn put_char(&mut self, ch: char) {
        let x = self.cursor.x;
        let y = self.cursor.y;

        if let Some(cell) = self.grid.cell_mut(x, y) {
            cell.ch = ch;
            cell.fg = self.pen.fg;
            cell.bg = self.pen.bg;
            cell.attrs = self.pen.attrs;
            self.dirty.mark(x, y);
        }

        // Advance cursor
        self.cursor.x += 1;

        // Handle wrap
        if self.cursor.x >= self.grid.width() {
            if self.modes.contains(TerminalModes::WRAP) {
                // Mark the current row as soft-wrapped
                self.grid.set_line_flag(y, LineFlag::SoftWrap);
                self.cursor.x = 0;
                self.cursor.y += 1;

                // Scroll if needed
                if self.cursor.y > self.scroll_region.1 {
                    self.scroll_up(1);
                    self.cursor.y = self.scroll_region.1;
                }
            } else {
                self.cursor.x = self.grid.width().saturating_sub(1);
            }
        }
    }

    /// Write a wide (double-width) character at the cursor position.
    ///
    /// Places the character in the current cell and a [`WIDE_CONTINUATION`]
    /// marker in the next cell. Advances cursor by 2 columns.
    pub fn put_char_wide(&mut self, ch: char) {
        let x = self.cursor.x;
        let y = self.cursor.y;
        let width = self.grid.width();

        // Need at least 2 columns remaining; if only 1, wrap first
        if x >= width.saturating_sub(1) && self.modes.contains(TerminalModes::WRAP) {
            // Not enough room — mark current row as soft-wrapped and wrap
            self.grid.set_line_flag(y, LineFlag::SoftWrap);
            self.cursor.x = 0;
            self.cursor.y += 1;
            if self.cursor.y > self.scroll_region.1 {
                self.scroll_up(1);
                self.cursor.y = self.scroll_region.1;
            }
        }

        let x = self.cursor.x;
        let y = self.cursor.y;

        // Place the wide character
        if let Some(cell) = self.grid.cell_mut(x, y) {
            cell.ch = ch;
            cell.fg = self.pen.fg;
            cell.bg = self.pen.bg;
            cell.attrs = self.pen.attrs;
            self.dirty.mark(x, y);
        }

        // Place the continuation marker
        if let Some(cell) = self.grid.cell_mut(x.saturating_add(1), y) {
            cell.ch = WIDE_CONTINUATION;
            cell.fg = self.pen.fg;
            cell.bg = self.pen.bg;
            cell.attrs = self.pen.attrs;
            self.dirty.mark(x.saturating_add(1), y);
        }

        // Advance cursor by 2
        self.cursor.x = self.cursor.x.saturating_add(2);

        // Handle wrap after placing both cells
        if self.cursor.x >= width {
            if self.modes.contains(TerminalModes::WRAP) {
                self.grid.set_line_flag(y, LineFlag::SoftWrap);
                self.cursor.x = 0;
                self.cursor.y += 1;
                if self.cursor.y > self.scroll_region.1 {
                    self.scroll_up(1);
                    self.cursor.y = self.scroll_region.1;
                }
            } else {
                self.cursor.x = width.saturating_sub(1);
            }
        }
    }

    /// Process a newline (LF): move cursor down, mark current row as hard newline.
    pub fn newline(&mut self) {
        let y = self.cursor.y;
        self.grid.set_line_flag(y, LineFlag::HardNewline);

        self.cursor.y += 1;
        if self.cursor.y > self.scroll_region.1 {
            self.scroll_up(1);
            self.cursor.y = self.scroll_region.1;
        }
    }

    /// Process a carriage return (CR): move cursor to column 0.
    pub fn carriage_return(&mut self) {
        self.cursor.x = 0;
    }

    /// Scroll the screen up by n lines.
    pub fn scroll_up(&mut self, n: u16) {
        let (top, bottom) = self.scroll_region;
        let n = n.min(bottom.saturating_sub(top) + 1);

        if n == 0 {
            return;
        }

        // If scrolling the entire screen, use grid method
        if top == 0 && bottom == self.grid.height().saturating_sub(1) {
            let scrolled_off = self.grid.scroll_up(n);
            self.scrollback.push_many_with_flags(scrolled_off);
        } else {
            // Scroll within region
            let width = self.grid.width() as usize;
            for y in top..=bottom.saturating_sub(n) {
                let src_y = y + n;
                if src_y <= bottom {
                    // Copy row src_y to row y
                    let src_start = (src_y as usize) * width;
                    let dst_start = (y as usize) * width;
                    self.grid
                        .cells
                        .copy_within(src_start..src_start + width, dst_start);
                    // Shift line flags too
                    let src_flag = self.grid.line_flag(src_y);
                    self.grid.set_line_flag(y, src_flag);
                }
            }
            // Clear bottom lines of region
            for y in (bottom + 1).saturating_sub(n)..=bottom {
                self.grid.clear_row(y);
            }
        }

        // Mark entire scroll region as dirty
        self.dirty
            .mark_rect(0, top, self.grid.width(), bottom - top + 1);
    }

    /// Scroll the screen down by n lines.
    pub fn scroll_down(&mut self, n: u16) {
        let (top, bottom) = self.scroll_region;
        let n = n.min(bottom.saturating_sub(top) + 1);

        if n == 0 {
            return;
        }

        if top == 0 && bottom == self.grid.height().saturating_sub(1) {
            self.grid.scroll_down(n);
        } else {
            // Scroll within region
            let width = self.grid.width() as usize;
            for y in (top + n..=bottom).rev() {
                let src_y = y - n;
                if src_y >= top {
                    // Copy row src_y to row y
                    let src_start = (src_y as usize) * width;
                    let dst_start = (y as usize) * width;
                    self.grid
                        .cells
                        .copy_within(src_start..src_start + width, dst_start);
                    // Shift line flags too
                    let src_flag = self.grid.line_flag(src_y);
                    self.grid.set_line_flag(y, src_flag);
                }
            }
            // Clear top lines of region
            for y in top..top + n {
                self.grid.clear_row(y);
            }
        }

        self.dirty
            .mark_rect(0, top, self.grid.width(), bottom - top + 1);
    }

    /// Clear a region of the screen.
    pub fn clear_region(&mut self, region: ClearRegion) {
        let (x, y) = (self.cursor.x, self.cursor.y);
        let width = self.grid.width();
        let height = self.grid.height();

        match region {
            ClearRegion::CursorToEnd => {
                // Clear from cursor to end of line
                for col in x..width {
                    if let Some(cell) = self.grid.cell_mut(col, y) {
                        *cell = Cell::default();
                        self.dirty.mark(col, y);
                    }
                }
                // Clear remaining lines
                for row in y + 1..height {
                    self.grid.clear_row(row);
                }
                self.dirty
                    .mark_rect(0, y + 1, width, height.saturating_sub(y).saturating_sub(1));
            }
            ClearRegion::StartToCursor => {
                // Clear lines before cursor
                for row in 0..y {
                    self.grid.clear_row(row);
                }
                self.dirty.mark_rect(0, 0, width, y);
                // Clear from start of line to cursor
                for col in 0..=x {
                    if let Some(cell) = self.grid.cell_mut(col, y) {
                        *cell = Cell::default();
                        self.dirty.mark(col, y);
                    }
                }
            }
            ClearRegion::All => {
                for row in 0..height {
                    self.grid.clear_row(row);
                }
                self.dirty.mark_all();
            }
            ClearRegion::LineFromCursor => {
                for col in x..width {
                    if let Some(cell) = self.grid.cell_mut(col, y) {
                        *cell = Cell::default();
                        self.dirty.mark(col, y);
                    }
                }
            }
            ClearRegion::LineToCursor => {
                for col in 0..=x {
                    if let Some(cell) = self.grid.cell_mut(col, y) {
                        *cell = Cell::default();
                        self.dirty.mark(col, y);
                    }
                }
            }
            ClearRegion::Line => {
                self.grid.clear_row(y);
                self.dirty.mark_rect(0, y, width, 1);
            }
        }
    }

    /// Set a terminal mode.
    pub fn set_mode(&mut self, mode: TerminalModes, enabled: bool) {
        self.modes = self.modes.set(mode, enabled);
    }

    /// Set the scroll region.
    pub fn set_scroll_region(&mut self, top: u16, bottom: u16) {
        let top = top.min(self.grid.height().saturating_sub(1));
        let bottom = bottom.min(self.grid.height().saturating_sub(1)).max(top);
        self.scroll_region = (top, bottom);
    }

    /// Reset scroll region to full screen.
    pub fn reset_scroll_region(&mut self) {
        self.scroll_region = (0, self.grid.height().saturating_sub(1));
    }

    /// Set the window title.
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = title.into();
    }

    /// Resize the terminal.
    pub fn resize(&mut self, width: u16, height: u16) {
        let width = width.max(1);
        let height = height.max(1);

        self.grid.resize(width, height);
        self.dirty.resize(width, height);
        self.dirty.mark_all();

        // Clamp cursor
        self.cursor.x = self.cursor.x.min(width.saturating_sub(1));
        self.cursor.y = self.cursor.y.min(height.saturating_sub(1));

        // Reset scroll region
        self.scroll_region = (0, height.saturating_sub(1));
    }

    /// Get a mutable reference to the pen.
    pub fn pen_mut(&mut self) -> &mut Pen {
        &mut self.pen
    }

    /// Reset the terminal to initial state.
    pub fn reset(&mut self) {
        let width = self.grid.width();
        let height = self.grid.height();

        self.grid = Grid::new(width, height);
        self.cursor = Cursor::new();
        self.modes = TerminalModes::WRAP.with(TerminalModes::CURSOR_VISIBLE);
        self.pen = Pen::default();
        self.scroll_region = (0, height.saturating_sub(1));
        self.title.clear();
        self.dirty.clear();
        self.dirty.mark_all();
    }

    /// Extract text from a selection range in the visible grid.
    ///
    /// Coordinates are clamped to grid bounds.  The selection is normalized
    /// so that `(start_x, start_y)` is always before `(end_x, end_y)` in
    /// reading order.
    ///
    /// # Copy extraction semantics
    ///
    /// - **Soft-wrapped rows** are joined without a newline (the wrap was
    ///   caused by terminal width, not by the content).
    /// - **Hard-newline rows** emit a `\n` between rows.
    /// - **Trailing whitespace** on each logical line is trimmed.
    /// - **Wide character continuation cells** ([`WIDE_CONTINUATION`]) are
    ///   skipped so each wide glyph appears exactly once.
    /// - **Grapheme clusters** stored as individual combining chars are
    ///   emitted in cell order.
    #[must_use]
    pub fn extract_text(&self, start_x: u16, start_y: u16, end_x: u16, end_y: u16) -> String {
        self.grid.extract_text(start_x, start_y, end_x, end_y)
    }
}

// =========================================================================
// Copy extraction
// =========================================================================

impl Grid {
    /// Extract text from a rectangular selection range.
    ///
    /// See [`TerminalState::extract_text`] for full semantics.
    #[must_use]
    pub fn extract_text(&self, start_x: u16, start_y: u16, end_x: u16, end_y: u16) -> String {
        // Normalize so start is before end in reading order.
        let (sx, sy, ex, ey) = if (start_y, start_x) <= (end_y, end_x) {
            (start_x, start_y, end_x, end_y)
        } else {
            (end_x, end_y, start_x, start_y)
        };

        // Clamp to grid bounds.
        let sy = sy.min(self.height.saturating_sub(1));
        let ey = ey.min(self.height.saturating_sub(1));
        let sx = sx.min(self.width.saturating_sub(1));
        let ex = ex.min(self.width.saturating_sub(1));

        if sy == ey {
            // Single-line selection: extract [sx..=ex] and trim.
            return self.extract_row_range(sy, sx, ex);
        }

        let mut result = String::new();

        // First row: sx .. end of row
        let first = self.extract_row_range(sy, sx, self.width.saturating_sub(1));
        result.push_str(&first);

        // Separator after first row depends on its line flag.
        if self.line_flag(sy) == LineFlag::HardNewline {
            result.push('\n');
        }

        // Middle rows (full rows)
        for y in (sy + 1)..ey {
            let row = self.extract_row_range(y, 0, self.width.saturating_sub(1));
            result.push_str(&row);
            if self.line_flag(y) == LineFlag::HardNewline {
                result.push('\n');
            }
        }

        // Last row: 0 .. ex
        let last = self.extract_row_range(ey, 0, ex);
        result.push_str(&last);

        // Trim trailing newlines — matches terminal emulator copy behavior
        // where trailing empty lines are not included in the clipboard.
        let trimmed = result.trim_end_matches('\n');
        trimmed.to_owned()
    }

    /// Extract and trim a single row segment `[col_start..=col_end]`.
    fn extract_row_range(&self, y: u16, col_start: u16, col_end: u16) -> String {
        let mut s = String::new();
        let start = col_start.min(self.width.saturating_sub(1));
        let end = col_end.min(self.width.saturating_sub(1));

        for x in start..=end {
            if let Some(cell) = self.cell(x, y) {
                // Skip wide-char continuation cells.
                if cell.is_wide_continuation() {
                    continue;
                }
                s.push(cell.ch);
            }
        }

        // Trim trailing whitespace (spaces only; preserve other content).
        let trimmed = s.trim_end_matches(' ');
        trimmed.to_owned()
    }
}

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

    #[test]
    fn test_cell_default() {
        let cell = Cell::default();
        assert_eq!(cell.ch, ' ');
        assert!(cell.fg.is_none());
        assert!(cell.bg.is_none());
        assert_eq!(cell.attrs.0, 0);
        assert!(cell.is_empty());
    }

    #[test]
    fn test_cell_attrs() {
        let attrs = CellAttrs::BOLD.with(CellAttrs::ITALIC);
        assert!(attrs.contains(CellAttrs::BOLD));
        assert!(attrs.contains(CellAttrs::ITALIC));
        assert!(!attrs.contains(CellAttrs::UNDERLINE));

        let attrs = attrs.without(CellAttrs::BOLD);
        assert!(!attrs.contains(CellAttrs::BOLD));
        assert!(attrs.contains(CellAttrs::ITALIC));
    }

    #[test]
    fn test_cursor_movement() {
        let mut state = TerminalState::new(80, 24);
        assert_eq!(state.cursor().x, 0);
        assert_eq!(state.cursor().y, 0);

        state.move_cursor(10, 5);
        assert_eq!(state.cursor().x, 10);
        assert_eq!(state.cursor().y, 5);

        // Test clamping
        state.move_cursor(100, 50);
        assert_eq!(state.cursor().x, 79);
        assert_eq!(state.cursor().y, 23);
    }

    #[test]
    fn test_cursor_relative_movement() {
        let mut state = TerminalState::new(80, 24);
        state.move_cursor(10, 10);

        state.move_cursor_relative(-5, 3);
        assert_eq!(state.cursor().x, 5);
        assert_eq!(state.cursor().y, 13);

        // Test clamping at boundaries
        state.move_cursor_relative(-100, -100);
        assert_eq!(state.cursor().x, 0);
        assert_eq!(state.cursor().y, 0);
    }

    #[test]
    fn test_put_char() {
        let mut state = TerminalState::new(80, 24);
        state.put_char('A');

        assert_eq!(state.cell(0, 0).unwrap().ch, 'A');
        assert_eq!(state.cursor().x, 1);
        assert!(state.dirty().is_dirty(0, 0));
    }

    #[test]
    fn test_scroll_up() {
        let mut state = TerminalState::new(10, 5);

        // Fill first line with 'A's
        for i in 0..10 {
            state.move_cursor(i, 0);
            state.put_char('A');
        }

        // Fill second line with 'B's
        for i in 0..10 {
            state.move_cursor(i, 1);
            state.put_char('B');
        }

        state.scroll_up(1);

        // First line should now have 'B's
        assert_eq!(state.cell(0, 0).unwrap().ch, 'B');
        // Scrollback should have 'A's
        assert_eq!(state.scrollback().line(0).unwrap()[0].ch, 'A');
    }

    #[test]
    fn test_scroll_down() {
        let mut state = TerminalState::new(10, 5);

        // Fill first line with 'A's
        for i in 0..10 {
            state.move_cursor(i, 0);
            state.put_char('A');
        }

        state.scroll_down(1);

        // First line should be empty
        assert_eq!(state.cell(0, 0).unwrap().ch, ' ');
        // Second line should have 'A's
        assert_eq!(state.cell(0, 1).unwrap().ch, 'A');
    }

    #[test]
    fn test_wrap_mode() {
        let mut state = TerminalState::new(5, 3);
        assert!(state.modes().contains(TerminalModes::WRAP));

        // Write past end of line
        for ch in "HELLO WORLD".chars() {
            state.put_char(ch);
        }

        // Should have wrapped
        assert_eq!(state.cell(0, 0).unwrap().ch, 'H');
        assert_eq!(state.cell(4, 0).unwrap().ch, 'O');
        assert_eq!(state.cell(0, 1).unwrap().ch, ' ');
        assert_eq!(state.cell(0, 2).unwrap().ch, 'D');
    }

    #[test]
    fn test_resize() {
        let mut state = TerminalState::new(10, 5);
        state.move_cursor(5, 3);
        state.put_char('X');

        state.resize(20, 10);

        assert_eq!(state.width(), 20);
        assert_eq!(state.height(), 10);
        // Content should be preserved
        assert_eq!(state.cell(5, 3).unwrap().ch, 'X');
    }

    #[test]
    fn test_resize_smaller() {
        let mut state = TerminalState::new(10, 5);
        state.move_cursor(8, 4);

        state.resize(5, 3);

        // Cursor should be clamped
        assert_eq!(state.cursor().x, 4);
        assert_eq!(state.cursor().y, 2);
    }

    #[test]
    fn test_dirty_tracking() {
        let mut state = TerminalState::new(10, 5);
        assert!(!state.dirty().has_dirty());

        state.put_char('A');
        assert!(state.dirty().has_dirty());
        assert!(state.dirty().is_dirty(0, 0));
        assert!(!state.dirty().is_dirty(1, 0));

        state.mark_clean();
        assert!(!state.dirty().has_dirty());
    }

    #[test]
    fn test_clear_region_all() {
        let mut state = TerminalState::new(10, 5);

        // Fill with content
        for i in 0..10 {
            state.move_cursor(i, 0);
            state.put_char('A');
        }

        state.clear_region(ClearRegion::All);

        // All cells should be empty
        for y in 0..5 {
            for x in 0..10 {
                assert!(state.cell(x, y).unwrap().is_empty());
            }
        }
    }

    #[test]
    fn test_clear_region_line() {
        let mut state = TerminalState::new(10, 5);

        // Fill line 2 with content
        for i in 0..10 {
            state.move_cursor(i, 2);
            state.put_char('A');
        }

        state.move_cursor(5, 2);
        state.clear_region(ClearRegion::Line);

        // Line 2 should be empty
        for x in 0..10 {
            assert!(state.cell(x, 2).unwrap().is_empty());
        }
    }

    #[test]
    fn test_save_restore_cursor() {
        let mut state = TerminalState::new(80, 24);
        state.move_cursor(10, 5);
        state.save_cursor();

        state.move_cursor(50, 20);
        assert_eq!(state.cursor().x, 50);

        state.restore_cursor();
        assert_eq!(state.cursor().x, 10);
        assert_eq!(state.cursor().y, 5);
    }

    #[test]
    fn test_scroll_region() {
        let mut state = TerminalState::new(10, 10);
        state.set_scroll_region(2, 7);

        // Fill line 2 with 'A's
        for i in 0..10 {
            state.move_cursor(i, 2);
            state.put_char('A');
        }

        // Scroll within region
        state.scroll_up(1);

        // Line 2 should now be empty (line 3 moved up)
        // Actually, let me check: line 3 content moved to line 2
        // Since line 3 was empty, line 2 should now be empty
        // But line 2 had 'A's, they should have scrolled into scrollback
        // No wait, scroll region doesn't go to scrollback if not at top

        // Reset and test properly
        state.reset();
        state.set_scroll_region(2, 7);

        for i in 0..10 {
            state.move_cursor(i, 2);
            state.put_char('A');
        }
        for i in 0..10 {
            state.move_cursor(i, 3);
            state.put_char('B');
        }

        state.scroll_up(1);

        // Line 2 should now have 'B's (from line 3)
        assert_eq!(state.cell(0, 2).unwrap().ch, 'B');
        // Line 7 should be cleared
        assert!(state.cell(0, 7).unwrap().is_empty());
    }

    #[test]
    fn test_scrollback() {
        let mut state = TerminalState::with_scrollback(10, 3, 10);

        // Disable wrap to prevent auto-scroll when filling last column
        state.set_mode(TerminalModes::WRAP, false);

        // Fill all lines
        for y in 0..3 {
            for x in 0..10 {
                state.move_cursor(x, y);
                state.put_char(char::from(b'A' + y as u8));
            }
        }

        // Scroll up
        state.scroll_up(1);

        // Check scrollback has the 'A' line
        assert_eq!(state.scrollback().len(), 1);
        assert_eq!(state.scrollback().line(0).unwrap()[0].ch, 'A');
    }

    #[test]
    fn test_pen_attributes() {
        let mut state = TerminalState::new(10, 5);

        state.pen_mut().attrs = CellAttrs::BOLD;
        state.pen_mut().fg = Some(Color::rgb(255, 0, 0));
        state.put_char('X');

        let cell = state.cell(0, 0).unwrap();
        assert!(cell.attrs.contains(CellAttrs::BOLD));
        assert_eq!(cell.fg, Some(Color::rgb(255, 0, 0)));
    }

    #[test]
    fn test_terminal_modes() {
        let modes = TerminalModes::WRAP.with(TerminalModes::CURSOR_VISIBLE);
        assert!(modes.contains(TerminalModes::WRAP));
        assert!(modes.contains(TerminalModes::CURSOR_VISIBLE));
        assert!(!modes.contains(TerminalModes::ALT_SCREEN));

        let modes = modes.set(TerminalModes::ALT_SCREEN, true);
        assert!(modes.contains(TerminalModes::ALT_SCREEN));

        let modes = modes.without(TerminalModes::WRAP);
        assert!(!modes.contains(TerminalModes::WRAP));
    }

    #[test]
    fn test_grid_resize_preserves_content() {
        let mut grid = Grid::new(10, 5);

        // Put 'X' at position (3, 2)
        if let Some(cell) = grid.cell_mut(3, 2) {
            cell.ch = 'X';
        }

        grid.resize(20, 10);

        assert_eq!(grid.cell(3, 2).unwrap().ch, 'X');
    }

    #[test]
    fn test_minimum_size() {
        let state = TerminalState::new(0, 0);
        assert_eq!(state.width(), 1);
        assert_eq!(state.height(), 1);
    }

    #[test]
    fn test_reset() {
        let mut state = TerminalState::new(10, 5);
        state.move_cursor(5, 3);
        state.put_char('X');
        state.set_title("Test");

        state.reset();

        assert_eq!(state.cursor().x, 0);
        assert_eq!(state.cursor().y, 0);
        assert!(state.cell(5, 3).unwrap().is_empty());
        assert!(state.title().is_empty());
    }

    #[test]
    fn test_cell_attrs_set() {
        let attrs = CellAttrs::NONE;
        let bold = attrs.set(CellAttrs::BOLD, true);
        assert!(bold.contains(CellAttrs::BOLD));
        let cleared = bold.set(CellAttrs::BOLD, false);
        assert!(!cleared.contains(CellAttrs::BOLD));
    }

    #[test]
    fn test_dirty_region_mark_rect() {
        let mut dirty = DirtyRegion::new(10, 5);
        dirty.mark_rect(2, 1, 3, 2);
        assert!(dirty.is_dirty(2, 1));
        assert!(dirty.is_dirty(4, 2));
        assert!(!dirty.is_dirty(5, 1));
        assert!(!dirty.is_dirty(2, 0));
        assert!(dirty.has_dirty());
    }

    #[test]
    fn test_dirty_region_mark_all() {
        let mut dirty = DirtyRegion::new(4, 4);
        dirty.mark_all();
        assert!(dirty.is_dirty(0, 0));
        assert!(dirty.is_dirty(3, 3));
        assert!(dirty.has_dirty());
    }

    #[test]
    fn test_dirty_region_resize_clears() {
        let mut dirty = DirtyRegion::new(10, 5);
        dirty.mark_all();
        assert!(dirty.has_dirty());
        dirty.resize(20, 10);
        assert!(!dirty.has_dirty());
        assert!(!dirty.is_dirty(0, 0));
    }

    #[test]
    fn test_scrollback_empty_and_clear() {
        let mut sb = Scrollback::new(10);
        assert!(sb.is_empty());
        assert_eq!(sb.len(), 0);
        sb.push(vec![Cell::default()]);
        assert!(!sb.is_empty());
        sb.clear();
        assert!(sb.is_empty());
    }

    #[test]
    fn test_scrollback_max_lines_zero_drops() {
        let mut sb = Scrollback::new(0);
        sb.push(vec![Cell::default()]);
        assert!(sb.is_empty());
    }

    #[test]
    fn test_scrollback_push_many_overflow() {
        let mut sb = Scrollback::new(3);
        sb.push_many((0..5).map(|i| vec![Cell::new((b'A' + i) as char)]));
        assert_eq!(sb.len(), 3);
        // Most recent is index 0
        assert_eq!(sb.line(0).unwrap()[0].ch, 'E');
        assert_eq!(sb.line(2).unwrap()[0].ch, 'C');
        assert!(sb.line(3).is_none());
    }

    #[test]
    fn test_pen_reset() {
        let mut pen = Pen {
            fg: Some(Color::rgb(255, 0, 0)),
            bg: Some(Color::rgb(0, 255, 0)),
            attrs: CellAttrs::BOLD,
        };
        pen.reset();
        assert_eq!(pen.fg, None);
        assert_eq!(pen.bg, None);
        assert_eq!(pen.attrs, CellAttrs::NONE);
    }

    #[test]
    fn test_set_cursor_visible() {
        let mut state = TerminalState::new(10, 5);
        assert!(state.cursor().visible);
        state.set_cursor_visible(false);
        assert!(!state.cursor().visible);
        assert!(!state.modes().contains(TerminalModes::CURSOR_VISIBLE));
        state.set_cursor_visible(true);
        assert!(state.cursor().visible);
        assert!(state.modes().contains(TerminalModes::CURSOR_VISIBLE));
    }

    #[test]
    fn test_clear_region_cursor_to_end() {
        let mut state = TerminalState::new(5, 3);
        for ch in ['A', 'B', 'C', 'D', 'E'] {
            state.put_char(ch);
        }
        state.move_cursor(0, 1);
        for ch in ['F', 'G', 'H', 'I', 'J'] {
            state.put_char(ch);
        }
        // Cursor at col 2, row 1 — clear from cursor to end of screen
        state.move_cursor(2, 0);
        state.clear_region(ClearRegion::CursorToEnd);
        assert_eq!(state.cell(0, 0).unwrap().ch, 'A');
        assert_eq!(state.cell(1, 0).unwrap().ch, 'B');
        assert!(state.cell(2, 0).unwrap().is_empty());
        assert!(state.cell(0, 1).unwrap().is_empty());
    }

    #[test]
    fn test_clear_region_start_to_cursor() {
        let mut state = TerminalState::new(5, 3);
        for ch in ['A', 'B', 'C', 'D', 'E'] {
            state.put_char(ch);
        }
        state.move_cursor(0, 1);
        for ch in ['F', 'G', 'H', 'I', 'J'] {
            state.put_char(ch);
        }
        state.move_cursor(2, 1);
        state.clear_region(ClearRegion::StartToCursor);
        // Row 0 should be cleared
        assert!(state.cell(0, 0).unwrap().is_empty());
        // Row 1, cols 0..=2 cleared
        assert!(state.cell(2, 1).unwrap().is_empty());
        // Row 1, col 3 preserved
        assert_eq!(state.cell(3, 1).unwrap().ch, 'I');
    }

    #[test]
    fn test_clear_region_line_from_cursor() {
        let mut state = TerminalState::new(5, 2);
        for ch in ['A', 'B', 'C', 'D', 'E'] {
            state.put_char(ch);
        }
        state.move_cursor(2, 0);
        state.clear_region(ClearRegion::LineFromCursor);
        assert_eq!(state.cell(0, 0).unwrap().ch, 'A');
        assert_eq!(state.cell(1, 0).unwrap().ch, 'B');
        assert!(state.cell(2, 0).unwrap().is_empty());
        assert!(state.cell(4, 0).unwrap().is_empty());
    }

    #[test]
    fn test_clear_region_line_to_cursor() {
        let mut state = TerminalState::new(5, 2);
        for ch in ['A', 'B', 'C', 'D', 'E'] {
            state.put_char(ch);
        }
        state.move_cursor(2, 0);
        state.clear_region(ClearRegion::LineToCursor);
        assert!(state.cell(0, 0).unwrap().is_empty());
        assert!(state.cell(2, 0).unwrap().is_empty());
        assert_eq!(state.cell(3, 0).unwrap().ch, 'D');
        assert_eq!(state.cell(4, 0).unwrap().ch, 'E');
    }

    // --- Edge case tests (bd-3halp) ---

    #[test]
    fn test_cell_new_constructor() {
        let cell = Cell::new('Z');
        assert_eq!(cell.ch, 'Z');
        assert!(cell.fg.is_none());
        assert!(cell.bg.is_none());
        assert_eq!(cell.attrs, CellAttrs::NONE);
        // Cell::new with space is empty
        assert!(Cell::new(' ').is_empty());
        // Cell::new with non-space is not empty
        assert!(!Cell::new('Z').is_empty());
    }

    #[test]
    fn test_cell_is_empty_with_attrs_is_not_empty() {
        let cell = Cell {
            ch: ' ',
            fg: None,
            bg: None,
            attrs: CellAttrs::BOLD,
        };
        assert!(!cell.is_empty(), "space with BOLD should not be empty");
    }

    #[test]
    fn test_cell_is_empty_with_fg_is_not_empty() {
        let cell = Cell {
            ch: ' ',
            fg: Some(Color::rgb(255, 0, 0)),
            bg: None,
            attrs: CellAttrs::NONE,
        };
        assert!(!cell.is_empty(), "space with fg color should not be empty");
    }

    #[test]
    fn test_cell_is_empty_with_bg_is_not_empty() {
        let cell = Cell {
            ch: ' ',
            fg: None,
            bg: Some(Color::rgb(0, 0, 255)),
            attrs: CellAttrs::NONE,
        };
        assert!(!cell.is_empty(), "space with bg color should not be empty");
    }

    #[test]
    fn test_cell_attrs_all_flags() {
        let all = CellAttrs::BOLD
            .with(CellAttrs::DIM)
            .with(CellAttrs::ITALIC)
            .with(CellAttrs::UNDERLINE)
            .with(CellAttrs::BLINK)
            .with(CellAttrs::REVERSE)
            .with(CellAttrs::HIDDEN)
            .with(CellAttrs::STRIKETHROUGH);
        assert!(all.contains(CellAttrs::BOLD));
        assert!(all.contains(CellAttrs::DIM));
        assert!(all.contains(CellAttrs::ITALIC));
        assert!(all.contains(CellAttrs::UNDERLINE));
        assert!(all.contains(CellAttrs::BLINK));
        assert!(all.contains(CellAttrs::REVERSE));
        assert!(all.contains(CellAttrs::HIDDEN));
        assert!(all.contains(CellAttrs::STRIKETHROUGH));
        assert_eq!(all.0, 0xFF);
    }

    #[test]
    fn test_cursor_shape_variants() {
        assert_eq!(CursorShape::default(), CursorShape::Block);
        let _ = CursorShape::Underline;
        let _ = CursorShape::Bar;
    }

    #[test]
    fn test_cursor_new_const() {
        let c = Cursor::new();
        assert_eq!(c.x, 0);
        assert_eq!(c.y, 0);
        assert!(c.visible);
        assert_eq!(c.shape, CursorShape::Block);
        assert!(c.saved.is_none());
    }

    #[test]
    fn test_dirty_region_mark_out_of_bounds_is_noop() {
        let mut dirty = DirtyRegion::new(5, 5);
        dirty.mark(10, 10);
        assert!(!dirty.has_dirty());
        dirty.mark(5, 0); // x == width, out of bounds
        assert!(!dirty.has_dirty());
        dirty.mark(0, 5); // y == height, out of bounds
        assert!(!dirty.has_dirty());
    }

    #[test]
    fn test_dirty_region_is_dirty_out_of_bounds_returns_false() {
        let mut dirty = DirtyRegion::new(5, 5);
        dirty.mark_all();
        assert!(!dirty.is_dirty(5, 0));
        assert!(!dirty.is_dirty(0, 5));
        assert!(!dirty.is_dirty(100, 100));
    }

    #[test]
    fn test_dirty_region_mark_rect_clamps_to_bounds() {
        let mut dirty = DirtyRegion::new(5, 5);
        // Rect extends past boundary — should not panic
        dirty.mark_rect(3, 3, 10, 10);
        assert!(dirty.is_dirty(4, 4));
        assert!(!dirty.is_dirty(2, 2));
        assert!(dirty.has_dirty());
    }

    #[test]
    fn test_grid_cell_out_of_bounds() {
        let grid = Grid::new(5, 5);
        assert!(grid.cell(5, 0).is_none());
        assert!(grid.cell(0, 5).is_none());
        assert!(grid.cell(100, 100).is_none());
    }

    #[test]
    fn test_grid_cell_mut_out_of_bounds() {
        let mut grid = Grid::new(5, 5);
        assert!(grid.cell_mut(5, 0).is_none());
        assert!(grid.cell_mut(0, 5).is_none());
    }

    #[test]
    fn test_grid_clear_row_out_of_bounds_is_noop() {
        let mut grid = Grid::new(5, 5);
        if let Some(cell) = grid.cell_mut(0, 0) {
            cell.ch = 'X';
        }
        grid.clear_row(10); // out of bounds — should not panic
        assert_eq!(grid.cell(0, 0).unwrap().ch, 'X'); // unchanged
    }

    #[test]
    fn test_grid_minimum_size_enforced() {
        let grid = Grid::new(0, 0);
        assert_eq!(grid.width(), 1);
        assert_eq!(grid.height(), 1);
        assert!(grid.cell(0, 0).is_some());
    }

    #[test]
    fn test_grid_resize_same_dimensions_is_noop() {
        let mut grid = Grid::new(5, 5);
        if let Some(cell) = grid.cell_mut(2, 2) {
            cell.ch = 'Q';
        }
        grid.resize(5, 5);
        assert_eq!(grid.cell(2, 2).unwrap().ch, 'Q');
    }

    #[test]
    fn test_grid_scroll_up_zero_returns_empty() {
        let mut grid = Grid::new(5, 5);
        let scrolled = grid.scroll_up(0);
        assert!(scrolled.is_empty());
    }

    #[test]
    fn test_grid_scroll_up_exceeds_height_clamped() {
        let mut grid = Grid::new(3, 3);
        if let Some(cell) = grid.cell_mut(0, 0) {
            cell.ch = 'A';
        }
        if let Some(cell) = grid.cell_mut(0, 1) {
            cell.ch = 'B';
        }
        if let Some(cell) = grid.cell_mut(0, 2) {
            cell.ch = 'C';
        }
        let scrolled = grid.scroll_up(100);
        // Should scroll all 3 lines
        assert_eq!(scrolled.len(), 3);
        assert_eq!(scrolled[0].0[0].ch, 'A');
        assert_eq!(scrolled[1].0[0].ch, 'B');
        assert_eq!(scrolled[2].0[0].ch, 'C');
        // All cells should now be empty
        assert!(grid.cell(0, 0).unwrap().is_empty());
    }

    #[test]
    fn test_grid_scroll_down_zero_is_noop() {
        let mut grid = Grid::new(5, 3);
        if let Some(cell) = grid.cell_mut(0, 0) {
            cell.ch = 'A';
        }
        grid.scroll_down(0);
        assert_eq!(grid.cell(0, 0).unwrap().ch, 'A');
    }

    #[test]
    fn test_grid_scroll_down_exceeds_height_clamped() {
        let mut grid = Grid::new(3, 3);
        if let Some(cell) = grid.cell_mut(0, 0) {
            cell.ch = 'A';
        }
        grid.scroll_down(100);
        // All cells should be empty after scrolling everything off
        assert!(grid.cell(0, 0).unwrap().is_empty());
        assert!(grid.cell(0, 1).unwrap().is_empty());
        assert!(grid.cell(0, 2).unwrap().is_empty());
    }

    #[test]
    fn test_scrollback_line_out_of_bounds() {
        let mut sb = Scrollback::new(5);
        sb.push(vec![Cell::new('A')]);
        assert!(sb.line(0).is_some());
        assert!(sb.line(1).is_none());
        assert!(sb.line(100).is_none());
    }

    #[test]
    fn test_scrollback_overflow_evicts_oldest() {
        let mut sb = Scrollback::new(2);
        sb.push(vec![Cell::new('A')]);
        sb.push(vec![Cell::new('B')]);
        sb.push(vec![Cell::new('C')]);
        assert_eq!(sb.len(), 2);
        // Most recent (index 0) is 'C', oldest (index 1) is 'B'
        assert_eq!(sb.line(0).unwrap()[0].ch, 'C');
        assert_eq!(sb.line(1).unwrap()[0].ch, 'B');
    }

    #[test]
    fn test_restore_cursor_when_nothing_saved_is_noop() {
        let mut state = TerminalState::new(10, 5);
        state.move_cursor(5, 3);
        state.restore_cursor(); // nothing saved
        assert_eq!(state.cursor().x, 5);
        assert_eq!(state.cursor().y, 3);
    }

    #[test]
    fn test_put_char_no_wrap_stays_at_edge() {
        let mut state = TerminalState::new(3, 2);
        state.set_mode(TerminalModes::WRAP, false);
        state.put_char('A');
        state.put_char('B');
        state.put_char('C'); // fills col 2
        state.put_char('D'); // no wrap, cursor stays at col 2

        assert_eq!(state.cursor().x, 2);
        assert_eq!(state.cursor().y, 0);
        assert_eq!(state.cell(0, 0).unwrap().ch, 'A');
        assert_eq!(state.cell(1, 0).unwrap().ch, 'B');
        // 'C' written at (2,0), then 'D' overwrites at clamped (2,0)
        assert_eq!(state.cell(2, 0).unwrap().ch, 'D');
    }

    #[test]
    fn test_put_char_wrap_triggers_scroll_at_bottom() {
        let mut state = TerminalState::new(2, 2);
        // Fill all 4 cells: row 0 = AB, row 1 = CD
        for ch in ['A', 'B', 'C', 'D'] {
            state.put_char(ch);
        }
        // Now at beginning of a new row, scroll should have happened
        assert_eq!(state.cursor().y, 1);
        // Row 0 should now have 'C', 'D' (scrolled up)
        assert_eq!(state.cell(0, 0).unwrap().ch, 'C');
        assert_eq!(state.cell(1, 0).unwrap().ch, 'D');
        // Scrollback should have original row 0
        assert_eq!(state.scrollback().line(0).unwrap()[0].ch, 'A');
    }

    #[test]
    fn test_put_char_applies_pen_bg() {
        let mut state = TerminalState::new(5, 2);
        state.pen_mut().bg = Some(Color::rgb(0, 128, 0));
        state.pen_mut().attrs = CellAttrs::ITALIC.with(CellAttrs::UNDERLINE);
        state.put_char('Z');

        let cell = state.cell(0, 0).unwrap();
        assert_eq!(cell.ch, 'Z');
        assert_eq!(cell.bg, Some(Color::rgb(0, 128, 0)));
        assert!(cell.attrs.contains(CellAttrs::ITALIC));
        assert!(cell.attrs.contains(CellAttrs::UNDERLINE));
    }

    #[test]
    fn test_terminal_state_cell_mut_marks_dirty() {
        let mut state = TerminalState::new(5, 5);
        assert!(!state.dirty().has_dirty());
        if let Some(cell) = state.cell_mut(2, 3) {
            cell.ch = 'X';
        }
        assert!(state.dirty().is_dirty(2, 3));
        assert!(state.dirty().has_dirty());
    }

    #[test]
    fn test_with_scrollback_constructor() {
        let state = TerminalState::with_scrollback(10, 5, 50);
        assert_eq!(state.width(), 10);
        assert_eq!(state.height(), 5);
        // Verify scrollback limit works by pushing more than 50 lines
        // (we test the limit indirectly via the Scrollback struct)
    }

    #[test]
    fn test_set_scroll_region_clamped() {
        let mut state = TerminalState::new(10, 5);
        // Set region beyond grid bounds
        state.set_scroll_region(100, 200);
        // bottom should be clamped to height-1 = 4, top should be clamped to 4
        // Since bottom.max(top), both end at 4
        assert_eq!(state.cursor().x, 0); // cursor unchanged
    }

    #[test]
    fn test_reset_scroll_region() {
        let mut state = TerminalState::new(10, 5);
        state.set_scroll_region(1, 3);
        state.reset_scroll_region();
        // After reset, scrolling full screen should work normally
        state.set_mode(TerminalModes::WRAP, false);
        for x in 0..10 {
            state.move_cursor(x, 0);
            state.put_char('A');
        }
        state.scroll_up(1);
        // Line 0 should be empty (scrolled off), row 0 is now what was row 1
        assert!(state.cell(0, 0).unwrap().is_empty());
        assert_eq!(state.scrollback().len(), 1);
    }

    #[test]
    fn test_scroll_up_zero_is_noop() {
        let mut state = TerminalState::new(5, 3);
        state.put_char('A');
        state.scroll_up(0);
        assert_eq!(state.cell(0, 0).unwrap().ch, 'A');
        assert!(state.scrollback().is_empty());
    }

    #[test]
    fn test_scroll_down_zero_is_noop() {
        let mut state = TerminalState::new(5, 3);
        state.put_char('A');
        state.scroll_down(0);
        assert_eq!(state.cell(0, 0).unwrap().ch, 'A');
    }

    #[test]
    fn test_scroll_down_within_region() {
        let mut state = TerminalState::new(5, 6);
        state.set_scroll_region(1, 4);

        state.set_mode(TerminalModes::WRAP, false);
        for x in 0..5 {
            state.move_cursor(x, 1);
            state.put_char('A');
        }
        for x in 0..5 {
            state.move_cursor(x, 2);
            state.put_char('B');
        }

        state.scroll_down(1);

        // Row 1 should be cleared (top of region)
        assert!(state.cell(0, 1).unwrap().is_empty());
        // Row 2 should have 'A' (shifted down from row 1)
        assert_eq!(state.cell(0, 2).unwrap().ch, 'A');
        // Row 3 should have 'B' (shifted down from row 2)
        assert_eq!(state.cell(0, 3).unwrap().ch, 'B');
        // Row 0 (outside region) should be unaffected
        assert!(state.cell(0, 0).unwrap().is_empty());
        // Row 5 (outside region) should be unaffected
        assert!(state.cell(0, 5).unwrap().is_empty());
    }

    #[test]
    fn test_scroll_up_within_region_no_scrollback() {
        let mut state = TerminalState::new(5, 6);
        state.set_scroll_region(1, 4);

        state.set_mode(TerminalModes::WRAP, false);
        for x in 0..5 {
            state.move_cursor(x, 1);
            state.put_char('A');
        }
        for x in 0..5 {
            state.move_cursor(x, 2);
            state.put_char('B');
        }

        state.scroll_up(1);

        // Row 1 should have 'B' (shifted up from row 2)
        assert_eq!(state.cell(0, 1).unwrap().ch, 'B');
        // Row 4 should be cleared (bottom of region)
        assert!(state.cell(0, 4).unwrap().is_empty());
        // Scrollback should be empty (region scroll doesn't add to scrollback)
        assert!(state.scrollback().is_empty());
    }

    #[test]
    fn test_set_mode_individual_flags() {
        let mut state = TerminalState::new(5, 5);
        state.set_mode(TerminalModes::ALT_SCREEN, true);
        assert!(state.modes().contains(TerminalModes::ALT_SCREEN));

        state.set_mode(TerminalModes::BRACKETED_PASTE, true);
        assert!(state.modes().contains(TerminalModes::BRACKETED_PASTE));
        assert!(state.modes().contains(TerminalModes::ALT_SCREEN));

        state.set_mode(TerminalModes::ALT_SCREEN, false);
        assert!(!state.modes().contains(TerminalModes::ALT_SCREEN));
        assert!(state.modes().contains(TerminalModes::BRACKETED_PASTE));
    }

    #[test]
    fn test_terminal_modes_mouse_tracking_and_focus_events() {
        let modes = TerminalModes::MOUSE_TRACKING.with(TerminalModes::FOCUS_EVENTS);
        assert!(modes.contains(TerminalModes::MOUSE_TRACKING));
        assert!(modes.contains(TerminalModes::FOCUS_EVENTS));
        assert!(!modes.contains(TerminalModes::INSERT));
    }

    #[test]
    fn test_terminal_modes_origin_and_insert() {
        let modes = TerminalModes::ORIGIN.with(TerminalModes::INSERT);
        assert!(modes.contains(TerminalModes::ORIGIN));
        assert!(modes.contains(TerminalModes::INSERT));
    }

    #[test]
    fn test_resize_to_minimum() {
        let mut state = TerminalState::new(10, 10);
        state.move_cursor(5, 5);
        state.resize(0, 0);
        assert_eq!(state.width(), 1);
        assert_eq!(state.height(), 1);
        assert_eq!(state.cursor().x, 0);
        assert_eq!(state.cursor().y, 0);
    }

    #[test]
    fn test_resize_marks_all_dirty() {
        let mut state = TerminalState::new(10, 5);
        state.mark_clean();
        assert!(!state.dirty().has_dirty());
        state.resize(20, 10);
        assert!(state.dirty().has_dirty());
        assert!(state.dirty().is_dirty(0, 0));
    }

    #[test]
    fn test_resize_resets_scroll_region() {
        let mut state = TerminalState::new(10, 10);
        state.set_scroll_region(2, 7);
        state.resize(10, 20);
        // After resize, scroll region should encompass the full new height
        // We verify by scrolling — if scroll region is full screen, scrollback gets lines
        state.set_mode(TerminalModes::WRAP, false);
        for x in 0..10 {
            state.move_cursor(x, 0);
            state.put_char('A');
        }
        state.scroll_up(1);
        assert_eq!(state.scrollback().len(), 1);
    }

    #[test]
    fn test_set_title() {
        let mut state = TerminalState::new(10, 5);
        assert!(state.title().is_empty());
        state.set_title("Hello World");
        assert_eq!(state.title(), "Hello World");
        state.set_title(String::from("Another Title"));
        assert_eq!(state.title(), "Another Title");
    }

    #[test]
    fn test_grid_resize_shrinks_preserves_visible_content() {
        let mut grid = Grid::new(10, 10);
        // Put content in corners
        if let Some(cell) = grid.cell_mut(0, 0) {
            cell.ch = 'A';
        }
        if let Some(cell) = grid.cell_mut(9, 9) {
            cell.ch = 'Z';
        }
        if let Some(cell) = grid.cell_mut(2, 2) {
            cell.ch = 'M';
        }

        grid.resize(5, 5);
        assert_eq!(grid.cell(0, 0).unwrap().ch, 'A');
        assert_eq!(grid.cell(2, 2).unwrap().ch, 'M');
        // (9,9) is outside new bounds
        assert!(grid.cell(9, 9).is_none());
    }

    #[test]
    fn test_move_cursor_relative_large_positive() {
        let mut state = TerminalState::new(10, 10);
        state.move_cursor(0, 0);
        state.move_cursor_relative(i16::MAX, i16::MAX);
        assert_eq!(state.cursor().x, 9);
        assert_eq!(state.cursor().y, 9);
    }

    #[test]
    fn test_move_cursor_relative_large_negative() {
        let mut state = TerminalState::new(10, 10);
        state.move_cursor(5, 5);
        state.move_cursor_relative(i16::MIN, i16::MIN);
        assert_eq!(state.cursor().x, 0);
        assert_eq!(state.cursor().y, 0);
    }

    #[test]
    fn test_reset_clears_pen_and_modes() {
        let mut state = TerminalState::new(10, 5);
        state.pen_mut().fg = Some(Color::rgb(255, 0, 0));
        state.pen_mut().attrs = CellAttrs::BOLD;
        state.set_mode(TerminalModes::ALT_SCREEN, true);

        state.reset();

        assert_eq!(state.pen().fg, None);
        assert_eq!(state.pen().attrs, CellAttrs::NONE);
        // After reset, modes should be back to default (WRAP + CURSOR_VISIBLE)
        assert!(state.modes().contains(TerminalModes::WRAP));
        assert!(state.modes().contains(TerminalModes::CURSOR_VISIBLE));
        assert!(!state.modes().contains(TerminalModes::ALT_SCREEN));
    }

    #[test]
    fn test_save_cursor_overwrites_previous_save() {
        let mut state = TerminalState::new(20, 20);
        state.move_cursor(5, 5);
        state.save_cursor();
        state.move_cursor(10, 10);
        state.save_cursor();
        state.move_cursor(0, 0);
        state.restore_cursor();
        assert_eq!(state.cursor().x, 10);
        assert_eq!(state.cursor().y, 10);
    }

    #[test]
    fn test_restore_cursor_clamps_after_resize() {
        let mut state = TerminalState::new(20, 20);
        state.move_cursor(15, 15);
        state.save_cursor();
        state.resize(5, 5);
        state.restore_cursor();
        // Saved position (15,15) should be clamped to (4,4)
        assert_eq!(state.cursor().x, 4);
        assert_eq!(state.cursor().y, 4);
    }

    #[test]
    fn test_dirty_region_clear_then_mark() {
        let mut dirty = DirtyRegion::new(5, 5);
        dirty.mark(2, 2);
        assert!(dirty.has_dirty());
        dirty.clear();
        assert!(!dirty.has_dirty());
        assert!(!dirty.is_dirty(2, 2));
        dirty.mark(3, 3);
        assert!(dirty.has_dirty());
        assert!(dirty.is_dirty(3, 3));
    }

    #[test]
    fn test_grid_scroll_up_preserves_remaining_content() {
        let mut grid = Grid::new(3, 4);
        for y in 0..4u16 {
            if let Some(cell) = grid.cell_mut(0, y) {
                cell.ch = (b'A' + y as u8) as char;
            }
        }
        let scrolled = grid.scroll_up(2);
        assert_eq!(scrolled.len(), 2);
        assert_eq!(scrolled[0].0[0].ch, 'A');
        assert_eq!(scrolled[1].0[0].ch, 'B');
        // Remaining content shifted up
        assert_eq!(grid.cell(0, 0).unwrap().ch, 'C');
        assert_eq!(grid.cell(0, 1).unwrap().ch, 'D');
        // Bottom rows cleared
        assert!(grid.cell(0, 2).unwrap().is_empty());
        assert!(grid.cell(0, 3).unwrap().is_empty());
    }

    #[test]
    fn test_grid_scroll_down_preserves_remaining_content() {
        let mut grid = Grid::new(3, 4);
        for y in 0..4u16 {
            if let Some(cell) = grid.cell_mut(0, y) {
                cell.ch = (b'A' + y as u8) as char;
            }
        }
        grid.scroll_down(2);
        // Top rows cleared
        assert!(grid.cell(0, 0).unwrap().is_empty());
        assert!(grid.cell(0, 1).unwrap().is_empty());
        // Content shifted down
        assert_eq!(grid.cell(0, 2).unwrap().ch, 'A');
        assert_eq!(grid.cell(0, 3).unwrap().ch, 'B');
    }

    #[test]
    fn test_cursor_shape_in_cursor_state() {
        let state = TerminalState::new(10, 5);
        assert_eq!(state.cursor().shape, CursorShape::Block);
    }

    #[test]
    fn test_terminal_modes_set_method() {
        let m = TerminalModes::default();
        assert!(!m.contains(TerminalModes::WRAP));
        let m = m.set(TerminalModes::WRAP, true);
        assert!(m.contains(TerminalModes::WRAP));
        let m = m.set(TerminalModes::WRAP, false);
        assert!(!m.contains(TerminalModes::WRAP));
    }

    #[test]
    fn test_pen_default() {
        let pen = Pen::default();
        assert_eq!(pen.fg, None);
        assert_eq!(pen.bg, None);
        assert_eq!(pen.attrs, CellAttrs::NONE);
    }

    // =====================================================================
    // LineFlag tests
    // =====================================================================

    #[test]
    fn line_flag_default_is_hard_newline() {
        assert_eq!(LineFlag::default(), LineFlag::HardNewline);
    }

    #[test]
    fn grid_line_flags_initialized_to_hard_newline() {
        let grid = Grid::new(10, 5);
        for y in 0..5 {
            assert_eq!(grid.line_flag(y), LineFlag::HardNewline);
        }
    }

    #[test]
    fn grid_set_and_get_line_flag() {
        let mut grid = Grid::new(10, 3);
        grid.set_line_flag(1, LineFlag::SoftWrap);
        assert_eq!(grid.line_flag(0), LineFlag::HardNewline);
        assert_eq!(grid.line_flag(1), LineFlag::SoftWrap);
        assert_eq!(grid.line_flag(2), LineFlag::HardNewline);
    }

    #[test]
    fn grid_line_flag_out_of_bounds_returns_default() {
        let grid = Grid::new(10, 3);
        assert_eq!(grid.line_flag(99), LineFlag::HardNewline);
    }

    #[test]
    fn grid_clear_row_resets_line_flag() {
        let mut grid = Grid::new(10, 3);
        grid.set_line_flag(1, LineFlag::SoftWrap);
        grid.clear_row(1);
        assert_eq!(grid.line_flag(1), LineFlag::HardNewline);
    }

    #[test]
    fn grid_resize_preserves_line_flags() {
        let mut grid = Grid::new(10, 3);
        grid.set_line_flag(0, LineFlag::SoftWrap);
        grid.set_line_flag(2, LineFlag::SoftWrap);

        // Grow
        grid.resize(10, 5);
        assert_eq!(grid.line_flag(0), LineFlag::SoftWrap);
        assert_eq!(grid.line_flag(2), LineFlag::SoftWrap);
        assert_eq!(grid.line_flag(3), LineFlag::HardNewline);
        assert_eq!(grid.line_flag(4), LineFlag::HardNewline);

        // Shrink
        grid.resize(10, 2);
        assert_eq!(grid.line_flag(0), LineFlag::SoftWrap);
        assert_eq!(grid.line_flag(1), LineFlag::HardNewline);
    }

    // =====================================================================
    // Wide character tests
    // =====================================================================

    #[test]
    fn wide_continuation_sentinel_is_recognized() {
        let cell = Cell::new(WIDE_CONTINUATION);
        assert!(cell.is_wide_continuation());
        assert!(!cell.is_empty());
    }

    #[test]
    fn normal_cell_is_not_wide_continuation() {
        assert!(!Cell::new('A').is_wide_continuation());
        assert!(!Cell::new(' ').is_wide_continuation());
        assert!(!Cell::default().is_wide_continuation());
    }

    #[test]
    fn put_char_wide_places_continuation_marker() {
        let mut state = TerminalState::new(10, 3);
        state.put_char_wide('æ¼¢');

        assert_eq!(state.cell(0, 0).unwrap().ch, 'æ¼¢');
        assert!(state.cell(1, 0).unwrap().is_wide_continuation());
        assert_eq!(state.cursor().x, 2);
    }

    #[test]
    fn put_char_wide_wraps_when_not_enough_room() {
        let mut state = TerminalState::new(5, 3);
        // Fill 4 cells to leave only 1 column
        for ch in ['A', 'B', 'C', 'D'] {
            state.put_char(ch);
        }
        assert_eq!(state.cursor().x, 4);

        // Place wide char — needs 2 columns, only 1 available → wrap
        state.put_char_wide('æ¼¢');

        // Row 0 should be soft-wrapped
        assert_eq!(state.grid().line_flag(0), LineFlag::SoftWrap);
        // Wide char placed at start of row 1
        assert_eq!(state.cell(0, 1).unwrap().ch, 'æ¼¢');
        assert!(state.cell(1, 1).unwrap().is_wide_continuation());
    }

    // =====================================================================
    // Soft wrap tracking in put_char
    // =====================================================================

    #[test]
    fn put_char_marks_soft_wrap_on_auto_wrap() {
        let mut state = TerminalState::new(3, 3);
        // Fill row 0: "ABC" → wraps to row 1
        for ch in ['A', 'B', 'C'] {
            state.put_char(ch);
        }
        assert_eq!(state.grid().line_flag(0), LineFlag::SoftWrap);
        assert_eq!(state.cursor().y, 1);
    }

    #[test]
    fn newline_marks_hard_newline() {
        let mut state = TerminalState::new(10, 3);
        state.put_char('A');
        state.newline();
        assert_eq!(state.grid().line_flag(0), LineFlag::HardNewline);
        assert_eq!(state.cursor().y, 1);
    }

    #[test]
    fn carriage_return_resets_column() {
        let mut state = TerminalState::new(10, 3);
        state.put_char('A');
        state.put_char('B');
        state.carriage_return();
        assert_eq!(state.cursor().x, 0);
        assert_eq!(state.cursor().y, 0);
    }

    // =====================================================================
    // Copy extraction tests
    // =====================================================================

    /// Helper: write a string into the terminal at the cursor.
    fn write_str(state: &mut TerminalState, s: &str) {
        for ch in s.chars() {
            match ch {
                '\n' => state.newline(),
                '\r' => state.carriage_return(),
                _ => state.put_char(ch),
            }
        }
    }

    #[test]
    fn extract_single_row_trims_trailing_spaces() {
        let mut state = TerminalState::new(10, 3);
        write_str(&mut state, "Hello");
        // Row 0 has "Hello     " (5 chars + 5 spaces)
        let text = state.extract_text(0, 0, 9, 0);
        assert_eq!(text, "Hello");
    }

    #[test]
    fn extract_partial_row() {
        let mut state = TerminalState::new(10, 3);
        write_str(&mut state, "ABCDEFGHIJ");
        let text = state.extract_text(2, 0, 5, 0);
        assert_eq!(text, "CDEF");
    }

    #[test]
    fn extract_multi_row_with_hard_newlines() {
        let mut state = TerminalState::new(10, 5);
        write_str(&mut state, "Hello");
        state.newline();
        state.carriage_return();
        write_str(&mut state, "World");

        let text = state.extract_text(0, 0, 9, 1);
        assert_eq!(text, "Hello\nWorld");
    }

    #[test]
    fn extract_soft_wrapped_rows_joined() {
        // Terminal width 5, text "HelloWorld" wraps at column 5
        let mut state = TerminalState::new(5, 3);
        write_str(&mut state, "HelloWorld");
        // Row 0: "Hello" (soft-wrapped)
        // Row 1: "World"
        assert_eq!(state.grid().line_flag(0), LineFlag::SoftWrap);

        let text = state.extract_text(0, 0, 4, 1);
        assert_eq!(text, "HelloWorld");
    }

    #[test]
    fn extract_mixed_soft_and_hard_wraps() {
        let mut state = TerminalState::new(5, 5);
        // "ABCDE" auto-wraps (soft), then "FG" + hard newline, then "HI"
        write_str(&mut state, "ABCDE");
        // Row 0 is soft-wrapped, cursor at (0,1)
        write_str(&mut state, "FG");
        state.newline();
        state.carriage_return();
        write_str(&mut state, "HI");

        let text = state.extract_text(0, 0, 4, 2);
        assert_eq!(text, "ABCDEFG\nHI");
    }

    #[test]
    fn extract_reversed_selection_normalizes() {
        let mut state = TerminalState::new(10, 3);
        write_str(&mut state, "Hello");
        state.newline();
        state.carriage_return();
        write_str(&mut state, "World");

        // End before start — should normalize
        let text = state.extract_text(4, 1, 0, 0);
        assert_eq!(text, "Hello\nWorld");
    }

    #[test]
    fn extract_wide_chars_emitted_once() {
        let mut state = TerminalState::new(10, 3);
        state.put_char_wide('æ¼¢');
        state.put_char_wide('å­—');

        let text = state.extract_text(0, 0, 9, 0);
        assert_eq!(text, "漢字");
    }

    #[test]
    fn extract_wide_chars_partial_selection() {
        let mut state = TerminalState::new(10, 3);
        state.put_char('A');
        state.put_char_wide('æ¼¢');
        state.put_char('B');

        // Select columns 1-3: should get 'æ¼¢' (col 1) + skip continuation (col 2) + 'B' (col 3)
        let text = state.extract_text(1, 0, 3, 0);
        assert_eq!(text, "æ¼¢B");
    }

    #[test]
    fn extract_empty_grid_returns_empty() {
        let state = TerminalState::new(10, 3);
        let text = state.extract_text(0, 0, 9, 2);
        assert_eq!(text, "");
    }

    #[test]
    fn extract_single_cell() {
        let mut state = TerminalState::new(10, 3);
        write_str(&mut state, "X");
        let text = state.extract_text(0, 0, 0, 0);
        assert_eq!(text, "X");
    }

    #[test]
    fn extract_out_of_bounds_clamped() {
        let mut state = TerminalState::new(5, 3);
        write_str(&mut state, "Hi");
        // Request coords beyond grid bounds
        let text = state.extract_text(0, 0, 100, 100);
        assert_eq!(text, "Hi");
    }

    #[test]
    fn scroll_up_preserves_line_flags_in_scrollback() {
        let mut state = TerminalState::new(5, 3);
        // Fill row 0 causing soft wrap
        write_str(&mut state, "ABCDE");
        assert_eq!(state.grid().line_flag(0), LineFlag::SoftWrap);

        // Write enough to scroll row 0 into scrollback
        state.newline();
        state.carriage_return();
        write_str(&mut state, "FG");
        state.newline();
        state.carriage_return();
        write_str(&mut state, "HI");
        state.newline();
        state.carriage_return();
        write_str(&mut state, "JK");

        // Row 0 should have scrolled into scrollback
        let (_, flag) = state
            .scrollback()
            .line_with_flag(0)
            .unwrap_or((&[], LineFlag::HardNewline));
        // The most recent scrollback line's flag depends on scroll order
        // but the soft-wrapped row should be preserved somewhere
        assert!(
            flag == LineFlag::SoftWrap || flag == LineFlag::HardNewline,
            "scrollback should store the line flag"
        );
    }

    #[test]
    fn extract_combining_marks_preserved() {
        let mut state = TerminalState::new(10, 3);
        // Base char + combining acute accent
        state.put_char('e');
        state.put_char('\u{0301}'); // combining acute
        state.put_char('!');

        let text = state.extract_text(0, 0, 9, 0);
        assert_eq!(text, "e\u{0301}!");
    }

    #[test]
    fn extract_scrollback_line_flag_round_trip() {
        let mut sb = Scrollback::new(10);
        sb.push_with_flag(vec![Cell::new('A')], LineFlag::SoftWrap);
        sb.push_with_flag(vec![Cell::new('B')], LineFlag::HardNewline);

        let (_, flag0) = sb.line_with_flag(0).unwrap(); // most recent = B
        let (_, flag1) = sb.line_with_flag(1).unwrap(); // older = A
        assert_eq!(flag0, LineFlag::HardNewline);
        assert_eq!(flag1, LineFlag::SoftWrap);
    }

    #[test]
    fn grid_scroll_down_shifts_line_flags() {
        let mut grid = Grid::new(5, 4);
        grid.set_line_flag(0, LineFlag::SoftWrap);
        grid.set_line_flag(1, LineFlag::SoftWrap);

        grid.scroll_down(1);

        // Row 0 (new) should be HardNewline
        assert_eq!(grid.line_flag(0), LineFlag::HardNewline);
        // Old row 0 (SoftWrap) should now be row 1
        assert_eq!(grid.line_flag(1), LineFlag::SoftWrap);
        // Old row 1 (SoftWrap) should now be row 2
        assert_eq!(grid.line_flag(2), LineFlag::SoftWrap);
    }

    #[test]
    fn grid_line_flags_slice() {
        let mut grid = Grid::new(5, 3);
        grid.set_line_flag(0, LineFlag::SoftWrap);
        let flags = grid.line_flags();
        assert_eq!(flags.len(), 3);
        assert_eq!(flags[0], LineFlag::SoftWrap);
        assert_eq!(flags[1], LineFlag::HardNewline);
        assert_eq!(flags[2], LineFlag::HardNewline);
    }
}