fresh-editor 0.4.0

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
//! Status bar and prompt/minibuffer rendering

use std::collections::HashMap;
use std::path::Path;

use crate::app::WarningLevel;
use crate::config::{StatusBarConfig, StatusBarElement};
use crate::primitives::display_width::{char_width, str_width};
use crate::state::EditorState;
use crate::view::prompt::Prompt;
use chrono::Timelike;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;
use rust_i18n::t;

/// Text that both marks a buffer as "edited over a disconnected SSH session"
/// and styles the prefix in the status bar. Kept as constants so `render_element`
/// and `element_spans` stay in sync.
const SSH_PREFIX: &str = "[SSH:";
const SSH_PREFIX_TERMINATOR: &str = "] ";

/// Categorization of how a rendered element should be styled and tracked for click detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ElementKind {
    /// Normal text using base status bar colors
    Normal,
    /// Line ending indicator (clickable)
    LineEnding,
    /// Encoding indicator (clickable)
    Encoding,
    /// Language indicator (clickable)
    Language,
    /// LSP status indicator (colored by warning level, clickable)
    Lsp,
    /// Warning badge (colored, clickable)
    WarningBadge,
    /// Update available indicator (highlighted)
    Update,
    /// Command palette shortcut hint (distinct style)
    Palette,
    /// Status message area (clickable to show history)
    Messages,
    /// Remote disconnected prefix (error colors)
    RemoteDisconnected,
    /// Clock element — colon rendered with hardware blink
    Clock,
    /// Remote authority indicator — styling driven by connection state
    RemoteIndicator(RemoteIndicatorState),
    /// Workspace-trust indicator — always present, styling driven by the
    /// active session's trust level. Clickable (opens the trust prompt).
    WorkspaceTrust(crate::services::workspace_trust::TrustLevel),
    /// Custom plugin token
    Custom,
}

/// Visual/semantic state of the remote authority indicator.
///
/// Covers the full dev-container UX lifecycle the spec asks for —
/// Local, Connecting to a remote authority, Connected, FailedAttach,
/// Disconnected — while remaining general enough for any remote
/// authority Fresh currently supports (SSH today; containers;
/// anything a plugin installs via `editor.setAuthority(...)`).
///
/// Variants deliberately hold no data — phase labels and error text
/// are passed alongside via `StatusBarContext::remote_state_override`
/// (added in Phase B-2) so the enum stays `Copy` and core code never
/// learns devcontainer-specific vocabulary (see
/// `AUTHORITY_DESIGN.md` principle 3).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RemoteIndicatorState {
    /// Editing local files; rendered with the default status-bar palette.
    #[default]
    Local,
    /// An attach (or reconnect) is in flight — rendered with a spinner
    /// glyph and the help-indicator palette. Plugins drive this via
    /// `setRemoteIndicatorState` before kicking off `devcontainer up`
    /// or similar long-running setup.
    Connecting,
    /// Connected to an SSH / container / other remote authority.
    Connected,
    /// The last attach attempt failed. Rendered with the error palette
    /// so the state is visible at a glance; the popup surfaces the
    /// error detail and a Retry action.
    FailedAttach,
    /// Connection lost — rendered with the error palette as a persistent
    /// warning that writes/saves are no longer reaching the authority.
    Disconnected,
}

/// Plugin-supplied override for the Remote Indicator. Carries both
/// the state enum and a user-visible label/error text so core doesn't
/// need to know how to phrase "Connecting..." or a specific failure
/// string — the plugin owns the copy.
///
/// Deserialized from the tagged JSON shape accepted by the
/// `SetRemoteIndicatorState` plugin op (see `fresh-core::api`). Kept
/// in the view crate so the enum lives next to the rendering that
/// consumes it.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum RemoteIndicatorOverride {
    /// Force the indicator to "Local" even when the authority would
    /// otherwise read as Connected. Rarely needed in practice.
    Local,
    /// Attach is in flight. `label` is the short text shown next to
    /// the spinner glyph (e.g. "Building", "Pulling image").
    Connecting {
        #[serde(default)]
        label: Option<String>,
    },
    /// Force Connected. `label` overrides the authority's display
    /// string if present; otherwise the derived label is shown.
    Connected {
        #[serde(default)]
        label: Option<String>,
    },
    /// Last attach attempt failed. `error` is the short message the
    /// indicator renders; longer context belongs in the popup.
    FailedAttach {
        #[serde(default)]
        error: Option<String>,
    },
    /// Explicitly disconnected (e.g. plugin detected a container
    /// stop that the authority doesn't know about yet).
    Disconnected {
        #[serde(default)]
        label: Option<String>,
    },
}

impl RemoteIndicatorOverride {
    /// Project into the Copy enum consumed by `element_style`.
    pub fn state(&self) -> RemoteIndicatorState {
        match self {
            Self::Local => RemoteIndicatorState::Local,
            Self::Connecting { .. } => RemoteIndicatorState::Connecting,
            Self::Connected { .. } => RemoteIndicatorState::Connected,
            Self::FailedAttach { .. } => RemoteIndicatorState::FailedAttach,
            Self::Disconnected { .. } => RemoteIndicatorState::Disconnected,
        }
    }

    /// Short label rendered inside the indicator element. Defaults
    /// are chosen so an override with no `label`/`error` field still
    /// displays something sensible.
    pub fn label(&self) -> String {
        match self {
            Self::Local => "Local".to_string(),
            Self::Connecting { label } => match label {
                Some(s) if !s.is_empty() => format!("{}", s),
                _ => "⠿ Connecting".to_string(),
            },
            Self::Connected { label } => label
                .as_deref()
                .filter(|s| !s.is_empty())
                .unwrap_or("Connected")
                .to_string(),
            Self::FailedAttach { error } => match error {
                Some(s) if !s.is_empty() => format!("Attach failed: {}", s),
                _ => "Attach failed".to_string(),
            },
            Self::Disconnected { label } => match label {
                Some(s) if !s.is_empty() => format!("{} (Disconnected)", s),
                _ => "Disconnected".to_string(),
            },
        }
    }
}

/// A single rendered status bar element with its text and styling info.
struct RenderedElement {
    text: String,
    kind: ElementKind,
    /// For `ElementKind::Custom` elements, the plugin-registered token
    /// key (`"<plugin>:<token>"`) — preserved here so the layout pass
    /// can record this element's screen area under the same key for
    /// click dispatch. `None` for every built-in element kind.
    token_key: Option<String>,
}

/// Three-state LSP status used by the status bar `Lsp` element.
///
/// Collapses the previous "running / auto_start-dormant / opt-in-dormant /
/// nothing" fan-out into the three user-meaningful buckets the indicator
/// actually needs to communicate:
///
/// - `On`            — at least one server for this language is running
/// - `Off`           — configured servers exist for this language, none are running
/// - `OffDismissed`  — like `Off`, but the user clicked "Disable" from the
///                     popup; rendered with a muted style so it stops
///                     shouting for attention while remaining clickable
///                     (so the user can still open the popup to re-enable
///                     or see install help).
/// - `Error`         — at least one server for this language is in the Error state
/// - `None`          — no LSP configured or running for this language
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LspIndicatorState {
    #[default]
    None,
    On,
    Off,
    OffDismissed,
    Error,
}

/// Editor state, theming, and runtime inputs needed to render a status bar frame.
pub struct StatusBarContext<'a> {
    pub state: &'a mut EditorState,
    pub cursors: &'a crate::model::cursor::Cursors,
    pub status_message: &'a Option<String>,
    pub plugin_status_message: &'a Option<String>,
    pub lsp_status: &'a str,
    /// Three-state LSP indicator: On / Off / Error / None.  Drives the
    /// indicator's background color independently of `warning_level` (the
    /// latter still scopes whether a warning badge is shown on the right
    /// side of the status bar).
    pub lsp_indicator_state: LspIndicatorState,
    pub theme: &'a crate::view::theme::Theme,
    pub display_name: &'a str,
    pub keybindings: &'a crate::input::keybindings::KeybindingResolver,
    pub chord_state: &'a [(crossterm::event::KeyCode, crossterm::event::KeyModifiers)],
    pub update_available: Option<&'a str>,
    pub warning_level: WarningLevel,
    pub general_warning_count: usize,
    pub hover: StatusBarHover,
    pub remote_connection: Option<&'a str>,
    pub session_name: Option<&'a str>,
    pub read_only: bool,
    /// Plugin-supplied override for the `{remote}` indicator. When
    /// `Some`, its state+label are rendered instead of the one
    /// derived from `remote_connection`. Set via the
    /// `SetRemoteIndicatorState` plugin op; cleared by
    /// `ClearRemoteIndicatorState` or by a `None` pass at the call
    /// site.
    pub remote_state_override: Option<&'a RemoteIndicatorOverride>,
    /// True when the active buffer is the synthesized placeholder kept
    /// alive by the close path with `auto_create_empty_buffer_on_last_buffer_close`
    /// disabled. Buffer-specific elements (filename, cursor, line ending,
    /// encoding, language, diagnostics) suppress themselves so the bar
    /// reflects "no real buffer is open" rather than `[No Name] | Ln 1, Col 1 …`.
    pub is_synthetic_placeholder: bool,
    /// True when the user's status-bar layout contains the
    /// `RemoteIndicator` element. Set by the renderer after
    /// inspecting `StatusBarConfig.left` / `.right`. Read by the
    /// `Filename` element's branch to decide whether to emit the
    /// legacy `[Container:<id>] ` / SSH prefix on the filename
    /// — when the dedicated indicator is on the bar that prefix
    /// is redundant; when it's not, the filename keeps the prefix
    /// so users still see the connection at a glance.
    pub remote_indicator_on_bar: bool,
    /// Values of custom status bar elements registered by plugins.
    /// Key: "plugin_name:token_name", Value: current value to render.
    /// Populated by `render.rs` before rendering.
    pub dynamic_status_bar_elements: HashMap<String, String>,
    /// Active session's workspace-trust level. Drives the always-present
    /// `{trust}` indicator (read from the active authority each frame, so it
    /// never goes stale or vanishes — unlike a per-buffer plugin token).
    pub workspace_trust_level: crate::services::workspace_trust::TrustLevel,
}

/// Layout information returned from status bar rendering for mouse click detection
#[derive(Debug, Clone, Default)]
pub struct StatusBarLayout {
    /// LSP indicator area (row, start_col, end_col) - None if no LSP indicator shown
    pub lsp_indicator: Option<(u16, u16, u16)>,
    /// Warning badge area (row, start_col, end_col) - None if no warnings
    pub warning_badge: Option<(u16, u16, u16)>,
    /// Line ending indicator area (row, start_col, end_col)
    pub line_ending_indicator: Option<(u16, u16, u16)>,
    /// Encoding indicator area (row, start_col, end_col)
    pub encoding_indicator: Option<(u16, u16, u16)>,
    /// Language indicator area (row, start_col, end_col)
    pub language_indicator: Option<(u16, u16, u16)>,
    /// Status message area (row, start_col, end_col) - clickable to show full history
    pub message_area: Option<(u16, u16, u16)>,
    /// Remote authority indicator area (row, start_col, end_col) - clickable
    /// to open the remote-authority context menu.
    pub remote_indicator: Option<(u16, u16, u16)>,
    /// Workspace-trust indicator area (row, start_col, end_col) - clickable
    /// to open the workspace-trust prompt.
    pub trust_indicator: Option<(u16, u16, u16)>,
    /// Plugin-registered status-bar token areas, keyed by the
    /// `"<plugin_name>:<token_name>"` registry key (same key the
    /// editor uses in `status_bar_token_registry`). Populated by the
    /// renderer when it draws each plugin token. Mouse click dispatch
    /// (`handle_click_status_bar`) walks this map after the built-in
    /// indicators; on a hit, it fires the `status_bar_token_clicked`
    /// hook so the plugin can react. This is what makes the env
    /// pill, trust chip, and any future plugin chip first-class
    /// affordances back to their decisions — see
    /// `docs/internal/trust-env-devcontainer-ux-plan.md`
    /// §"Path from here to the North Star".
    pub plugin_token_areas: std::collections::HashMap<String, (u16, u16, u16)>,
}

/// Status bar hover state for styling clickable indicators
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StatusBarHover {
    #[default]
    None,
    /// Mouse is over the LSP indicator
    LspIndicator,
    /// Mouse is over the warning badge
    WarningBadge,
    /// Mouse is over the line ending indicator
    LineEndingIndicator,
    /// Mouse is over the encoding indicator
    EncodingIndicator,
    /// Mouse is over the language indicator
    LanguageIndicator,
    /// Mouse is over the status message area
    MessageArea,
    /// Mouse is over the remote authority indicator
    RemoteIndicator,
    /// Mouse is over the workspace-trust indicator
    WorkspaceTrust,
}

/// Which search option checkbox is being hovered
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SearchOptionsHover {
    #[default]
    None,
    CaseSensitive,
    WholeWord,
    Regex,
    ConfirmEach,
}

/// Layout information for search options bar hit testing
#[derive(Debug, Clone, Default)]
pub struct SearchOptionsLayout {
    /// Row where the search options are rendered
    pub row: u16,
    /// Case Sensitive checkbox area (start_col, end_col)
    pub case_sensitive: Option<(u16, u16)>,
    /// Whole Word checkbox area (start_col, end_col)
    pub whole_word: Option<(u16, u16)>,
    /// Regex checkbox area (start_col, end_col)
    pub regex: Option<(u16, u16)>,
    /// Confirm Each checkbox area (start_col, end_col) - only present in replace mode
    pub confirm_each: Option<(u16, u16)>,
}

impl SearchOptionsLayout {
    /// Check which search option checkbox (if any) is at the given position
    pub fn checkbox_at(&self, x: u16, y: u16) -> Option<SearchOptionsHover> {
        if y != self.row {
            return None;
        }

        if let Some((start, end)) = self.case_sensitive {
            if x >= start && x < end {
                return Some(SearchOptionsHover::CaseSensitive);
            }
        }
        if let Some((start, end)) = self.whole_word {
            if x >= start && x < end {
                return Some(SearchOptionsHover::WholeWord);
            }
        }
        if let Some((start, end)) = self.regex {
            if x >= start && x < end {
                return Some(SearchOptionsHover::Regex);
            }
        }
        if let Some((start, end)) = self.confirm_each {
            if x >= start && x < end {
                return Some(SearchOptionsHover::ConfirmEach);
            }
        }
        None
    }
}

/// Result of truncating a path for display
#[derive(Debug, Clone)]
pub struct TruncatedPath {
    /// The first component(s) of the path (e.g. "/home" or "C:\Users")
    pub prefix: String,
    /// Whether truncation occurred (if true, display "[...]" between prefix and suffix)
    pub truncated: bool,
    /// The last components of the path (e.g. "project/src")
    pub suffix: String,
    /// The path's own separator, reused when re-joining the prefix /
    /// ellipsis / suffix so a Windows `\`-path doesn't display with `/`.
    pub sep: char,
}

impl TruncatedPath {
    /// Get the full display string (without styling)
    pub fn to_string_plain(&self) -> String {
        if self.truncated {
            format!("{}{}[...]{}", self.prefix, self.sep, self.suffix)
        } else {
            format!("{}{}", self.prefix, self.suffix)
        }
    }

    /// Get the display length. The ellipsis marker is one separator column
    /// plus "[...]" regardless of which separator is in use.
    pub fn display_len(&self) -> usize {
        if self.truncated {
            self.prefix.len() + self.sep.len_utf8() + "[...]".len() + self.suffix.len()
        } else {
            self.prefix.len() + self.suffix.len()
        }
    }
}

/// The separator a path string is written with: `\` for a Windows-style
/// path (so it round-trips natively), `/` otherwise. Splitting always
/// accepts both — this only decides how pieces are re-joined for display.
fn path_display_sep(path_str: &str) -> char {
    if path_str.contains('\\') {
        '\\'
    } else {
        '/'
    }
}

/// Truncate a path for display, showing the first component, [...], and last components
///
/// For example, `/private/var/folders/p6/nlmq.../T/.tmpNYt4Fc/project/file.txt`
/// becomes `/private/[...]/project/file.txt`
///
/// # Arguments
/// * `path` - The path to truncate
/// * `max_len` - Maximum length for the display string
///
/// # Returns
/// A TruncatedPath struct with prefix, truncation indicator, and suffix
pub fn truncate_path(path: &Path, max_len: usize) -> TruncatedPath {
    let path_str = path.to_string_lossy();
    // Re-join pieces with the path's own separator so a Windows `\`-path
    // doesn't render as `C:/[...]/x`. Splitting accepts both separators —
    // crucially so a `\`-path isn't treated as one giant component (which
    // previously forced the crude end-truncation branch on Windows).
    let sep = path_display_sep(&path_str);

    // If path fits, return as-is
    if path_str.len() <= max_len {
        return TruncatedPath {
            prefix: String::new(),
            truncated: false,
            suffix: path_str.to_string(),
            sep,
        };
    }

    let components: Vec<&str> = path_str
        .split(['/', '\\'])
        .filter(|s| !s.is_empty())
        .collect();

    if components.is_empty() {
        return TruncatedPath {
            prefix: sep.to_string(),
            truncated: false,
            suffix: String::new(),
            sep,
        };
    }

    // Keep "root + first directory" as the prefix, like the Unix display
    // (`/private/[...]`). A Windows drive letter ("C:") plays the part of
    // the root, so keep `C:\<firstdir>` to stay symmetric instead of just
    // the bare drive.
    let leading_sep = path_str.starts_with('/') || path_str.starts_with('\\');
    let is_drive = |c: &str| {
        let b = c.as_bytes();
        b.len() == 2 && b[1] == b':' && b[0].is_ascii_alphabetic()
    };
    let prefix_count = if !leading_sep && is_drive(components[0]) {
        2
    } else {
        1
    }
    .min(components.len());
    let sep_str = sep.to_string();
    let prefix = {
        let joined = components[..prefix_count].join(&sep_str);
        if leading_sep {
            format!("{}{}", sep, joined)
        } else {
            joined
        }
    };

    // The "<sep>[...]" marker takes 6 bytes (separator + "[...]").
    let ellipsis_len = sep.len_utf8() + "[...]".len();

    // Calculate how much space we have for the suffix
    let available_for_suffix = max_len.saturating_sub(prefix.len() + ellipsis_len);

    if available_for_suffix < 5 || components.len() <= prefix_count {
        // Not enough space or nothing past the prefix, just truncate the
        // end. Walk back to a char boundary so paths with non-ASCII
        // components (e.g. `/home/ユーザー/project`) don't byte-slice
        // through a multi-byte UTF-8 sequence and panic (same class as
        // #1718).
        let truncated_path = if path_str.len() > max_len.saturating_sub(3) {
            let cut = path_str.floor_char_boundary(max_len.saturating_sub(3));
            format!("{}...", &path_str[..cut])
        } else {
            path_str.to_string()
        };
        return TruncatedPath {
            prefix: String::new(),
            truncated: false,
            suffix: truncated_path,
            sep,
        };
    }

    // Build suffix from the last components that fit
    let mut suffix_parts: Vec<&str> = Vec::new();
    let mut suffix_len = 0;

    for component in components.iter().skip(prefix_count).rev() {
        let component_len = component.len() + 1; // +1 for the separator
        if suffix_len + component_len <= available_for_suffix {
            suffix_parts.push(component);
            suffix_len += component_len;
        } else {
            break;
        }
    }

    suffix_parts.reverse();

    // If we included all remaining components, no truncation needed
    if suffix_parts.len() == components.len() - prefix_count {
        return TruncatedPath {
            prefix: String::new(),
            truncated: false,
            suffix: path_str.to_string(),
            sep,
        };
    }

    let suffix = if suffix_parts.is_empty() {
        // Can't fit any suffix components, truncate the last component.
        // floor_char_boundary keeps the slice on a valid UTF-8 boundary
        // when `last` contains non-ASCII characters.
        let last = components.last().unwrap_or(&"");
        let truncate_to = available_for_suffix.saturating_sub(4); // "/.." and some chars
        if truncate_to > 0 && last.len() > truncate_to {
            let cut = last.floor_char_boundary(truncate_to);
            format!("{}{}...", sep, &last[..cut])
        } else {
            format!("{}{}", sep, last)
        }
    } else {
        format!("{}{}", sep, suffix_parts.join(&sep_str))
    };

    TruncatedPath {
        prefix,
        truncated: true,
        suffix,
        sep,
    }
}

/// Truncate a string to fit within `max_width` display columns, appending "..." if truncated.
fn truncate_to_width(s: &str, max_width: usize) -> String {
    let width = str_width(s);
    if width <= max_width {
        return s.to_string();
    }
    let truncate_at = max_width.saturating_sub(3);
    if truncate_at == 0 {
        return if max_width >= 3 {
            "...".to_string()
        } else {
            s.chars().take(max_width).collect()
        };
    }
    let mut w = 0;
    let truncated: String = s
        .chars()
        .take_while(|ch| {
            let cw = char_width(*ch);
            if w + cw <= truncate_at {
                w += cw;
                true
            } else {
                false
            }
        })
        .collect();
    format!("{}...", truncated)
}

/// Minimum column-width to reserve for the column number portion of the
/// cursor indicator. Chosen so the bar stays stable across lines with up
/// to 3-digit column numbers without showing leading padding for the
/// common single-digit case (the text is suffix-padded, not number-padded).
const CURSOR_COL_RESERVE: usize = 3;

/// Compute the cursor column as the number of grapheme clusters between the
/// start of the cursor's line and the cursor. The line start is derived from
/// the live cursor byte position (not a cached line number), so it stays
/// correct in diff/split views where the two can disagree. Counting graphemes
/// — rather than bytes or code points — keeps the reported column consistent
/// with the editor's grapheme-based cursor movement.
fn cursor_column(buffer: &mut crate::model::buffer::TextBuffer, cursor_position: usize) -> usize {
    let mut iter = buffer.line_iterator(cursor_position, 80);
    let line_start = iter.current_position();
    let byte_col = cursor_position.saturating_sub(line_start);
    if byte_col == 0 {
        return 0;
    }
    // Prefer counting grapheme clusters over the line's text so multi-byte
    // characters advance the column by one (issue #2090). Composite/diff
    // buffers don't expose readable line content here; in that case fall back
    // to the byte distance, which equals the grapheme count for the ASCII
    // content those views render and matches the prior behavior.
    match iter.next_line() {
        Some((_, text)) if text.len() >= byte_col => {
            let mut end = byte_col;
            while end > 0 && !text.is_char_boundary(end) {
                end -= 1;
            }
            crate::primitives::grapheme::grapheme_count(&text[..end])
        }
        _ => byte_col,
    }
}

/// Format the cursor's `Ln X, Col Y` indicator so its rendered width is
/// stable as the cursor moves. The numbers themselves are emitted with
/// their natural width — preserving the format existing tests and screen-
/// readers rely on — and trailing spaces are appended to reach a minimum
/// width derived from the buffer's total line count and a fixed reserve
/// for the column number. Fixes the status bar shifting reported in
/// issue #1967.
fn format_cursor_position(line: usize, col: usize, line_count: usize) -> String {
    let text = format!("Ln {line}, Col {col}");
    let line_digits = line_count.max(1).to_string().len();
    // "Ln , Col " literals are 9 ASCII chars.
    let min_width = 9 + line_digits + CURSOR_COL_RESERVE;
    if text.len() < min_width {
        format!("{text:<min_width$}")
    } else {
        text
    }
}

/// Compact variant of `format_cursor_position`, used by
/// `StatusBarElement::CursorCompact`. Renders as `line:col` with the same
/// stable-width trailing-space strategy.
fn format_cursor_position_compact(line: usize, col: usize, line_count: usize) -> String {
    let text = format!("{line}:{col}");
    let line_digits = line_count.max(1).to_string().len();
    // ":" literal is 1 ASCII char.
    let min_width = 1 + line_digits + CURSOR_COL_RESERVE;
    if text.len() < min_width {
        format!("{text:<min_width$}")
    } else {
        text
    }
}

/// Renders the status bar and prompt/minibuffer
pub struct StatusBarRenderer;

impl StatusBarRenderer {
    /// Render only the status bar (without prompt).
    ///
    /// Returns layout information with positions of clickable indicators.
    pub fn render_status_bar(
        frame: &mut Frame,
        area: Rect,
        ctx: &mut StatusBarContext<'_>,
        config: &StatusBarConfig,
    ) -> StatusBarLayout {
        Self::render_status(frame, area, ctx, config)
    }

    /// Render the prompt/minibuffer
    pub fn render_prompt(
        frame: &mut Frame,
        area: Rect,
        prompt: &Prompt,
        theme: &crate::view::theme::Theme,
    ) {
        let base_style = Style::default().fg(theme.prompt_fg).bg(theme.prompt_bg);

        // Create spans for the prompt
        let mut spans = vec![Span::styled(prompt.message.clone(), base_style)];

        // If there's a selection, split the input into parts
        if let Some((sel_start, sel_end)) = prompt.selection_range() {
            let input = &prompt.input;

            // Text before selection
            if sel_start > 0 {
                spans.push(Span::styled(input[..sel_start].to_string(), base_style));
            }

            // Selected text (blue background for visibility, cursor remains visible)
            if sel_start < sel_end {
                // Use theme colors for selection to ensure consistency across themes
                let selection_style = Style::default()
                    .fg(theme.prompt_selection_fg)
                    .bg(theme.prompt_selection_bg);
                spans.push(Span::styled(
                    input[sel_start..sel_end].to_string(),
                    selection_style,
                ));
            }

            // Text after selection
            if sel_end < input.len() {
                spans.push(Span::styled(input[sel_end..].to_string(), base_style));
            }
        } else {
            // No selection, render entire input normally
            spans.push(Span::styled(prompt.input.clone(), base_style));
        }

        let line = Line::from(spans);
        let prompt_line = Paragraph::new(line).style(base_style);

        frame.render_widget(prompt_line, area);

        // Set cursor position in the prompt
        // Use display width (not byte length) for proper handling of:
        // - Double-width CJK characters
        // - Zero-width combining characters (Thai diacritics, etc.)
        let message_width = str_width(&prompt.message);
        let input_width_before_cursor = str_width(&prompt.input[..prompt.cursor_pos]);
        let cursor_x = (message_width + input_width_before_cursor) as u16;
        if cursor_x < area.width {
            frame.set_cursor_position((area.x + cursor_x, area.y));
        }
    }

    /// Render the file open prompt with colorized path
    /// Shows: "Open: /path/to/current/dir/filename" where the directory part is dimmed
    /// Long paths are truncated: "/private/[...]/project/" with [...] styled differently
    pub fn render_file_open_prompt(
        frame: &mut Frame,
        area: Rect,
        prompt: &Prompt,
        file_open_state: &crate::app::file_open::FileOpenState,
        theme: &crate::view::theme::Theme,
    ) {
        let base_style = Style::default().fg(theme.prompt_fg).bg(theme.prompt_bg);
        let dir_style = Style::default()
            .fg(theme.help_separator_fg)
            .bg(theme.prompt_bg);
        // Style for the [...] ellipsis - use a more visible color
        let ellipsis_style = Style::default()
            .fg(theme.menu_highlight_fg)
            .bg(theme.prompt_bg);

        let mut spans = Vec::new();

        // "Open: " prefix
        let open_prompt = t!("file.open_prompt").to_string();
        spans.push(Span::styled(open_prompt.clone(), base_style));

        // Calculate if we need to truncate
        // Only truncate if full path + input exceeds 90% of available width
        let prefix_len = str_width(&open_prompt);
        let dir_path = file_open_state.current_dir.to_string_lossy();
        let dir_path_len = dir_path.len() + 1; // +1 for trailing slash
        let input_len = prompt.input.len();
        let total_len = prefix_len + dir_path_len + input_len;
        let threshold = (area.width as usize * 90) / 100;

        // Truncate the path only if total length exceeds 90% of width
        let truncated = if total_len > threshold {
            // Calculate how much space we have for the path after truncation
            let available_for_path = threshold
                .saturating_sub(prefix_len)
                .saturating_sub(input_len);
            truncate_path(&file_open_state.current_dir, available_for_path)
        } else {
            // No truncation needed - return full path
            TruncatedPath {
                prefix: String::new(),
                truncated: false,
                suffix: dir_path.to_string(),
                sep: path_display_sep(&dir_path),
            }
        };

        // Build the directory display with separate spans for styling
        if truncated.truncated {
            // Prefix (dimmed)
            spans.push(Span::styled(truncated.prefix.clone(), dir_style));
            // Ellipsis "<sep>[...]" (highlighted)
            spans.push(Span::styled(
                format!("{}[...]", truncated.sep),
                ellipsis_style,
            ));
            // Suffix with trailing slash (dimmed)
            let suffix_with_slash = if truncated.suffix.ends_with('/') {
                truncated.suffix.clone()
            } else {
                format!("{}/", truncated.suffix)
            };
            spans.push(Span::styled(suffix_with_slash, dir_style));
        } else {
            // No truncation - just show the path with trailing slash
            let path_display = if truncated.suffix.ends_with('/') {
                truncated.suffix.clone()
            } else {
                format!("{}/", truncated.suffix)
            };
            spans.push(Span::styled(path_display, dir_style));
        }

        // User input (the filename part) - normal color
        spans.push(Span::styled(prompt.input.clone(), base_style));

        let line = Line::from(spans);
        let prompt_line = Paragraph::new(line).style(base_style);

        frame.render_widget(prompt_line, area);

        // Set cursor position in the prompt
        // Use display width for proper handling of Unicode characters
        // We need to calculate the visual width of: "Open: " + dir_display + input[..cursor_pos]
        let prefix_width = str_width(&open_prompt);
        let dir_display_width = if truncated.truncated {
            let suffix_with_slash = if truncated.suffix.ends_with('/') {
                &truncated.suffix
            } else {
                // We already added "/" in the suffix_with_slash above, so approximate
                &truncated.suffix
            };
            str_width(&truncated.prefix) + str_width("/[...]") + str_width(suffix_with_slash) + 1
        } else {
            str_width(&truncated.suffix) + 1 // +1 for trailing slash
        };
        let input_width_before_cursor = str_width(&prompt.input[..prompt.cursor_pos]);
        let cursor_x = (prefix_width + dir_display_width + input_width_before_cursor) as u16;
        if cursor_x < area.width {
            frame.set_cursor_position((area.x + cursor_x, area.y));
        }
    }

    /// Render a single element to its text representation.
    /// Returns None if the element has nothing to display.
    fn render_element(
        element: &StatusBarElement,
        ctx: &mut StatusBarContext<'_>,
    ) -> Option<RenderedElement> {
        // Buffer-specific elements have nothing meaningful to show when
        // the active buffer is just a synthesized placeholder kept alive
        // for editor invariants. Suppress them so the status bar tells
        // the truth: there's no real file open.
        if ctx.is_synthetic_placeholder
            && matches!(
                element,
                StatusBarElement::Filename
                    | StatusBarElement::Cursor
                    | StatusBarElement::CursorCompact
                    | StatusBarElement::CursorCount
                    | StatusBarElement::Diagnostics
                    | StatusBarElement::LineEnding
                    | StatusBarElement::Encoding
                    | StatusBarElement::Language
            )
        {
            return None;
        }
        match element {
            StatusBarElement::Filename => {
                let modified = if ctx.state.buffer.is_modified() {
                    " [+]"
                } else {
                    ""
                };
                let read_only_indicator = if ctx.read_only { " [RO]" } else { "" };
                let remote_disconnected = ctx
                    .remote_connection
                    .map(|conn| conn.contains("(Disconnected)"))
                    .unwrap_or(false);
                // The `[Container:<id>] ` / `<SSH_PREFIX>conn<...>`
                // prefix is redundant when the dedicated `{remote}`
                // indicator is on the bar — same identity, two
                // places. Skip it then. When `{remote}` is NOT on
                // the bar, keep the prefix so users still see the
                // connection at a glance from the filename.
                let remote_prefix = if ctx.remote_indicator_on_bar {
                    String::new()
                } else {
                    ctx.remote_connection
                        .map(|conn| {
                            if conn.starts_with("Container:") {
                                format!("[{}] ", conn)
                            } else {
                                format!("{SSH_PREFIX}{conn}{SSH_PREFIX_TERMINATOR}")
                            }
                        })
                        .unwrap_or_default()
                };
                let session_prefix = ctx
                    .session_name
                    .map(|name| format!("[{}] ", name))
                    .unwrap_or_default();
                let display_name = ctx.display_name;
                let text = format!(
                    "{session_prefix}{remote_prefix}{display_name}{modified}{read_only_indicator}"
                );
                let kind = if remote_disconnected {
                    ElementKind::RemoteDisconnected
                } else {
                    ElementKind::Normal
                };
                Some(RenderedElement {
                    text,
                    kind,
                    token_key: None,
                })
            }
            StatusBarElement::Cursor => {
                if !ctx.state.show_cursors {
                    return None;
                }
                let cursor = *ctx.cursors.primary();
                let line_count = ctx.state.buffer.line_count();
                let text = if let Some(lc) = line_count {
                    let line = ctx.state.primary_cursor_line_number.value();
                    let col = cursor_column(&mut ctx.state.buffer, cursor.position);
                    format_cursor_position(line + 1, col + 1, lc)
                } else {
                    format!("Byte {}", cursor.position)
                };
                Some(RenderedElement {
                    text,
                    kind: ElementKind::Normal,
                    token_key: None,
                })
            }
            StatusBarElement::CursorCompact => {
                if !ctx.state.show_cursors {
                    return None;
                }
                let cursor = *ctx.cursors.primary();
                let line_count = ctx.state.buffer.line_count();
                let text = if let Some(lc) = line_count {
                    let line = ctx.state.primary_cursor_line_number.value();
                    let col = cursor_column(&mut ctx.state.buffer, cursor.position);
                    format_cursor_position_compact(line + 1, col + 1, lc)
                } else {
                    format!("{}", cursor.position)
                };
                Some(RenderedElement {
                    text,
                    kind: ElementKind::Normal,
                    token_key: None,
                })
            }
            StatusBarElement::Diagnostics => {
                let diagnostics = ctx.state.overlays.all();
                let mut error_count = 0usize;
                let mut warning_count = 0usize;
                let mut info_count = 0usize;
                let diagnostic_ns = crate::services::lsp::diagnostics::lsp_diagnostic_namespace();
                for overlay in diagnostics {
                    if overlay.namespace.as_ref() == Some(&diagnostic_ns) {
                        match overlay.priority {
                            100 => error_count += 1,
                            50 => warning_count += 1,
                            _ => info_count += 1,
                        }
                    }
                }
                if error_count + warning_count + info_count == 0 {
                    return None;
                }
                let mut parts = Vec::new();
                if error_count > 0 {
                    parts.push(format!("E:{}", error_count));
                }
                if warning_count > 0 {
                    parts.push(format!("W:{}", warning_count));
                }
                if info_count > 0 {
                    parts.push(format!("I:{}", info_count));
                }
                Some(RenderedElement {
                    text: parts.join(" "),
                    kind: ElementKind::Normal,
                    token_key: None,
                })
            }
            StatusBarElement::CursorCount => {
                if ctx.cursors.count() <= 1 {
                    return None;
                }
                Some(RenderedElement {
                    text: t!("status.cursors", count = ctx.cursors.count()).to_string(),
                    kind: ElementKind::Normal,
                    token_key: None,
                })
            }
            StatusBarElement::Messages => {
                let mut parts: Vec<&str> = Vec::new();
                if let Some(msg) = ctx.status_message {
                    if !msg.is_empty() {
                        parts.push(msg);
                    }
                }
                if let Some(msg) = ctx.plugin_status_message {
                    if !msg.is_empty() {
                        parts.push(msg);
                    }
                }
                if parts.is_empty() {
                    return None;
                }
                Some(RenderedElement {
                    text: parts.join(" | "),
                    kind: ElementKind::Messages,
                    token_key: None,
                })
            }
            StatusBarElement::Chord => {
                if ctx.chord_state.is_empty() {
                    return None;
                }
                let chord_str = ctx
                    .chord_state
                    .iter()
                    .map(|(code, modifiers)| {
                        crate::input::keybindings::format_keybinding(code, modifiers)
                    })
                    .collect::<Vec<_>>()
                    .join(" ");
                Some(RenderedElement {
                    text: format!("[{}]", chord_str),
                    kind: ElementKind::Normal,
                    token_key: None,
                })
            }
            StatusBarElement::LineEnding => Some(RenderedElement {
                text: ctx.state.buffer.line_ending().display_name().to_string(),
                kind: ElementKind::LineEnding,
                token_key: None,
            }),
            StatusBarElement::Encoding => Some(RenderedElement {
                text: ctx.state.buffer.encoding().display_name().to_string(),
                kind: ElementKind::Encoding,
                token_key: None,
            }),
            StatusBarElement::Language => {
                let text = if ctx.state.language == "text"
                    && ctx.state.display_name != "Text"
                    && ctx.state.display_name != "Plain Text"
                    && ctx.state.display_name != "text"
                {
                    format!("{} [syntax only]", &ctx.state.display_name)
                } else {
                    ctx.state.display_name.to_string()
                };
                Some(RenderedElement {
                    text,
                    kind: ElementKind::Language,
                    token_key: None,
                })
            }
            StatusBarElement::Lsp => {
                if ctx.lsp_status.is_empty() {
                    return None;
                }
                Some(RenderedElement {
                    text: ctx.lsp_status.to_string(),
                    kind: ElementKind::Lsp,
                    token_key: None,
                })
            }
            StatusBarElement::Warnings => {
                if ctx.general_warning_count == 0 {
                    return None;
                }
                Some(RenderedElement {
                    text: format!("[\u{26a0} {}]", ctx.general_warning_count),
                    kind: ElementKind::WarningBadge,
                    token_key: None,
                })
            }
            StatusBarElement::Update => {
                let version = ctx.update_available?;
                Some(RenderedElement {
                    text: t!("status.update_available", version = version).to_string(),
                    kind: ElementKind::Update,
                    token_key: None,
                })
            }
            StatusBarElement::Palette => {
                let shortcut = ctx
                    .keybindings
                    .get_keybinding_for_action(
                        &crate::input::keybindings::Action::QuickOpen,
                        crate::input::keybindings::KeyContext::Global,
                    )
                    .unwrap_or_else(|| "?".to_string());
                Some(RenderedElement {
                    text: t!("status.palette", shortcut = shortcut).to_string(),
                    kind: ElementKind::Palette,
                    token_key: None,
                })
            }
            StatusBarElement::Clock => {
                let now = chrono::Local::now();
                let text = format!("{:02}:{:02}", now.hour(), now.minute());
                Some(RenderedElement {
                    text,
                    kind: ElementKind::Clock,
                    token_key: None,
                })
            }
            StatusBarElement::RemoteIndicator => {
                // Persistent remote-authority entry point. When local we
                // still emit a short label so the indicator is visible —
                // the spec calls for a persistent control, not one that
                // vanishes when there is nothing to report.
                //
                // Precedence: plugin-supplied override (via
                // `SetRemoteIndicatorState`) wins over the authority-
                // derived state. The override carries its own label;
                // derived states synthesize one from `remote_connection`.
                let (text, state) = if let Some(over) = ctx.remote_state_override {
                    (over.label(), over.state())
                } else {
                    match ctx.remote_connection {
                        None => ("Local".to_string(), RemoteIndicatorState::Local),
                        Some(conn) if conn.contains("(Disconnected)") => {
                            (conn.to_string(), RemoteIndicatorState::Disconnected)
                        }
                        Some(conn) => (conn.to_string(), RemoteIndicatorState::Connected),
                    }
                };
                Some(RenderedElement {
                    text,
                    kind: ElementKind::RemoteIndicator(state),
                    token_key: None,
                })
            }
            StatusBarElement::WorkspaceTrust => {
                // Always-present trust control, read from the active session's
                // trust level each frame. Persistent like `{remote}` — it never
                // vanishes, so the user always knows whether repo-controlled
                // execution is gated. Capitalized for a status-bar label.
                use crate::services::workspace_trust::TrustLevel;
                let level = ctx.workspace_trust_level;
                let text = match level {
                    TrustLevel::Trusted => t!("statusbar.trust.trusted"),
                    TrustLevel::Restricted => t!("statusbar.trust.restricted"),
                    TrustLevel::Blocked => t!("statusbar.trust.blocked"),
                }
                .to_string();
                Some(RenderedElement {
                    text,
                    kind: ElementKind::WorkspaceTrust(level),
                    token_key: None,
                })
            }
            StatusBarElement::CustomToken(key) => {
                if let Some(value) = ctx.dynamic_status_bar_elements.get(key) {
                    Some(RenderedElement {
                        text: value.clone(),
                        kind: ElementKind::Custom,
                        token_key: Some(key.clone()),
                    })
                } else {
                    None // Skip rendering if no value set
                }
            }
        }
    }

    /// Get the style for a rendered element based on its kind, theme, and hover state.
    fn element_style(
        kind: ElementKind,
        theme: &crate::view::theme::Theme,
        hover: StatusBarHover,
        _warning_level: WarningLevel,
        lsp_state: LspIndicatorState,
    ) -> Style {
        match kind {
            ElementKind::Normal | ElementKind::Messages | ElementKind::Clock => Style::default()
                .fg(theme.status_bar_fg)
                .bg(theme.status_bar_bg),
            ElementKind::RemoteDisconnected => Style::default()
                .fg(theme.status_error_indicator_fg)
                .bg(theme.status_error_indicator_bg),
            ElementKind::LineEnding => {
                let is_hovering = hover == StatusBarHover::LineEndingIndicator;
                let (fg, bg) = if is_hovering {
                    (theme.menu_hover_fg, theme.menu_hover_bg)
                } else {
                    (theme.status_bar_fg, theme.status_bar_bg)
                };
                let mut style = Style::default().fg(fg).bg(bg);
                if is_hovering {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                style
            }
            ElementKind::Encoding => {
                let is_hovering = hover == StatusBarHover::EncodingIndicator;
                let (fg, bg) = if is_hovering {
                    (theme.menu_hover_fg, theme.menu_hover_bg)
                } else {
                    (theme.status_bar_fg, theme.status_bar_bg)
                };
                let mut style = Style::default().fg(fg).bg(bg);
                if is_hovering {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                style
            }
            ElementKind::Language => {
                let is_hovering = hover == StatusBarHover::LanguageIndicator;
                let (fg, bg) = if is_hovering {
                    (theme.menu_hover_fg, theme.menu_hover_bg)
                } else {
                    (theme.status_bar_fg, theme.status_bar_bg)
                };
                let mut style = Style::default().fg(fg).bg(bg);
                if is_hovering {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                style
            }
            ElementKind::Lsp => {
                let is_hovering = hover == StatusBarHover::LspIndicator;
                // Color by LSP state:
                //   Error  → diagnostic_error_*       (red-ish; problem)
                //   Off    → status_lsp_actionable_*  (prominent; click to act)
                //   On     → status_lsp_on_*          (neutral; healthy)
                //   Dismissed/None → status-bar palette (muted; nothing to do)
                //
                // Off is the indicator's main signal that the user has
                // useful options behind a click — drawn prominently so
                // it stands out in the status bar without auto-popping
                // a dialog.
                let (fg, bg) = match lsp_state {
                    LspIndicatorState::Error => {
                        (theme.diagnostic_error_fg, theme.diagnostic_error_bg)
                    }
                    LspIndicatorState::Off => (
                        theme.status_lsp_actionable_fg,
                        theme.status_lsp_actionable_bg,
                    ),
                    LspIndicatorState::On => (theme.status_lsp_on_fg, theme.status_lsp_on_bg),
                    LspIndicatorState::OffDismissed => (theme.status_bar_fg, theme.status_bar_bg),
                    LspIndicatorState::None => (theme.status_bar_fg, theme.status_bar_bg),
                };
                let mut style = Style::default().fg(fg).bg(bg);
                // Always underline on hover — the indicator is clickable
                // in all non-empty states.  Previously we only underlined
                // when warning_level != None, so "LSP (on)" gave no hover
                // cue that it was clickable.
                if is_hovering && lsp_state != LspIndicatorState::None {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                style
            }
            ElementKind::WarningBadge => {
                let is_hovering = hover == StatusBarHover::WarningBadge;
                let (fg, bg) = if is_hovering {
                    (
                        theme.status_warning_indicator_hover_fg,
                        theme.status_warning_indicator_hover_bg,
                    )
                } else {
                    (
                        theme.status_warning_indicator_fg,
                        theme.status_warning_indicator_bg,
                    )
                };
                let mut style = Style::default().fg(fg).bg(bg);
                if is_hovering {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                style
            }
            ElementKind::Update => Style::default()
                .fg(theme.menu_highlight_fg)
                .bg(theme.menu_dropdown_bg),
            // The palette shortcut hint is purely informational — driven
            // by the dedicated `status_palette_*` theme keys (default
            // to the neutral status-bar palette so it blends into the
            // bar instead of breaking the color band at the right edge).
            ElementKind::Palette => Style::default()
                .fg(theme.status_palette_fg)
                .bg(theme.status_palette_bg),
            ElementKind::Custom => Style::default()
                .fg(theme.status_bar_fg)
                .bg(theme.status_bar_bg),
            ElementKind::RemoteIndicator(state) => {
                let is_hovering = hover == StatusBarHover::RemoteIndicator;
                let (fg, bg) = match state {
                    // Connecting and Connected share the "help
                    // indicator" palette so the transition from one to
                    // the other is a glyph swap rather than a color
                    // flash — the user's eye tracks the indicator
                    // changing, not disappearing.
                    RemoteIndicatorState::Connecting | RemoteIndicatorState::Connected => {
                        (theme.help_indicator_fg, theme.help_indicator_bg)
                    }
                    // FailedAttach + Disconnected share the error
                    // palette. Both are "the remote isn't reaching you
                    // right now" states, differing only in cause.
                    RemoteIndicatorState::FailedAttach | RemoteIndicatorState::Disconnected => (
                        theme.status_error_indicator_fg,
                        theme.status_error_indicator_bg,
                    ),
                    // Local: neutral status-bar palette.
                    RemoteIndicatorState::Local => (theme.status_bar_fg, theme.status_bar_bg),
                };
                let mut style = Style::default().fg(fg).bg(bg);
                if is_hovering {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                style
            }
            ElementKind::WorkspaceTrust(level) => {
                use crate::services::workspace_trust::TrustLevel;
                let is_hovering = hover == StatusBarHover::WorkspaceTrust;
                let (fg, bg) = match level {
                    // Gated states reuse the warning indicator palette so
                    // "execution is restricted/blocked" reads at a glance.
                    TrustLevel::Restricted | TrustLevel::Blocked => (
                        theme.status_warning_indicator_fg,
                        theme.status_warning_indicator_bg,
                    ),
                    // Trusted: neutral status-bar palette (everything-works).
                    TrustLevel::Trusted => (theme.status_bar_fg, theme.status_bar_bg),
                };
                let mut style = Style::default().fg(fg).bg(bg);
                if is_hovering {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                style
            }
        }
    }

    /// Map a rendered element to the layout field(s) it should populate.
    /// Built-in indicators get their dedicated `Option<(row, start_col,
    /// end_col)>` slot. Plugin tokens (`ElementKind::Custom` carrying a
    /// `token_key`) get an entry in `plugin_token_areas`, keyed by the
    /// plugin's registry key — that's what `handle_click_status_bar`
    /// uses to dispatch clicks back to the right plugin.
    fn update_layout_for_element(
        layout: &mut StatusBarLayout,
        kind: ElementKind,
        token_key: Option<&str>,
        row: u16,
        start_col: u16,
        end_col: u16,
    ) {
        match kind {
            ElementKind::LineEnding => {
                layout.line_ending_indicator = Some((row, start_col, end_col))
            }
            ElementKind::Encoding => layout.encoding_indicator = Some((row, start_col, end_col)),
            ElementKind::Language => layout.language_indicator = Some((row, start_col, end_col)),
            ElementKind::Lsp => layout.lsp_indicator = Some((row, start_col, end_col)),
            ElementKind::WarningBadge => layout.warning_badge = Some((row, start_col, end_col)),
            ElementKind::Messages => layout.message_area = Some((row, start_col, end_col)),
            ElementKind::RemoteIndicator(_) => {
                layout.remote_indicator = Some((row, start_col, end_col))
            }
            ElementKind::WorkspaceTrust(_) => {
                layout.trust_indicator = Some((row, start_col, end_col))
            }
            ElementKind::Custom => {
                if let Some(key) = token_key {
                    layout
                        .plugin_token_areas
                        .insert(key.to_string(), (row, start_col, end_col));
                }
            }
            _ => {}
        }
    }

    /// Build the styled spans for a single rendered element, honoring the
    /// special-case two-color rendering for a disconnected remote filename.
    ///
    /// Returns the spans and the total display width of the emitted text.
    fn element_spans(
        rendered: &RenderedElement,
        theme: &crate::view::theme::Theme,
        hover: StatusBarHover,
        warning_level: WarningLevel,
        lsp_state: LspIndicatorState,
    ) -> (Vec<Span<'static>>, usize) {
        let base_style = Style::default()
            .fg(theme.status_bar_fg)
            .bg(theme.status_bar_bg);
        // Each entry carries a one-space margin on each side painted in its own
        // style, so entries with a distinct background (LSP / warnings / update
        // / palette / remote) render as a padded pill. The separator is then a
        // bare glyph drawn between these padded entries.
        let width = str_width(&rendered.text) + 2;

        if rendered.kind == ElementKind::RemoteDisconnected && rendered.text.starts_with(SSH_PREFIX)
        {
            let error_style = Style::default()
                .fg(theme.status_error_indicator_fg)
                .bg(theme.status_error_indicator_bg);
            if let Some(term_off) = rendered.text.find(SSH_PREFIX_TERMINATOR) {
                let split_at = term_off + SSH_PREFIX_TERMINATOR.len();
                let prefix = rendered.text[..split_at].to_string();
                let rest = rendered.text[split_at..].to_string();
                return (
                    vec![
                        Span::styled(" ", error_style),
                        Span::styled(prefix, error_style),
                        Span::styled(rest, base_style),
                        Span::styled(" ", base_style),
                    ],
                    width,
                );
            }
            return (
                vec![
                    Span::styled(" ", error_style),
                    Span::styled(rendered.text.clone(), error_style),
                    Span::styled(" ", error_style),
                ],
                width,
            );
        }

        let style = Self::element_style(rendered.kind, theme, hover, warning_level, lsp_state);
        let mut spans = vec![Span::styled(" ", style)];
        if rendered.kind == ElementKind::Clock {
            // "HH:MM" — blink the colon via terminal hardware (SGR 5)
            spans.push(Span::styled(rendered.text[..2].to_string(), style));
            spans.push(Span::styled(
                ":".to_string(),
                style.add_modifier(Modifier::SLOW_BLINK),
            ));
            spans.push(Span::styled(rendered.text[3..].to_string(), style));
        } else {
            spans.push(Span::styled(rendered.text.clone(), style));
        }
        spans.push(Span::styled(" ", style));
        (spans, width)
    }

    /// Render a configured side (left/right) into styled per-element groups.
    /// Each tuple carries the rendered spans, total width, the kind tag
    /// (for layout/click-area routing of built-ins), and the plugin
    /// token key (`Some` only for `ElementKind::Custom`) so the
    /// placement loops can record the screen area under the same key
    /// the plugin registered.
    fn render_side(
        config_side: &[StatusBarElement],
        ctx: &mut StatusBarContext<'_>,
    ) -> Vec<(Vec<Span<'static>>, usize, ElementKind, Option<String>)> {
        let rendered: Vec<RenderedElement> = config_side
            .iter()
            .filter_map(|elem| Self::render_element(elem, ctx))
            .filter(|e| !e.text.is_empty())
            .collect();

        let theme = ctx.theme;
        let hover = ctx.hover;
        let warning_level = ctx.warning_level;
        let lsp_state = ctx.lsp_indicator_state;
        rendered
            .into_iter()
            .map(|r| {
                let kind = r.kind;
                let token_key = r.token_key.clone();
                let (spans, width) =
                    Self::element_spans(&r, theme, hover, warning_level, lsp_state);
                (spans, width, kind, token_key)
            })
            .collect()
    }

    /// Render the normal status bar (config-driven).
    fn render_status(
        frame: &mut Frame,
        area: Rect,
        ctx: &mut StatusBarContext<'_>,
        config: &StatusBarConfig,
    ) -> StatusBarLayout {
        let mut layout = StatusBarLayout::default();
        let base_style = Style::default()
            .fg(ctx.theme.status_bar_fg)
            .bg(ctx.theme.status_bar_bg);
        let available_width = area.width as usize;

        if available_width == 0 || area.height == 0 {
            return layout;
        }

        // Tell the per-element renderer whether the dedicated
        // RemoteIndicator is on the bar so the Filename branch
        // can drop its now-redundant `[Container:<id>] ` /
        // SSH prefix.
        ctx.remote_indicator_on_bar = config
            .left
            .iter()
            .chain(config.right.iter())
            .any(|e| matches!(e, StatusBarElement::RemoteIndicator));

        let left_items = Self::render_side(&config.left, ctx);
        let mut right_items = Self::render_side(&config.right, ctx);

        // Separator drawn between elements, used verbatim from config.
        // An empty value disables separators and consumes no width.
        let separator: &str = &config.separator;
        let separator_width = str_width(separator);
        // The separator glyph is colored by the theme's dedicated separator
        // keys so it can be dimmed against the bar; both fall back to the bar.
        let separator_style = Style::default()
            .fg(ctx.theme.status_separator_fg)
            .bg(ctx.theme.status_separator_bg);

        // Reserve a sane minimum for the left side so the buffer name and
        // cursor position aren't truncated to a single character on narrow
        // terminals (regression originally reported as
        // `t  LF  ASCII  Markdown ...`).  Drop low-priority right elements
        // (configured right-most first) until the remaining right side fits
        // alongside that minimum left budget.  We never drop the *first*
        // right element so the user keeps at least one piece of right-side
        // status if any was configured.
        let total_right_width: usize = right_items.iter().map(|(_, w, _, _)| *w).sum::<usize>()
            + separator_width * right_items.len().saturating_sub(1);
        let left_min_target = available_width
            .saturating_mul(2)
            .saturating_div(5) // ~40% of width reserved for left when feasible
            .min(40); // but never demand more than 40 cols even on wide terminals
        let right_budget = available_width.saturating_sub(left_min_target + 1);
        if total_right_width > right_budget && right_items.len() > 1 {
            let mut current = total_right_width;
            while current > right_budget && right_items.len() > 1 {
                if let Some(dropped) = right_items.pop() {
                    current = current.saturating_sub(dropped.1);
                    // Also remove the separator that preceded the dropped
                    // element (always present since we never drop the first)
                    current = current.saturating_sub(separator_width);
                } else {
                    break;
                }
            }
        }

        let right_width: usize = right_items.iter().map(|(_, w, _, _)| *w).sum::<usize>()
            + separator_width * right_items.len().saturating_sub(1);

        let narrow = available_width < 15;
        let left_max_width = if narrow {
            available_width
        } else if available_width > right_width + 1 {
            available_width - right_width - 1
        } else {
            1
        };

        // Emit left side, consuming `left_items` so each element's spans move
        // directly into the output without a clone. Widths are cached so the
        // truncation check doesn't re-measure text.
        let mut spans: Vec<Span<'static>> = Vec::new();
        let mut used_left: usize = 0;

        for (idx, (item_spans, width, kind, token_key)) in left_items.into_iter().enumerate() {
            let sep_width = if idx == 0 { 0 } else { separator_width };
            if used_left + sep_width >= left_max_width {
                break;
            }
            if sep_width > 0 {
                spans.push(Span::styled(separator.to_string(), separator_style));
                used_left += sep_width;
            }

            let remaining = left_max_width - used_left;
            let start_col = used_left;

            if width <= remaining {
                spans.extend(item_spans);
                used_left += width;

                Self::update_layout_for_element(
                    &mut layout,
                    kind,
                    token_key.as_deref(),
                    area.y,
                    area.x + start_col as u16,
                    area.x + (start_col + width) as u16,
                );
            } else {
                // Overflow: truncate the concatenated text of this element.
                // Per-span styling is lost for the overflowed slice — we fall
                // back to whatever `element_style` would have returned.
                let group_text: String = item_spans.iter().map(|s| s.content.as_ref()).collect();
                let truncated = truncate_to_width(&group_text, remaining);
                let truncated_width = str_width(&truncated);
                let overflow_style = Self::element_style(
                    kind,
                    ctx.theme,
                    ctx.hover,
                    ctx.warning_level,
                    ctx.lsp_indicator_state,
                );
                spans.push(Span::styled(truncated, overflow_style));
                used_left += truncated_width;

                Self::update_layout_for_element(
                    &mut layout,
                    kind,
                    token_key.as_deref(),
                    area.y,
                    area.x + start_col as u16,
                    area.x + (start_col + truncated_width) as u16,
                );
                break;
            }
        }

        if narrow {
            if used_left < available_width {
                spans.push(Span::styled(
                    " ".repeat(available_width - used_left),
                    base_style,
                ));
            }
            frame.render_widget(Paragraph::new(Line::from(spans)), area);
            return layout;
        }

        let mut col_offset = used_left;
        if col_offset + right_width < available_width {
            let padding = available_width - col_offset - right_width;
            spans.push(Span::styled(" ".repeat(padding), base_style));
            col_offset = available_width - right_width;
        } else if col_offset < available_width {
            spans.push(Span::styled(" ", base_style));
            col_offset += 1;
        }

        let mut current_col = area.x + col_offset as u16;
        for (idx, (item_spans, width, kind, token_key)) in right_items.into_iter().enumerate() {
            if idx > 0 && separator_width > 0 {
                spans.push(Span::styled(separator.to_string(), separator_style));
                current_col += separator_width as u16;
            }
            Self::update_layout_for_element(
                &mut layout,
                kind,
                token_key.as_deref(),
                area.y,
                current_col,
                current_col + width as u16,
            );
            spans.extend(item_spans);
            current_col += width as u16;
        }

        frame.render_widget(Paragraph::new(Line::from(spans)), area);
        layout
    }

    /// Render the search options bar (shown when search prompt is active)
    ///
    /// Displays checkboxes for search options with their keyboard shortcuts:
    /// - Case Sensitive (Alt+C)
    /// - Whole Word (Alt+W)
    /// - Regex (Alt+R)
    /// - Confirm Each (Alt+I) - only shown in replace mode
    ///
    /// # Returns
    /// Layout information for hit testing mouse clicks on checkboxes
    #[allow(clippy::too_many_arguments)]
    pub fn render_search_options(
        frame: &mut Frame,
        area: Rect,
        case_sensitive: bool,
        whole_word: bool,
        use_regex: bool,
        confirm_each: Option<bool>, // None = don't show, Some(value) = show with this state
        theme: &crate::view::theme::Theme,
        keybindings: &crate::input::keybindings::KeybindingResolver,
        hover: SearchOptionsHover,
    ) -> SearchOptionsLayout {
        use crate::primitives::display_width::str_width;

        let mut layout = SearchOptionsLayout {
            row: area.y,
            ..Default::default()
        };

        // Use menu dropdown background (dark gray) for the options bar
        let base_style = Style::default()
            .fg(theme.menu_dropdown_fg)
            .bg(theme.menu_dropdown_bg);

        // Style for hovered options - use menu hover colors
        let hover_style = Style::default()
            .fg(theme.menu_hover_fg)
            .bg(theme.menu_hover_bg);

        // Helper to look up keybinding for an action (Prompt context first, then Global)
        let get_shortcut = |action: &crate::input::keybindings::Action| -> Option<String> {
            keybindings
                .get_keybinding_for_action(action, crate::input::keybindings::KeyContext::Prompt)
                .or_else(|| {
                    keybindings.get_keybinding_for_action(
                        action,
                        crate::input::keybindings::KeyContext::Global,
                    )
                })
        };

        // Get keybindings for search options
        let case_shortcut =
            get_shortcut(&crate::input::keybindings::Action::ToggleSearchCaseSensitive);
        let word_shortcut = get_shortcut(&crate::input::keybindings::Action::ToggleSearchWholeWord);
        let regex_shortcut = get_shortcut(&crate::input::keybindings::Action::ToggleSearchRegex);

        // Build the options display with checkboxes
        let case_checkbox = if case_sensitive { "[x]" } else { "[ ]" };
        let word_checkbox = if whole_word { "[x]" } else { "[ ]" };
        let regex_checkbox = if use_regex { "[x]" } else { "[ ]" };

        // Style for active (checked) options - highlighted with menu highlight colors
        let active_style = Style::default()
            .fg(theme.menu_highlight_fg)
            .bg(theme.menu_dropdown_bg);

        // Style for keyboard shortcuts - use theme color for consistency
        let shortcut_style = Style::default()
            .fg(theme.help_separator_fg)
            .bg(theme.menu_dropdown_bg);

        // Hovered shortcut style
        let hover_shortcut_style = Style::default()
            .fg(theme.menu_hover_fg)
            .bg(theme.menu_hover_bg);

        let mut spans = Vec::new();
        let mut current_col = area.x;

        // Left padding
        spans.push(Span::styled(" ", base_style));
        current_col += 1;

        // Helper to get style based on hover and checked state
        let get_checkbox_style = |is_hovered: bool, is_checked: bool| -> Style {
            if is_hovered {
                hover_style
            } else if is_checked {
                active_style
            } else {
                base_style
            }
        };

        // Case Sensitive option
        let case_hovered = hover == SearchOptionsHover::CaseSensitive;
        let case_start = current_col;
        let case_label = format!("{} {}", case_checkbox, t!("search.case_sensitive"));
        let case_shortcut_text = case_shortcut
            .as_ref()
            .map(|s| format!(" ({})", s))
            .unwrap_or_default();
        let case_full_width = str_width(&case_label) + str_width(&case_shortcut_text);

        spans.push(Span::styled(
            case_label,
            get_checkbox_style(case_hovered, case_sensitive),
        ));
        if !case_shortcut_text.is_empty() {
            spans.push(Span::styled(
                case_shortcut_text,
                if case_hovered {
                    hover_shortcut_style
                } else {
                    shortcut_style
                },
            ));
        }
        current_col += case_full_width as u16;
        layout.case_sensitive = Some((case_start, current_col));

        // Separator
        spans.push(Span::styled("   ", base_style));
        current_col += 3;

        // Whole Word option
        let word_hovered = hover == SearchOptionsHover::WholeWord;
        let word_start = current_col;
        let word_label = format!("{} {}", word_checkbox, t!("search.whole_word"));
        let word_shortcut_text = word_shortcut
            .as_ref()
            .map(|s| format!(" ({})", s))
            .unwrap_or_default();
        let word_full_width = str_width(&word_label) + str_width(&word_shortcut_text);

        spans.push(Span::styled(
            word_label,
            get_checkbox_style(word_hovered, whole_word),
        ));
        if !word_shortcut_text.is_empty() {
            spans.push(Span::styled(
                word_shortcut_text,
                if word_hovered {
                    hover_shortcut_style
                } else {
                    shortcut_style
                },
            ));
        }
        current_col += word_full_width as u16;
        layout.whole_word = Some((word_start, current_col));

        // Separator
        spans.push(Span::styled("   ", base_style));
        current_col += 3;

        // Regex option
        let regex_hovered = hover == SearchOptionsHover::Regex;
        let regex_start = current_col;
        let regex_label = format!("{} {}", regex_checkbox, t!("search.regex"));
        let regex_shortcut_text = regex_shortcut
            .as_ref()
            .map(|s| format!(" ({})", s))
            .unwrap_or_default();
        let regex_full_width = str_width(&regex_label) + str_width(&regex_shortcut_text);

        spans.push(Span::styled(
            regex_label,
            get_checkbox_style(regex_hovered, use_regex),
        ));
        if !regex_shortcut_text.is_empty() {
            spans.push(Span::styled(
                regex_shortcut_text,
                if regex_hovered {
                    hover_shortcut_style
                } else {
                    shortcut_style
                },
            ));
        }
        current_col += regex_full_width as u16;
        layout.regex = Some((regex_start, current_col));

        // Show capture group hint when regex is enabled in replace mode
        if use_regex && confirm_each.is_some() {
            let hint = " \u{2502} $1,$2,…";
            spans.push(Span::styled(hint, shortcut_style));
            current_col += str_width(hint) as u16;
        }

        // Confirm Each option (only shown in replace mode)
        if let Some(confirm_value) = confirm_each {
            let confirm_shortcut =
                get_shortcut(&crate::input::keybindings::Action::ToggleSearchConfirmEach);
            let confirm_checkbox = if confirm_value { "[x]" } else { "[ ]" };

            // Separator
            spans.push(Span::styled("   ", base_style));
            current_col += 3;

            let confirm_hovered = hover == SearchOptionsHover::ConfirmEach;
            let confirm_start = current_col;
            let confirm_label = format!("{} {}", confirm_checkbox, t!("search.confirm_each"));
            let confirm_shortcut_text = confirm_shortcut
                .as_ref()
                .map(|s| format!(" ({})", s))
                .unwrap_or_default();
            let confirm_full_width = str_width(&confirm_label) + str_width(&confirm_shortcut_text);

            spans.push(Span::styled(
                confirm_label,
                get_checkbox_style(confirm_hovered, confirm_value),
            ));
            if !confirm_shortcut_text.is_empty() {
                spans.push(Span::styled(
                    confirm_shortcut_text,
                    if confirm_hovered {
                        hover_shortcut_style
                    } else {
                        shortcut_style
                    },
                ));
            }
            current_col += confirm_full_width as u16;
            layout.confirm_each = Some((confirm_start, current_col));
        }

        // Fill remaining space
        let current_width = (current_col - area.x) as usize;
        let available_width = area.width as usize;
        if current_width < available_width {
            spans.push(Span::styled(
                " ".repeat(available_width.saturating_sub(current_width)),
                base_style,
            ));
        }

        let options_line = Paragraph::new(Line::from(spans));
        frame.render_widget(options_line, area);

        layout
    }
}

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

    #[test]
    fn test_truncate_path_short_path() {
        let path = PathBuf::from("/home/user/project");
        let result = truncate_path(&path, 50);

        assert!(!result.truncated);
        assert_eq!(result.suffix, "/home/user/project");
        assert!(result.prefix.is_empty());
    }

    #[test]
    fn test_truncate_path_long_path() {
        let path = PathBuf::from(
            "/private/var/folders/p6/nlmq3k8146990kpkxl73mq340000gn/T/.tmpNYt4Fc/project_root",
        );
        let result = truncate_path(&path, 40);

        assert!(result.truncated, "Path should be truncated");
        assert_eq!(result.prefix, "/private");
        assert!(
            result.suffix.contains("project_root"),
            "Suffix should contain project_root"
        );
    }

    #[test]
    fn test_truncate_path_preserves_last_components() {
        let path = PathBuf::from("/a/b/c/d/e/f/g/h/i/j/project/src");
        let result = truncate_path(&path, 30);

        assert!(result.truncated);
        // Should preserve the last components that fit
        assert!(
            result.suffix.contains("src"),
            "Should preserve last component 'src', got: {}",
            result.suffix
        );
    }

    #[test]
    fn test_truncate_path_display_len() {
        let path = PathBuf::from("/private/var/folders/deep/nested/path/here");
        let result = truncate_path(&path, 30);

        // The display length should not exceed max_len (approximately)
        let display = result.to_string_plain();
        assert!(
            display.len() <= 35, // Allow some slack for trailing slash
            "Display should be truncated to around 30 chars, got {} chars: {}",
            display.len(),
            display
        );
    }

    #[test]
    fn test_truncate_path_root_only() {
        let path = PathBuf::from("/");
        let result = truncate_path(&path, 50);

        assert!(!result.truncated);
        assert_eq!(result.suffix, "/");
    }

    #[test]
    fn test_truncate_path_multibyte_single_component_does_not_panic() {
        // Routes into the "truncate the end" branch (line 414): the prefix
        // alone exceeds max_len, so available_for_suffix becomes 0. Before
        // the fix, byte-slicing `path_str` at `max_len - 3 = 2` lands
        // inside the 3-byte UTF-8 sequence for `ユ` and panicked the
        // editor — same class as #1718.
        let path = PathBuf::from("/ユーザーのプロジェクト名前/file");
        let result = truncate_path(&path, 5);
        let display = result.to_string_plain();
        assert!(display.is_char_boundary(display.len()));
        assert!(display.ends_with("..."));
    }

    #[test]
    fn test_truncate_path_multibyte_last_component_does_not_panic() {
        // Routes into the "truncate the last component" branch (line 453):
        // available_for_suffix is large enough to enter the suffix-build
        // loop, but the only remaining component doesn't fit, so we fall
        // back to truncating it. Before the fix, byte-slicing the
        // non-ASCII component at `truncate_to = 1` lands inside the 3-byte
        // UTF-8 sequence for `ユ` and panicked.
        let path = PathBuf::from("/a/ユーザーのプロジェクト名前");
        let result = truncate_path(&path, 13);
        let display = result.to_string_plain();
        assert!(display.is_char_boundary(display.len()));
    }

    #[test]
    fn test_truncated_path_to_string_plain() {
        let truncated = TruncatedPath {
            prefix: "/home".to_string(),
            truncated: true,
            suffix: "/project/src".to_string(),
            sep: '/',
        };

        assert_eq!(truncated.to_string_plain(), "/home/[...]/project/src");
    }

    #[test]
    fn test_truncated_path_to_string_plain_no_truncation() {
        let truncated = TruncatedPath {
            prefix: String::new(),
            truncated: false,
            suffix: "/home/user/project".to_string(),
            sep: '/',
        };

        assert_eq!(truncated.to_string_plain(), "/home/user/project");
    }

    /// A Windows-style "\"-path must middle-truncate (keeping drive +
    /// first dir and the tail) and render with backslashes — not fall into
    /// the crude end-truncation that `split('/')` forced because a
    /// backslash path has no '/' to split on. (We can exercise this on any
    /// OS because `truncate_path` works on the path *string*.)
    #[test]
    fn test_truncate_path_windows_backslashes() {
        let path = Path::new(r"C:\Users\me\projects\fresh\crates\editor\src\main.rs");
        let t = truncate_path(path, 34);
        assert!(t.truncated, "long backslash path should middle-truncate");
        assert_eq!(t.sep, '\\', "should re-join with backslashes");
        let shown = t.to_string_plain();
        assert!(
            shown.starts_with(r"C:\Users"),
            "keeps drive + first dir: {shown}"
        );
        assert!(
            shown.contains(r"\[...]\"),
            "uses a backslash ellipsis: {shown}"
        );
        assert!(shown.ends_with("main.rs"), "keeps the tail: {shown}");
        assert!(!shown.contains('/'), "no forward slashes leak in: {shown}");
        assert!(shown.len() <= 34, "respects max_len: {shown}");
    }

    /// A short backslash path that fits is returned unchanged.
    #[test]
    fn test_truncate_path_windows_short_unchanged() {
        let path = Path::new(r"C:\a\b");
        let t = truncate_path(path, 80);
        assert!(!t.truncated);
        assert_eq!(t.to_string_plain(), r"C:\a\b");
    }

    #[test]
    fn test_remote_indicator_element_kind_equality() {
        // Each lifecycle state produces a distinct ElementKind so the styler
        // can pick the right palette for Local / Connecting / Connected /
        // FailedAttach / Disconnected.
        assert_eq!(
            ElementKind::RemoteIndicator(RemoteIndicatorState::Local),
            ElementKind::RemoteIndicator(RemoteIndicatorState::Local)
        );
        let distinct = [
            RemoteIndicatorState::Local,
            RemoteIndicatorState::Connecting,
            RemoteIndicatorState::Connected,
            RemoteIndicatorState::FailedAttach,
            RemoteIndicatorState::Disconnected,
        ];
        for (i, a) in distinct.iter().enumerate() {
            for (j, b) in distinct.iter().enumerate() {
                if i == j {
                    continue;
                }
                assert_ne!(
                    ElementKind::RemoteIndicator(*a),
                    ElementKind::RemoteIndicator(*b),
                    "expected {:?} != {:?}",
                    a,
                    b
                );
            }
        }
    }

    #[test]
    fn test_remote_indicator_state_default_is_local() {
        // `Default` → `Local` is relied on by callers that construct the
        // indicator before a connection is known.
        assert_eq!(RemoteIndicatorState::default(), RemoteIndicatorState::Local);
    }

    #[test]
    fn test_remote_indicator_override_deserializes_kind_tags() {
        // Pins the wire shape the `SetRemoteIndicatorState` plugin op
        // accepts. A breaking change here would silently reject plugin
        // payloads after upgrade.
        let cases: &[(&str, RemoteIndicatorOverride)] = &[
            (r#"{"kind":"local"}"#, RemoteIndicatorOverride::Local),
            (
                r#"{"kind":"connecting","label":"Building"}"#,
                RemoteIndicatorOverride::Connecting {
                    label: Some("Building".into()),
                },
            ),
            (
                r#"{"kind":"connecting"}"#,
                RemoteIndicatorOverride::Connecting { label: None },
            ),
            (
                r#"{"kind":"connected","label":"Container:abc"}"#,
                RemoteIndicatorOverride::Connected {
                    label: Some("Container:abc".into()),
                },
            ),
            (
                r#"{"kind":"failed_attach","error":"exit 1"}"#,
                RemoteIndicatorOverride::FailedAttach {
                    error: Some("exit 1".into()),
                },
            ),
            (
                r#"{"kind":"disconnected","label":"Container:abc"}"#,
                RemoteIndicatorOverride::Disconnected {
                    label: Some("Container:abc".into()),
                },
            ),
        ];
        for (json, expected) in cases {
            let parsed: RemoteIndicatorOverride = serde_json::from_str(json)
                .unwrap_or_else(|e| panic!("failed to parse {}: {}", json, e));
            assert_eq!(&parsed, expected, "wire shape mismatch for {}", json);
        }
    }

    #[test]
    fn test_remote_indicator_override_labels() {
        // Labels surface in the `{remote}` element text directly, so
        // defaults matter — a missing `label` must still produce
        // something readable.
        let connecting = RemoteIndicatorOverride::Connecting { label: None };
        assert!(
            connecting.label().contains("Connecting"),
            "connecting default label should mention Connecting, got {:?}",
            connecting.label()
        );

        let connecting_labeled = RemoteIndicatorOverride::Connecting {
            label: Some("Building".into()),
        };
        assert!(
            connecting_labeled.label().contains("Building"),
            "labeled connecting should include the label, got {:?}",
            connecting_labeled.label()
        );

        let failed_bare = RemoteIndicatorOverride::FailedAttach { error: None };
        assert_eq!(failed_bare.label(), "Attach failed");

        let failed_detail = RemoteIndicatorOverride::FailedAttach {
            error: Some("exit 1".into()),
        };
        assert!(
            failed_detail.label().contains("exit 1"),
            "failed with error should include the error, got {:?}",
            failed_detail.label()
        );
    }

    #[test]
    fn test_palette_and_lsp_on_use_dedicated_theme_keys() {
        // Repro for issue #1711: the Palette hint and the "LSP on"
        // indicator used distinct palettes (help-indicator and
        // diagnostic-info), causing the status bar's color band to
        // break at the far right.
        //
        // Now they're driven by dedicated theme keys whose defaults
        // resolve to the status-bar palette, so the bar reads as a
        // single continuous color out of the box, while still letting
        // themes override these elements independently. Off / Error
        // LSP states keep their vivid diagnostic palette so real
        // problems still pop.
        let theme = crate::view::theme::Theme::from_json(
            r#"{"name":"t","editor":{},"ui":{},"search":{},"diagnostic":{},"syntax":{}}"#,
        )
        .expect("minimal theme should parse");

        // Defaults: dedicated keys resolve to the status-bar palette.
        assert_eq!(theme.status_palette_fg, theme.status_bar_fg);
        assert_eq!(theme.status_palette_bg, theme.status_bar_bg);
        assert_eq!(theme.status_lsp_on_fg, theme.status_bar_fg);
        assert_eq!(theme.status_lsp_on_bg, theme.status_bar_bg);

        let palette_style = StatusBarRenderer::element_style(
            ElementKind::Palette,
            &theme,
            StatusBarHover::None,
            WarningLevel::None,
            LspIndicatorState::None,
        );
        assert_eq!(palette_style.fg, Some(theme.status_palette_fg));
        assert_eq!(palette_style.bg, Some(theme.status_palette_bg));

        let lsp_on_style = StatusBarRenderer::element_style(
            ElementKind::Lsp,
            &theme,
            StatusBarHover::None,
            WarningLevel::None,
            LspIndicatorState::On,
        );
        assert_eq!(lsp_on_style.fg, Some(theme.status_lsp_on_fg));
        assert_eq!(lsp_on_style.bg, Some(theme.status_lsp_on_bg));

        // Sanity: Off / Error must still differ from the status-bar
        // palette so they remain user-visible signals.
        let lsp_off_style = StatusBarRenderer::element_style(
            ElementKind::Lsp,
            &theme,
            StatusBarHover::None,
            WarningLevel::None,
            LspIndicatorState::Off,
        );
        assert_eq!(lsp_off_style.fg, Some(theme.status_lsp_actionable_fg));
        assert_eq!(lsp_off_style.bg, Some(theme.status_lsp_actionable_bg));

        let lsp_error_style = StatusBarRenderer::element_style(
            ElementKind::Lsp,
            &theme,
            StatusBarHover::None,
            WarningLevel::None,
            LspIndicatorState::Error,
        );
        assert_eq!(lsp_error_style.fg, Some(theme.diagnostic_error_fg));
        assert_eq!(lsp_error_style.bg, Some(theme.diagnostic_error_bg));
    }

    #[test]
    fn test_status_palette_and_lsp_on_keys_override_independently() {
        // A theme that only sets the new keys should produce styles
        // that follow the override, not the underlying status_bar_*
        // colors. This is the entire point of introducing dedicated
        // keys: themes can repaint these specific indicators without
        // touching the rest of the status bar.
        let theme_json = r#"{
            "name":"t",
            "editor":{},
            "ui":{
                "status_bar_fg":"White",
                "status_bar_bg":"DarkGray",
                "status_palette_fg":"Black",
                "status_palette_bg":"Yellow",
                "status_lsp_on_fg":"Black",
                "status_lsp_on_bg":"Cyan"
            },
            "search":{},
            "diagnostic":{},
            "syntax":{}
        }"#;
        let theme = crate::view::theme::Theme::from_json(theme_json).expect("theme should parse");
        assert_ne!(theme.status_palette_fg, theme.status_bar_fg);
        assert_ne!(theme.status_palette_bg, theme.status_bar_bg);
        assert_ne!(theme.status_lsp_on_fg, theme.status_bar_fg);
        assert_ne!(theme.status_lsp_on_bg, theme.status_bar_bg);
    }

    #[test]
    fn test_status_separator_keys_default_and_override() {
        // The separator glyph is painted by dedicated theme keys so it can
        // be dimmed against the bar. By default both resolve to the
        // status-bar palette, keeping the bar a single continuous color.
        let theme = crate::view::theme::Theme::from_json(
            r#"{"name":"t","editor":{},"ui":{},"search":{},"diagnostic":{},"syntax":{}}"#,
        )
        .expect("minimal theme should parse");
        assert_eq!(theme.status_separator_fg, theme.status_bar_fg);
        assert_eq!(theme.status_separator_bg, theme.status_bar_bg);

        // A theme that sets only the separator keys repaints the glyph
        // without touching the rest of the bar.
        let theme = crate::view::theme::Theme::from_json(
            r#"{
                "name":"t",
                "editor":{},
                "ui":{
                    "status_bar_fg":"White",
                    "status_bar_bg":"DarkGray",
                    "status_separator_fg":"Gray",
                    "status_separator_bg":"Black"
                },
                "search":{},
                "diagnostic":{},
                "syntax":{}
            }"#,
        )
        .expect("theme should parse");
        assert_ne!(theme.status_separator_fg, theme.status_bar_fg);
        assert_ne!(theme.status_separator_bg, theme.status_bar_bg);
    }

    #[test]
    fn test_remote_indicator_override_state_projection() {
        assert_eq!(
            RemoteIndicatorOverride::Local.state(),
            RemoteIndicatorState::Local
        );
        assert_eq!(
            RemoteIndicatorOverride::Connecting { label: None }.state(),
            RemoteIndicatorState::Connecting
        );
        assert_eq!(
            RemoteIndicatorOverride::Connected { label: None }.state(),
            RemoteIndicatorState::Connected
        );
        assert_eq!(
            RemoteIndicatorOverride::FailedAttach { error: None }.state(),
            RemoteIndicatorState::FailedAttach
        );
        assert_eq!(
            RemoteIndicatorOverride::Disconnected { label: None }.state(),
            RemoteIndicatorState::Disconnected
        );
    }

    // Regression coverage for issue #1967 — the cursor indicator must keep
    // a stable rendered width as the cursor moves so the bar doesn't
    // shift. The helpers reserve the digit count of the buffer's total
    // line count for the line number and `CURSOR_COL_RESERVE` for the
    // column number, suffix-padding the text without altering the
    // numbers themselves so existing screen assertions still see
    // literals like "Ln 1, Col 1".

    #[test]
    fn test_cursor_position_widths_stable_across_cursor_movement() {
        let line_count = 50;
        // Movement across a 50-line file (two-digit line_count) should
        // produce a constant rendered width regardless of cursor position.
        let widths: Vec<usize> = [(1, 1), (5, 12), (12, 5), (50, 100), (1, 1)]
            .into_iter()
            .map(|(ln, col)| format_cursor_position(ln, col, line_count).len())
            .collect();
        assert!(
            widths.windows(2).all(|w| w[0] == w[1]),
            "rendered widths drift across cursor movements: {widths:?}"
        );
    }

    #[test]
    fn test_cursor_position_preserves_natural_number_text() {
        // The natural "Ln 1, Col 1" substring must remain intact so
        // existing screen-content assertions (and screen readers) keep
        // working. Padding is suffix-only.
        let text = format_cursor_position(1, 1, 50);
        assert!(
            text.starts_with("Ln 1, Col 1"),
            "expected text to start with natural numbers, got {text:?}"
        );
        assert!(
            text.ends_with(' '),
            "expected trailing padding, got {text:?}"
        );
    }

    #[test]
    fn test_cursor_position_no_padding_for_single_line_buffer() {
        // For a single-line buffer the reserved line-digit width is 1,
        // so a small column number still produces the canonical
        // "Ln 1, Col 1" with reserve-only trailing padding.
        let text = format_cursor_position(1, 1, 1);
        // Min width = "Ln , Col ".len()(=9) + 1 (line_digits) + 3 (col reserve) = 13
        assert_eq!(text.len(), 13);
        assert!(text.starts_with("Ln 1, Col 1"));
    }

    #[test]
    fn test_cursor_position_does_not_shrink_below_actual() {
        // When the actual numbers exceed the reserve, the rendered text
        // is returned unmodified (rare wide-line case).
        let text = format_cursor_position(99, 99999, 50);
        assert_eq!(text, "Ln 99, Col 99999");
    }

    #[test]
    fn test_cursor_position_compact_widths_stable() {
        let line_count = 50;
        let widths: Vec<usize> = [(1, 1), (5, 12), (12, 5), (50, 100), (1, 1)]
            .into_iter()
            .map(|(ln, col)| format_cursor_position_compact(ln, col, line_count).len())
            .collect();
        assert!(
            widths.windows(2).all(|w| w[0] == w[1]),
            "compact widths drift across cursor movements: {widths:?}"
        );
    }

    #[test]
    fn test_cursor_position_compact_preserves_natural_text() {
        let text = format_cursor_position_compact(1, 1, 50);
        assert!(
            text.starts_with("1:1"),
            "expected text to start with natural numbers, got {text:?}"
        );
    }

    #[test]
    fn test_cursor_position_scales_with_line_count() {
        // Larger buffers reserve more line-digit width so that line
        // numbers at the high end of the buffer don't widen the bar.
        let short = format_cursor_position(1, 1, 9);
        let long = format_cursor_position(1, 1, 10_000);
        assert!(
            long.len() > short.len(),
            "wider buffers should reserve more width: {short:?} vs {long:?}"
        );
        // And the wide-buffer rendering should match what a top-of-file
        // line number near the buffer's high end would render to.
        let top = format_cursor_position(1, 1, 10_000);
        let high = format_cursor_position(9_999, 999, 10_000);
        assert_eq!(top.len(), high.len());
    }

    #[test]
    fn test_cursor_column_counts_chars_not_bytes() {
        let mut buf =
            crate::model::buffer::TextBuffer::from_str_test("hello\ncafé résumé\nworld\n");
        let line_start = buf.line_start_offset(1).unwrap();

        // 'r' starts at byte 6 ("café " = 5 chars / 6 bytes), char column 5.
        let col = cursor_column(&mut buf, line_start + 6);
        assert_eq!(
            col, 5,
            "cursor at 'r' should be column 5, not byte offset 6"
        );

        // 'é' starts at byte 3 (after "caf"), column 3.
        let col = cursor_column(&mut buf, line_start + 3);
        assert_eq!(col, 3, "cursor at 'é' should be column 3");

        // 'u' in "résumé" sits at byte 10, column 8.
        let col = cursor_column(&mut buf, line_start + 10);
        assert_eq!(col, 8, "cursor at 'u' should be column 8");
    }

    #[test]
    fn test_cursor_column_counts_grapheme_clusters() {
        // Line 1 is "e + combining acute" followed by 'x'. The accented 'e' is
        // two code points but one grapheme; counting graphemes (not chars or
        // bytes) keeps the column aligned with grapheme-based cursor movement.
        let mut buf = crate::model::buffer::TextBuffer::from_str_test("ab\ne\u{0301}x\n");
        let line_start = buf.line_start_offset(1).unwrap();

        // 'x' sits after the 1-byte 'e' and 2-byte combining accent (byte 3),
        // which is char column 2 but grapheme column 1.
        let col = cursor_column(&mut buf, line_start + 3);
        assert_eq!(
            col, 1,
            "accented 'e' is one grapheme; 'x' should be column 1, not 2"
        );
    }

    #[test]
    fn test_cursor_column_zwj_emoji_is_one_grapheme() {
        // Family emoji is several code points joined by ZWJ but a single
        // grapheme cluster (18 bytes).
        let mut buf = crate::model::buffer::TextBuffer::from_str_test("👨\u{200D}👩\u{200D}👧z\n");
        let line_start = buf.line_start_offset(0).unwrap();

        let col = cursor_column(&mut buf, line_start + 18);
        assert_eq!(col, 1, "ZWJ family emoji should count as one column");
    }

    #[test]
    fn test_cursor_column_at_line_start_is_zero() {
        let mut buf = crate::model::buffer::TextBuffer::from_str_test("hello\nworld\n");
        let line_start = buf.line_start_offset(1).unwrap();
        assert_eq!(cursor_column(&mut buf, line_start), 0);
    }
}