termina 0.3.3

A cross-platform VT manipulation library
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
//! Strongly typed Control Sequence Introducer (CSI) escape sequences.
//!
//! Each enum in this module represents a family of CSI sequences and implements [`Display`] where
//! Termina knows how to emit the sequence. This keeps terminal control code explicit while still
//! allowing applications to write the formatted value directly to any [`std::io::Write`] target.
//!
//! # Implementation Notes
//!
//! This module is adapted from [termwiz's CSI support], but it was incrementally pulled across
//! rather than copied wholesale. Termina keeps a curated subset, removes conversion traits, and
//! adds focused extensions such as Contour theme reporting and the [`SgrAttributes`] /
//! [`SgrModifiers`] grouping types. TermWiz has a more complete set of CSI escape sequences.
//!
//! [termwiz's CSI support]: https://docs.rs/termwiz/latest/termwiz/escape/enum.Csi.html

use std::{
    fmt::{self, Display},
    num::NonZeroU16,
};

use crate::{
    event::Modifiers,
    style::{Blink, ColorSpec, CursorStyle, Font, Intensity, RgbaColor, Underline, VerticalAlign},
    OneBased,
};

/// A Control Sequence Introducer command.
///
/// Formatting writes the `ESC [` introducer followed by the command family payload. CSI commands
/// are the main terminal protocol surface for cursor movement, text styling, mode changes, device
/// reports, mouse reports, and window operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Csi {
    /// Select Graphic Rendition commands described by [`Sgr`].
    ///
    /// These `CSI ... m` commands change how subsequent cells are rendered.
    Sgr(Sgr),

    /// Cursor commands described by [`Cursor`].
    ///
    /// This family covers cursor movement, cursor shape, margins, and position reports.
    Cursor(Cursor),

    /// Text and display editing commands described by [`Edit`].
    ///
    /// This family covers insert, delete, erase, repeat, and scroll operations.
    Edit(Edit),

    /// Terminal mode commands described by [`Mode`].
    ///
    /// This family covers setting, resetting, saving, restoring, querying, and reporting terminal
    /// modes.
    Mode(Mode),

    /// Mouse input reports described by [`MouseReport`].
    ///
    /// Modes such as [`DecPrivateModeCode::MouseTracking`],
    /// [`DecPrivateModeCode::ButtonEventMouse`], and [`DecPrivateModeCode::AnyEventMouse`]
    /// control when terminals send these reports.
    Mouse(MouseReport),

    /// Kitty keyboard protocol commands described by [`Keyboard`].
    ///
    /// This family covers flag query, report, push, pop, and set commands.
    Keyboard(Keyboard),

    /// Device and status commands described by [`Device`].
    ///
    /// This family covers device attributes, terminal status, terminal identity, and terminal
    /// parameters.
    Device(Device),

    /// Window commands described by [`Window`].
    ///
    /// This family covers window manipulation and reports, mostly from xterm-compatible
    /// extensions.
    Window(Box<Window>),
}

impl Display for Csi {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // This here is the "control sequence introducer" (CSI):
        f.write_str(super::CSI)?;
        match self {
            Self::Sgr(sgr) => write!(f, "{sgr}m"),
            Self::Cursor(cursor) => cursor.fmt(f),
            Self::Edit(edit) => edit.fmt(f),
            Self::Mode(mode) => mode.fmt(f),
            Self::Mouse(report) => report.fmt(f),
            Self::Keyboard(keyboard) => keyboard.fmt(f),
            Self::Device(device) => device.fmt(f),
            Self::Window(window) => window.fmt(f),
        }
    }
}

/// A Select Graphic Rendition (`CSI ... m`) attribute update.
///
/// SGR changes rendering state for text written after the sequence: color, intensity, underline,
/// blink, and related cell attributes. Terminals keep that state until another SGR command changes
/// it or [`Self::Reset`] clears it. The VT510 reference documents the standard [SGR] parameter
/// meanings that Termina models here.
///
/// [SGR]: https://vt100.net/docs/vt510-rm/SGR.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sgr {
    /// SGR 0: reset all graphic rendition attributes to terminal defaults.
    Reset,

    /// Set text intensity described by [`Intensity`].
    Intensity(Intensity),

    /// Set underline style described by [`Underline`].
    ///
    /// This includes Kitty's styled underline extension when terminals support it.
    Underline(Underline),

    /// Set blink behavior described by [`Blink`].
    Blink(Blink),

    /// Enable SGR 3 italic text or disable it with SGR 23.
    Italic(bool),

    /// Enable SGR 7 reverse video or disable it with SGR 27.
    Reverse(bool),

    /// Enable SGR 8 invisible text or disable it with SGR 28.
    Invisible(bool),

    /// Enable SGR 9 strikethrough text or disable it with SGR 29.
    StrikeThrough(bool),

    /// Enable SGR 53 overline text or disable it with SGR 55.
    Overline(bool),

    /// Select the active font described by [`Font`].
    Font(Font),

    /// Set vertical alignment described by [`VerticalAlign`].
    VerticalAlign(VerticalAlign),

    /// Set the foreground color described by [`ColorSpec`].
    Foreground(ColorSpec),

    /// Set the background color described by [`ColorSpec`].
    Background(ColorSpec),

    /// Set the underline color described by [`ColorSpec`].
    ///
    /// This uses the SGR 58 underline-color extension.
    UnderlineColor(ColorSpec),

    /// Combine multiple SGR updates described by [`SgrAttributes`].
    Attributes(SgrAttributes),
}

impl Display for Sgr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fn write_true_color(
            code: u8,
            RgbaColor {
                red,
                green,
                blue,
                alpha,
            }: RgbaColor,
            f: &mut fmt::Formatter,
        ) -> fmt::Result {
            if alpha == 255 {
                // [ITU T.416](https://www.itu.int/rec/T-REC-T.416-199303-I/en) § 13.1.8
                // says that the correct way to format true colors, even for foreground/background
                // is  `{code}:2:{colorspace (optional)}:{red}:{green}:{blue}`. More commonly than
                // not though terminals support the semicolon format shown below. We use semicolon
                // as it seems to have better compatibility in the wild, especially with legacy or
                // limited terminals like Windows conhost.
                //
                // The Microsoft docs also recommend the semicolon format (however Windows
                // Terminal accepts either):
                // <https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#extended-colors>
                write!(f, "{code};2;{red};{green};{blue}")
            } else {
                write!(f, "{code}:6::{red}:{green}:{blue}:{alpha}")
            }
        }

        // CSI <n> m
        match self {
            // The proper thing to do here is `write!(f, "0")?`. By default though when no Ps
            // is specified the terminal defaults to 0, so we can save a byte here.
            Self::Reset => (),
            Self::Intensity(Intensity::Normal) => write!(f, "22")?,
            Self::Intensity(Intensity::Bold) => write!(f, "1")?,
            Self::Intensity(Intensity::Dim) => write!(f, "2")?,
            Self::Underline(Underline::None) => write!(f, "24")?,
            Self::Underline(Underline::Single) => write!(f, "4")?,
            Self::Underline(Underline::Double) => write!(f, "21")?,
            Self::Underline(Underline::Curly) => write!(f, "4:3")?,
            Self::Underline(Underline::Dotted) => write!(f, "4:4")?,
            Self::Underline(Underline::Dashed) => write!(f, "4:5")?,
            Self::Blink(Blink::None) => write!(f, "25")?,
            Self::Blink(Blink::Slow) => write!(f, "5")?,
            Self::Blink(Blink::Rapid) => write!(f, "6")?,
            Self::Italic(true) => write!(f, "3")?,
            Self::Italic(false) => write!(f, "23")?,
            Self::Reverse(true) => write!(f, "7")?,
            Self::Reverse(false) => write!(f, "27")?,
            Self::Invisible(true) => write!(f, "8")?,
            Self::Invisible(false) => write!(f, "28")?,
            Self::StrikeThrough(true) => write!(f, "9")?,
            Self::StrikeThrough(false) => write!(f, "29")?,
            Self::Overline(true) => write!(f, "53")?,
            Self::Overline(false) => write!(f, "55")?,
            Self::Font(Font::Default) => write!(f, "10")?,
            Self::Font(Font::Alternate(1)) => write!(f, "11")?,
            Self::Font(Font::Alternate(2)) => write!(f, "12")?,
            Self::Font(Font::Alternate(3)) => write!(f, "13")?,
            Self::Font(Font::Alternate(4)) => write!(f, "14")?,
            Self::Font(Font::Alternate(5)) => write!(f, "15")?,
            Self::Font(Font::Alternate(6)) => write!(f, "16")?,
            Self::Font(Font::Alternate(7)) => write!(f, "17")?,
            Self::Font(Font::Alternate(8)) => write!(f, "18")?,
            Self::Font(Font::Alternate(9)) => write!(f, "19")?,
            Self::Font(_) => (),
            Self::VerticalAlign(VerticalAlign::BaseLine) => write!(f, "75")?,
            Self::VerticalAlign(VerticalAlign::SuperScript) => write!(f, "73")?,
            Self::VerticalAlign(VerticalAlign::SubScript) => write!(f, "74")?,
            Self::Foreground(ColorSpec::Reset) => write!(f, "39")?,
            Self::Foreground(ColorSpec::BLACK) => write!(f, "30")?,
            Self::Foreground(ColorSpec::RED) => write!(f, "31")?,
            Self::Foreground(ColorSpec::GREEN) => write!(f, "32")?,
            Self::Foreground(ColorSpec::YELLOW) => write!(f, "33")?,
            Self::Foreground(ColorSpec::BLUE) => write!(f, "34")?,
            Self::Foreground(ColorSpec::MAGENTA) => write!(f, "35")?,
            Self::Foreground(ColorSpec::CYAN) => write!(f, "36")?,
            Self::Foreground(ColorSpec::WHITE) => write!(f, "37")?,
            Self::Foreground(ColorSpec::BRIGHT_BLACK) => write!(f, "90")?,
            Self::Foreground(ColorSpec::BRIGHT_RED) => write!(f, "91")?,
            Self::Foreground(ColorSpec::BRIGHT_GREEN) => write!(f, "92")?,
            Self::Foreground(ColorSpec::BRIGHT_YELLOW) => write!(f, "93")?,
            Self::Foreground(ColorSpec::BRIGHT_BLUE) => write!(f, "94")?,
            Self::Foreground(ColorSpec::BRIGHT_MAGENTA) => write!(f, "95")?,
            Self::Foreground(ColorSpec::BRIGHT_CYAN) => write!(f, "96")?,
            Self::Foreground(ColorSpec::BRIGHT_WHITE) => write!(f, "97")?,
            Self::Foreground(ColorSpec::PaletteIndex(idx)) => write!(f, "38;5;{idx}")?,
            Self::Foreground(ColorSpec::TrueColor(color)) => write_true_color(38, *color, f)?,
            Self::Background(ColorSpec::Reset) => write!(f, "49")?,
            Self::Background(ColorSpec::BLACK) => write!(f, "40")?,
            Self::Background(ColorSpec::RED) => write!(f, "41")?,
            Self::Background(ColorSpec::GREEN) => write!(f, "42")?,
            Self::Background(ColorSpec::YELLOW) => write!(f, "43")?,
            Self::Background(ColorSpec::BLUE) => write!(f, "44")?,
            Self::Background(ColorSpec::MAGENTA) => write!(f, "45")?,
            Self::Background(ColorSpec::CYAN) => write!(f, "46")?,
            Self::Background(ColorSpec::WHITE) => write!(f, "47")?,
            Self::Background(ColorSpec::BRIGHT_BLACK) => write!(f, "100")?,
            Self::Background(ColorSpec::BRIGHT_RED) => write!(f, "101")?,
            Self::Background(ColorSpec::BRIGHT_GREEN) => write!(f, "102")?,
            Self::Background(ColorSpec::BRIGHT_YELLOW) => write!(f, "103")?,
            Self::Background(ColorSpec::BRIGHT_BLUE) => write!(f, "104")?,
            Self::Background(ColorSpec::BRIGHT_MAGENTA) => write!(f, "105")?,
            Self::Background(ColorSpec::BRIGHT_CYAN) => write!(f, "106")?,
            Self::Background(ColorSpec::BRIGHT_WHITE) => write!(f, "107")?,
            Self::Background(ColorSpec::PaletteIndex(idx)) => write!(f, "48;5;{idx}")?,
            Self::Background(ColorSpec::TrueColor(color)) => write_true_color(48, *color, f)?,
            Self::UnderlineColor(ColorSpec::Reset) => write!(f, "59")?,
            Self::UnderlineColor(ColorSpec::PaletteIndex(idx)) => write!(f, "58:5:{idx}")?,
            Self::UnderlineColor(ColorSpec::TrueColor(RgbaColor {
                red,
                green,
                blue,
                alpha: 255,
            })) => {
                // As mentioned above in `write_true_color`, this is the _correct_ format for a
                // true color. Styled and colored underlines are a relatively new extension and
                // terminals tend to support colon syntax since it is correct.
                write!(f, "58:2::{red}:{green}:{blue}")?;
            }
            Self::UnderlineColor(ColorSpec::TrueColor(RgbaColor {
                red,
                green,
                blue,
                alpha,
            })) => {
                write!(f, "58:6::{red}:{green}:{blue}:{alpha}")?;
            }
            Self::Attributes(attributes) => {
                use SgrModifiers as Mod;

                let ps_budget = attributes.parameter_chunk_size.get();
                let mut ps_written = 0;
                let mut first = true;
                let mut write = |sgr: Self, n_ps: u16| {
                    // If writing this parameter would exceed the budget, finish this CSI sequence
                    // and start a new one which will start with this SGR.
                    ps_written += n_ps;
                    if ps_written > ps_budget {
                        write!(f, "m{}", super::CSI)?;
                        ps_written = n_ps;
                    } else if !first {
                        f.write_str(";")?;
                    }
                    first = false;
                    write!(f, "{sgr}")
                };
                if attributes.modifiers.contains(Mod::RESET) {
                    write(Self::Reset, 0)?;
                }
                if let Some(color) = attributes.foreground {
                    write(
                        Self::Foreground(color),
                        // TODO: for colors currently we estimate the largest Ps count. This could
                        // be fine-tuned a bit more.
                        match color {
                            ColorSpec::Reset => 1,
                            ColorSpec::PaletteIndex(_) => 3,
                            ColorSpec::TrueColor(RgbaColor { alpha: 255, .. }) => 5,
                            ColorSpec::TrueColor(_) => 6,
                        },
                    )?;
                }
                if let Some(color) = attributes.background {
                    write(
                        Self::Background(color),
                        match color {
                            ColorSpec::Reset => 1,
                            ColorSpec::PaletteIndex(_) => 3,
                            ColorSpec::TrueColor(RgbaColor { alpha: 255, .. }) => 5,
                            ColorSpec::TrueColor(_) => 6,
                        },
                    )?;
                }
                if let Some(color) = attributes.underline_color {
                    write(
                        Self::UnderlineColor(color),
                        match color {
                            ColorSpec::Reset => 1,
                            ColorSpec::PaletteIndex(_) => 3,
                            ColorSpec::TrueColor(_) => 6,
                        },
                    )?;
                }
                if attributes.modifiers.contains(Mod::INTENSITY_NORMAL) {
                    write(Self::Intensity(Intensity::Normal), 1)?;
                }
                if attributes.modifiers.contains(Mod::INTENSITY_DIM) {
                    write(Self::Intensity(Intensity::Dim), 1)?;
                }
                if attributes.modifiers.contains(Mod::INTENSITY_BOLD) {
                    write(Self::Intensity(Intensity::Bold), 1)?;
                }
                if attributes.modifiers.contains(Mod::UNDERLINE_NONE) {
                    write(Self::Underline(Underline::None), 1)?;
                }
                if attributes.modifiers.contains(Mod::UNDERLINE_SINGLE) {
                    write(Self::Underline(Underline::Single), 1)?;
                }
                if attributes.modifiers.contains(Mod::UNDERLINE_DOUBLE) {
                    write(Self::Underline(Underline::Double), 1)?;
                }
                if attributes.modifiers.contains(Mod::UNDERLINE_CURLY) {
                    write(Self::Underline(Underline::Curly), 2)?;
                }
                if attributes.modifiers.contains(Mod::UNDERLINE_DOTTED) {
                    write(Self::Underline(Underline::Dotted), 2)?;
                }
                if attributes.modifiers.contains(Mod::UNDERLINE_DASHED) {
                    write(Self::Underline(Underline::Dashed), 2)?;
                }
                if attributes.modifiers.contains(Mod::BLINK_NONE) {
                    write(Self::Blink(Blink::None), 1)?;
                }
                if attributes.modifiers.contains(Mod::BLINK_SLOW) {
                    write(Self::Blink(Blink::Slow), 1)?;
                }
                if attributes.modifiers.contains(Mod::BLINK_RAPID) {
                    write(Self::Blink(Blink::Rapid), 1)?;
                }
                if attributes.modifiers.contains(Mod::ITALIC) {
                    write(Self::Italic(true), 1)?;
                }
                if attributes.modifiers.contains(Mod::NO_ITALIC) {
                    write(Self::Italic(false), 1)?;
                }
                if attributes.modifiers.contains(Mod::REVERSE) {
                    write(Self::Reverse(true), 1)?;
                }
                if attributes.modifiers.contains(Mod::NO_REVERSE) {
                    write(Self::Reverse(false), 1)?;
                }
                if attributes.modifiers.contains(Mod::INVISIBLE) {
                    write(Self::Invisible(true), 1)?;
                }
                if attributes.modifiers.contains(Mod::NO_INVISIBLE) {
                    write(Self::Invisible(false), 1)?;
                }
                if attributes.modifiers.contains(Mod::STRIKE_THROUGH) {
                    write(Self::StrikeThrough(true), 1)?;
                }
                if attributes.modifiers.contains(Mod::NO_STRIKE_THROUGH) {
                    write(Self::StrikeThrough(false), 1)?;
                }
            }
        }
        Ok(())
    }
}

/// A grouped SGR update.
///
/// [`Sgr`] accepts more than one parameter in a single `CSI ... m` sequence, so one escape can set
/// the foreground color, background color, underline color, and text modifiers together. Grouping
/// related changes reduces the number of bytes written and the number of CSI sequences the
/// terminal has to parse.
///
/// Note that if no attributes are set ([`SgrAttributes::default`]) the terminal will treat the
/// escape the same as [`Sgr::Reset`]. So if you are using this type you may wish to compare the
/// attributes you've built with [`SgrAttributes::default`] to decide whether or not you want to
/// write it to the terminal. Otherwise the escape codes for this type do not reset SGR. The
/// example below sets a green foreground and bold intensity but would not affect any other SGR
/// settings like underline or background color.
///
/// ```
/// # use termina::escape::csi::{Csi, Sgr, SgrAttributes, SgrModifiers};
/// # use termina::style::{ColorSpec, Intensity};
/// let attributes = SgrAttributes {
///     foreground: Some(ColorSpec::GREEN),
///     modifiers: SgrModifiers::INTENSITY_BOLD,
///     ..Default::default()
/// };
/// // Both SGR codes are in one CSI escape.
/// assert_eq!(Csi::Sgr(Sgr::Attributes(attributes)).to_string(), "\x1b[32;1m");
/// // Compare to emitting them separately:
/// assert_eq!(Csi::Sgr(Sgr::Foreground(ColorSpec::GREEN)).to_string(), "\x1b[32m");
/// assert_eq!(Csi::Sgr(Sgr::Intensity(Intensity::Bold)).to_string(), "\x1b[1m");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
// > You can use more than one Ps value to select different character attributes.
// <https://vt100.net/docs/vt510-rm/SGR>
pub struct SgrAttributes {
    /// The foreground color used to paint text.
    pub foreground: Option<ColorSpec>,

    /// The background color used to paint the cell.
    pub background: Option<ColorSpec>,

    /// The color of the underline in the current cell.
    pub underline_color: Option<ColorSpec>,

    /// Bitflags for text modifiers such as intensity, underline, blink, italic, and reverse video.
    pub modifiers: SgrModifiers,

    /// The number of parameters which are allowed in a chunk.
    ///
    /// The VT parsers used in many terminal emulators set limits on the number of parameters a
    /// CSI sequence can use. After the limit they typically ignore all other parameters. For
    /// many terminals this is a relatively high number like 256 but some set their limit as low as
    /// 10. For maximum compatibility this is set to 10 by default.
    ///
    /// The number of parameters taken to describe a modifier varies by modifier. True-color colors
    /// (foreground, background, underline color) take the most while simple modifiers like
    /// [`SgrModifiers::ITALIC`] take just one.
    pub parameter_chunk_size: NonZeroU16,
}

impl Default for SgrAttributes {
    fn default() -> Self {
        Self {
            foreground: Default::default(),
            background: Default::default(),
            underline_color: Default::default(),
            modifiers: Default::default(),
            parameter_chunk_size: unsafe { NonZeroU16::new_unchecked(10) },
        }
    }
}

impl SgrAttributes {
    /// Returns `true` if no attributes are set, `false` otherwise.
    ///
    /// When empty attributes are displayed they produce the same escape sequence as [`Sgr::Reset`].
    /// If you are building attributes incrementally starting with [`SgrAttributes::default`] then
    /// you may wish to check whether the attributes are empty to decide whether or not you should
    /// write them to the terminal.
    ///
    /// ```
    /// # use termina::escape::csi::{Csi, Sgr, SgrAttributes, SgrModifiers};
    /// let mut attributes = SgrAttributes::default();
    /// assert!(attributes.is_empty());
    /// assert_eq!(
    ///     Csi::Sgr(Sgr::Reset).to_string(),
    ///     Csi::Sgr(Sgr::Attributes(attributes)).to_string(),
    /// );
    ///
    /// attributes.modifiers |= SgrModifiers::ITALIC;
    /// assert!(!attributes.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.foreground.is_none()
            && self.background.is_none()
            && self.underline_color.is_none()
            && self.modifiers.is_empty()
    }
}

// We could represent `SgrAttributes` as a `Vec<Sgr>` but we can flatten the type out to have a
// more compact representation with bitflags for each SGR instead:
bitflags::bitflags! {
    /// SGR modifier bits used by [`SgrAttributes`].
    ///
    /// These flags mirror SGR attributes that can be represented without carrying additional
    /// color or font data.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct SgrModifiers: u32 {
        /// No SGR modifiers.
        const NONE = 0;

        /// SGR 0: reset all attributes.
        const RESET = 1 << 1;

        /// SGR 22: normal intensity.
        const INTENSITY_NORMAL = 1 << 2;

        /// SGR 2: dim intensity.
        const INTENSITY_DIM = 1 << 3;

        /// SGR 1: bold intensity.
        const INTENSITY_BOLD = 1 << 4;

        /// SGR 24: no underline.
        const UNDERLINE_NONE = 1 << 5;

        /// SGR 4: single underline.
        const UNDERLINE_SINGLE = 1 << 6;

        /// SGR 21: double underline.
        const UNDERLINE_DOUBLE = 1 << 7;

        /// Kitty underline style 3: curly underline.
        const UNDERLINE_CURLY = 1 << 8;

        /// Kitty underline style 4: dotted underline.
        const UNDERLINE_DOTTED = 1 << 9;

        /// Kitty underline style 5: dashed underline.
        const UNDERLINE_DASHED = 1 << 10;

        /// SGR 25: no blink.
        const BLINK_NONE = 1 << 11;

        /// SGR 5: slow blink.
        const BLINK_SLOW = 1 << 12;

        /// SGR 6: rapid blink.
        const BLINK_RAPID = 1 << 13;

        /// SGR 3: italic.
        const ITALIC = 1 << 14;

        /// SGR 23: no italic.
        const NO_ITALIC = 1 << 15;

        /// SGR 7: reverse video.
        const REVERSE = 1 << 16;

        /// SGR 27: no reverse video.
        const NO_REVERSE = 1 << 17;

        /// SGR 8: invisible text.
        const INVISIBLE = 1 << 18;

        /// SGR 28: visible text.
        const NO_INVISIBLE = 1 << 19;

        /// SGR 9: strikethrough.
        const STRIKE_THROUGH = 1 << 20;

        /// SGR 29: no strikethrough.
        const NO_STRIKE_THROUGH = 1 << 21;
        // Support font and vertical align? They're not well supported in terminals so I think
        // it's fine to leave them out of this type.
    }
}

impl Default for SgrModifiers {
    fn default() -> Self {
        Self::NONE
    }
}

// Cursor

/// The cursor shape for the Kitty multi-cursor protocol.
///
/// This represents either a specific [`CursorStyle`] (protocol values 0-6)
/// or the special "follow main cursor" value (protocol value 29).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MultiCursorShape {
    /// Use a specific cursor style for secondary cursors.
    Style(CursorStyle),

    /// Use the main cursor's current shape for secondary cursors.
    FollowMainCursor,
}

/// Supported operations in the kitty multi-cursor protocol.
///
/// Returned in the capability query response (`CSI > SP q`). Each variant
/// corresponds to a protocol operation code the terminal advertises support for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MultiCursorCapability {
    /// Block cursor shape.
    BlockShape = 1,
    /// Beam cursor shape.
    BeamShape = 2,
    /// Underline cursor shape.
    UnderlineShape = 3,
    /// Follow the main cursor's shape.
    FollowMainCursorShape = 29,
    /// Change the color of text under extra cursors.
    TextColor = 30,
    /// Change the color of extra cursors.
    CursorColor = 40,
    /// Query currently set cursors.
    QueryCurrentCursors = 100,
    /// Query extra cursor colors.
    QueryColors = 101,
}

impl TryFrom<u8> for MultiCursorCapability {
    type Error = u8;

    fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
        match value {
            1 => Ok(Self::BlockShape),
            2 => Ok(Self::BeamShape),
            3 => Ok(Self::UnderlineShape),
            29 => Ok(Self::FollowMainCursorShape),
            30 => Ok(Self::TextColor),
            40 => Ok(Self::CursorColor),
            100 => Ok(Self::QueryCurrentCursors),
            101 => Ok(Self::QueryColors),
            _ => Err(value),
        }
    }
}

/// Cursor-related CSI commands.
///
/// This includes cursor movement, tabulation, position reports, margins, cursor style, and
/// Kitty's multi-cursor extension.
///
/// ```
/// use termina::{
///     escape::csi::{Csi, Cursor},
///     OneBased,
/// };
///
/// let position = Cursor::Position {
///     line: OneBased::new(3).unwrap(),
///     col: OneBased::new(10).unwrap(),
/// };
/// assert_eq!(Csi::Cursor(position).to_string(), "\x1b[3;10H");
/// assert_eq!(Csi::Cursor(Cursor::default_position()).to_string(), "\x1b[1;1H");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Cursor {
    /// CBT Moves cursor to the Ps tabs backward. The default value of Ps is 1.
    BackwardTabulation(u32),

    /// TBC - TABULATION CLEAR
    TabulationClear(TabulationClear),

    /// CHA: Moves cursor to the Ps-th column of the active line. The default
    /// value of Ps is 1.
    CharacterAbsolute(OneBased),

    /// HPA CHARACTER POSITION ABSOLUTE
    /// HPA Moves cursor to the Ps-th column of the active line. The default
    /// value of Ps is 1.
    CharacterPositionAbsolute(OneBased),

    /// HPB - CHARACTER POSITION BACKWARD
    /// HPB Moves cursor to the left Ps columns. The default value of Ps is 1.
    CharacterPositionBackward(u32),

    /// HPR - CHARACTER POSITION FORWARD
    /// HPR Moves cursor to the right Ps columns. The default value of Ps is 1.
    CharacterPositionForward(u32),

    /// HVP - CHARACTER AND LINE POSITION
    /// HVP Moves cursor to the Ps1-th line and to the Ps2-th column. The
    /// default value of Ps1 and Ps2 is 1.
    CharacterAndLinePosition {
        /// The destination line.
        line: OneBased,
        /// The destination column.
        col: OneBased,
    },

    /// VPA - LINE POSITION ABSOLUTE
    /// Move to the corresponding vertical position (line Ps) of the current
    /// column. The default value of Ps is 1.
    LinePositionAbsolute(u32),

    /// VPB - LINE POSITION BACKWARD
    /// Moves cursor up Ps lines in the same column. The default value of Ps is
    /// 1.
    LinePositionBackward(u32),

    /// VPR - LINE POSITION FORWARD
    /// Moves cursor down Ps lines in the same column. The default value of Ps
    /// is 1.
    LinePositionForward(u32),

    /// CHT
    /// Moves cursor to the Ps tabs forward. The default value of Ps is 1.
    ForwardTabulation(u32),

    /// CNL Moves cursor to the first column of Ps-th following line. The
    /// default value of Ps is 1.
    NextLine(u32),

    /// CPL Moves cursor to the first column of Ps-th preceding line. The
    /// default value of Ps is 1.
    PrecedingLine(u32),

    /// CPR - ACTIVE POSITION REPORT
    /// If the DEVICE COMPONENT SELECT MODE (DCSM)
    /// is set to PRESENTATION, CPR is used to report the active presentation
    /// position of the sending device as residing in the presentation
    /// component at the n-th line position according to the line progression
    /// and at the m-th character position according to the character path,
    /// where n equals the value of Pn1 and m equal s the value of Pn2.
    /// If the DEVICE COMPONENT SELECT MODE (DCSM) is set to DATA, CPR is used
    /// to report the active data position of the sending device as
    /// residing in the data component at the n-th line position according
    /// to the line progression and at the m-th character position
    /// according to the character progression, where n equals the value of
    /// Pn1 and m equals the value of Pn2. CPR may be solicited by a DEVICE
    /// STATUS REPORT (DSR) or be sent unsolicited .
    ActivePositionReport {
        /// The reported line.
        line: OneBased,
        /// The reported column.
        col: OneBased,
    },

    /// CPR: this is the request from the client.
    /// The terminal will respond with ActivePositionReport.
    RequestActivePositionReport,

    /// SCP - Save Cursor Position.
    /// Only works when DECLRMM is disabled
    SaveCursor,

    /// RCP - Restore Cursor Position.
    RestoreCursor,

    /// CTC - CURSOR TABULATION CONTROL
    /// CTC causes one or more tabulation stops to be set or cleared in the
    /// presentation component, depending on the parameter values.
    /// In the case of parameter values 0, 2 or 4 the number of lines affected
    /// depends on the setting of the TABULATION STOP MODE (TSM).
    TabulationControl(CursorTabulationControl),

    /// CUB - Cursor Left
    /// Moves cursor to the left Ps columns. The default value of Ps is 1.
    Left(u32),

    /// CUD - Cursor Down
    Down(u32),

    /// CUF - Cursor Right
    Right(u32),

    /// CUU - Cursor Up
    Up(u32),

    /// CUP - Cursor Position
    /// Moves cursor to the Ps1-th line and to the Ps2-th column. The default
    /// value of Ps1 and Ps2 is 1.
    Position {
        /// The destination line.
        line: OneBased,
        /// The destination column.
        col: OneBased,
    },

    /// CVT - Cursor Line Tabulation
    /// CVT causes the active presentation position to be moved to the
    /// corresponding character position of the line corresponding to the n-th
    /// following line tabulation stop in the presentation component, where n
    /// equals the value of Pn.
    LineTabulation(u32),

    /// DECSTBM - Set top and bottom margins.
    SetTopAndBottomMargins {
        /// The top margin line.
        top: OneBased,
        /// The bottom margin line.
        bottom: OneBased,
    },

    /// [DECSLRM] - Set left and right margins.
    ///
    /// [DECSLRM]: https://vt100.net/docs/vt510-rm/DECSLRM.html
    SetLeftAndRightMargins {
        /// The left margin column.
        left: OneBased,
        /// The right margin column.
        right: OneBased,
    },

    /// Set the cursor style.
    CursorStyle(CursorStyle),

    /// Query the current cursor shape.
    QueryCursorShape,

    /// Capability query response (kitty multi-cursor protocol).
    ///
    /// Contains the set of operations the terminal supports. An empty list
    /// means the protocol is not supported.
    CursorShapeQueryResponse(Vec<MultiCursorCapability>),

    /// Set secondary cursor positions for the kitty multi-cursor protocol.
    SetMultipleCursors {
        /// The shape used for the secondary cursors.
        shape: MultiCursorShape,

        /// The one-based `(line, column)` positions of the secondary cursors.
        positions: Vec<(OneBased, OneBased)>,
    },

    /// Clear all secondary cursors from the kitty multi-cursor protocol.
    ClearSecondaryCursors,
}

impl Cursor {
    /// Returns the home cursor position, line 1 column 1.
    pub const fn default_position() -> Self {
        Self::Position {
            line: OneBased::from_zero_based(0),
            col: OneBased::from_zero_based(0),
        }
    }
}

impl Display for Cursor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fn write_csi<T: Default + Eq + Display>(
            value: T,
            f: &mut fmt::Formatter<'_>,
            control: &str,
        ) -> fmt::Result {
            if value == T::default() {
                write!(f, "{control}")
            } else {
                write!(f, "{value}{control}")
            }
        }

        match self {
            Cursor::BackwardTabulation(n) => write_csi(*n, f, "Z"),
            Cursor::TabulationClear(n) => write_csi(*n, f, "g"),
            Cursor::CharacterAbsolute(n) => write_csi(*n, f, "G"),
            Cursor::CharacterPositionAbsolute(n) => write_csi(*n, f, "``"),
            Cursor::CharacterPositionBackward(n) => write_csi(*n, f, "j"),
            Cursor::CharacterPositionForward(n) => write_csi(*n, f, "a"),
            Cursor::CharacterAndLinePosition { line, col } => write!(f, "{line};{col}f"),
            Cursor::LinePositionAbsolute(n) => write_csi(*n, f, "d"),
            Cursor::LinePositionBackward(n) => write_csi(*n, f, "k"),
            Cursor::LinePositionForward(n) => write_csi(*n, f, "e"),
            Cursor::ForwardTabulation(n) => write_csi(*n, f, "I"),
            Cursor::NextLine(n) => write_csi(*n, f, "E"),
            Cursor::PrecedingLine(n) => write_csi(*n, f, "F"),
            Cursor::ActivePositionReport { line, col } => write!(f, "{line};{col}R"),
            Cursor::RequestActivePositionReport => write!(f, "6n"),
            Cursor::SaveCursor => write!(f, "s"),
            Cursor::RestoreCursor => write!(f, "u"),
            Cursor::TabulationControl(n) => write_csi(*n, f, "W"),
            Cursor::Left(n) => write_csi(*n, f, "D"),
            Cursor::Down(n) => write_csi(*n, f, "B"),
            Cursor::Right(n) => write_csi(*n, f, "C"),
            Cursor::Up(n) => write_csi(*n, f, "A"),
            Cursor::Position { line, col } => write!(f, "{line};{col}H"),
            Cursor::LineTabulation(n) => write_csi(*n, f, "Y"),
            Cursor::SetTopAndBottomMargins { top, bottom } => {
                if top.get() == 1 && bottom.get() == u16::MAX {
                    write!(f, "r")
                } else {
                    write!(f, "{top};{bottom}r")
                }
            }
            Cursor::SetLeftAndRightMargins { left, right } => {
                if left.get() == 1 && right.get() == u16::MAX {
                    write!(f, "s")
                } else {
                    write!(f, "{left};{right}s")
                }
            }
            Cursor::CursorStyle(style) => write!(f, "{} q", *style as u8),
            Cursor::QueryCursorShape => write!(f, "> q"),
            Cursor::CursorShapeQueryResponse(caps) => {
                write!(f, ">")?;
                for (i, cap) in caps.iter().enumerate() {
                    if i > 0 {
                        write!(f, ";")?;
                    }
                    write!(f, "{}", *cap as u8)?;
                }
                write!(f, " q")
            }
            Cursor::SetMultipleCursors { shape, positions } => {
                write!(
                    f,
                    ">{}",
                    match shape {
                        MultiCursorShape::Style(style) => *style as u8,
                        MultiCursorShape::FollowMainCursor => 29,
                    }
                )?;
                for (line, col) in positions {
                    write!(f, ";2:{}:{}", line, col)?;
                }
                write!(f, " q")
            }
            Cursor::ClearSecondaryCursors => write!(f, ">0;4 q"),
        }
    }
}

/// Cursor tabulation control actions for CTC.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum CursorTabulationControl {
    /// Set a character tab stop at the active position.
    #[default]
    SetCharacterTabStopAtActivePosition = 0,

    /// Set a line tab stop at the active line.
    SetLineTabStopAtActiveLine = 1,

    /// Clear the character tab stop at the active position.
    ClearCharacterTabStopAtActivePosition = 2,

    /// Clear the line tab stop at the active line.
    ClearLineTabstopAtActiveLine = 3,

    /// Clear all character tab stops on the active line.
    ClearAllCharacterTabStopsAtActiveLine = 4,

    /// Clear all character tab stops.
    ClearAllCharacterTabStops = 5,

    /// Clear all line tab stops.
    ClearAllLineTabStops = 6,
}

impl Display for CursorTabulationControl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", *self as u8)
    }
}

/// Tab-stop clearing actions for TBC.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum TabulationClear {
    /// Clear the character tab stop at the active position.
    #[default]
    ClearCharacterTabStopAtActivePosition = 0,

    /// Clear the line tab stop at the active line.
    ClearLineTabStopAtActiveLine = 1,

    /// Clear all character tab stops on the active line.
    ClearCharacterTabStopsAtActiveLine = 2,

    /// Clear all character tab stops.
    ClearAllCharacterTabStops = 3,

    /// Clear all line tab stops.
    ClearAllLineTabStops = 4,

    /// Clear all character and line tab stops.
    ClearAllTabStops = 5,
}

impl Display for TabulationClear {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", *self as u8)
    }
}

// Edit

/// Text and display editing CSI commands.
///
/// ```
/// use termina::escape::csi::{Csi, Edit, EraseInDisplay, EraseInLine};
///
/// assert_eq!(
///     Csi::Edit(Edit::EraseInLine(EraseInLine::EraseToEndOfLine)).to_string(),
///     "\x1b[0K",
/// );
/// assert_eq!(
///     Csi::Edit(Edit::EraseInDisplay(EraseInDisplay::EraseDisplay)).to_string(),
///     "\x1b[2J",
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Edit {
    /// DCH - DELETE CHARACTER
    /// Deletes Ps characters from the cursor position to the right. The
    /// default value of Ps is 1. If the DEVICE COMPONENT SELECT MODE
    /// (DCSM) is set to PRESENTATION, DCH causes the contents of the
    /// active presentation position and, depending on the setting of the
    /// CHARACTER EDITING MODE (HEM), the contents of the n-1 preceding or
    /// following character positions to be removed from the presentation
    /// component, where n equals the value of Pn. The resulting gap is
    /// closed by shifting the contents of the adjacent character positions
    /// towards the active presentation position. At the other end of the
    /// shifted part, n character positions are put into the erased state.
    DeleteCharacter(u32),

    /// DL - DELETE LINE
    /// If the DEVICE COMPONENT SELECT MODE (DCSM) is set to PRESENTATION, DL
    /// causes the contents of the active line (the line that contains the
    /// active presentation position) and, depending on the setting of the
    /// LINE EDITING MODE (VEM), the contents of the n-1 preceding or
    /// following lines to be removed from the presentation component, where n
    /// equals the value of Pn. The resulting gap is closed by shifting the
    /// contents of a number of adjacent lines towards the active line. At
    /// the other end of the shifted part, n lines are put into the
    /// erased state.  The active presentation position is moved to the line
    /// home position in the active line. The line home position is
    /// established by the parameter value of SET LINE HOME (SLH). If the
    /// TABULATION STOP MODE (TSM) is set to SINGLE, character tabulation stops
    /// are cleared in the lines that are put into the erased state.  The
    /// extent of the shifted part is established by SELECT EDITING EXTENT
    /// (SEE).  Any occurrences of the start or end of a selected area, the
    /// start or end of a qualified area, or a tabulation stop in the shifted
    /// part, are also shifted.
    DeleteLine(u32),

    /// ECH - ERASE CHARACTER
    /// If the DEVICE COMPONENT SELECT MODE (DCSM) is set to PRESENTATION, ECH
    /// causes the active presentation position and the n-1 following
    /// character positions in the presentation component to be put into
    /// the erased state, where n equals the value of Pn.
    EraseCharacter(u32),

    /// EL - ERASE IN LINE
    /// If the DEVICE COMPONENT SELECT MODE (DCSM) is set to PRESENTATION, EL
    /// causes some or all character positions of the active line (the line
    /// which contains the active presentation position in the presentation
    /// component) to be put into the erased state, depending on the
    /// parameter values
    EraseInLine(EraseInLine),

    /// ICH - INSERT CHARACTER
    /// If the DEVICE COMPONENT SELECT MODE (DCSM) is set to PRESENTATION, ICH
    /// is used to prepare the insertion of n characters, by putting into the
    /// erased state the active presentation position and, depending on the
    /// setting of the CHARACTER EDITING MODE (HEM), the n-1 preceding or
    /// following character positions in the presentation component, where n
    /// equals the value of Pn. The previous contents of the active
    /// presentation position and an adjacent string of character positions are
    /// shifted away from the active presentation position. The contents of n
    /// character positions at the other end of the shifted part are removed.
    /// The active presentation position is moved to the line home position in
    /// the active line. The line home position is established by the parameter
    /// value of SET LINE HOME (SLH).
    InsertCharacter(u32),

    /// IL - INSERT LINE
    /// If the DEVICE COMPONENT SELECT MODE (DCSM) is set to PRESENTATION, IL
    /// is used to prepare the insertion of n lines, by putting into the
    /// erased state in the presentation component the active line (the
    /// line that contains the active presentation position) and, depending on
    /// the setting of the LINE EDITING MODE (VEM), the n-1 preceding or
    /// following lines, where n equals the value of Pn. The previous
    /// contents of the active line and of adjacent lines are shifted away
    /// from the active line. The contents of n lines at the other end of the
    /// shifted part are removed. The active presentation position is moved
    /// to the line home position in the active line. The line home
    /// position is established by the parameter value of SET LINE
    /// HOME (SLH).
    InsertLine(u32),

    /// SD - SCROLL DOWN
    /// SD causes the data in the presentation component to be moved by n line
    /// positions if the line orientation is horizontal, or by n character
    /// positions if the line orientation is vertical, such that the data
    /// appear to move down; where n equals the value of Pn. The active
    /// presentation position is not affected by this control function.
    ///
    /// Also known as Pan Up in DEC; see [SD].
    ///
    /// [SD]: https://vt100.net/docs/vt510-rm/SD.html
    ScrollDown(u32),

    /// SU - SCROLL UP
    /// SU causes the data in the presentation component to be moved by n line
    /// positions if the line orientation is horizontal, or by n character
    /// positions if the line orientation is vertical, such that the data
    /// appear to move up; where n equals the value of Pn. The active
    /// presentation position is not affected by this control function.
    ScrollUp(u32),

    /// ED - ERASE IN PAGE (XTerm calls this Erase in Display)
    EraseInDisplay(EraseInDisplay),

    /// REP - Repeat the preceding character n times
    Repeat(u32),
}

impl Display for Edit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fn write_csi(param: u32, f: &mut fmt::Formatter<'_>, control: &str) -> fmt::Result {
            if param == 1 {
                write!(f, "{control}")
            } else {
                write!(f, "{param}{control}")
            }
        }

        match self {
            Self::DeleteCharacter(n) => write_csi(*n, f, "P"),
            Self::DeleteLine(n) => write_csi(*n, f, "M"),
            Self::EraseCharacter(n) => write_csi(*n, f, "X"),
            Self::EraseInLine(n) => write_csi(*n as u32, f, "K"),
            Self::InsertCharacter(n) => write_csi(*n, f, "@"),
            Self::InsertLine(n) => write_csi(*n, f, "L"),
            Self::ScrollDown(n) => write_csi(*n, f, "T"),
            Self::ScrollUp(n) => write_csi(*n, f, "S"),
            Self::EraseInDisplay(n) => write_csi(*n as u32, f, "J"),
            Self::Repeat(n) => write_csi(*n, f, "b"),
        }
    }
}

/// Erase-in-line modes for EL.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum EraseInLine {
    /// Erase from the active position to the end of the line.
    #[default]
    EraseToEndOfLine = 0,

    /// Erase from the start of the line through the active position.
    EraseToStartOfLine = 1,

    /// Erase the entire active line.
    EraseLine = 2,
}

/// Erase-in-display modes for ED.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum EraseInDisplay {
    /// the active presentation position and the character positions up to the
    /// end of the page are put into the erased state
    #[default]
    EraseToEndOfDisplay = 0,
    /// the character positions from the beginning of the page up to and
    /// including the active presentation position are put into the erased
    /// state
    EraseToStartOfDisplay = 1,
    /// all character positions of the page are put into the erased state
    EraseDisplay = 2,
    /// Clears the scrollback.  This is an Xterm extension to ECMA-48.
    EraseScrollback = 3,
}

// Mode

/// Terminal mode CSI commands.
///
/// This enum covers Digital Equipment Corporation (DEC) private modes
/// (`CSI ? ... h/l/s/r/$p`), standard modes, xterm key modifier resources, and terminal theme
/// query/report extensions.
///
/// ```
/// use termina::escape::csi::{Csi, DecPrivateMode, DecPrivateModeCode, Mode};
///
/// let bracketed_paste = DecPrivateMode::Code(DecPrivateModeCode::BracketedPaste);
/// assert_eq!(
///     Csi::Mode(Mode::SetDecPrivateMode(bracketed_paste)).to_string(),
///     "\x1b[?2004h",
/// );
/// assert_eq!(
///     Csi::Mode(Mode::ResetDecPrivateMode(bracketed_paste)).to_string(),
///     "\x1b[?2004l",
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    /// Set a DEC private mode.
    SetDecPrivateMode(DecPrivateMode),

    /// Reset a DEC private mode.
    ResetDecPrivateMode(DecPrivateMode),

    /// Save a DEC private mode.
    SaveDecPrivateMode(DecPrivateMode),

    /// Restore a DEC private mode.
    RestoreDecPrivateMode(DecPrivateMode),

    /// Query a DEC private mode setting.
    QueryDecPrivateMode(DecPrivateMode),

    /// Report a DEC private mode setting.
    ReportDecPrivateMode {
        /// The DEC private mode being reported.
        mode: DecPrivateMode,

        /// The current setting state for the mode.
        setting: DecModeSetting,
    },

    /// Set a standard terminal mode.
    SetMode(TerminalMode),

    /// Reset a standard terminal mode.
    ResetMode(TerminalMode),

    /// Query a standard terminal mode.
    QueryMode(TerminalMode),

    /// Set or query an xterm key modifier resource.
    XtermKeyMode {
        /// The xterm key modifier resource.
        resource: XtermKeyModifierResource,

        /// The resource value, or `None` to query it.
        value: Option<i64>,
    },

    /// Query the current terminal theme.
    QueryTheme,

    /// Report the current terminal theme.
    ReportTheme(ThemeMode),
}

impl Display for Mode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SetDecPrivateMode(mode) => write!(f, "?{mode}h"),
            Self::ResetDecPrivateMode(mode) => write!(f, "?{mode}l"),
            Self::SaveDecPrivateMode(mode) => write!(f, "?{mode}s"),
            Self::RestoreDecPrivateMode(mode) => write!(f, "?{mode}r"),
            Self::QueryDecPrivateMode(mode) => write!(f, "?{mode}$p"),
            Self::ReportDecPrivateMode { mode, setting } => {
                write!(f, "?{mode};{}$y", *setting as u8)
            }
            Self::SetMode(mode) => write!(f, "{mode}h"),
            Self::ResetMode(mode) => write!(f, "{mode}l"),
            Self::QueryMode(mode) => write!(f, "?{mode}$p"),
            Self::XtermKeyMode { resource, value } => {
                write!(f, ">{}", *resource as u8)?;
                if let Some(value) = value {
                    write!(f, ";{}", value)?;
                } else {
                    write!(f, ";")?;
                }
                write!(f, "m")
            }
            Self::QueryTheme => write!(f, "?996n"),
            Self::ReportTheme(mode) => write!(f, "?997;{}n", *mode as u8),
        }
    }
}

/// A Digital Equipment Corporation private mode value.
///
/// DEC private modes are terminal-specific mode numbers encoded with `CSI ? ...` sequences. Many
/// modern terminal emulators still use this namespace for xterm-compatible extensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecPrivateMode {
    /// A known DEC private mode code.
    Code(DecPrivateModeCode),

    /// A DEC private mode code not modeled by [`DecPrivateModeCode`].
    Unspecified(u16),
}

impl Display for DecPrivateMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let code = match *self {
            Self::Code(code) => code as u16,
            Self::Unspecified(code) => code,
        };
        write!(f, "{code}")
    }
}

/// Known Digital Equipment Corporation private mode numbers.
///
/// The DEC private-mode namespace started with DEC terminals and now also carries common
/// xterm-compatible extensions such as mouse tracking, alternate screens, and bracketed paste.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecPrivateModeCode {
    /// Mode 1: [DECCKM] - Application Cursor Keys.
    ///
    /// This mode is only effective when the terminal is in keypad application mode (see DECKPAM)
    /// and the ANSI/VT52 mode (DECANM) is set (see DECANM). Under these conditions, if the cursor
    /// key mode is reset, the four cursor function keys will send ANSI cursor control commands. If
    /// cursor key mode is set, the four cursor function keys will send application functions.
    ///
    /// [DECCKM]: https://vt100.net/docs/vt510-rm/DECCKM.html
    ApplicationCursorKeys = 1,

    /// Mode 2: [DECANM] - behave like a VT52.
    ///
    /// This is a historical compatibility mode. Modern terminal applications usually leave the
    /// terminal in ANSI mode and should not need to enable VT52 behavior.
    ///
    /// [DECANM]: https://vt100.net/docs/vt510-rm/DECANM.html
    DecAnsiMode = 2,

    /// Mode 3: [DECCOLM] - Select 132 columns.
    ///
    /// Setting this mode asks the terminal to switch from 80 columns to 132 columns. Many modern
    /// terminals ignore or restrict this behavior.
    ///
    /// [DECCOLM]: https://vt100.net/docs/vt510-rm/DECCOLM.html
    Select132Columns = 3,

    /// Mode 4: [DECSCLM] - Smooth scroll.
    ///
    /// This mode controls whether scrolling should be smooth or jump-scroll style on DEC
    /// terminals. Modern emulators commonly ignore it.
    ///
    /// [DECSCLM]: https://vt100.net/docs/vt510-rm/DECSCLM.html
    SmoothScroll = 4,

    /// Mode 5: [DECSCNM] - Reverse video.
    ///
    /// Setting this mode swaps the screen's foreground and background presentation.
    ///
    /// [DECSCNM]: https://vt100.net/docs/vt510-rm/DECSCNM.html
    ReverseVideo = 5,

    /// Mode 6: [DECOM] - Origin Mode.
    ///
    /// When enabled, OriginMode constrains cursor to the scroll region and makes its position
    /// relative to that region.
    ///
    /// [DECOM]: https://vt100.net/docs/vt510-rm/DECOM.html
    OriginMode = 6,

    /// Mode 7: [DECAWM] - Auto Wrap.
    ///
    /// When enabled, wrap to next line. Otherwise replace the last character.
    ///
    /// [DECAWM]: https://vt100.net/docs/vt510-rm/DECAWM.html
    AutoWrap = 7,

    /// Mode 8: [DECARM] - Auto Repeat.
    ///
    /// This controls whether held-down keys repeatedly generate input.
    ///
    /// [DECARM]: https://vt100.net/docs/vt510-rm/DECARM.html
    AutoRepeat = 8,

    /// Mode 12: start blinking the cursor.
    ///
    /// This is an xterm cursor-control extension, not a DEC private mode from the VT manuals.
    StartBlinkingCursor = 12,

    /// Mode 25: show the cursor.
    ///
    /// Applications commonly set this mode while running and reset it to hide the cursor during
    /// full-screen drawing.
    ShowCursor = 25,

    /// Mode 45: reverse-wrap from the left edge to the previous line.
    ///
    /// This xterm extension controls whether cursor-left from column 1 wraps to the previous line.
    ReverseWraparound = 45,

    /// Mode 69: [DECLRMM] - Left Right Margin Mode.
    ///
    /// [DECLRMM]: https://vt100.net/docs/vt510-rm/DECLRMM.html
    LeftRightMarginMode = 69,

    /// Mode 80: DECSDM - Sixel Display Mode.
    ///
    /// See the sixel mode discussion in [EK-VT38T-UG-001].
    ///
    /// [EK-VT38T-UG-001]: https://vt100.net/dec/ek-vt38t-ug-001.pdf#page=132
    SixelDisplayMode = 80,

    /// Mode 1000: enable mouse button press/release reporting.
    ///
    /// xterm mouse tracking defines the report encoding for this mode. Termina parses compatible
    /// reports as [`crate::Event::Mouse`].
    ///
    /// [xterm mouse tracking]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    MouseTracking = 1000,

    /// Mode 1001: enable highlight mouse tracking.
    ///
    /// Warning: this requires a cooperative and timely application response; otherwise the
    /// terminal can hang. xterm mouse tracking defines the report encoding for this mode.
    /// Termina parses compatible reports as [`crate::Event::Mouse`].
    ///
    /// [xterm mouse tracking]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    HighlightMouseTracking = 1001,

    /// Mode 1002: enable mouse button press/release and drag reporting.
    ///
    /// xterm mouse tracking defines the report encoding for this mode. Termina parses compatible
    /// reports as [`crate::Event::Mouse`].
    ///
    /// [xterm mouse tracking]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    ButtonEventMouse = 1002,

    /// Mode 1003: enable mouse motion, button press/release, and drag reporting.
    ///
    /// xterm mouse tracking defines the report encoding for this mode. Termina parses compatible
    /// reports as [`crate::Event::Mouse`].
    ///
    /// [xterm mouse tracking]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    AnyEventMouse = 1003,

    /// Mode 1004: enable FocusIn/FocusOut events.
    ///
    /// When enabled, compatible terminals send focus events as CSI `I` and CSI `O`. Termina parses
    /// those reports as [`crate::Event::FocusIn`] and [`crate::Event::FocusOut`].
    FocusTracking = 1004,

    /// Mode 1005: use the UTF-8 mouse coordinate encoding.
    ///
    /// This is an older extended-coordinate encoding. New applications generally prefer
    /// [`Self::SGRMouse`] when supported.
    Utf8Mouse = 1005,

    /// Mode 1006: use SGR extended coordinates in mouse reporting.
    ///
    /// This does not enable mouse reporting itself; it only controls how reports are encoded.
    /// xterm extended coordinates define the `CSI < ... M/m` report shape parsed as
    /// [`MouseReport::Sgr1006`].
    ///
    /// [xterm extended coordinates]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    SGRMouse = 1006,

    /// Mode 1015: use RXVT mouse coordinate encoding.
    ///
    /// xterm extended coordinates document this as the older urxvt-style coordinate extension.
    ///
    /// [xterm extended coordinates]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    RXVTMouse = 1015,

    /// Mode 1016: use pixels rather than text cells in mouse reporting.
    ///
    /// This does not enable mouse reporting itself; it only controls how reports are encoded.
    /// xterm SGR pixels define the pixel-coordinate report shape parsed as
    /// [`MouseReport::Sgr1016`].
    ///
    /// [xterm SGR pixels]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-SGR-Pixels
    SGRPixelsMouse = 1016,

    /// Mode 1036: make xterm send Escape before Meta-modified keys.
    ///
    /// This changes keyboard encoding so Meta-modified input is delivered as an Escape-prefixed
    /// sequence.
    XTermMetaSendsEscape = 1036,

    /// Mode 1039: make xterm send Escape before Alt-modified keys.
    ///
    /// This changes keyboard encoding so Alt-modified input is delivered as an Escape-prefixed
    /// sequence.
    XTermAltSendsEscape = 1039,

    /// Mode 1048: save cursor as in DECSC.
    ///
    /// Full-screen applications often combine this with alternate-screen modes so they can restore
    /// the user's cursor position on exit.
    SaveCursor = 1048,

    /// Mode 1049: clear and switch to the alternate screen.
    ///
    /// This is the common xterm private mode for full-screen applications. It switches away from
    /// the scrollback-backed main screen and clears the alternate screen.
    ClearAndEnableAlternateScreen = 1049,

    /// Mode 47: switch to the alternate screen.
    ///
    /// This is an older alternate-screen mode. New applications usually use mode 1049.
    EnableAlternateScreen = 47,

    /// Mode 1047: switch to the alternate screen using xterm's optional alternate-screen mode.
    ///
    /// Unlike mode 1049, this does not imply saving/restoring the cursor.
    OptEnableAlternateScreen = 1047,

    /// Mode 2004: enable bracketed paste mode.
    ///
    /// When enabled, pasted text is wrapped in explicit start/end markers so applications can
    /// distinguish paste from typed input. Termina parses the wrapped payload as
    /// [`crate::Event::Paste`]. xterm documents this mode as [bracketed paste mode].
    ///
    /// [bracketed paste mode]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Bracketed-Paste-Mode
    BracketedPaste = 2004,

    /// Mode 2027: grapheme clustering mode from [Contour Unicode core].
    ///
    /// [Contour Unicode core]: https://github.com/contour-terminal/terminal-unicode-core/
    GraphemeClustering = 2027,

    /// Mode 2031: theme notification mode from [Contour color-palette notifications].
    ///
    /// [Contour color-palette notifications]: https://github.com/contour-terminal/contour/
    Theme = 2031,

    /// Mode 1070: use private color registers for each sixel or ReGIS graphic.
    ///
    /// This keeps image color registers local to a graphic instead of sharing them globally across
    /// the terminal session.
    UsePrivateColorRegistersForEachGraphic = 1070,

    /// Mode 2026: [Synchronized output proposal] mode.
    ///
    /// [Synchronized output proposal]: https://gist.github.com/christianparpart/
    SynchronizedOutput = 2026,

    /// Mode 7727: enable MinTTY application Escape key mode.
    ///
    /// This MinTTY extension changes how the Escape key is encoded in application contexts.
    MinTTYApplicationEscapeKeyMode = 7727,

    /// Mode 8452: adjust cursor positioning after emitting sixel.
    ///
    /// This xterm mode controls whether sixel output advances the cursor as if the graphic
    /// occupied cells.
    SixelScrollsRight = 8452,

    /// Mode 9001: Windows Terminal win32-input-mode from [Microsoft terminal keyboard handling].
    ///
    /// [Microsoft terminal keyboard handling]: https://github.com/microsoft/terminal/
    Win32InputMode = 9001,
}

/// A standard terminal mode value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TerminalMode {
    /// A known standard terminal mode code.
    Code(TerminalModeCode),

    /// A standard terminal mode code not modeled by [`TerminalModeCode`].
    Unspecified(u16),
}

impl Display for TerminalMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let code = match *self {
            Self::Code(code) => code as u16,
            Self::Unspecified(code) => code,
        };
        write!(f, "{code}")
    }
}

/// Known standard terminal mode numbers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TerminalModeCode {
    /// Mode 2: [KAM] - Keyboard Action Mode.
    ///
    /// When set, keyboard input is disabled. Applications usually avoid this mode unless they are
    /// intentionally locking local keyboard entry.
    ///
    /// [KAM]: https://vt100.net/docs/vt510-rm/KAM.html
    KeyboardAction = 2,

    /// Mode 4: [IRM] - Insert Replace Mode.
    ///
    /// When set, printable characters insert at the cursor and shift existing content right.
    /// When reset, printable characters replace existing cells.
    ///
    /// [IRM]: https://vt100.net/docs/vt510-rm/IRM.html
    Insert = 4,

    /// Mode 8: bidirectional support mode.
    ///
    /// The terminal-wg bidi recommendation uses this mode to enable bidirectional text support in
    /// terminals that implement the proposal.
    ///
    /// [Terminal WG bidi recommendation]: https://terminal-wg.pages.freedesktop.org/bidi/
    BiDirectionalSupportMode = 8,

    /// Mode 12: [SRM] - Send Receive Mode.
    ///
    /// ECMA-48 defines this as local echo control. Microsoft terminal implementations also use
    /// mode 12 for cursor blinking, so callers should be aware of that compatibility difference.
    ///
    /// [SRM]: https://vt100.net/docs/vt510-rm/SRM.html
    SendReceive = 12,

    /// Mode 20: [LNM] - Line Feed/New Line Mode.
    ///
    /// When set, line feed also performs carriage return. When reset, line feed only moves to the
    /// next line.
    ///
    /// [LNM]: https://vt100.net/docs/vt510-rm/LNM.html
    AutomaticNewline = 20,

    /// Mode 25: Microsoft terminal cursor visibility.
    ///
    /// This mirrors the DEC private cursor-visibility mode in some Microsoft terminal contexts.
    ShowCursor = 25,
}

/// xterm key modifier resources addressed by `CSI > ... m`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XtermKeyModifierResource {
    /// Resource 0: xterm keyboard modifier keys.
    ///
    /// This resource controls xterm's general modified-key behavior.
    Keyboard = 0,

    /// Resource 1: xterm cursor-key modifier keys.
    ///
    /// This resource controls modified arrow-key and navigation-key encodings.
    CursorKeys = 1,

    /// Resource 2: xterm function-key modifier keys.
    ///
    /// This resource controls modified function-key encodings.
    FunctionKeys = 2,

    /// Resource 4: xterm other-key modifier keys.
    ///
    /// This resource controls modified-key encodings for keys outside the cursor/function groups.
    OtherKeys = 4,
}

/// Reported state for a DEC private mode query.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecModeSetting {
    /// Report value 0: the terminal does not recognize the requested mode.
    NotRecognized = 0,

    /// Report value 1: the mode is set.
    Set = 1,

    /// Report value 2: the mode is reset.
    Reset = 2,

    /// Report value 3: the mode is permanently set and cannot be changed.
    PermanentlySet = 3,

    /// Report value 4: the mode is permanently reset and cannot be changed.
    PermanentlyReset = 4,
}

/// Terminal theme values reported by the Contour theme extension.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThemeMode {
    /// Report value 1: the terminal is using a dark theme.
    Dark = 1,

    /// Report value 2: the terminal is using a light theme.
    Light = 2,
}

// Mouse

/// Mouse reports emitted by terminal mouse tracking modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseReport {
    /// An SGR 1006 mouse report using text-cell coordinates.
    ///
    /// This is the report format enabled by [`DecPrivateModeCode::SGRMouse`]. Coordinates are
    /// one-based terminal cell positions.
    Sgr1006 {
        /// The one-based cell column.
        x: u16,

        /// The one-based cell row.
        y: u16,

        /// The reported mouse button action.
        button: MouseButton,

        /// The modifiers active during the mouse event.
        modifiers: Modifiers,
    },

    /// An SGR 1016 mouse report using pixel coordinates.
    ///
    /// This is the report format enabled by [`DecPrivateModeCode::SGRPixelsMouse`]. Coordinates
    /// are pixel positions instead of terminal cell positions.
    Sgr1016 {
        /// The x coordinate in pixels.
        x_pixels: u16,

        /// The y coordinate in pixels.
        y_pixels: u16,

        /// The reported mouse button action.
        button: MouseButton,

        /// The modifiers active during the mouse event.
        modifiers: Modifiers,
    },
}

impl Display for MouseReport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MouseReport::Sgr1006 {
                x,
                y,
                button,
                modifiers,
            } => {
                let mut b = 0;
                // TODO: check this.
                if (*modifiers & Modifiers::SHIFT) != Modifiers::NONE {
                    b |= 4;
                }
                if (*modifiers & Modifiers::ALT) != Modifiers::NONE {
                    b |= 8;
                }
                if (*modifiers & Modifiers::CONTROL) != Modifiers::NONE {
                    b |= 16;
                }
                b |= match button {
                    MouseButton::Button1Press | MouseButton::Button1Release => 0,
                    MouseButton::Button2Press | MouseButton::Button2Release => 1,
                    MouseButton::Button3Press | MouseButton::Button3Release => 2,
                    MouseButton::Button4Press | MouseButton::Button4Release => 64,
                    MouseButton::Button5Press | MouseButton::Button5Release => 65,
                    MouseButton::Button6Press | MouseButton::Button6Release => 66,
                    MouseButton::Button7Press | MouseButton::Button7Release => 67,
                    MouseButton::Button1Drag => 32,
                    MouseButton::Button2Drag => 33,
                    MouseButton::Button3Drag => 34,
                    MouseButton::None => 35,
                };
                let trailer = match button {
                    MouseButton::Button1Press
                    | MouseButton::Button2Press
                    | MouseButton::Button3Press
                    | MouseButton::Button4Press
                    | MouseButton::Button5Press
                    | MouseButton::Button1Drag
                    | MouseButton::Button2Drag
                    | MouseButton::Button3Drag
                    | MouseButton::None => 'M',
                    _ => 'm',
                };
                write!(f, "<{b};{x};{y}{trailer}")
            }
            MouseReport::Sgr1016 {
                x_pixels,
                y_pixels,
                button,
                modifiers,
            } => {
                let mut b = 0;
                // TODO: check this.
                if (*modifiers & Modifiers::SHIFT) != Modifiers::NONE {
                    b |= 4;
                }
                if (*modifiers & Modifiers::ALT) != Modifiers::NONE {
                    b |= 8;
                }
                if (*modifiers & Modifiers::CONTROL) != Modifiers::NONE {
                    b |= 16;
                }
                b |= match button {
                    MouseButton::Button1Press | MouseButton::Button1Release => 0,
                    MouseButton::Button2Press | MouseButton::Button2Release => 1,
                    MouseButton::Button3Press | MouseButton::Button3Release => 2,
                    MouseButton::Button4Press | MouseButton::Button4Release => 64,
                    MouseButton::Button5Press | MouseButton::Button5Release => 65,
                    MouseButton::Button6Press | MouseButton::Button6Release => 66,
                    MouseButton::Button7Press | MouseButton::Button7Release => 67,
                    MouseButton::Button1Drag => 32,
                    MouseButton::Button2Drag => 33,
                    MouseButton::Button3Drag => 34,
                    MouseButton::None => 35,
                };
                let trailer = match button {
                    MouseButton::Button1Press
                    | MouseButton::Button2Press
                    | MouseButton::Button3Press
                    | MouseButton::Button4Press
                    | MouseButton::Button5Press
                    | MouseButton::Button1Drag
                    | MouseButton::Button2Drag
                    | MouseButton::Button3Drag
                    | MouseButton::None => 'M',
                    _ => 'm',
                };
                write!(f, "<{b};{x_pixels};{y_pixels}{trailer}")
            }
        }
    }
}

/// Mouse button actions encoded in SGR mouse reports.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseButton {
    /// Button 1 was pressed; encoded with button value 0 and trailer `M`.
    Button1Press,

    /// Button 2 was pressed; encoded with button value 1 and trailer `M`.
    Button2Press,

    /// Button 3 was pressed; encoded with button value 2 and trailer `M`.
    Button3Press,

    /// Button 4 was pressed; encoded with button value 64 and trailer `M`.
    Button4Press,

    /// Button 5 was pressed; encoded with button value 65 and trailer `M`.
    Button5Press,

    /// Button 6 was pressed; encoded with button value 66.
    Button6Press,

    /// Button 7 was pressed; encoded with button value 67.
    Button7Press,

    /// Button 1 was released; encoded with button value 0 and trailer `m`.
    Button1Release,

    /// Button 2 was released; encoded with button value 1 and trailer `m`.
    Button2Release,

    /// Button 3 was released; encoded with button value 2 and trailer `m`.
    Button3Release,

    /// Button 4 was released; encoded with button value 64 and trailer `m`.
    Button4Release,

    /// Button 5 was released; encoded with button value 65 and trailer `m`.
    Button5Release,

    /// Button 6 was released; encoded with button value 66.
    Button6Release,

    /// Button 7 was released; encoded with button value 67.
    Button7Release,

    /// Button 1 was dragged; encoded with button value 32 and trailer `M`.
    Button1Drag,

    /// Button 2 was dragged; encoded with button value 33 and trailer `M`.
    Button2Drag,

    /// Button 3 was dragged; encoded with button value 34 and trailer `M`.
    Button3Drag,

    /// No mouse button was involved; encoded with button value 35 and trailer `M`.
    None,
}

// --- Kitty keyboard protocol ---
//
// <https://sw.kovidgoyal.net/kitty/keyboard-protocol/>.

bitflags::bitflags! {
    /// Feature flags for the Kitty keyboard protocol.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct KittyKeyboardFlags: u8 {
        /// No keyboard enhancement flags.
        const NONE = 0;

        /// Disambiguate escape codes for keys that otherwise share encodings.
        const DISAMBIGUATE_ESCAPE_CODES = 1;

        /// Report press, release, and repeat event types.
        const REPORT_EVENT_TYPES = 2;

        /// Report alternate key values.
        const REPORT_ALTERNATE_KEYS = 4;

        /// Report all keys as escape codes.
        const REPORT_ALL_KEYS_AS_ESCAPE_CODES = 8;

        /// Report associated text for key events.
        const REPORT_ASSOCIATED_TEXT = 16;
    }
}

impl Display for KittyKeyboardFlags {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.bits())
    }
}

/// CSI sequences for interacting with the [Kitty Keyboard Protocol].
///
/// [Kitty Keyboard Protocol]: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
///
/// Note that the Kitty Keyboard Protocol requires terminals to maintain different stacks for the
/// main and alternate screens. This means that applications which use alternate screens do not
/// necessarily need to pop flags (via [`Self::PopFlags`]) when exiting. By entering the main screen
/// the flags must be automatically reset by the terminal. Any flags which were pushed, however,
/// will remain active in the alternate screen, even if the alternate screen is entered by a
/// different application. Pop flags during shutdown when the application pushed flags itself.
///
/// ```
/// use termina::escape::csi::{
///     Csi, Keyboard, KittyKeyboardFlags, SetKeyboardFlagsMode,
/// };
///
/// let command = Keyboard::SetFlags {
///     flags: KittyKeyboardFlags::REPORT_EVENT_TYPES,
///     mode: SetKeyboardFlagsMode::SetSpecified,
/// };
/// assert_eq!(Csi::Keyboard(command).to_string(), "\x1b[=2;2u");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Keyboard {
    /// Query the current values of the flags.
    QueryFlags,

    /// A report from the terminal declaring which flags are currently set.
    ReportFlags(KittyKeyboardFlags),

    /// Pushes the given flags onto the terminal's stack.
    PushFlags(KittyKeyboardFlags),

    /// Pops the given number of stack entries from the terminal's stack.
    PopFlags(u8),

    /// Requests keyboard enhancement with the given flags according to the mode.
    ///
    /// Also see [SetKeyboardFlagsMode].
    SetFlags {
        /// The flags to assign, set, or clear.
        flags: KittyKeyboardFlags,
        /// How the terminal should apply `flags`.
        mode: SetKeyboardFlagsMode,
    },
}

impl Display for Keyboard {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::QueryFlags => write!(f, "?u"),
            // NOTE: this is sent by the terminal, not meant to be sent by the application.
            Self::ReportFlags(flags) => write!(f, "?{flags}u"),
            Self::PushFlags(flags) => write!(f, ">{flags}u"),
            Self::PopFlags(number) => write!(f, "<{number}u"),
            Self::SetFlags { flags, mode } => write!(f, "={flags};{mode}u"),
        }
    }
}

/// Controls how the flags passed in [Keyboard::SetFlags] are interpreted by the terminal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SetKeyboardFlagsMode {
    /// Request any of the given flags and reset any flags which are not given.
    AssignAll = 1,

    /// Request the given flags and ignore any flags which are not given.
    SetSpecified = 2,

    /// Clear the given flags and ignore any flags which are not given.
    ClearSpecified = 3,
}

impl Display for SetKeyboardFlagsMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", *self as u8)
    }
}

/// Device and status CSI commands.
///
/// ```
/// use termina::escape::csi::{Csi, Device};
///
/// assert_eq!(Csi::Device(Device::StatusReport).to_string(), "\x1b[5n");
/// assert_eq!(
///     Csi::Device(Device::RequestPrimaryDeviceAttributes).to_string(),
///     "\x1b[c",
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Device {
    /// A device-attributes response.
    DeviceAttributes(()),

    /// [DECSTR] - soft terminal reset.
    ///
    /// [DECSTR]: https://vt100.net/docs/vt510-rm/DECSTR.html
    SoftReset,

    /// Request primary device attributes.
    RequestPrimaryDeviceAttributes,

    /// Request secondary device attributes.
    RequestSecondaryDeviceAttributes,

    /// Request tertiary device attributes.
    RequestTertiaryDeviceAttributes,

    /// Request terminal status.
    StatusReport,

    /// Request the terminal name and version.
    ///
    /// Mintty and GNOME VTE discuss this query in [Mintty issue #881] and [GNOME VTE issue #235].
    ///
    /// [Mintty issue #881]: https://github.com/mintty/mintty/issues/881
    /// [GNOME VTE issue #235]: https://gitlab.gnome.org/GNOME/vte/-/issues/235
    RequestTerminalNameAndVersion,

    /// Request terminal parameters.
    RequestTerminalParameters(i64),
}

impl Display for Device {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DeviceAttributes(_) => unimplemented!(),
            Self::SoftReset => write!(f, "!p"),
            Self::RequestPrimaryDeviceAttributes => write!(f, "c"),
            Self::RequestSecondaryDeviceAttributes => write!(f, ">c"),
            Self::RequestTertiaryDeviceAttributes => write!(f, "=c"),
            Self::StatusReport => write!(f, "5n"),
            Self::RequestTerminalNameAndVersion => write!(f, ">q"),
            Self::RequestTerminalParameters(n) => write!(f, "{};1;1;128;128;1;0x", n + 2),
        }
    }
}

// Window

/// Window manipulation and window report CSI commands.
///
/// ```
/// use termina::escape::csi::{Csi, Window};
///
/// assert_eq!(
///     Csi::Window(Box::new(Window::ReportWindowTitle)).to_string(),
///     "\x1b[21t",
/// );
/// assert_eq!(
///     Csi::Window(Box::new(Window::ResizeWindowCells {
///         width: Some(120),
///         height: Some(40),
///     }))
///     .to_string(),
///     "\x1b[8;40;120t",
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Window {
    /// De-iconify the window.
    DeIconify,

    /// Iconify the window.
    Iconify,

    /// Move the window to a pixel position.
    MoveWindow {
        /// The x coordinate in pixels.
        x: i64,

        /// The y coordinate in pixels.
        y: i64,
    },

    /// Resize the window to a pixel size.
    ResizeWindowPixels {
        /// The desired width in pixels.
        width: Option<i64>,

        /// The desired height in pixels.
        height: Option<i64>,
    },

    /// Raise the window.
    RaiseWindow,

    /// Lower the window.
    LowerWindow,

    /// Refresh the window.
    RefreshWindow,

    /// Resize the window to a cell size.
    ResizeWindowCells {
        /// The desired width in cells.
        width: Option<i64>,

        /// The desired height in cells.
        height: Option<i64>,
    },

    /// Restore a maximized window.
    RestoreMaximizedWindow,

    /// Maximize the window.
    MaximizeWindow,

    /// Maximize the window vertically.
    MaximizeWindowVertically,

    /// Maximize the window horizontally.
    MaximizeWindowHorizontally,

    /// Leave fullscreen mode.
    UndoFullScreenMode,

    /// Enter fullscreen mode.
    ChangeToFullScreenMode,

    /// Toggle fullscreen mode.
    ToggleFullScreen,

    /// Request the window state.
    ReportWindowState,

    /// Request the window position.
    ReportWindowPosition,

    /// Request the text-area position.
    ReportTextAreaPosition,

    /// Request the text-area size in pixels.
    ReportTextAreaSizePixels,

    /// Request the window size in pixels.
    ReportWindowSizePixels,

    /// Request the screen size in pixels.
    ReportScreenSizePixels,

    /// Request the cell size in pixels.
    ReportCellSizePixels,

    /// Report the cell size in pixels.
    ReportCellSizePixelsResponse {
        /// The reported cell width in pixels.
        width: Option<i64>,

        /// The reported cell height in pixels.
        height: Option<i64>,
    },

    /// Request the text-area size in cells.
    ReportTextAreaSizeCells,

    /// Request the screen size in cells.
    ReportScreenSizeCells,

    /// Request the icon label.
    ReportIconLabel,

    /// Request the window title.
    ReportWindowTitle,

    /// Push the icon and window title onto the title stack.
    PushIconAndWindowTitle,

    /// Push the icon title onto the title stack.
    PushIconTitle,

    /// Push the window title onto the title stack.
    PushWindowTitle,

    /// Pop the icon and window title from the title stack.
    PopIconAndWindowTitle,

    /// Pop the icon title from the title stack.
    PopIconTitle,

    /// Pop the window title from the title stack.
    PopWindowTitle,

    /// DECRQCRA; used by esctest
    ChecksumRectangularArea {
        /// The checksum request identifier.
        request_id: i64,

        /// The page number to checksum.
        page_number: i64,

        /// The top row of the rectangular area.
        top: OneBased,

        /// The left column of the rectangular area.
        left: OneBased,

        /// The bottom row of the rectangular area.
        bottom: OneBased,

        /// The right column of the rectangular area.
        right: OneBased,
    },
}

impl Display for Window {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        struct NumstrOrEmpty(Option<i64>);
        impl Display for NumstrOrEmpty {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                if let Some(x) = self.0 {
                    write!(f, "{x}")?
                }
                Ok(())
            }
        }

        match self {
            Window::DeIconify => write!(f, "1t"),
            Window::Iconify => write!(f, "2t"),
            Window::MoveWindow { x, y } => write!(f, "3;{x};{y}t"),
            Window::ResizeWindowPixels { width, height } => {
                write!(f, "4;{};{}t", NumstrOrEmpty(*height), NumstrOrEmpty(*width))
            }
            Window::RaiseWindow => write!(f, "5t"),
            Window::LowerWindow => write!(f, "6t"),
            Window::RefreshWindow => write!(f, "7t"),
            Window::ResizeWindowCells { width, height } => {
                write!(f, "8;{};{}t", NumstrOrEmpty(*height), NumstrOrEmpty(*width))
            }
            Window::RestoreMaximizedWindow => write!(f, "9;0t"),
            Window::MaximizeWindow => write!(f, "9;1t"),
            Window::MaximizeWindowVertically => write!(f, "9;2t"),
            Window::MaximizeWindowHorizontally => write!(f, "9;3t"),
            Window::UndoFullScreenMode => write!(f, "10;0t"),
            Window::ChangeToFullScreenMode => write!(f, "10;1t"),
            Window::ToggleFullScreen => write!(f, "10;2t"),
            Window::ReportWindowState => write!(f, "11t"),
            Window::ReportWindowPosition => write!(f, "13t"),
            Window::ReportTextAreaPosition => write!(f, "13;2t"),
            Window::ReportTextAreaSizePixels => write!(f, "14t"),
            Window::ReportWindowSizePixels => write!(f, "14;2t"),
            Window::ReportScreenSizePixels => write!(f, "15t"),
            Window::ReportCellSizePixels => write!(f, "16t"),
            Window::ReportCellSizePixelsResponse { width, height } => {
                write!(f, "6;{};{}t", NumstrOrEmpty(*height), NumstrOrEmpty(*width))
            }
            Window::ReportTextAreaSizeCells => write!(f, "18t"),
            Window::ReportScreenSizeCells => write!(f, "19t"),
            Window::ReportIconLabel => write!(f, "20t"),
            Window::ReportWindowTitle => write!(f, "21t"),
            Window::PushIconAndWindowTitle => write!(f, "22;0t"),
            Window::PushIconTitle => write!(f, "22;1t"),
            Window::PushWindowTitle => write!(f, "22;2t"),
            Window::PopIconAndWindowTitle => write!(f, "23;0t"),
            Window::PopIconTitle => write!(f, "23;1t"),
            Window::PopWindowTitle => write!(f, "23;2t"),
            Window::ChecksumRectangularArea {
                request_id,
                page_number,
                top,
                left,
                bottom,
                right,
            } => write!(
                f,
                "{request_id};{page_number};{top};{left};{bottom};{right}*y"
            ),
        }
    }
}

#[cfg(test)]
mod test {
    use crate::style::RgbColor;

    use super::*;

    const ENTER_ALTERNATE_SCREEN: Csi = Csi::Mode(Mode::SetDecPrivateMode(DecPrivateMode::Code(
        DecPrivateModeCode::ClearAndEnableAlternateScreen,
    )));

    const EXIT_ALTERNATE_SCREEN: Csi = Csi::Mode(Mode::ResetDecPrivateMode(DecPrivateMode::Code(
        DecPrivateModeCode::ClearAndEnableAlternateScreen,
    )));

    #[test]
    fn encoding() {
        // Enter the alternate screen using the mode part of CSI.
        // <https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#alternate-screen-buffer>
        assert_eq!("\x1b[?1049h", ENTER_ALTERNATE_SCREEN.to_string());
        assert_eq!("\x1b[?1049l", EXIT_ALTERNATE_SCREEN.to_string());

        // Push Kitty keyboard flags used by Helix and Kakoune at time of writing.
        assert_eq!(
            "\x1b[>5u",
            Csi::Keyboard(Keyboard::PushFlags(
                KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES
                    | KittyKeyboardFlags::REPORT_ALTERNATE_KEYS
            ))
            .to_string()
        );

        // Common SGR: turn the text (i.e. foreground) green
        assert_eq!(
            "\x1b[32m",
            Csi::Sgr(Sgr::Foreground(ColorSpec::GREEN)).to_string(),
        );
        // ... and then reset to turn off the green.
        assert_eq!(
            "\x1b[39m",
            Csi::Sgr(Sgr::Foreground(ColorSpec::Reset)).to_string(),
        );

        // Push current window title to the terminal's stack.
        assert_eq!(
            "\x1b[22;0t",
            Csi::Window(Box::new(Window::PushIconAndWindowTitle)).to_string(),
        );
        // ... and pop it.
        assert_eq!(
            "\x1b[23;0t",
            Csi::Window(Box::new(Window::PopIconAndWindowTitle)).to_string(),
        );

        // Set the cursor style to the terminal's default.
        // <https://terminalguide.namepad.de/seq/csi_sq_t_space/>
        assert_eq!(
            "\x1b[0 q",
            Csi::Cursor(Cursor::CursorStyle(CursorStyle::Default)).to_string()
        );
    }

    #[test]
    fn sgr_attributes_csi_param_limit() {
        let mut attributes = SgrAttributes {
            foreground: Some(ColorSpec::TrueColor(RgbColor::new(80, 100, 120).into())),
            background: Some(ColorSpec::TrueColor(RgbColor::new(80, 100, 120).into())),
            underline_color: Some(ColorSpec::TrueColor(RgbColor::new(80, 100, 120).into())),
            modifiers: SgrModifiers::UNDERLINE_CURLY,
            ..Default::default()
        };
        // The sequence must be chunked into two since the chunk size is exceeded.
        // Here it is perfectly chunked so that the foreground and background are a full chunk.
        let expected = "\x1b[38;2;80;100;120;48;2;80;100;120m\x1b[58:2::80:100:120;4:3m";
        assert_eq!(expected, Csi::Sgr(Sgr::Attributes(attributes)).to_string());
        // If we make the chunk size bigger, we still chunk the same way. We wouldn't cut an SGR
        // sequence up in the middle: that would make it nonsense.
        attributes.parameter_chunk_size = NonZeroU16::new(12).unwrap();
        assert_eq!(expected, Csi::Sgr(Sgr::Attributes(attributes)).to_string());
    }

    #[test]
    fn multi_cursor_encoding() {
        // QueryCursorShape
        assert_eq!(
            "\x1b[> q",
            Csi::Cursor(Cursor::QueryCursorShape).to_string()
        );

        // CursorShapeQueryResponse with capability codes
        assert_eq!(
            "\x1b[>1;2;29;100 q",
            Csi::Cursor(Cursor::CursorShapeQueryResponse(vec![
                MultiCursorCapability::BlockShape,
                MultiCursorCapability::BeamShape,
                MultiCursorCapability::FollowMainCursorShape,
                MultiCursorCapability::QueryCurrentCursors,
            ]))
            .to_string()
        );

        // SetMultipleCursors with MultiCursorShape::FollowMainCursor
        assert_eq!(
            "\x1b[>29;2:1:1;2:2:5 q",
            Csi::Cursor(Cursor::SetMultipleCursors {
                shape: MultiCursorShape::FollowMainCursor,
                positions: vec![
                    (OneBased::new(1).unwrap(), OneBased::new(1).unwrap()),
                    (OneBased::new(2).unwrap(), OneBased::new(5).unwrap()),
                ],
            })
            .to_string()
        );

        // SetMultipleCursors with MultiCursorShape::Style
        assert_eq!(
            "\x1b[>2;2:3:10 q",
            Csi::Cursor(Cursor::SetMultipleCursors {
                shape: MultiCursorShape::Style(CursorStyle::SteadyBlock),
                positions: vec![(OneBased::new(3).unwrap(), OneBased::new(10).unwrap()),],
            })
            .to_string()
        );

        // ClearSecondaryCursors
        assert_eq!(
            "\x1b[>0;4 q",
            Csi::Cursor(Cursor::ClearSecondaryCursors).to_string()
        );
    }
}