tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
//! List widget.

use std::sync::Arc;

use crate::callback::{Callback, KeyHandler};
use crate::core::element::{Element, ElementKind};
use crate::core::event::{KeyEvent, MouseEvent};
use crate::style::{BorderStyle, Length, Padding, ScrollbarConfig, Span, Style, StyleSlot};
use unicode_width::UnicodeWidthStr;

use super::{Spinner, SpinnerSpeed, SpinnerStyle};
use crate::widgets::scroll::{ScrollAction, ScrollKeymap, scroll_action_from_key};

/// Shared configuration for the inner [`List`] used by composite widgets
/// like [`Select`](super::Select), [`ComboBox`](super::ComboBox),
/// [`MultiSelect`](super::MultiSelect), and [`SearchPalette`](super::SearchPalette).
///
/// Each of these widgets composes a `List` and proxies many list-related style
/// fields. `ListConfig` deduplicates the common subset so that a single struct
/// can carry them all.
///
/// Individual convenience setters (e.g. `.list_style(...)`) are still available
/// on each widget; they delegate to the embedded `ListConfig`.
#[derive(Clone, Debug, PartialEq)]
pub struct ListConfig {
    /// Whether to draw a border around the list.
    pub border: bool,
    /// Border style.
    /// Default: `BorderStyle::Plain`.
    pub border_style: BorderStyle,
    /// Inner padding.
    /// Default: `Padding::default()`.
    pub padding: Padding,
    /// Base style.
    pub style: Style,
    /// Style applied to the active_index (selected) item.
    pub selection_style: StyleSlot,
    /// Style applied to the selected item while the list is not focused.
    ///
    /// When unset, `selection_style` is used regardless of focus state.
    pub unfocused_selection_style: StyleSlot,
    /// Per-row hover style. `None` lets each host widget apply its own default
    /// (several fall back to `selection_style`).
    pub item_hover_style: Option<StyleSlot>,
    /// Whether the highlight spans the full row width.
    pub selection_full_width: bool,
    /// Symbol shown on the active_index item.
    pub selection_symbol: Option<Arc<str>>,
    /// Trailing symbol after the selected item's label (pairs with
    /// `selection_symbol` for "pill" caps). Shares `selection_symbol_style`.
    pub selection_symbol_right: Option<Arc<str>>,
    /// Style for the highlight symbol.
    pub selection_symbol_style: Option<Style>,
    /// Style for the highlight symbol while the list is not focused.
    ///
    /// When unset, `selection_symbol_style` is used regardless of focus state.
    pub unfocused_selection_symbol_style: Option<Style>,
    /// Whether to enable the left symbol/status column.
    pub symbol_column: bool,
    /// Explicit gap after the widest item gutter.
    pub gutter_gap: u16,
    /// Whether non-selectable rows participate in gutter alignment.
    pub gutter_for_non_selectable: bool,
    /// Left/right padding for normal rows (interior to the selection highlight).
    pub item_horizontal_padding: Padding,
    /// Left/right padding for header rows.
    pub header_horizontal_padding: Padding,
    /// Style for the empty-state placeholder text.
    pub empty_text_style: Style,
    /// Whether to show a vertical scrollbar when content overflows.
    pub scrollbar: bool,
    /// Scrollbar configuration.
    pub scrollbar_config: ScrollbarConfig,
}

impl Default for ListConfig {
    fn default() -> Self {
        Self {
            border: true,
            border_style: BorderStyle::Plain,
            padding: Padding::default(),
            style: Style::default(),
            selection_style: StyleSlot::Inherit,
            unfocused_selection_style: StyleSlot::Inherit,
            item_hover_style: None,
            selection_full_width: false,
            selection_symbol: None,
            selection_symbol_right: None,
            selection_symbol_style: None,
            unfocused_selection_symbol_style: None,
            symbol_column: true,
            gutter_gap: 0,
            gutter_for_non_selectable: false,
            item_horizontal_padding: Padding::default(),
            header_horizontal_padding: Padding::default(),
            empty_text_style: Style::default(),
            scrollbar: false,
            scrollbar_config: ScrollbarConfig::default(),
        }
    }
}

impl ListConfig {
    /// Create a new `ListConfig` with defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set border visibility.
    pub fn border(mut self, border: bool) -> Self {
        self.border = border;
        self
    }

    /// Set border style.
    pub fn border_style(mut self, style: BorderStyle) -> Self {
        self.border_style = style;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
        self.padding = padding.into();
        self
    }

    /// Set base style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set highlight style.
    pub fn selection_style(mut self, style: Style) -> Self {
        self.selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the theme selection style with this partial overlay.
    pub fn extend_selection_style(mut self, style: Style) -> Self {
        self.selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the active theme selection style.
    pub fn inherit_selection_style(mut self) -> Self {
        self.selection_style = StyleSlot::Inherit;
        self
    }

    /// Set highlight style while the list is not focused.
    pub fn unfocused_selection_style(mut self, style: Style) -> Self {
        self.unfocused_selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the theme unfocused-selection style with this partial overlay.
    pub fn extend_unfocused_selection_style(mut self, style: Style) -> Self {
        self.unfocused_selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the active theme unfocused-selection style.
    pub fn inherit_unfocused_selection_style(mut self) -> Self {
        self.unfocused_selection_style = StyleSlot::Inherit;
        self
    }

    /// Set whether highlight spans the full row width.
    pub fn selection_full_width(mut self, full_width: bool) -> Self {
        self.selection_full_width = full_width;
        self
    }

    /// Set highlight symbol.
    pub fn selection_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.selection_symbol = symbol.map(Into::into);
        self
    }

    /// Set the trailing selection symbol (right "pill" cap). Pairs with
    /// [`Self::selection_symbol`] and shares [`Self::selection_symbol_style`].
    pub fn selection_symbol_right(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.selection_symbol_right = symbol.map(Into::into);
        self
    }

    /// Set highlight symbol style.
    pub fn selection_symbol_style(mut self, style: Style) -> Self {
        self.selection_symbol_style = Some(style);
        self
    }

    /// Set highlight symbol style while the list is not focused.
    pub fn unfocused_selection_symbol_style(mut self, style: Style) -> Self {
        self.unfocused_selection_symbol_style = Some(style);
        self
    }

    /// Set whether the left symbol/status column is enabled.
    pub fn symbol_column(mut self, enabled: bool) -> Self {
        self.symbol_column = enabled;
        self
    }

    /// Set the explicit gap after item gutters.
    pub fn gutter_gap(mut self, gap: u16) -> Self {
        self.gutter_gap = gap;
        self
    }

    /// Set whether headers/spacers participate in gutter column alignment.
    pub fn gutter_for_non_selectable(mut self, enabled: bool) -> Self {
        self.gutter_for_non_selectable = enabled;
        self
    }

    /// Set the per-row hover style.
    pub fn item_hover_style(mut self, style: Style) -> Self {
        self.item_hover_style = Some(StyleSlot::Replace(style));
        self
    }

    /// Set the per-row hover style slot directly.
    pub fn item_hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.item_hover_style = Some(slot);
        self
    }

    /// Set left/right padding for normal rows (interior to the highlight).
    pub fn item_horizontal_padding(mut self, padding: impl Into<Padding>) -> Self {
        self.item_horizontal_padding = padding.into();
        self
    }

    /// Set left/right padding for header rows.
    pub fn header_horizontal_padding(mut self, padding: impl Into<Padding>) -> Self {
        self.header_horizontal_padding = padding.into();
        self
    }

    /// Set the empty-state placeholder text style.
    pub fn empty_text_style(mut self, style: Style) -> Self {
        self.empty_text_style = style;
        self
    }

    /// Set scrollbar visibility.
    pub fn scrollbar(mut self, scrollbar: bool) -> Self {
        self.scrollbar = scrollbar;
        self
    }

    /// Set scrollbar configuration.
    pub fn scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
        self.scrollbar_config = config;
        self
    }
}

/// A list selection event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ListEvent {
    /// Selected item index.
    pub index: usize,
}

/// Semantic role of a list row.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ListItemRole {
    /// Regular selectable row.
    #[default]
    Normal,
    /// Non-selectable section/header row.
    Header,
    /// Non-selectable blank spacer row.
    Spacer,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum ListItemPrefixKind {
    Plain,
    Numbered(usize),
}

/// Placement of list symbols relative to the main label content.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ListSymbolPosition {
    /// Render the symbol in the left symbol column.
    #[default]
    Left,
    /// Render the symbol immediately after the label content.
    Right,
}

/// A fixed-width left gutter rendered before a list row label.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListItemGutter {
    pub(crate) kind: ListItemGutterKind,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ListItemGutterKind {
    Text(Vec<Span>),
    Spinner(ListItemSpinnerGutter),
}

/// Per-row status content rendered in the list symbol column.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListItemStatus {
    pub(crate) kind: ListItemStatusKind,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ListItemStatusKind {
    Text(Vec<Span>),
    Spinner(ListItemSpinnerGutter),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ListItemSpinnerGutter {
    pub spinner_style: SpinnerStyle,
    pub speed: SpinnerSpeed,
    pub frame: usize,
    pub auto_frame: bool,
    pub label: Option<Arc<str>>,
    pub gap: u16,
    pub style: Style,
    pub label_style: Style,
}

impl ListItemGutter {
    /// Create a plain text gutter.
    pub fn text(text: impl Into<Arc<str>>) -> Self {
        Self::from_spans([Span::new(text)])
    }

    /// Create a rich-text gutter.
    pub fn from_spans(spans: impl IntoIterator<Item = Span>) -> Self {
        Self {
            kind: ListItemGutterKind::Text(spans.into_iter().collect()),
        }
    }

    /// Create a spinner gutter.
    pub fn spinner(spinner: Spinner) -> Self {
        spinner.into()
    }

    pub(crate) fn width(&self) -> u16 {
        match &self.kind {
            ListItemGutterKind::Text(spans) => spans
                .iter()
                .map(|span| UnicodeWidthStr::width(span.content.as_ref()))
                .sum::<usize>()
                .min(u16::MAX as usize) as u16,
            ListItemGutterKind::Spinner(spinner) => {
                let label_width = spinner
                    .label
                    .as_ref()
                    .map(|label| UnicodeWidthStr::width(label.as_ref()) as u16)
                    .unwrap_or(0);
                let gap = if label_width > 0 { spinner.gap } else { 0 };
                spinner
                    .spinner_style
                    .width()
                    .saturating_add(gap)
                    .saturating_add(label_width)
            }
        }
    }

    pub(crate) fn has_spinner(&self) -> bool {
        matches!(self.kind, ListItemGutterKind::Spinner(_))
    }

    pub(crate) fn spinner_mut(&mut self) -> Option<&mut ListItemSpinnerGutter> {
        match &mut self.kind {
            ListItemGutterKind::Spinner(spinner) => Some(spinner),
            ListItemGutterKind::Text(_) => None,
        }
    }
}

impl From<Spinner> for ListItemGutter {
    fn from(spinner: Spinner) -> Self {
        Self {
            kind: ListItemGutterKind::Spinner(ListItemSpinnerGutter {
                spinner_style: spinner.spinner_style,
                speed: spinner.speed,
                frame: spinner.frame.unwrap_or(0),
                auto_frame: spinner.frame.is_none(),
                label: spinner.label,
                gap: spinner.gap,
                style: spinner.style,
                label_style: spinner.label_style,
            }),
        }
    }
}

impl ListItemStatus {
    /// Create a plain-text status symbol.
    pub fn text(text: impl Into<Arc<str>>) -> Self {
        Self::from_spans([Span::new(text)])
    }

    /// Create a rich-text status symbol.
    pub fn from_spans(spans: impl IntoIterator<Item = Span>) -> Self {
        Self {
            kind: ListItemStatusKind::Text(spans.into_iter().collect()),
        }
    }

    /// Create an animated spinner status symbol.
    pub fn spinner(spinner: Spinner) -> Self {
        spinner.into()
    }

    pub(crate) fn width(&self) -> u16 {
        match &self.kind {
            ListItemStatusKind::Text(spans) => spans
                .iter()
                .map(|span| UnicodeWidthStr::width(span.content.as_ref()))
                .sum::<usize>()
                .min(u16::MAX as usize) as u16,
            ListItemStatusKind::Spinner(spinner) => spinner.spinner_style.width(),
        }
    }

    pub(crate) fn has_spinner(&self) -> bool {
        matches!(self.kind, ListItemStatusKind::Spinner(_))
    }

    pub(crate) fn spinner_mut(&mut self) -> Option<&mut ListItemSpinnerGutter> {
        match &mut self.kind {
            ListItemStatusKind::Spinner(spinner) => Some(spinner),
            ListItemStatusKind::Text(_) => None,
        }
    }
}

impl From<Spinner> for ListItemStatus {
    fn from(spinner: Spinner) -> Self {
        Self {
            kind: ListItemStatusKind::Spinner(ListItemSpinnerGutter {
                spinner_style: spinner.spinner_style,
                speed: spinner.speed,
                frame: spinner.frame.unwrap_or(0),
                auto_frame: spinner.frame.is_none(),
                label: None,
                gap: 0,
                style: spinner.style,
                label_style: spinner.label_style,
            }),
        }
    }
}

/// A list item.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListItem {
    pub(crate) spans: Vec<Span>,
    pub(crate) description_spans: Vec<Span>,
    pub(crate) extra_lines: Vec<ListItemLine>,
    pub(crate) status: Option<ListItemStatus>,
    pub(crate) gutter: Option<ListItemGutter>,
    pub(crate) gutter_line: usize,
    pub(crate) prefix: Option<Arc<str>>,
    pub(crate) prefix_kind: ListItemPrefixKind,
    pub(crate) prefix_style: Option<Style>,
    pub(crate) extra_line_indent: u16,
    pub(crate) style: Style,
    pub(crate) role: ListItemRole,
    pub(crate) active: bool,
    pub(crate) primary_selection_label: bool,
    pub(crate) primary_selection_description: bool,
    pub(crate) primary_hover_label: bool,
    pub(crate) primary_hover_description: bool,
    pub(crate) primary_truncate_description_first: bool,
    pub(crate) primary_wrap_label: bool,
    pub(crate) primary_wrap_description: bool,
    pub(crate) primary_max_label_width: Option<u16>,
    pub(crate) primary_max_description_width: Option<u16>,
    pub(crate) symbol_line: usize,
}

/// An additional rendered line for a [`ListItem`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListItemLine {
    pub(crate) spans: Vec<Span>,
    pub(crate) description_spans: Vec<Span>,
    pub(crate) style: Style,
    pub(crate) selection_label: bool,
    pub(crate) selection_description: bool,
    pub(crate) hover_label: bool,
    pub(crate) hover_description: bool,
    pub(crate) truncate_description_first: bool,
    pub(crate) wrap_label: bool,
    pub(crate) wrap_description: bool,
    pub(crate) max_label_width: Option<u16>,
    pub(crate) max_description_width: Option<u16>,
}

impl ListItemLine {
    /// Create a new line.
    pub fn new(content: impl Into<Arc<str>>) -> Self {
        Self {
            spans: vec![Span::new(content)],
            description_spans: Vec::new(),
            style: Style::default(),
            selection_label: true,
            selection_description: true,
            hover_label: true,
            hover_description: true,
            truncate_description_first: false,
            wrap_label: false,
            wrap_description: false,
            max_label_width: None,
            max_description_width: None,
        }
    }

    /// Create from multiple spans.
    pub fn from_spans(spans: impl IntoIterator<Item = Span>) -> Self {
        Self {
            spans: spans.into_iter().collect(),
            description_spans: Vec::new(),
            style: Style::default(),
            selection_label: true,
            selection_description: true,
            hover_label: true,
            hover_description: true,
            truncate_description_first: false,
            wrap_label: false,
            wrap_description: false,
            max_label_width: None,
            max_description_width: None,
        }
    }

    /// Add description (right-aligned) content.
    pub fn description_spans(mut self, spans: impl IntoIterator<Item = Span>) -> Self {
        self.description_spans = spans.into_iter().collect();
        self
    }

    /// Set description as plain text (creates a single unstyled span).
    pub fn description(mut self, text: impl Into<Arc<str>>) -> Self {
        self.description_spans = vec![Span::new(text)];
        self
    }

    /// Apply a style to all current description spans.
    pub fn description_style(mut self, style: Style) -> Self {
        for span in &mut self.description_spans {
            span.style = style;
        }
        self
    }

    /// Set line style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Control whether selection highlight applies to the label side.
    pub fn selection_label(mut self, highlight: bool) -> Self {
        self.selection_label = highlight;
        self
    }

    /// Control whether selection highlight applies to the description side.
    pub fn selection_description(mut self, highlight: bool) -> Self {
        self.selection_description = highlight;
        self
    }

    /// Control whether hover style applies to the label side.
    pub fn hover_label(mut self, hover: bool) -> Self {
        self.hover_label = hover;
        self
    }

    /// Control whether hover style applies to the description side.
    pub fn hover_description(mut self, hover: bool) -> Self {
        self.hover_description = hover;
        self
    }

    /// Prefer truncating description content before label content.
    pub fn truncate_description_first(mut self, truncate: bool) -> Self {
        self.truncate_description_first = truncate;
        self
    }

    /// Wrap label content onto additional lines when needed.
    pub fn wrap_label(mut self, wrap: bool) -> Self {
        self.wrap_label = wrap;
        self
    }

    /// Wrap description content onto extra lines when needed.
    pub fn wrap_description(mut self, wrap: bool) -> Self {
        self.wrap_description = wrap;
        self
    }

    /// Limit the label to at most this many columns. Surplus space goes to the description.
    pub fn max_label_width(mut self, width: u16) -> Self {
        self.max_label_width = Some(width);
        self
    }

    /// Limit the description to at most this many columns. Surplus space goes to the label.
    pub fn max_description_width(mut self, width: u16) -> Self {
        self.max_description_width = Some(width);
        self
    }
}

impl ListItem {
    /// Create a new list item.
    pub fn new(content: impl Into<Arc<str>>) -> Self {
        Self {
            spans: vec![Span::new(content)],
            description_spans: Vec::new(),
            extra_lines: Vec::new(),
            status: None,
            gutter: None,
            gutter_line: 0,
            prefix: None,
            prefix_kind: ListItemPrefixKind::Plain,
            prefix_style: None,
            extra_line_indent: 0,
            style: Style::default(),
            role: ListItemRole::Normal,
            active: false,
            primary_selection_label: true,
            primary_selection_description: true,
            primary_hover_label: true,
            primary_hover_description: true,
            primary_truncate_description_first: false,
            primary_wrap_label: false,
            primary_wrap_description: false,
            primary_max_label_width: None,
            primary_max_description_width: None,
            symbol_line: 0,
        }
    }

    /// Create a non-selectable section/header row.
    pub fn header(content: impl Into<Arc<str>>) -> Self {
        Self::new(content)
            .role(ListItemRole::Header)
            .style(Style::default())
    }

    /// Create a non-selectable blank spacer row.
    pub fn spacer() -> Self {
        Self {
            spans: vec![Span::new("")],
            description_spans: Vec::new(),
            extra_lines: Vec::new(),
            status: None,
            gutter: None,
            gutter_line: 0,
            prefix: None,
            prefix_kind: ListItemPrefixKind::Plain,
            prefix_style: None,
            extra_line_indent: 0,
            style: Style::default(),
            role: ListItemRole::Spacer,
            active: false,
            primary_selection_label: true,
            primary_selection_description: true,
            primary_hover_label: true,
            primary_hover_description: true,
            primary_truncate_description_first: false,
            primary_wrap_label: false,
            primary_wrap_description: false,
            primary_max_label_width: None,
            primary_max_description_width: None,
            symbol_line: 0,
        }
    }

    /// Create from multiple spans.
    pub fn from_spans(spans: impl IntoIterator<Item = Span>) -> Self {
        Self {
            spans: spans.into_iter().collect(),
            description_spans: Vec::new(),
            extra_lines: Vec::new(),
            status: None,
            gutter: None,
            gutter_line: 0,
            prefix: None,
            prefix_kind: ListItemPrefixKind::Plain,
            prefix_style: None,
            extra_line_indent: 0,
            style: Style::default(),
            role: ListItemRole::Normal,
            active: false,
            primary_selection_label: true,
            primary_selection_description: true,
            primary_hover_label: true,
            primary_hover_description: true,
            primary_truncate_description_first: false,
            primary_wrap_label: false,
            primary_wrap_description: false,
            primary_max_label_width: None,
            primary_max_description_width: None,
            symbol_line: 0,
        }
    }

    /// Add description (right-aligned) content.
    pub fn description_spans(mut self, spans: impl IntoIterator<Item = Span>) -> Self {
        self.description_spans = spans.into_iter().collect();
        self
    }

    /// Set description as plain text (creates a single unstyled span).
    pub fn description(mut self, text: impl Into<Arc<str>>) -> Self {
        self.description_spans = vec![Span::new(text)];
        self
    }

    /// Apply a style to all current description spans.
    pub fn description_style(mut self, style: Style) -> Self {
        for span in &mut self.description_spans {
            span.style = style;
        }
        self
    }

    /// Add an extra rendered line below the primary line.
    pub fn line(mut self, line: impl Into<ListItemLine>) -> Self {
        self.extra_lines.push(line.into());
        self
    }

    /// Set extra lines below the primary line.
    pub fn lines(mut self, lines: impl IntoIterator<Item = ListItemLine>) -> Self {
        self.extra_lines = lines.into_iter().collect();
        self
    }

    /// Set a fixed-width left gutter rendered before the primary label column.
    pub fn gutter(mut self, gutter: impl Into<ListItemGutter>) -> Self {
        self.gutter = Some(gutter.into());
        self
    }

    /// Set per-row status content in the existing list symbol column.
    ///
    /// The status is used only when the row is not showing an active or selected
    /// symbol. This is useful for status markers such as a busy spinner without
    /// creating an extra label gutter column.
    pub fn status(mut self, status: impl Into<ListItemStatus>) -> Self {
        self.status = Some(status.into());
        self
    }

    /// Set a plain-text per-row status symbol.
    pub fn status_symbol(self, symbol: impl Into<Arc<str>>) -> Self {
        self.status(ListItemStatus::text(symbol))
    }

    /// Set an animated per-row status spinner.
    pub fn status_spinner(self, spinner: Spinner) -> Self {
        self.status(ListItemStatus::spinner(spinner))
    }

    /// Set which visual line carries the gutter.
    ///
    /// `0` means primary line, `1` means first extra line, etc.
    pub fn gutter_line(mut self, line: usize) -> Self {
        self.gutter_line = line;
        self
    }

    /// Set a text prefix for the primary line.
    pub fn prefix(mut self, prefix: impl Into<Arc<str>>) -> Self {
        let prefix = prefix.into();
        self.extra_line_indent =
            UnicodeWidthStr::width(prefix.as_ref()).min(u16::MAX as usize) as u16;
        self.prefix = Some(prefix);
        self.prefix_kind = ListItemPrefixKind::Plain;
        self
    }

    /// Set style for the primary-line prefix.
    pub fn prefix_style(mut self, style: Style) -> Self {
        self.prefix_style = Some(style);
        self
    }

    /// Prefix the row with `"N. "`.
    pub fn numbered(mut self, n: usize) -> Self {
        let prefix = format!("{n}. ");
        self.extra_line_indent =
            UnicodeWidthStr::width(prefix.as_str()).min(u16::MAX as usize) as u16;
        self.prefix = Some(prefix.into());
        self.prefix_kind = ListItemPrefixKind::Numbered(n);
        self
    }

    /// Prefix the row with `"<ch> "`.
    pub fn bulleted(self, ch: char) -> Self {
        self.prefix(format!("{ch} "))
    }

    /// Set extra-line indentation width in terminal columns.
    pub fn extra_line_indent(mut self, indent: u16) -> Self {
        self.extra_line_indent = indent;
        self
    }

    /// Control whether selection highlight applies to primary label content.
    pub fn primary_selection_label(mut self, highlight: bool) -> Self {
        self.primary_selection_label = highlight;
        self
    }

    /// Control whether selection highlight applies to primary description content.
    pub fn primary_selection_description(mut self, highlight: bool) -> Self {
        self.primary_selection_description = highlight;
        self
    }

    /// Control whether hover style applies to primary label content.
    pub fn primary_hover_label(mut self, hover: bool) -> Self {
        self.primary_hover_label = hover;
        self
    }

    /// Control whether hover style applies to primary description content.
    pub fn primary_hover_description(mut self, hover: bool) -> Self {
        self.primary_hover_description = hover;
        self
    }

    /// Prefer truncating primary description content before primary label content.
    pub fn primary_truncate_description_first(mut self, truncate: bool) -> Self {
        self.primary_truncate_description_first = truncate;
        self
    }

    /// Wrap primary label content onto extra lines when needed.
    pub fn primary_wrap_label(mut self, wrap: bool) -> Self {
        self.primary_wrap_label = wrap;
        self
    }

    /// Wrap primary description content onto extra lines when needed.
    pub fn primary_wrap_description(mut self, wrap: bool) -> Self {
        self.primary_wrap_description = wrap;
        self
    }

    /// Limit the primary label to at most this many columns.
    pub fn primary_max_label_width(mut self, width: u16) -> Self {
        self.primary_max_label_width = Some(width);
        self
    }

    /// Limit the primary description to at most this many columns.
    pub fn primary_max_description_width(mut self, width: u16) -> Self {
        self.primary_max_description_width = Some(width);
        self
    }

    /// Set which visual line carries the symbol column.
    ///
    /// `0` means primary line, `1` means first extra line, etc.
    pub fn symbol_line(mut self, line: usize) -> Self {
        self.symbol_line = line;
        self
    }

    /// Set base style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set semantic row role.
    pub fn role(mut self, role: ListItemRole) -> Self {
        self.role = role;
        self
    }

    /// Mark this row as active.
    pub fn active(mut self, active: bool) -> Self {
        self.active = active;
        self
    }

    /// Whether this row is keyboard/mouse selectable.
    pub fn is_selectable(&self) -> bool {
        matches!(self.role, ListItemRole::Normal)
    }

    /// Whether this row is active.
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Returns the concatenated plain text content.
    pub fn plain_content(&self) -> String {
        let mut s = String::new();
        for span in &self.spans {
            s.push_str(&span.content);
        }
        for line in &self.extra_lines {
            s.push('\n');
            for span in &line.spans {
                s.push_str(&span.content);
            }
        }
        s
    }

    pub(crate) fn line_count(&self) -> usize {
        1 + self.extra_lines.len()
    }
}

impl From<&'static str> for ListItemLine {
    fn from(value: &'static str) -> Self {
        Self::new(value)
    }
}

impl From<String> for ListItemLine {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

impl From<Arc<str>> for ListItemLine {
    fn from(value: Arc<str>) -> Self {
        Self::new(value)
    }
}

impl From<&'static str> for ListItem {
    fn from(value: &'static str) -> Self {
        Self::new(value)
    }
}

impl From<String> for ListItem {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

impl From<Arc<str>> for ListItem {
    fn from(value: Arc<str>) -> Self {
        Self::new(value)
    }
}

pub(crate) fn reserved_symbol_width_for_items(
    items: &[ListItem],
    symbol_column: bool,
    active_symbol_position: ListSymbolPosition,
    active_symbol: Option<&str>,
    selection_symbol: Option<&str>,
    unselected_symbol: Option<&str>,
) -> u16 {
    if !symbol_column {
        return 0;
    }

    let status_width = items
        .iter()
        .filter(|item| item.is_selectable())
        .filter_map(|item| item.status.as_ref())
        .map(ListItemStatus::width)
        .max()
        .unwrap_or(0) as usize;

    selection_symbol
        .map(UnicodeWidthStr::width)
        .unwrap_or(0)
        .max(
            if matches!(active_symbol_position, ListSymbolPosition::Left) {
                active_symbol.map(UnicodeWidthStr::width).unwrap_or(0)
            } else {
                0
            },
        )
        .max(unselected_symbol.map(UnicodeWidthStr::width).unwrap_or(0))
        .max(status_width)
        .min(u16::MAX as usize) as u16
}

pub(crate) fn reserved_symbol_width(list: &List) -> u16 {
    reserved_symbol_width_for_items(
        &list.items,
        list.symbol_column,
        list.active_symbol_position,
        list.active_symbol.as_deref(),
        list.selection_symbol.as_deref(),
        list.unselected_symbol.as_deref(),
    )
}

pub(crate) struct ListSymbolWidthCtx<'a> {
    pub active_symbol_position: ListSymbolPosition,
    pub active_symbol: Option<&'a str>,
    pub selection_symbol: Option<&'a str>,
    pub unselected_symbol: Option<&'a str>,
}

pub(crate) fn item_symbol_width_for_reserved(
    reserved: u16,
    item: &ListItem,
    is_selected: bool,
    ctx: ListSymbolWidthCtx<'_>,
) -> u16 {
    let ListSymbolWidthCtx {
        active_symbol_position,
        active_symbol,
        selection_symbol,
        unselected_symbol,
    } = ctx;
    if reserved == 0 || !item.is_selectable() {
        return 0;
    }

    if matches!(active_symbol_position, ListSymbolPosition::Left)
        && item.is_active()
        && let Some(symbol) = active_symbol
    {
        return UnicodeWidthStr::width(symbol).min(u16::MAX as usize) as u16;
    }

    if item.status.is_some() {
        return reserved;
    }

    if is_selected {
        return selection_symbol
            .map(|symbol| UnicodeWidthStr::width(symbol).min(u16::MAX as usize) as u16)
            .unwrap_or(reserved);
    }

    unselected_symbol
        .map(|symbol| UnicodeWidthStr::width(symbol).min(u16::MAX as usize) as u16)
        .unwrap_or(reserved)
}

pub(crate) fn item_symbol_width(list: &List, item: &ListItem, is_selected: bool) -> u16 {
    item_symbol_width_for_reserved(
        reserved_symbol_width(list),
        item,
        is_selected,
        ListSymbolWidthCtx {
            active_symbol_position: list.active_symbol_position,
            active_symbol: list.active_symbol.as_deref(),
            selection_symbol: list.selection_symbol.as_deref(),
            unselected_symbol: list.unselected_symbol.as_deref(),
        },
    )
}

pub(crate) fn item_active_right_symbol_width(list: &List, item: &ListItem) -> u16 {
    if matches!(list.active_symbol_position, ListSymbolPosition::Right)
        && item.is_active()
        && let Some(symbol) = list.active_symbol.as_deref()
    {
        UnicodeWidthStr::width(symbol).min(u16::MAX as usize) as u16
    } else {
        0
    }
}

/// Width of the trailing selection symbol for a given row.
///
/// Returns 0 unless the row is the selected, selectable row and a
/// `selection_symbol_right` is configured. A right-positioned `active_symbol`
/// occupies the same trailing slot and takes priority, so this returns 0 when
/// that symbol applies to the row to avoid double-reserving width.
pub(crate) fn item_selection_right_symbol_width(
    list: &List,
    item: &ListItem,
    is_selected: bool,
) -> u16 {
    if is_selected
        && item.is_selectable()
        && item_active_right_symbol_width(list, item) == 0
        && let Some(symbol) = list.selection_symbol_right.as_deref()
    {
        UnicodeWidthStr::width(symbol).min(u16::MAX as usize) as u16
    } else {
        0
    }
}

pub(crate) fn item_uses_gutter(item: &ListItem, gutter_for_non_selectable: bool) -> bool {
    item.is_selectable() || gutter_for_non_selectable
}

pub(crate) fn reserved_gutter_width_for_items(
    items: &[ListItem],
    gutter_gap: u16,
    gutter_for_non_selectable: bool,
) -> u16 {
    let width = items
        .iter()
        .filter(|item| item_uses_gutter(item, gutter_for_non_selectable))
        .filter_map(|item| item.gutter.as_ref())
        .map(ListItemGutter::width)
        .max()
        .unwrap_or(0);
    if width > 0 {
        width.saturating_add(gutter_gap)
    } else {
        0
    }
}

pub(crate) fn reserved_gutter_width(list: &List) -> u16 {
    reserved_gutter_width_for_items(&list.items, list.gutter_gap, list.gutter_for_non_selectable)
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct ListLeadingMetrics {
    pub symbol_width: u16,
    pub gutter_width: u16,
    pub active_right_symbol_width: u16,
    pub selection_right_symbol_width: u16,
}

pub(crate) fn leading_metrics(
    list: &List,
    item: &ListItem,
    is_selected: bool,
) -> ListLeadingMetrics {
    ListLeadingMetrics {
        symbol_width: item_symbol_width(list, item, is_selected),
        gutter_width: if item_uses_gutter(item, list.gutter_for_non_selectable) {
            reserved_gutter_width(list)
        } else {
            0
        },
        active_right_symbol_width: item_active_right_symbol_width(list, item),
        selection_right_symbol_width: item_selection_right_symbol_width(list, item, is_selected),
    }
}

pub(crate) fn max_numbered_prefix_width(list: &List) -> u16 {
    list.items
        .iter()
        .filter_map(|item| match item.prefix_kind {
            ListItemPrefixKind::Numbered(n) => Some(n),
            ListItemPrefixKind::Plain => None,
        })
        .map(|n| UnicodeWidthStr::width(format!("{n}. ").as_str()))
        .max()
        .unwrap_or(0)
        .min(u16::MAX as usize) as u16
}

pub(crate) fn max_numbered_prefix_width_for_items(items: &[ListItem]) -> u16 {
    items
        .iter()
        .filter_map(|item| match item.prefix_kind {
            ListItemPrefixKind::Numbered(n) => Some(n),
            ListItemPrefixKind::Plain => None,
        })
        .map(|n| UnicodeWidthStr::width(format!("{n}. ").as_str()))
        .max()
        .unwrap_or(0)
        .min(u16::MAX as usize) as u16
}

pub(crate) fn effective_prefix_for_width<'a>(
    item: &'a ListItem,
    numbered_prefix_width: u16,
) -> Option<std::borrow::Cow<'a, str>> {
    match (&item.prefix, &item.prefix_kind) {
        (Some(prefix), ListItemPrefixKind::Numbered(n)) => {
            let target_width = numbered_prefix_width as usize;
            let text = format!("{n}. ");
            let width = UnicodeWidthStr::width(text.as_str());
            if target_width > width {
                Some(std::borrow::Cow::Owned(format!(
                    "{}{}",
                    " ".repeat(target_width - width),
                    text
                )))
            } else {
                Some(std::borrow::Cow::Borrowed(prefix.as_ref()))
            }
        }
        (Some(prefix), ListItemPrefixKind::Plain) => {
            Some(std::borrow::Cow::Borrowed(prefix.as_ref()))
        }
        (None, _) => None,
    }
}

pub(crate) fn effective_extra_line_indent_for_width(
    item: &ListItem,
    numbered_prefix_width: u16,
) -> u16 {
    match item.prefix_kind {
        ListItemPrefixKind::Numbered(_) => numbered_prefix_width,
        ListItemPrefixKind::Plain => item.extra_line_indent,
    }
}

pub(crate) fn effective_prefix<'a>(
    list: &List,
    item: &'a ListItem,
) -> Option<std::borrow::Cow<'a, str>> {
    effective_prefix_for_width(item, max_numbered_prefix_width(list))
}

pub(crate) fn effective_extra_line_indent(list: &List, item: &ListItem) -> u16 {
    effective_extra_line_indent_for_width(item, max_numbered_prefix_width(list))
}

/// A vertically scrolling list.
#[derive(Clone)]
pub struct List {
    pub(crate) items: Arc<[ListItem]>,
    pub(crate) selected: usize,
    pub(crate) scroll_keys: ScrollKeymap,
    pub(crate) scroll_wheel: bool,
    pub(crate) style: Style,
    pub(crate) hover_style: StyleSlot,
    pub(crate) item_hover_style: StyleSlot,
    pub(crate) active_style: StyleSlot,
    pub(crate) selection_style: StyleSlot,
    pub(crate) unfocused_selection_style: StyleSlot,
    pub(crate) active_symbol: Option<Arc<str>>,
    pub(crate) active_symbol_position: ListSymbolPosition,
    pub(crate) active_symbol_style: Option<Style>,
    pub(crate) selection_symbol: Option<Arc<str>>,
    pub(crate) selection_symbol_right: Option<Arc<str>>,
    pub(crate) selection_symbol_style: Option<Style>,
    pub(crate) unfocused_selection_symbol_style: Option<Style>,
    pub(crate) unselected_symbol: Option<Arc<str>>,
    pub(crate) symbol_column: bool,
    pub(crate) gutter_gap: u16,
    pub(crate) gutter_for_non_selectable: bool,
    pub(crate) selection_full_width: bool,
    pub(crate) item_horizontal_padding: Padding,
    pub(crate) header_horizontal_padding: Padding,
    pub(crate) border: bool,
    pub(crate) border_style: BorderStyle,
    pub(crate) title: Option<Arc<str>>,
    pub(crate) title_style: Style,
    pub(crate) padding: Padding,
    pub(crate) scrollbar: bool,
    pub(crate) scrollbar_config: ScrollbarConfig,
    pub(crate) width: Length,
    pub(crate) height: Length,
    pub(crate) on_select: Option<Callback<ListEvent>>,
    pub(crate) on_item_click: Option<Callback<ListEvent>>,
    pub(crate) on_activate: Option<Callback<ListEvent>>,
    pub(crate) on_click: Option<Callback<MouseEvent>>,
    pub(crate) activate_on_click: bool,
    pub(crate) on_scroll_to: Option<Callback<usize>>,
    pub(crate) on_key: Option<KeyHandler>,
    pub(crate) disabled: bool,
    pub(crate) disabled_style: Style,
    pub(crate) focusable: bool,
    pub(crate) show_scroll_indicators: bool,
    pub(crate) scroll_indicator_style: Style,
    pub(crate) empty_text: Option<Arc<str>>,
    pub(crate) empty_text_style: Style,
    pub(crate) force_scroll_to_selected: bool,
}

impl Default for List {
    fn default() -> Self {
        Self {
            items: Arc::new([]),
            selected: 0,
            scroll_keys: ScrollKeymap::default(),
            scroll_wheel: true,
            style: Style::default(),
            hover_style: StyleSlot::Inherit,
            item_hover_style: StyleSlot::Inherit,
            active_style: StyleSlot::Inherit,
            selection_style: StyleSlot::Inherit,
            unfocused_selection_style: StyleSlot::Inherit,
            active_symbol: None,
            active_symbol_position: ListSymbolPosition::Left,
            active_symbol_style: None,
            selection_symbol: None,
            selection_symbol_right: None,
            selection_symbol_style: None,
            unfocused_selection_symbol_style: None,
            unselected_symbol: None,
            symbol_column: true,
            gutter_gap: 0,
            gutter_for_non_selectable: false,
            selection_full_width: false,
            item_horizontal_padding: Padding::default(),
            header_horizontal_padding: Padding::default(),
            border: false,
            border_style: BorderStyle::Plain,
            title: None,
            title_style: Style::default(),
            padding: Padding::default(),
            scrollbar: false,
            scrollbar_config: ScrollbarConfig::default(),
            width: Length::Flex(1),
            height: Length::Flex(1),
            on_select: None,
            on_item_click: None,
            on_activate: None,
            on_click: None,
            activate_on_click: true,
            on_scroll_to: None,
            on_key: None,
            disabled: false,
            disabled_style: Style::default(),
            focusable: true,
            show_scroll_indicators: false,
            scroll_indicator_style: Style::default(),
            empty_text: None,
            empty_text_style: Style::default(),
            force_scroll_to_selected: false,
        }
    }
}

impl List {
    /// Create an empty list.
    pub fn new() -> Self {
        Self::default()
    }

    /// Replace list items.
    pub fn items<I>(mut self, items: I) -> Self
    where
        I: IntoIterator<Item = ListItem>,
    {
        self.items = items.into_iter().collect();
        self
    }

    /// Add an item.
    ///
    /// Note: This clones the internal Arc if it's shared, which might be expensive.
    /// For bulk updates, use `items()`.
    pub fn item(mut self, item: impl Into<ListItem>) -> Self {
        let mut items = self.items.to_vec();
        items.push(item.into());
        self.items = items.into();
        self
    }

    /// Set items from a shared slice.
    ///
    /// This is efficient for avoiding allocations when the list items don't change often.
    pub fn items_arc(mut self, items: Arc<[ListItem]>) -> Self {
        self.items = items;
        self
    }

    /// Set selected item index.
    pub fn selected(mut self, selected: usize) -> Self {
        self.selected = selected;
        self
    }

    /// Force scroll to make the selected item visible on next render.
    ///
    /// Use this when you want the list to jump directly to the selected item,
    /// bypassing the smart scroll logic that normally preserves scroll position.
    /// This is useful for auto-follow scenarios where you always want to show
    /// the latest item.
    pub fn force_scroll_to_selected(mut self, force: bool) -> Self {
        self.force_scroll_to_selected = force;
        self
    }

    /// Configure which keys move the selection.
    pub fn scroll_keys(mut self, keys: ScrollKeymap) -> Self {
        self.scroll_keys = keys;
        self
    }

    /// Enable mouse wheel selection changes.
    pub fn scroll_wheel(mut self, enabled: bool) -> Self {
        self.scroll_wheel = enabled;
        self
    }

    /// Set base style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set style when list is hovered.
    pub fn hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the theme hover style with this partial overlay.
    pub fn extend_hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the active theme hover style.
    pub fn inherit_hover_style(mut self) -> Self {
        self.hover_style = StyleSlot::Inherit;
        self
    }

    /// Set hover style slot directly for composite forwarding.
    pub fn hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.hover_style = slot;
        self
    }

    /// Set style for hovered items.
    pub fn item_hover_style(mut self, style: Style) -> Self {
        self.item_hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the theme item-hover style with this partial overlay.
    pub fn extend_item_hover_style(mut self, style: Style) -> Self {
        self.item_hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the active theme item-hover style.
    pub fn inherit_item_hover_style(mut self) -> Self {
        self.item_hover_style = StyleSlot::Inherit;
        self
    }

    /// Set item-hover style slot directly for composite forwarding.
    pub fn item_hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.item_hover_style = slot;
        self
    }

    /// Set style for active rows.
    pub fn active_style(mut self, style: Style) -> Self {
        self.active_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the theme active style with this partial overlay.
    pub fn extend_active_style(mut self, style: Style) -> Self {
        self.active_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the active theme active-row style.
    pub fn inherit_active_style(mut self) -> Self {
        self.active_style = StyleSlot::Inherit;
        self
    }

    /// Set active style slot directly for composite forwarding.
    pub fn active_style_slot(mut self, slot: StyleSlot) -> Self {
        self.active_style = slot;
        self
    }

    /// Set selected item style.
    pub fn selection_style(mut self, style: Style) -> Self {
        self.selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the theme selection style with this partial overlay.
    pub fn extend_selection_style(mut self, style: Style) -> Self {
        self.selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the active theme selection style.
    pub fn inherit_selection_style(mut self) -> Self {
        self.selection_style = StyleSlot::Inherit;
        self
    }

    /// Set selection style slot directly for composite forwarding.
    pub fn selection_style_slot(mut self, slot: StyleSlot) -> Self {
        self.selection_style = slot;
        self
    }

    /// Set selected item style while the list is not focused.
    ///
    /// When unset, [`Self::selection_style`] is used regardless of focus state.
    pub fn unfocused_selection_style(mut self, style: Style) -> Self {
        self.unfocused_selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the theme unfocused-selection style with this partial overlay.
    pub fn extend_unfocused_selection_style(mut self, style: Style) -> Self {
        self.unfocused_selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the active theme unfocused-selection style.
    pub fn inherit_unfocused_selection_style(mut self) -> Self {
        self.unfocused_selection_style = StyleSlot::Inherit;
        self
    }

    /// Set unfocused-selection style slot directly for composite forwarding.
    pub fn unfocused_selection_style_slot(mut self, slot: StyleSlot) -> Self {
        self.unfocused_selection_style = slot;
        self
    }

    /// Set the symbol shown on the selected (active_index) item.
    ///
    /// Symbols are shown with a consistent left-aligned prefix column whose width
    /// is the maximum of `selection_symbol`, `unselected_symbol`, and `active_symbol`
    /// when `active_symbol_position` is [`ListSymbolPosition::Left`].
    ///
    /// Symbol rendering priority per row:
    /// 1. `active_symbol` - shown whenever the item is active (overrides highlight).
    /// 2. `selection_symbol` - shown for the selected item when not active.
    /// 3. `unselected_symbol` - shown for all other rows.
    /// 4. Spaces - auto-padding to the reserved column width when no symbol matches.
    pub fn selection_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.selection_symbol = symbol.map(Into::into);
        self
    }

    /// Set a trailing symbol rendered immediately after the selected row's
    /// label content.
    ///
    /// This mirrors [`Self::selection_symbol`] (the leading symbol) on the right
    /// side, enabling "pill"/capsule selection styles: pair a left cap (e.g.
    /// `""`) via `selection_symbol` with a right cap (e.g. `""`) here, and
    /// color both with [`Self::selection_symbol_style`] so their foreground
    /// equals the selection background. Combine with
    /// `selection_full_width(false)` so the highlight hugs the label and the
    /// caps sit on the surrounding background.
    ///
    /// The trailing symbol shares [`Self::selection_symbol_style`] /
    /// [`Self::unfocused_selection_symbol_style`] with the leading symbol — both
    /// caps of a pill are the same color by definition. It renders only on the
    /// selected row's symbol line, in the same position as a right-positioned
    /// [`Self::active_symbol`]; when a row is both selected and shows a
    /// right-positioned active symbol, the active symbol takes priority.
    pub fn selection_symbol_right(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.selection_symbol_right = symbol.map(Into::into);
        self
    }

    /// Set the symbol shown on active rows.
    ///
    /// When `active_symbol_position` is [`ListSymbolPosition::Left`], `active_symbol`
    /// takes priority over `selection_symbol`, even when the item is both active and
    /// selected. When set to [`ListSymbolPosition::Right`], the active symbol is rendered
    /// immediately after the label content instead.
    pub fn active_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.active_symbol = symbol.map(Into::into);
        self
    }

    /// Set where the active symbol is rendered.
    pub fn active_symbol_position(mut self, position: ListSymbolPosition) -> Self {
        self.active_symbol_position = position;
        self
    }

    /// Set style for the active symbol.
    ///
    /// If not set, it defaults to the computed row style.
    pub fn active_symbol_style(mut self, style: Style) -> Self {
        self.active_symbol_style = Some(style);
        self
    }

    /// Set style for the highlight symbol.
    ///
    /// If not set, it defaults to the computed row style (with `selection_style` applied).
    pub fn selection_symbol_style(mut self, style: Style) -> Self {
        self.selection_symbol_style = Some(style);
        self
    }

    /// Set selected item symbol style while the list is not focused.
    ///
    /// When unset, [`Self::selection_symbol_style`] is used regardless of focus state.
    pub fn unfocused_selection_symbol_style(mut self, style: Style) -> Self {
        self.unfocused_selection_symbol_style = Some(style);
        self
    }

    /// Set the symbol shown for rows that are neither selected nor active.
    ///
    /// When not set, non-selected rows are padded with spaces equal to the
    /// reserved prefix column width (the max of all three symbol widths), keeping
    /// all content aligned. To remove all left padding set this to `Some("")`.
    pub fn unselected_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.unselected_symbol = symbol.map(Into::into);
        self
    }

    /// Set whether the left symbol/status column is enabled.
    ///
    /// This does not affect right-positioned active symbols.
    pub fn symbol_column(mut self, enabled: bool) -> Self {
        self.symbol_column = enabled;
        self
    }

    /// Set the explicit gap after the widest item gutter.
    pub fn gutter_gap(mut self, gap: u16) -> Self {
        self.gutter_gap = gap;
        self
    }

    /// Set whether non-selectable rows reserve the gutter column.
    pub fn gutter_for_non_selectable(mut self, enabled: bool) -> Self {
        self.gutter_for_non_selectable = enabled;
        self
    }

    /// Set whether the highlight should span the full width of the list.
    pub fn selection_full_width(mut self, full_width: bool) -> Self {
        self.selection_full_width = full_width;
        self
    }

    /// Set row padding for normal rows.
    ///
    /// Only left/right are used by the renderer; top/bottom are ignored.
    pub fn item_horizontal_padding(mut self, padding: impl Into<Padding>) -> Self {
        self.item_horizontal_padding = padding.into();
        self
    }

    /// Set row padding for header rows.
    ///
    /// Only left/right are used by the renderer; top/bottom are ignored.
    pub fn header_horizontal_padding(mut self, padding: impl Into<Padding>) -> Self {
        self.header_horizontal_padding = padding.into();
        self
    }

    /// Draw a border.
    pub fn border(mut self, border: bool) -> Self {
        self.border = border;
        self
    }

    /// Set border style.
    pub fn border_style(mut self, border_style: BorderStyle) -> Self {
        self.border_style = border_style;
        self
    }

    /// Set the title (only visible when `border` is true).
    pub fn title(mut self, title: impl Into<Arc<str>>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set title style.
    pub fn title_style(mut self, style: Style) -> Self {
        self.title_style = style;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
        self.padding = padding.into();
        self
    }

    /// Draw a vertical scrollbar.
    pub fn scrollbar(mut self, scrollbar: bool) -> Self {
        self.scrollbar = scrollbar;
        self
    }

    /// Set scrollbar configuration.
    pub fn scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
        self.scrollbar_config = config;
        self
    }

    /// Override requested width.
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    /// Override requested height.
    pub fn height(mut self, height: Length) -> Self {
        self.height = height;
        self
    }

    /// Callback fired when selection changes (keyboard or mouse).
    pub fn on_select(mut self, cb: Callback<ListEvent>) -> Self {
        self.on_select = Some(cb);
        self
    }

    /// Callback fired when a row is clicked with the mouse.
    pub fn on_item_click(mut self, cb: Callback<ListEvent>) -> Self {
        self.on_item_click = Some(cb);
        self
    }

    /// Callback fired on activation (Enter).
    pub fn on_activate(mut self, cb: Callback<ListEvent>) -> Self {
        self.on_activate = Some(cb);
        self
    }

    /// Control whether clicking an item emits `on_activate`.
    pub fn activate_on_click(mut self, activate: bool) -> Self {
        self.activate_on_click = activate;
        self
    }

    /// Set on-click handler.
    pub fn on_click(mut self, cb: Callback<MouseEvent>) -> Self {
        self.on_click = Some(cb);
        self
    }

    /// Set on-scroll-to handler.
    pub fn on_scroll_to(mut self, cb: Callback<usize>) -> Self {
        self.on_scroll_to = Some(cb);
        self
    }

    /// Set on-key handler.
    pub fn on_key(mut self, handler: KeyHandler) -> Self {
        self.on_key = Some(handler);
        self
    }

    /// Set disabled state.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Set disabled style.
    pub fn disabled_style(mut self, style: Style) -> Self {
        self.disabled_style = style;
        self
    }

    /// Control whether the node is focusable.
    pub fn focusable(mut self, focusable: bool) -> Self {
        self.focusable = focusable;
        self
    }

    /// Enable "N more" scroll indicators when items are hidden.
    pub fn show_scroll_indicators(mut self, show: bool) -> Self {
        self.show_scroll_indicators = show;
        self
    }

    /// Set style for scroll indicators.
    pub fn scroll_indicator_style(mut self, style: Style) -> Self {
        self.scroll_indicator_style = style;
        self
    }

    /// Set text to display when the list is empty.
    pub fn empty_text(mut self, text: impl Into<Arc<str>>) -> Self {
        self.empty_text = Some(text.into());
        self
    }

    /// Set style for empty text.
    pub fn empty_text_style(mut self, style: Style) -> Self {
        self.empty_text_style = style;
        self
    }

    pub(crate) fn first_selectable_index(items: &[ListItem]) -> Option<usize> {
        items.iter().position(ListItem::is_selectable)
    }

    pub(crate) fn last_selectable_index(items: &[ListItem]) -> Option<usize> {
        items.iter().rposition(ListItem::is_selectable)
    }

    pub(crate) fn is_selectable_index(items: &[ListItem], index: usize) -> bool {
        items.get(index).is_some_and(ListItem::is_selectable)
    }

    pub(crate) fn selectable_at_or_after(items: &[ListItem], start: usize) -> Option<usize> {
        if items.is_empty() {
            return None;
        }

        let from = start.min(items.len().saturating_sub(1));
        items
            .iter()
            .enumerate()
            .skip(from)
            .find_map(|(idx, item)| item.is_selectable().then_some(idx))
    }

    pub(crate) fn selectable_at_or_before(items: &[ListItem], start: usize) -> Option<usize> {
        if items.is_empty() {
            return None;
        }

        let to = start.min(items.len().saturating_sub(1));
        items
            .iter()
            .enumerate()
            .take(to.saturating_add(1))
            .rfind(|(_, item)| item.is_selectable())
            .map(|(idx, _)| idx)
    }

    pub(crate) fn nearest_selectable_index(items: &[ListItem], preferred: usize) -> Option<usize> {
        if items.is_empty() {
            return None;
        }

        let preferred = preferred.min(items.len().saturating_sub(1));
        if Self::is_selectable_index(items, preferred) {
            return Some(preferred);
        }

        let before = Self::selectable_at_or_before(items, preferred);
        let after = Self::selectable_at_or_after(items, preferred);

        match (before, after) {
            (Some(left), Some(right)) => {
                if preferred.abs_diff(right) <= preferred.abs_diff(left) {
                    Some(right)
                } else {
                    Some(left)
                }
            }
            (Some(left), None) => Some(left),
            (None, Some(right)) => Some(right),
            (None, None) => None,
        }
    }

    pub(crate) fn selection_for_action(
        selected: usize,
        items: &[ListItem],
        action: ScrollAction,
    ) -> Option<usize> {
        let len = items.len();
        if len == 0 {
            return None;
        }

        let mut selected = Self::nearest_selectable_index(items, selected)?;

        let next = match action {
            ScrollAction::LineUp(lines) => {
                for _ in 0..lines {
                    if selected == 0 {
                        break;
                    }

                    let Some(prev) = Self::selectable_at_or_before(items, selected - 1) else {
                        break;
                    };
                    selected = prev;
                }
                selected
            }
            ScrollAction::LineDown(lines) => {
                for _ in 0..lines {
                    if selected.saturating_add(1) >= len {
                        break;
                    }

                    let Some(next) = Self::selectable_at_or_after(items, selected + 1) else {
                        break;
                    };
                    selected = next;
                }
                selected
            }
            ScrollAction::LineLeft(_) | ScrollAction::LineRight(_) => return None,
            ScrollAction::Home => Self::first_selectable_index(items)?,
            ScrollAction::End => Self::last_selectable_index(items)?,
        };

        Some(next)
    }

    pub(crate) fn selection_for_action_in_len(
        selected: usize,
        len: usize,
        action: ScrollAction,
    ) -> Option<usize> {
        if len == 0 {
            return None;
        }

        let selected = selected.min(len.saturating_sub(1));
        let next = match action {
            ScrollAction::LineUp(lines) => selected.saturating_sub(lines),
            ScrollAction::LineDown(lines) => (selected + lines).min(len.saturating_sub(1)),
            ScrollAction::LineLeft(_) | ScrollAction::LineRight(_) => return None,
            ScrollAction::Home => 0,
            ScrollAction::End => len.saturating_sub(1),
        };

        Some(next)
    }

    pub(crate) fn next_selection(
        selected: usize,
        items: &[ListItem],
        key: &KeyEvent,
        scroll_keys: ScrollKeymap,
    ) -> Option<usize> {
        let action = scroll_action_from_key(key, scroll_keys)?;
        Self::selection_for_action(selected, items, action)
    }
}

impl From<List> for Element {
    fn from(value: List) -> Self {
        Element::new(ElementKind::List(Box::new(value)))
    }
}

fn hash_list_item_layout(item: &ListItem, hasher: &mut impl std::hash::Hasher) {
    use std::hash::Hash;

    item.role.hash(hasher);
    item.active.hash(hasher);
    item.status.as_ref().map(ListItemStatus::width).hash(hasher);
    item.gutter.as_ref().map(ListItemGutter::width).hash(hasher);
    item.gutter_line.hash(hasher);
    item.prefix.hash(hasher);
    item.prefix_kind.hash(hasher);
    item.extra_line_indent.hash(hasher);
    item.primary_truncate_description_first.hash(hasher);
    item.primary_wrap_label.hash(hasher);
    item.primary_wrap_description.hash(hasher);
    item.primary_max_label_width.hash(hasher);
    item.primary_max_description_width.hash(hasher);
    item.symbol_line.hash(hasher);
}

fn hash_list_item_line_layout(line: &ListItemLine, hasher: &mut impl std::hash::Hasher) {
    use std::hash::Hash;

    line.truncate_description_first.hash(hasher);
    line.wrap_label.hash(hasher);
    line.wrap_description.hash(hasher);
    line.max_label_width.hash(hasher);
    line.max_description_width.hash(hasher);
}

impl crate::layout::hash::LayoutHash for List {
    fn layout_hash(
        &self,
        hasher: &mut impl std::hash::Hasher,
        _recurse: &dyn Fn(&Element) -> Option<u64>,
    ) -> Option<()> {
        use crate::layout::hash::hash_spans_content;
        use std::hash::Hash;

        self.width.hash(hasher);
        self.height.hash(hasher);
        self.border.hash(hasher);
        self.scrollbar.hash(hasher);
        self.scrollbar_config.variant.hash(hasher);
        self.scrollbar_config.gap.hash(hasher);
        self.padding.hash(hasher);
        self.item_horizontal_padding.hash(hasher);
        self.header_horizontal_padding.hash(hasher);
        self.symbol_column.hash(hasher);
        self.gutter_gap.hash(hasher);
        self.gutter_for_non_selectable.hash(hasher);

        let needs_content = matches!(self.width, Length::Auto);
        let needs_len = matches!(self.height, Length::Auto) || self.scrollbar;

        if needs_len {
            self.items.len().hash(hasher);
        }

        if needs_content || needs_len {
            self.selected.hash(hasher);
            for item in self.items.iter() {
                hash_list_item_layout(item, hasher);
                hash_spans_content(&item.spans, hasher);
                hash_spans_content(&item.description_spans, hasher);
                for line in &item.extra_lines {
                    hash_list_item_line_layout(line, hasher);
                    hash_spans_content(&line.spans, hasher);
                    hash_spans_content(&line.description_spans, hasher);
                }
            }
        }

        self.title.hash(hasher);
        self.empty_text.hash(hasher);
        self.selection_symbol.hash(hasher);
        self.selection_symbol_right.hash(hasher);
        self.active_symbol.hash(hasher);
        self.active_symbol_position.hash(hasher);
        self.unselected_symbol.hash(hasher);
        Some(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::element::Element;
    use crate::core::event::{KeyCode, KeyEvent, KeyMods};
    use crate::layout::hash::element_layout_hash;
    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent {
            code,
            mods: KeyMods::default(),
        }
    }

    fn fixture_items() -> Vec<ListItem> {
        vec![
            ListItem::header("Group A"),
            ListItem::new("Alpha"),
            ListItem::new("Beta"),
            ListItem::spacer(),
            ListItem::header("Group B"),
            ListItem::new("Gamma"),
        ]
    }

    #[test]
    fn keyboard_navigation_skips_headers_and_spacers() {
        let items = fixture_items();
        let next = List::next_selection(1, &items, &key(KeyCode::Down), ScrollKeymap::default());
        assert_eq!(next, Some(2));

        let next = List::next_selection(2, &items, &key(KeyCode::Down), ScrollKeymap::default());
        assert_eq!(next, Some(5));
    }

    #[test]
    fn home_end_resolve_to_selectable_rows() {
        let items = fixture_items();

        let home = List::next_selection(5, &items, &key(KeyCode::Home), ScrollKeymap::default());
        assert_eq!(home, Some(1));

        let end = List::next_selection(1, &items, &key(KeyCode::End), ScrollKeymap::default());
        assert_eq!(end, Some(5));
    }

    #[test]
    fn all_non_selectable_rows_have_no_selection_target() {
        let items = vec![ListItem::header("Section"), ListItem::spacer()];

        assert_eq!(List::first_selectable_index(&items), None);
        assert_eq!(
            List::next_selection(0, &items, &key(KeyCode::Down), ScrollKeymap::default()),
            None
        );
    }

    #[test]
    fn auto_height_layout_hash_tracks_extra_line_content() {
        let base: Element = List::new()
            .items(vec![ListItem::new("Type your own answer")])
            .height(Length::Auto)
            .into();
        let with_answer: Element = List::new()
            .items(vec![
                ListItem::new("Type your own answer").line("saved answer"),
            ])
            .height(Length::Auto)
            .into();

        assert_ne!(
            element_layout_hash(&base),
            element_layout_hash(&with_answer)
        );
    }

    #[test]
    fn layout_hash_tracks_leading_column_config() {
        let base: Element = List::new()
            .items(vec![ListItem::new("Alpha").gutter(Spinner::new())])
            .width(Length::Auto)
            .into();
        let no_symbol: Element = List::new()
            .items(vec![ListItem::new("Alpha").gutter(Spinner::new())])
            .width(Length::Auto)
            .symbol_column(false)
            .into();
        let gap: Element = List::new()
            .items(vec![ListItem::new("Alpha").gutter(Spinner::new())])
            .width(Length::Auto)
            .gutter_gap(1)
            .into();

        assert_ne!(element_layout_hash(&base), element_layout_hash(&no_symbol));
        assert_ne!(element_layout_hash(&base), element_layout_hash(&gap));
    }

    #[test]
    fn layout_hash_tracks_row_leading_width_fields() {
        let base: Element = List::new()
            .items(vec![ListItem::new("A")])
            .width(Length::Auto)
            .into();
        let with_status: Element = List::new()
            .items(vec![ListItem::new("A").status_symbol("!!")])
            .width(Length::Auto)
            .into();
        let with_gutter: Element = List::new()
            .items(vec![ListItem::new("A").gutter(ListItemGutter::text(">>"))])
            .width(Length::Auto)
            .into();
        let header_with_gutter: Element = List::new()
            .items(vec![
                ListItem::header("A").gutter(ListItemGutter::text(">>")),
            ])
            .width(Length::Auto)
            .into();

        assert_ne!(
            element_layout_hash(&base),
            element_layout_hash(&with_status)
        );
        assert_ne!(
            element_layout_hash(&base),
            element_layout_hash(&with_gutter)
        );
        assert_ne!(
            element_layout_hash(&with_gutter),
            element_layout_hash(&header_with_gutter)
        );
    }

    #[test]
    fn layout_hash_tracks_wrap_flags_for_auto_height() {
        let base: Element = List::new()
            .items(vec![ListItem::new("alpha beta gamma")])
            .height(Length::Auto)
            .into();
        let wrapped: Element = List::new()
            .items(vec![
                ListItem::new("alpha beta gamma").primary_wrap_label(true),
            ])
            .height(Length::Auto)
            .into();

        assert_ne!(element_layout_hash(&base), element_layout_hash(&wrapped));
    }
}

pub(crate) mod layout;
pub(crate) mod node;
pub(crate) mod reconcile;
pub(crate) mod utils;

pub use node::ListNode;
pub(crate) use reconcile::reconcile_list;