gitwig 2.3.5

a rust based tui, an alternative to sourcetree and gitui
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
//! Rendering for the main list + status bar + help overlay.
//!
//! All drawing reads from `&App`; nothing here mutates state. Adding a new
//! keybinding means updating the help line list helpers (under `src/popups/help.rs`
//! or `src/popups/detail_help.rs`) AND the status bar text (under `src/components/cmd_bar.rs`),
//! so the help overlay and the bottom bar stay aligned.
//!
//! Visual conventions live in the `theme` block at the top — keep visual
//! choices centralized so the look stays coherent.

use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Position, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Padding, Paragraph};

use crate::app::{App, Mode};
use crate::components::cmd_bar::StatusEntry;
use crate::config::SortOrder;
use crate::repo::{ItemStatus, RepoState, RepoSummary, format_relative_time};

// ── Theme ──────────────────────────────────────────────────────────────────
// Colors are kept minimal so the app works on both dark and light terminal
// backgrounds. Plain text is left at the terminal's default foreground —
// never hard-code `White` / `Gray` / `DarkGray` for text, because what reads
// as "muted" on one background reads as invisible on the other. Use the
// helpers below: `muted_style()` for de-emphasis, bold for emphasis, and the
// accent colors only for true accents (selection, mode, warnings).

pub use super::style::*;

/// Width of the per-item status zone on the right of each card. Wide
/// enough to fit a busy repo's worth of indicators ("● 99+ 99! 99? 99↑")
/// with a little breathing room. Right-aligned, so it crops on the left
/// for the unusual case of 3-digit counts on every indicator.
const STATUS_ZONE_WIDTH: u16 = 22;

const UNSELECTED_INDENT: &str = "  ";

/// Top-level draw entry point invoked from inside `terminal.draw`.
pub fn draw(
    f: &mut Frame,
    app: &App,
    area: Rect,
    inner_area: Rect,
    visible_count: usize,
    detail_areas: &mut crate::ui_detail::DetailAreas,
    main_areas: &mut Vec<Rect>,
) {
    let mut swapped_theme = None;
    if let Some(repo_path) = app.get_selected_item() {
        if matches!(
            app.mode,
            Mode::Detail
                | Mode::DetailHelp
                | Mode::CommitInput
                | Mode::BranchCreateInput
                | Mode::TagCreateInput
                | Mode::StashingUI
                | Mode::WorktreeAddBranchInput
                | Mode::WorktreeAddPathInput
                | Mode::WorktreeLockReasonInput
                | Mode::WorktreeRemoveConfirm
                | Mode::BranchDeleteConfirm
                | Mode::BranchCheckoutConfirm
                | Mode::SubmoduleAddUrlInput
                | Mode::SubmoduleAddPathInput
                | Mode::SubmoduleDeleteConfirm
                | Mode::TagCheckoutConfirm
                | Mode::BranchPushConfirm
                | Mode::BranchMergeConfirm
                | Mode::BranchRebaseConfirm
                | Mode::BranchInteractiveRebaseConfirm
                | Mode::TagDeleteConfirm
                | Mode::TagPushConfirm
                | Mode::TagPushAllConfirm
                | Mode::StashDeleteConfirm
                | Mode::StashApplyConfirm
                | Mode::CherryPickConfirm
                | Mode::RevertConfirm
                | Mode::MergeAbortConfirm
                | Mode::MergeContinueConfirm
                | Mode::StashCreateInput
                | Mode::RemotePicker
                | Mode::CommitSearchInput
                | Mode::DiscardChangesConfirm
                | Mode::Inspect
                | Mode::SearchColumnPicker
                | Mode::Logs
                | Mode::LogsSearchInput
                | Mode::RemoteAddNameInput
                | Mode::RemoteAddUrlInput
                | Mode::RemoteDeleteConfirm
                | Mode::RepoSettings
                | Mode::Overview
        ) {
            if let Some(repo_theme) = app.repo_theme_cache.get(repo_path) {
                // Save current theme state
                let current_theme_config = {
                    let lock =
                        crate::ui::style::THEME.read().expect("theme lock should be acquired");
                    crate::config::ThemeConfig {
                        accent: crate::ui::style::format_color(lock.accent),
                        warning: crate::ui::style::format_color(lock.warning),
                        danger: crate::ui::style::format_color(lock.danger),
                        success: crate::ui::style::format_color(lock.success),
                        border_type: crate::ui::style::format_border_type(lock.border_type),
                    }
                };

                // Swap with repo-specific theme
                crate::ui::update_theme(repo_theme);
                swapped_theme = Some(current_theme_config);
            }
        }
    }

    draw_outer_frame(f, area, app);

    // Always reserve the bottom row for the status bar, regardless of mode.
    let (content_area, status_chunk) = content_and_status_chunks(inner_area, app.status_height());

    if app.loading_repo_path.is_some() {
        crate::popups::loading::draw_loading_screen(f, content_area, app);
    } else if matches!(
        app.mode,
        Mode::Detail
            | Mode::DetailHelp
            | Mode::CommitInput
            | Mode::BranchCreateInput
            | Mode::TagCreateInput
            | Mode::StashingUI
            | Mode::WorktreeAddBranchInput
            | Mode::WorktreeAddPathInput
            | Mode::WorktreeLockReasonInput
            | Mode::WorktreeRemoveConfirm
            | Mode::BranchDeleteConfirm
            | Mode::BranchCheckoutConfirm
            | Mode::SubmoduleAddUrlInput
            | Mode::SubmoduleAddPathInput
            | Mode::SubmoduleDeleteConfirm
            | Mode::TagCheckoutConfirm
            | Mode::BranchPushConfirm
            | Mode::BranchMergeConfirm
            | Mode::BranchRebaseConfirm
            | Mode::BranchInteractiveRebaseConfirm
            | Mode::TagDeleteConfirm
            | Mode::TagPushConfirm
            | Mode::TagPushAllConfirm
            | Mode::StashDeleteConfirm
            | Mode::StashApplyConfirm
            | Mode::CherryPickConfirm
            | Mode::RevertConfirm
            | Mode::MergeAbortConfirm
            | Mode::MergeContinueConfirm
            | Mode::StashCreateInput
            | Mode::RemotePicker
            | Mode::CommitSearchInput
            | Mode::DiscardChangesConfirm
            | Mode::Inspect
            | Mode::SearchColumnPicker
            | Mode::Logs
            | Mode::LogsSearchInput
            | Mode::RemoteAddNameInput
            | Mode::RemoteAddUrlInput
            | Mode::RemoteDeleteConfirm
            | Mode::RepoSettings
            | Mode::Overview
    ) || (app.mode == Mode::UpdateConfirm && app.current_detail.is_some())
    {
        if let Some(detail) = &app.current_detail {
            let item_name = app.get_selected_item().map(String::as_str).unwrap_or("");
            crate::ui_detail::draw(
                f,
                item_name,
                detail,
                &app.mode,
                &app.detail_focus,
                app.last_staging_focus,
                app.commit_list.selection,
                &app.commit_list.search_query,
                app.status_list.file_selection,
                &app.diff.file_diff,
                app.diff.diff_scroll,
                app.status_list.staging_file_selection,
                app.commit_list.details_scroll,
                app.branch_list.local_branch_selection,
                app.branch_list.remote_branch_selection,
                app.tag_list.local_tag_selection,
                app.branch_list.remote_selection,
                app.remote_picker_selection,
                app.stash_list.stash_selection,
                app.stash_list.stash_file_selection,
                app.file_tree.file_list_selection,
                app.file_tree.file_content_scroll,
                &app.file_tree.visible_files,
                app.detail_tab,
                app.graph_scroll,
                app.help_scroll,
                detail_areas,
                &app.input_buffer,
                app.commit_popup.editing,
                &app.branch_action_target,
                &app.tag_action_target_oid,
                &app.tag_delete_target,
                &app.tag_push_target,
                &app.discard_target,
                app.stash_apply_delete_after,
                app.commit_popup.amend,
                app.commit_input_scroll,
                app.inspect_horizontal_split_pct,
                app.inspect_vertical_split_pct,
                app.workspace_main_split_pct,
                app.files_horizontal_split_pct,
                app.branches_horizontal_split_pct,
                app.stashes_horizontal_split_pct,
                app.stashes_vertical_split_pct,
                app.overview_horizontal_split_pct,
                app,
                content_area,
            );
        }
    } else if app.mode == Mode::FileHistory {
        crate::tabs::FileHistoryTab::draw_file_history(f, app, content_area);
    } else if app.mode == Mode::Settings {
        crate::popups::settings::draw_settings_page(f, app, content_area);
    } else if app.mode == Mode::DebugLogs {
        crate::popups::debug::draw_debug_logs(f, app, content_area);
    } else if app.config.items.is_empty() {
        draw_empty_state(f, content_area);
    } else if app.get_items_len() == 0 {
        if let Some(ref query) = app.repo_search_query {
            draw_search_empty_state(f, content_area, query);
        } else {
            draw_empty_state(f, content_area);
        }
    } else {
        let layout_parts = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(1), // Global summary bar
                Constraint::Length(1), // Spacer
                Constraint::Min(0),    // Rest of content
            ])
            .split(content_area);

        draw_global_summary_bar(f, layout_parts[0], app);
        let list_area_parent = layout_parts[2];

        let (header_area, list_area) = if app.config.compact_view {
            let parts = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(1), Constraint::Min(0)])
                .split(list_area_parent);
            (Some(parts[0]), parts[1])
        } else {
            (None, list_area_parent)
        };

        if let Some(hdr) = header_area {
            draw_compact_headers(f, hdr, app);
        }

        let list_chunks = item_chunks(list_area, visible_count, app);
        *main_areas = list_chunks.clone();
        draw_items(f, app, &list_chunks);
    }

    crate::components::cmd_bar::draw_status_bar(f, app, status_chunk);

    if matches!(app.mode, Mode::Help) {
        crate::popups::help::draw_help_overlay(f, app, area, app.help_scroll);
    }

    if matches!(app.mode, Mode::About) {
        crate::popups::about::draw_about_popup(f, area, app);
    }

    if matches!(app.mode, Mode::Legend) {
        crate::popups::legend::draw_legend_popup(f, area, app);
    }

    if matches!(app.mode, Mode::RepoSettings) {
        crate::popups::repo_settings::RepoSettingsPopup::draw(f, app, area);
    }

    if matches!(app.mode, Mode::RepoJump) {
        draw_repo_jump_popup(f, app, area);
    }

    if matches!(app.mode, Mode::ImportUrlInput | Mode::ImportDestInput | Mode::ImportNameInput) {
        crate::popups::import::draw_import_popup(f, area, app);
    }

    if let Some(ref err) = app.error_message {
        crate::popups::error::draw_error_popup(f, app, area, err);
    } else if app.fetching {
        crate::popups::loading::draw_progress_popup(f, area, app);
    }

    if let Some(original_theme) = swapped_theme {
        crate::ui::update_theme(&original_theme);
    }
}

fn draw_outer_frame(f: &mut Frame, area: Rect, app: &App) {
    let show_sort = matches!(
        app.mode,
        Mode::Normal
            | Mode::Adding
            | Mode::Editing
            | Mode::ConfirmDelete
            | Mode::Help
            | Mode::About
            | Mode::Legend
            | Mode::BulkAddInput
    );

    let mut block = Block::default()
        .borders(Borders::ALL)
        .border_type(CARD_BORDER())
        .border_style(muted_style())
        .title(
            Line::from(vec![
                Span::raw(" "),
                Span::styled("Gitwig", accent_style()),
                Span::raw(" "),
            ])
            .alignment(Alignment::Left),
        );

    if show_sort {
        let sort_label = match app.config.sort_by {
            SortOrder::Custom => "Sort: Custom",
            SortOrder::Alphabetical => "Sort: Alphabetical",
            SortOrder::RecentVisit => "Sort: Recent Visit",
            SortOrder::LatestChanges => "Sort: Latest Changes",
        };
        let sort_label_with_dir = if app.config.sort_reverse {
            format!("{} (Rev)", sort_label)
        } else {
            sort_label.to_string()
        };

        block = block.title(
            Line::from(vec![
                Span::raw(" "),
                Span::styled(sort_label_with_dir, accent_style()),
                Span::raw(" "),
            ])
            .alignment(Alignment::Center),
        );
    }

    let mut right_spans =
        vec![Span::styled(format!(" v{} ", env!("CARGO_PKG_VERSION")), muted_style())];
    if let Some(ref latest) = app.update_available {
        right_spans.insert(0, Span::raw(" "));
        right_spans.insert(
            0,
            Span::styled(
                format!("[Update to v{}]", latest),
                Style::default().fg(ratatui::style::Color::LightGreen).add_modifier(Modifier::BOLD),
            ),
        );
    }
    if app.implicit_network_count > 0 {
        let spinner_chars = ["", "", "", "", "", "", "", "", "", ""];
        let idx = ((app.fetch_progress / 10) % 10) as usize;
        right_spans.insert(0, Span::raw(" "));
        right_spans.insert(
            0,
            Span::styled(
                spinner_chars[idx],
                Style::default().fg(ratatui::style::Color::Cyan).add_modifier(Modifier::BOLD),
            ),
        );
    }
    block = block.title(Line::from(right_spans).alignment(Alignment::Right));
    f.render_widget(block, area);
}

/// Reserve the bottom row for the status bar. The remainder is the
/// "content area" — list view, detail view, or anything else a mode
/// wants to draw.
fn content_and_status_chunks(inner_area: Rect, status_height: u16) -> (Rect, Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(0), Constraint::Length(status_height)])
        .split(inner_area);
    (chunks[0], chunks[1])
}

/// Within the content area, split into N item rows + a flex spacer so the
/// list is top-aligned and never pushes against the status bar.
fn item_chunks(content_area: Rect, visible_count: usize, app: &App) -> Vec<Rect> {
    let rows = app.get_home_rows();
    let upper = (app.scroll_top + visible_count).min(rows.len());
    let visible_rows = &rows[app.scroll_top..upper];

    let mut constraints = Vec::new();
    for row in visible_rows {
        let h = match row {
            crate::app::HomeRow::GroupHeader { .. } => {
                if app.config.compact_view {
                    1
                } else {
                    2
                }
            }
            crate::app::HomeRow::Repo { .. } => {
                if app.config.compact_view {
                    1
                } else {
                    4
                }
            }
        };
        constraints.push(Constraint::Length(h));
    }
    constraints.push(Constraint::Min(0));

    let chunks: Vec<Rect> = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .split(content_area)
        .to_vec();
    chunks[..visible_count].to_vec()
}

fn draw_global_summary_bar(f: &mut Frame, area: Rect, app: &App) {
    let mut total_repos = 0;
    let mut dirty_count = 0;
    let mut ahead_count = 0;
    let mut stale_count = 0;

    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);
    let stale_threshold = 30 * 24 * 60 * 60; // 30 days

    for status in &app.statuses {
        match status {
            ItemStatus::GitRepo(Some(summary)) => {
                total_repos += 1;
                if !summary.is_clean() {
                    dirty_count += 1;
                }
                if summary.ahead > 0 {
                    ahead_count += 1;
                }
                if let Some(t) = summary.last_commit_time {
                    if now - t > stale_threshold {
                        stale_count += 1;
                    }
                }
            }
            ItemStatus::GitRepo(None) => {
                total_repos += 1;
            }
            _ => {}
        }
    }

    let dot = Span::styled("", muted_style());
    let mut spans = vec![
        Span::styled(format!(" {} ", total_repos), primary_style().fg(ACCENT())),
        Span::styled("repos", muted_style()),
    ];

    spans.push(dot.clone());
    let dirty_color = if dirty_count > 0 { WARNING() } else { SUCCESS() };
    spans.push(Span::styled(format!(" {} ", dirty_count), primary_style().fg(dirty_color)));
    spans.push(Span::styled("dirty", muted_style()));

    spans.push(dot.clone());
    spans.push(Span::styled(
        format!(" {} ", ahead_count),
        primary_style().fg(if ahead_count > 0 { ACCENT() } else { ratatui::style::Color::Gray }),
    ));
    spans.push(Span::styled("ahead", muted_style()));

    spans.push(dot);
    let stale_color = if stale_count > 0 { DANGER() } else { SUCCESS() };
    spans.push(Span::styled(format!(" {} ", stale_count), primary_style().fg(stale_color)));
    spans.push(Span::styled("stale", muted_style()));

    let line = Line::from(spans).alignment(Alignment::Center);
    f.render_widget(Paragraph::new(line), area);
}

fn draw_compact_headers(f: &mut Frame, area: Rect, _app: &App) {
    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Min(0),
            Constraint::Length(28),
            Constraint::Length(STATUS_ZONE_WIDTH),
        ])
        .split(area);

    let header_style = muted_style().add_modifier(Modifier::BOLD);

    let col_repo = Line::from(vec![Span::raw("   "), Span::styled("REPOSITORY", header_style)]);
    let col_branch = Line::from(Span::styled("ACTIVE BRANCH", header_style));
    let col_status = Line::from(Span::styled("STATUS", header_style)).alignment(Alignment::Right);

    f.render_widget(Paragraph::new(col_repo), cols[0]);
    f.render_widget(Paragraph::new(col_branch), cols[1]);
    f.render_widget(Paragraph::new(col_status), cols[2]);
}

fn draw_items(f: &mut Frame, app: &App, chunks: &[Rect]) {
    let rows = app.get_home_rows();
    let upper = (app.scroll_top + chunks.len()).min(rows.len());
    let visible_rows = &rows[app.scroll_top..upper];

    for (i, row) in visible_rows.iter().enumerate() {
        let display_index = i + app.scroll_top;
        let is_selected = display_index == app.selected_index;

        match row {
            crate::app::HomeRow::GroupHeader { name, count, collapsed } => {
                let arrow = if *collapsed { "" } else { "" };
                let header_text = format!("{} [{}] ({} repos)", arrow, name, count);
                let style = if is_selected {
                    Style::default().fg(ACCENT()).add_modifier(Modifier::BOLD)
                } else {
                    muted_style().add_modifier(Modifier::BOLD)
                };
                let mut block = Block::default();
                if !app.config.compact_view {
                    let border_style =
                        if is_selected { Style::default().fg(ACCENT()) } else { muted_style() };
                    block = block.borders(Borders::BOTTOM).border_style(border_style);
                }

                let p = Paragraph::new(Line::from(vec![
                    Span::styled(
                        if is_selected { app.sym("selection_mark") } else { UNSELECTED_INDENT },
                        if is_selected { Style::default().fg(ACCENT()) } else { Style::default() },
                    ),
                    Span::styled(header_text, style),
                ]))
                .block(block);
                f.render_widget(p, chunks[i]);
            }
            crate::app::HomeRow::Repo { actual_index, path: item, .. } => {
                let actual_index = *actual_index;
                let fallback = ItemStatus::Missing;
                let status = app.statuses.get(actual_index).unwrap_or(&fallback);
                let is_git = matches!(status, ItemStatus::GitRepo(_));

                let mut is_partial = false;
                if let ItemStatus::GitRepo(Some(summary)) = status {
                    if summary.staged > 0 && (summary.modified > 0 || summary.untracked > 0) {
                        is_partial = true;
                    }
                }

                let pending_delete = is_selected && matches!(app.mode, Mode::ConfirmDelete);
                let pending_edit = is_selected && matches!(app.mode, Mode::Editing);
                let is_pinned = app.config.pinned.contains(item);
                let is_starred = app.config.starred.contains(item);

                // Selected/pending cards use an accent color; unselected cards use
                // the terminal's default foreground (dimmed) so they stay legible
                // on both light and dark backgrounds.
                let border_style = if pending_delete {
                    Style::default().fg(DANGER())
                } else if pending_edit {
                    Style::default().fg(WARNING())
                } else if is_selected {
                    Style::default().fg(ACCENT())
                } else if is_partial || is_pinned {
                    Style::default().fg(WARNING())
                } else {
                    muted_style()
                };

                let (mark, mark_style, text_style) = if is_selected {
                    (app.sym("selection_mark"), border_style, primary_style())
                } else {
                    (UNSELECTED_INDENT, Style::default(), Style::default())
                };

                let repo_name = std::path::Path::new(item)
                    .file_name()
                    .and_then(|s| s.to_str())
                    .unwrap_or(item.as_str());

                if app.config.compact_view {
                    let cols = Layout::default()
                        .direction(Direction::Horizontal)
                        .constraints([
                            Constraint::Min(0),
                            Constraint::Length(28),
                            Constraint::Length(STATUS_ZONE_WIDTH),
                        ])
                        .split(chunks[i]);

                    // 1. Repo name and labels
                    let mut left_spans = vec![Span::styled(mark, mark_style)];
                    if is_git {
                        left_spans.push(Span::styled(
                            app.sym("git_repo"),
                            if is_selected {
                                primary_style().add_modifier(Modifier::BOLD)
                            } else {
                                muted_style().add_modifier(Modifier::BOLD)
                            },
                        ));
                    }
                    left_spans.push(Span::styled(
                        repo_name,
                        if is_selected {
                            Style::default().fg(ACCENT()).add_modifier(Modifier::BOLD)
                        } else {
                            text_style
                        },
                    ));
                    if let ItemStatus::GitRepo(Some(summary)) = status {
                        let (state_str, state_style) = match summary.state {
                            RepoState::Merge => (
                                " ⚠ MERGE_HEAD",
                                Style::default().fg(DANGER()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Rebase => (
                                " 🚧 REBASING",
                                Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::CherryPick => (
                                " ⚡ CHERRY-PICK",
                                Style::default().fg(ACCENT()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Revert => (
                                " ⚡ REVERTING",
                                Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Bisect => (
                                " 🔍 BISECTING",
                                Style::default()
                                    .fg(ratatui::style::Color::Magenta)
                                    .add_modifier(Modifier::BOLD),
                            ),
                            RepoState::ApplyMailbox => (
                                " 📬 APPLYING",
                                Style::default()
                                    .fg(ratatui::style::Color::Cyan)
                                    .add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Clean => (" ✓ CLEAN", muted_style()),
                        };
                        left_spans.push(Span::styled(
                            state_str,
                            if is_selected {
                                state_style
                                    .remove_modifier(Modifier::DIM)
                                    .add_modifier(Modifier::BOLD)
                            } else {
                                state_style
                            },
                        ));
                        if summary.staged > 0 && (summary.modified > 0 || summary.untracked > 0) {
                            left_spans.push(Span::styled(
                                " ⚠ PARTIAL",
                                Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
                            ));
                        }
                    }
                    if let Some(lbls) = app.config.labels.get(item) {
                        for lbl in lbls {
                            left_spans.push(Span::raw(" "));
                            left_spans.push(Span::styled(
                                format!("[{}]", lbl),
                                if is_selected {
                                    Style::default().fg(ACCENT()).add_modifier(Modifier::BOLD)
                                } else {
                                    Style::default().fg(ACCENT()).add_modifier(Modifier::DIM)
                                },
                            ));
                        }
                    }
                    let is_starred = app.config.starred.contains(item);
                    if is_starred {
                        left_spans.push(Span::raw(" "));
                        left_spans.push(Span::styled(
                            app.sym("star").trim(),
                            if is_selected {
                                Style::default()
                                    .fg(ratatui::style::Color::Yellow)
                                    .add_modifier(Modifier::BOLD)
                            } else {
                                Style::default().fg(ratatui::style::Color::Yellow)
                            },
                        ));
                    }
                    if is_pinned {
                        left_spans.push(Span::raw(" "));
                        left_spans.push(Span::styled(
                            app.sym("pinned").trim(),
                            if is_selected {
                                Style::default().fg(WARNING()).add_modifier(Modifier::BOLD)
                            } else {
                                Style::default().fg(WARNING())
                            },
                        ));
                    }
                    f.render_widget(Paragraph::new(Line::from(left_spans)), cols[0]);

                    // 2. Branch and relative time
                    let branch_line = match status {
                        ItemStatus::GitRepo(Some(s)) => {
                            if let Some(b) = &s.branch {
                                let mut parts = vec![
                                    Span::styled(
                                        format!("{} ", app.sym("branch")),
                                        if is_selected {
                                            primary_style().add_modifier(Modifier::BOLD)
                                        } else {
                                            muted_style()
                                        },
                                    ),
                                    Span::styled(
                                        b.clone(),
                                        if is_selected {
                                            Style::default()
                                                .fg(ACCENT())
                                                .add_modifier(Modifier::BOLD)
                                        } else {
                                            Style::default().fg(ACCENT())
                                        },
                                    ),
                                ];
                                if let Some(time) = s.last_commit_time {
                                    parts.push(Span::styled(
                                        format!(" ({})", format_relative_time(time)),
                                        if is_selected {
                                            primary_style().add_modifier(Modifier::BOLD)
                                        } else {
                                            muted_style()
                                        },
                                    ));
                                }
                                Line::from(parts)
                            } else {
                                Line::from("")
                            }
                        }
                        _ => Line::from(""),
                    };
                    f.render_widget(Paragraph::new(branch_line), cols[1]);

                    // 3. Status indicator line
                    let status_line =
                        status_indicator_line(app, status).alignment(Alignment::Right);
                    f.render_widget(Paragraph::new(status_line), cols[2]);
                } else {
                    let border_type =
                        if is_selected { BorderType::LightDoubleDashed } else { CARD_BORDER() };

                    // Render the border block; split its inner rect into two rows:
                    //   row 0 — item path (left) + status indicator (right)
                    //   row 1 — branch name (left-aligned, muted)
                    let block = Block::default()
                        .borders(Borders::ALL)
                        .border_type(border_type)
                        .border_style(border_style)
                        .padding(Padding::horizontal(1));
                    let inner = block.inner(chunks[i]);
                    f.render_widget(block, chunks[i]);

                    let rows = Layout::default()
                        .direction(Direction::Vertical)
                        .constraints([Constraint::Length(1), Constraint::Min(0)])
                        .split(inner);

                    let name_cols = Layout::default()
                        .direction(Direction::Horizontal)
                        .constraints([Constraint::Min(0), Constraint::Length(8)])
                        .split(rows[0]);

                    let mut spans = vec![Span::styled(mark, mark_style)];
                    if is_git {
                        spans.push(Span::styled(
                            app.sym("git_repo"),
                            muted_style().add_modifier(Modifier::BOLD),
                        ));
                    }
                    spans.push(Span::styled(repo_name, text_style));
                    if let ItemStatus::GitRepo(Some(summary)) = status {
                        let (state_str, state_style) = match summary.state {
                            RepoState::Merge => (
                                " ⚠ MERGE_HEAD",
                                Style::default().fg(DANGER()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Rebase => (
                                " 🚧 REBASING",
                                Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::CherryPick => (
                                " ⚡ CHERRY-PICK",
                                Style::default().fg(ACCENT()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Revert => (
                                " ⚡ REVERTING",
                                Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Bisect => (
                                " 🔍 BISECTING",
                                Style::default()
                                    .fg(ratatui::style::Color::Magenta)
                                    .add_modifier(Modifier::BOLD),
                            ),
                            RepoState::ApplyMailbox => (
                                " 📬 APPLYING",
                                Style::default()
                                    .fg(ratatui::style::Color::Cyan)
                                    .add_modifier(Modifier::BOLD),
                            ),
                            RepoState::Clean => (" ✓ CLEAN", muted_style()),
                        };
                        spans.push(Span::styled(state_str, state_style));
                        if summary.staged > 0 && (summary.modified > 0 || summary.untracked > 0) {
                            spans.push(Span::styled(
                                " ⚠ PARTIAL",
                                Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
                            ));
                        }
                    }
                    if let Some(lbls) = app.config.labels.get(item) {
                        for lbl in lbls {
                            spans.push(Span::raw(" "));
                            spans.push(Span::styled(
                                format!("[{}]", lbl),
                                Style::default().fg(ACCENT()).add_modifier(Modifier::DIM),
                            ));
                        }
                    }
                    let name_line = Line::from(spans);
                    f.render_widget(Paragraph::new(name_line), name_cols[0]);

                    let mut right_spans = Vec::new();
                    if is_starred {
                        right_spans.push(Span::styled(
                            format!("{} ", app.sym("star").trim()),
                            Style::default().fg(ratatui::style::Color::Yellow),
                        ));
                    }
                    if is_pinned {
                        right_spans.push(Span::styled(
                            app.sym("pinned").trim(),
                            Style::default().fg(WARNING()),
                        ));
                    }
                    if !right_spans.is_empty() {
                        let icon_line = Line::from(right_spans).alignment(Alignment::Right);
                        f.render_widget(Paragraph::new(icon_line), name_cols[1]);
                    }

                    // Row 1: Left column (branch name) and Right column (status section)
                    let row1_cols = Layout::default()
                        .direction(Direction::Horizontal)
                        .constraints([Constraint::Min(0), Constraint::Length(STATUS_ZONE_WIDTH)])
                        .split(rows[1]);

                    let branch_line = match status {
                        ItemStatus::GitRepo(Some(s)) => {
                            if let Some(b) = &s.branch {
                                let mut parts = vec![
                                    Span::raw(UNSELECTED_INDENT),
                                    Span::styled(format!("{} ", app.sym("branch")), muted_style()),
                                    Span::styled(b.clone(), Style::default().fg(ACCENT())),
                                ];
                                if let Some(time) = s.last_commit_time {
                                    parts.push(Span::raw(" ("));
                                    parts.push(Span::styled(
                                        format_relative_time(time),
                                        muted_style(),
                                    ));
                                    parts.push(Span::raw(")"));
                                }
                                Line::from(parts)
                            } else {
                                Line::from("")
                            }
                        }
                        _ => Line::from(""),
                    };
                    f.render_widget(Paragraph::new(branch_line), row1_cols[0]);

                    let status_line =
                        status_indicator_line(app, status).alignment(Alignment::Right);
                    f.render_widget(Paragraph::new(status_line), row1_cols[1]);
                }
            }
        }
    }
}

/// Renders a centered empty-state message when no items are in the list.
fn draw_empty_state(f: &mut Frame, area: Rect) {
    // Vertical: push content to the upper-middle third of the area.
    let vert = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Percentage(25), Constraint::Min(0), Constraint::Percentage(40)])
        .split(area);

    let lines = vec![
        Line::from(vec![Span::styled("No repositories tracked yet.", primary_style())]),
        Line::from(""),
        Line::from(vec![
            Span::raw("Press  "),
            Span::styled("a", accent_style()),
            Span::raw("  to add a repository or directory path"),
        ]),
        Line::from(vec![
            Span::raw("Press  "),
            Span::styled("e", accent_style()),
            Span::raw("  to edit the selected item"),
        ]),
        Line::from(vec![
            Span::raw("Press  "),
            Span::styled("d", accent_style()),
            Span::raw("  to delete the selected item"),
        ]),
        Line::from(vec![
            Span::raw("Press  "),
            Span::styled("?", accent_style()),
            Span::raw("  to see all shortcuts"),
        ]),
        Line::from(vec![
            Span::raw("Press  "),
            Span::styled("q", accent_style()),
            Span::raw("  to quit"),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::styled("Tip: ", muted_style()),
            Span::styled("paths support ~ expansion  (e.g. ~/code/my-project)", muted_style()),
        ]),
    ];

    let para = Paragraph::new(lines).alignment(Alignment::Center);
    f.render_widget(para, vert[1]);
}

/// Renders a centered empty-state message when search matches no repositories.
fn draw_search_empty_state(f: &mut Frame, area: Rect, query: &str) {
    let vert = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Percentage(25), Constraint::Min(0), Constraint::Percentage(40)])
        .split(area);

    let lines = vec![
        Line::from(vec![Span::styled(
            format!("No repositories matching '{}'.", query),
            primary_style(),
        )]),
        Line::from(""),
        Line::from(vec![
            Span::raw("Press  "),
            Span::styled("Esc", accent_style()),
            Span::raw("  to clear the search filter"),
        ]),
    ];

    let p = Paragraph::new(lines).alignment(Alignment::Center);
    f.render_widget(p, vert[1]);
}

/// Renders the per-item status as a colored symbol + (for git repos) a
/// compact set of `N+` (staged), `N!` (modified), `N?` (untracked),
/// `N↑` (commits ahead), `N↓` (commits behind) suffixes. Only non-zero
/// counts are shown so the indicator stays compact for the common case.
fn status_indicator_line(app: &App, status: &ItemStatus) -> Line<'static> {
    match status {
        ItemStatus::Missing => Line::from(vec![
            Span::styled(app.sym("close"), Style::default().fg(DANGER())),
            Span::raw(" "),
            Span::styled("missing", muted_style()),
        ]),
        ItemStatus::Directory => Line::from(vec![
            Span::styled(app.sym("bullet_empty"), Style::default().fg(WARNING())),
            Span::raw(" "),
            Span::styled("dir", muted_style()),
        ]),
        ItemStatus::GitRepo(None) => Line::from(vec![
            Span::styled(app.sym("bullet_filled"), Style::default().fg(SUCCESS())),
            Span::raw(" "),
            Span::styled("?", muted_style()),
        ]),
        ItemStatus::GitRepo(Some(summary)) => repo_indicator_line(app, summary),
    }
}

fn repo_indicator_line(app: &App, summary: &RepoSummary) -> Line<'static> {
    let dot_color = if summary.conflicted > 0 { DANGER() } else { SUCCESS() };
    let mut spans = vec![Span::styled(app.sym("bullet_filled"), Style::default().fg(dot_color))];
    if summary.unchanged() {
        spans.push(Span::raw(" "));
        spans.push(Span::styled("clean", muted_style()));
        return Line::from(spans);
    }
    let ahead_style = if summary.ahead <= 3 {
        Style::default().fg(SUCCESS()).add_modifier(Modifier::BOLD)
    } else if summary.ahead <= 10 {
        Style::default().fg(WARNING()).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(DANGER()).add_modifier(Modifier::BOLD)
    };

    let behind_style = if summary.behind <= 5 {
        Style::default().fg(WARNING()).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(DANGER()).add_modifier(Modifier::BOLD)
    };

    // Each (count, symbol, style) is rendered only if count > 0. The
    // ordering matches the Detail view's worktree section for consistency.
    let parts = [
        (summary.staged, "+", Style::default().fg(ACCENT())),
        (summary.modified, "!", Style::default().fg(WARNING())),
        (summary.untracked, "?", muted_style()),
        (summary.conflicted, app.sym("action").trim(), Style::default().fg(DANGER())),
        (summary.ahead, app.sym("up"), ahead_style),
        (summary.behind, app.sym("down"), behind_style),
    ];
    for (count, symbol, style) in parts {
        if count > 0 {
            spans.push(Span::raw(" "));
            spans.push(Span::styled(format!("{}{}", count, symbol), style));
        }
    }
    Line::from(spans)
}

pub(crate) fn draw_input_status(
    f: &mut Frame,
    area: Rect,
    verb: &str,
    buffer: &str,
    is_compat: bool,
) {
    let mut spans = Vec::new();

    // Add Mode Badge
    spans.push(Span::styled(
        "INPUT",
        Style::default().fg(ratatui::style::Color::Red).add_modifier(Modifier::BOLD),
    ));

    let mode_sep = if is_compat { " > " } else { "" };
    spans.push(Span::styled(mode_sep, muted_style()));

    let prefix = format!("{}", verb);
    spans.push(Span::styled(
        prefix.clone(),
        Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
    ));
    spans.push(Span::styled(buffer.to_string(), primary_style()));

    let separator = if is_compat { " > " } else { "" };
    spans.push(Span::styled(separator, muted_style()));
    spans.push(Span::raw("Save"));
    spans.push(Span::raw(" "));
    spans.push(Span::styled("[", muted_style()));
    spans.push(Span::styled("", accent_style()));
    spans.push(Span::styled("]", muted_style()));

    spans.push(Span::styled(separator, muted_style()));
    spans.push(Span::raw("Cancel"));
    spans.push(Span::raw(" "));
    spans.push(Span::styled("[", muted_style()));
    let cancel_key = if is_compat { "Esc" } else { "" };
    spans.push(Span::styled(cancel_key, accent_style()));
    spans.push(Span::styled("]", muted_style()));

    let para = Paragraph::new(Line::from(spans));
    f.render_widget(para, area);

    // Cursor position calculation includes the Mode Badge (5 chars) and Mode Sep (3 chars)
    let badge_offset = 5 + 3;
    let cursor_offset = (badge_offset + prefix.chars().count() + buffer.chars().count()) as u16;
    let cursor_x = area.x.saturating_add(cursor_offset.min(area.width.saturating_sub(1)));
    f.set_cursor_position(Position::new(cursor_x, area.y));
}

pub(crate) fn wrap_str(s: &str, max_width: usize) -> Vec<String> {
    if s.is_empty() {
        return vec![String::new()];
    }
    let mut chunks = Vec::new();
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        let end = (i + max_width).min(chars.len());
        chunks.push(chars[i..end].iter().collect());
        i = end;
    }
    chunks
}

/// Pack comma-separated `val_str` items onto lines of at most `max_width` chars.
/// Items that are individually wider than `max_width` are hard-wrapped by
/// `wrap_str`. The function always returns at least one (possibly empty) entry.
pub(crate) fn wrap_excludes(val_str: &str, max_width: usize) -> Vec<String> {
    let mut lines: Vec<String> = Vec::new();
    let mut current_line = String::new();
    let parts: Vec<&str> = val_str.split(',').collect();
    for (idx, part) in parts.iter().enumerate() {
        let suffix = if idx + 1 < parts.len() { "," } else { "" };
        let item = format!("{}{}", part, suffix);

        if current_line.chars().count() + item.chars().count() > max_width {
            if !current_line.is_empty() {
                lines.push(current_line);
                current_line = String::new();
            }
            if item.chars().count() > max_width {
                let mut sub_chunks = wrap_str(&item, max_width);
                if let Some(last) = sub_chunks.pop() {
                    current_line = last;
                }
                lines.extend(sub_chunks);
            } else {
                current_line = item;
            }
        } else {
            current_line.push_str(&item);
        }
    }
    if !current_line.is_empty() {
        lines.push(current_line);
    }
    if lines.is_empty() { vec![String::new()] } else { lines }
}

#[allow(clippy::needless_range_loop)]

/// Returns a `Rect` of `(percent_x, percent_y)` dimensions, centered inside `area`.

pub(crate) fn confirm_tag_delete_entries(
    target: &str,
    is_on_remote: bool,
) -> (Option<Vec<Span<'static>>>, Vec<StatusEntry>) {
    let mut spans = vec![
        Span::raw("Delete tag "),
        Span::styled(
            format!("\"{}\"", target),
            Style::default().fg(DANGER()).add_modifier(Modifier::BOLD),
        ),
        Span::raw("? "),
    ];
    if is_on_remote {
        spans.push(Span::styled(
            "(will also delete from remote) ",
            Style::default().fg(WARNING()).add_modifier(Modifier::BOLD),
        ));
    }
    let message_spans = Some(spans);
    let entries = vec![
        StatusEntry::new(vec![
            Span::raw("Confirm"),
            Span::raw(" "),
            Span::styled("[", muted_style()),
            Span::styled("y", Style::default().fg(DANGER()).add_modifier(Modifier::BOLD)),
            Span::styled("]", muted_style()),
        ]),
        StatusEntry::new(vec![
            Span::styled(" ", muted_style()),
            Span::raw("Cancel"),
            Span::raw(" "),
            Span::styled("[", muted_style()),
            Span::styled("n/⎋", accent_style()),
            Span::styled("]", muted_style()),
        ]),
    ];
    (message_spans, entries)
}

pub(crate) fn confirm_tag_push_entries(
    target: &str,
) -> (Option<Vec<Span<'static>>>, Vec<StatusEntry>) {
    let message_spans = Some(vec![
        Span::raw("Push tag "),
        Span::styled(
            format!("\"{}\"", target),
            Style::default().fg(SUCCESS()).add_modifier(Modifier::BOLD),
        ),
        Span::raw("? "),
    ]);
    let entries = vec![
        StatusEntry::new(vec![
            Span::raw("Confirm"),
            Span::raw(" "),
            Span::styled("[", muted_style()),
            Span::styled("y", Style::default().fg(SUCCESS()).add_modifier(Modifier::BOLD)),
            Span::styled("]", muted_style()),
        ]),
        StatusEntry::new(vec![
            Span::styled(" ", muted_style()),
            Span::raw("Cancel"),
            Span::raw(" "),
            Span::styled("[", muted_style()),
            Span::styled("n/⎋", accent_style()),
            Span::styled("]", muted_style()),
        ]),
    ];
    (message_spans, entries)
}

pub(crate) fn confirm_tag_push_all_entries() -> (Option<Vec<Span<'static>>>, Vec<StatusEntry>) {
    let message_spans = Some(vec![
        Span::raw("Push "),
        Span::styled("ALL", Style::default().fg(WARNING()).add_modifier(Modifier::BOLD)),
        Span::raw(" local tags? "),
    ]);
    let entries = vec![
        StatusEntry::new(vec![
            Span::raw("Confirm"),
            Span::raw(" "),
            Span::styled("[", muted_style()),
            Span::styled("y", Style::default().fg(SUCCESS()).add_modifier(Modifier::BOLD)),
            Span::styled("]", muted_style()),
        ]),
        StatusEntry::new(vec![
            Span::styled(" ", muted_style()),
            Span::raw("Cancel"),
            Span::raw(" "),
            Span::styled("[", muted_style()),
            Span::styled("n/⎋", accent_style()),
            Span::styled("]", muted_style()),
        ]),
    ];
    (message_spans, entries)
}

#[cfg(unix)]
#[allow(unsafe_code)]
pub(crate) fn get_process_stats(app: &App) -> (f64, f64) {
    if let Ok(mut guard) = app.cpu_tracker.lock() {
        let now = std::time::Instant::now();

        // If 2000 ms has not elapsed, return the cached values
        if let Some((_, prev_time, cached_cpu, cached_rss)) = *guard {
            if now.duration_since(prev_time) < std::time::Duration::from_millis(2000) {
                return (cached_rss, cached_cpu);
            }
        }

        let mut usage = std::mem::MaybeUninit::<libc::rusage>::uninit();
        let res = unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) };
        if res != 0 {
            if let Some((_, _, cached_cpu, cached_rss)) = *guard {
                return (cached_rss, cached_cpu);
            }
            return (0.0, 0.0);
        }
        let usage = unsafe { usage.assume_init() };

        #[cfg(target_os = "macos")]
        let rss_bytes = usage.ru_maxrss as f64;
        #[cfg(not(target_os = "macos"))]
        let rss_bytes = (usage.ru_maxrss * 1024) as f64;
        let rss_mb = rss_bytes / (1024.0 * 1024.0);

        let user_sec = usage.ru_utime.tv_sec as f64 + (usage.ru_utime.tv_usec as f64 / 1_000_000.0);
        let sys_sec = usage.ru_stime.tv_sec as f64 + (usage.ru_stime.tv_usec as f64 / 1_000_000.0);
        let total_cpu_sec = user_sec + sys_sec;

        let mut cpu_pct = 0.0;
        if let Some((prev_cpu, prev_time, _, _)) = *guard {
            let delta_cpu = total_cpu_sec - prev_cpu;
            let delta_time = now.duration_since(prev_time).as_secs_f64();
            if delta_time > 0.0 {
                cpu_pct = (delta_cpu / delta_time) * 100.0;
            }
        }
        *guard = Some((total_cpu_sec, now, cpu_pct, rss_mb));

        (rss_mb, cpu_pct)
    } else {
        (0.0, 0.0)
    }
}

#[cfg(not(unix))]
pub(crate) fn get_process_stats(_app: &App) -> (f64, f64) {
    (0.0, 0.0)
}

pub fn draw_repo_jump_popup(f: &mut Frame, app: &App, area: Rect) {
    let popup_area = crate::ui::layout::centered_rect(60, 50, area);
    f.render_widget(ratatui::widgets::Clear, popup_area);

    let border_style = Style::default().fg(ACCENT());
    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(CARD_BORDER())
        .border_style(border_style)
        .title(Line::from(vec![
            Span::raw(" "),
            Span::styled("Jump to Repository", primary_style().add_modifier(Modifier::BOLD)),
            Span::raw(" "),
        ]))
        .padding(Padding::horizontal(1));

    let inner = block.inner(popup_area);
    f.render_widget(block, popup_area);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Input box
            Constraint::Min(1),    // Match list
            Constraint::Length(1), // Hint
        ])
        .split(inner);

    // 1. Draw input box
    let input_block =
        Block::default().borders(Borders::ALL).border_style(muted_style()).title(" Search Query ");
    let input_p = Paragraph::new(Line::from(vec![
        Span::raw("> "),
        Span::styled(app.input_buffer.clone(), primary_style()),
    ]))
    .block(input_block);
    f.render_widget(input_p, chunks[0]);

    // 2. Draw matches
    let matches = app.get_jump_matches();
    let list_items: Vec<ratatui::widgets::ListItem> = matches
        .iter()
        .enumerate()
        .map(|(i, (_, path, name))| {
            let style = if i == app.repo_jump_selection {
                accent_style().add_modifier(Modifier::BOLD | Modifier::REVERSED)
            } else {
                primary_style()
            };
            ratatui::widgets::ListItem::new(Line::from(vec![
                Span::raw("  "),
                Span::styled(name.clone(), style),
                Span::raw("   "),
                Span::styled(path.clone(), muted_style()),
            ]))
        })
        .collect();

    let mut list_state = ratatui::widgets::ListState::default();
    if !matches.is_empty() {
        list_state.select(Some(app.repo_jump_selection));
    }
    f.render_stateful_widget(ratatui::widgets::List::new(list_items), chunks[1], &mut list_state);

    // 3. Draw hint
    let hint = Line::from(vec![
        Span::styled("Type to filter  ", muted_style()),
        Span::styled("↑↓ navigate  ", muted_style()),
        Span::styled("Enter", accent_style().add_modifier(Modifier::BOLD)),
        Span::styled(" jump  ", muted_style()),
        Span::styled("Esc", accent_style().add_modifier(Modifier::BOLD)),
        Span::styled(" cancel", muted_style()),
    ]);
    f.render_widget(Paragraph::new(hint), chunks[2]);
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::app::{App, DetailSection};
    use crate::components::cmd_bar::{detail_dismiss_entries, inspect_dismiss_entries};
    use crate::config::{Config, FzfConfig, SortOrder, ThemeConfig};
    use crate::repo::{FileEntry, ItemDetail, RepoInfo};
    use std::collections::HashMap;
    use std::path::PathBuf;

    #[test]
    fn test_inspect_status_bar_shortcuts() {
        let config = Config {
            items: vec![],
            poll_interval_ms: 100,
            max_commits: 0,
            page_size: 10,
            sort_by: SortOrder::Custom,
            visits: HashMap::new(),
            labels: std::collections::HashMap::new(),
            sort_reverse: false,
            pinned: std::collections::HashSet::new(),
            theme: ThemeConfig::default(),
            theme_name: "default".to_string(),
            fzf: FzfConfig::default(),
            git_app: "gitui".to_string(),
            compatibility_mode: false,
            detail_cache_ttl_secs: 30,
            enable_commit_signatures: false,
            tab_ttl_secs: 60,
            resync_on_tab_change: false,
            graph_max_commits: 1000,
            ..Default::default()
        };
        let mut app = App::new(config, PathBuf::from("dummy_path.toml"));

        // 1. Setup dirty working tree detail
        let mut info = RepoInfo::default();
        info.changes.staged.push(FileEntry { path: "file.txt".to_string(), label: "M" });

        app.current_detail =
            Some(ItemDetail::Repo { resolved: PathBuf::from("/dummy"), info: Box::new(info) });
        app.commit_list.selection = 0; // selection = uncommitted
        app.in_logs_ui = false;

        // A) Staged focus -> Unstage File [↵]
        app.detail_focus = DetailSection::Staged;
        let (_, entries) = inspect_dismiss_entries(&app);
        let entry_labels: Vec<String> = entries
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels.iter().any(|label| label.contains("Unstage File [↵]")));
        assert!(entry_labels.iter().any(|label| label.contains("Unstage All [a]")));
        assert!(entry_labels.iter().any(|label| label.contains("Discard [x]")));
        assert!(entry_labels.iter().any(|label| label.contains("Discard All [X]")));
        assert!(entry_labels.iter().any(|label| label.contains("Commit/Amend [c/C]")));

        // B) Unstaged focus -> Stage File [↵]
        app.detail_focus = DetailSection::Unstaged;
        let (_, entries) = inspect_dismiss_entries(&app);
        let entry_labels: Vec<String> = entries
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels.iter().any(|label| label.contains("Stage File [↵]")));
        assert!(entry_labels.iter().any(|label| label.contains("Stage All [a]")));
        assert!(entry_labels.iter().any(|label| label.contains("Discard [x]")));
        assert!(entry_labels.iter().any(|label| label.contains("Discard All [X]")));

        // C) StagingDetails with last_staging_focus == Staged -> Unstage Hunk [↵]
        app.detail_focus = DetailSection::StagingDetails;
        app.last_staging_focus = DetailSection::Staged;
        let (_, entries) = inspect_dismiss_entries(&app);
        let entry_labels: Vec<String> = entries
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels.iter().any(|label| label.contains("Unstage Hunk [↵]")));

        // D) StagingDetails with last_staging_focus == Unstaged -> Stage Hunk [↵]
        app.detail_focus = DetailSection::StagingDetails;
        app.last_staging_focus = DetailSection::Unstaged;
        let (_, entries) = inspect_dismiss_entries(&app);
        let entry_labels: Vec<String> = entries
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels.iter().any(|label| label.contains("Stage Hunk [↵]")));
        assert!(entry_labels.iter().any(|label| label.contains("Discard Hunk [x/Del]")));

        // D2) StagingDetails with last_staging_focus == Unstaged and diff_line_mode == true -> Stage Line [↵] & Discard Line [x/Del] & Hunk Mode [l]
        app.diff.diff_line_mode = true;
        let (_, entries) = inspect_dismiss_entries(&app);
        let entry_labels: Vec<String> = entries
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels.iter().any(|label| label.contains("Stage Line [↵]")));
        assert!(entry_labels.iter().any(|label| label.contains("Discard Line [x/Del]")));
        assert!(entry_labels.iter().any(|label| label.contains("Hunk Mode [l]")));

        // D3) Full screen diff mode -> Commit [c]
        app.inspect_full_diff = true;
        let (_, entries_full) = inspect_dismiss_entries(&app);
        let entry_labels_full: Vec<String> = entries_full
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_full.iter().any(|label| label.contains("Commit/Amend [c/C]")));

        // E) If in_logs_ui is true, it should NOT render any staging entry
        app.inspect_full_diff = false;
        app.in_logs_ui = true;
        let (_, entries) = inspect_dismiss_entries(&app);
        let entry_labels: Vec<String> = entries
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(
            !entry_labels.iter().any(|label| label.contains("Stage") || label.contains("Unstage"))
        );

        // F) Conflicts focus in Inspect mode -> Accept Ours [o] etc.
        app.in_logs_ui = false;
        app.detail_focus = DetailSection::Conflicts;
        let (_, entries_c) = inspect_dismiss_entries(&app);
        let entry_labels_c: Vec<String> = entries_c
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_c.iter().any(|label| label.contains("Accept Ours [o]")));
        assert!(entry_labels_c.iter().any(|label| label.contains("Accept Theirs [t]")));
        assert!(entry_labels_c.iter().any(|label| label.contains("Mark Resolved [r]")));
        assert!(entry_labels_c.iter().any(|label| label.contains("Abort Merge [A]")));
        assert!(entry_labels_c.iter().any(|label| label.contains("Continue Merge [C]")));
        assert!(entry_labels_c.iter().any(|label| label.contains("Inspect [↵/→]")));

        // G) ConflictDiff focus in Inspect mode -> Accept Ours [o] etc.
        app.detail_focus = DetailSection::ConflictDiff;
        let (_, entries_cd) = inspect_dismiss_entries(&app);
        let entry_labels_cd: Vec<String> = entries_cd
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_cd.iter().any(|label| label.contains("Accept Ours [o]")));
        assert!(entry_labels_cd.iter().any(|label| label.contains("Accept Theirs [t]")));
        assert!(entry_labels_cd.iter().any(|label| label.contains("Mark Resolved [r]")));
        assert!(entry_labels_cd.iter().any(|label| label.contains("Abort Merge [A]")));
        assert!(entry_labels_cd.iter().any(|label| label.contains("Continue Merge [C]")));
        assert!(entry_labels_cd.iter().any(|label| label.contains("Scroll Diff [↑↓/⇟⇞]")));
    }

    #[test]
    fn test_detail_dismiss_entries_shortcuts() {
        let config = Config {
            items: vec![],
            poll_interval_ms: 100,
            max_commits: 0,
            page_size: 10,
            sort_by: SortOrder::Custom,
            visits: HashMap::new(),
            labels: std::collections::HashMap::new(),
            sort_reverse: false,
            pinned: std::collections::HashSet::new(),
            theme: ThemeConfig::default(),
            theme_name: "default".to_string(),
            fzf: FzfConfig::default(),
            git_app: "gitui".to_string(),
            compatibility_mode: false,
            detail_cache_ttl_secs: 30,
            enable_commit_signatures: false,
            tab_ttl_secs: 60,
            resync_on_tab_change: false,
            graph_max_commits: 1000,
            ..Default::default()
        };
        let mut app = App::new(config, PathBuf::from("dummy_path.toml"));

        // Tab 0: Workspace, Commits focus (default)
        app.detail_tab = 0;
        app.detail_focus = DetailSection::Commits;
        let (_, entries_w) = detail_dismiss_entries(&app);
        let entry_labels_w: Vec<String> = entries_w
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_w.iter().any(|label| label.contains("Inspect [↵/→]")));
        assert!(entry_labels_w.iter().any(|label| label.contains("Tag [t]")));
        assert!(entry_labels_w.iter().any(|label| label.contains("Load More [G]")));
        assert!(entry_labels_w.iter().any(|label| label.contains("Yank Hash [y]")));

        // Setup uncommitted changes mock for Tab 0 uncommitted shortcuts
        let mut info = RepoInfo::default();
        info.changes.staged.push(FileEntry { path: "file.txt".to_string(), label: "M" });
        info.changes.unstaged.push(FileEntry { path: "other.txt".to_string(), label: "M" });
        app.current_detail =
            Some(ItemDetail::Repo { resolved: PathBuf::from("/dummy"), info: Box::new(info) });
        app.commit_list.selection = 0; // selection = uncommitted

        // Tab 0: Workspace, Staged focus
        app.detail_focus = DetailSection::Staged;
        let (_, entries_s) = detail_dismiss_entries(&app);
        let entry_labels_s: Vec<String> = entries_s
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_s.iter().any(|label| label.contains("Inspect [→]")));
        assert!(entry_labels_s.iter().any(|label| label.contains("Unstage All [a]")));
        assert!(entry_labels_s.iter().any(|label| label.contains("Discard All [X]")));
        assert!(!entry_labels_s.iter().any(|label| label.contains("Tag [t]")));

        // Tab 0: Workspace, Unstaged focus
        app.detail_focus = DetailSection::Unstaged;
        let (_, entries_u) = detail_dismiss_entries(&app);
        let entry_labels_u: Vec<String> = entries_u
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_u.iter().any(|label| label.contains("Stage All [a]")));
        assert!(entry_labels_u.iter().any(|label| label.contains("Discard All [X]")));

        // Tab 0: Workspace, StagingDetails focus
        app.detail_focus = DetailSection::StagingDetails;
        let (_, entries_sd) = detail_dismiss_entries(&app);
        let entry_labels_sd: Vec<String> = entries_sd
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_sd.iter().any(|label| label.contains("Inspect [→]")));
        assert!(!entry_labels_sd.iter().any(|label| label.contains("Tag [t]")));

        // Tab 1: Files - Files Focus
        app.detail_tab = 1;
        app.detail_focus = DetailSection::Files;
        let (_, entries_f1) = detail_dismiss_entries(&app);
        let entry_labels_f1: Vec<String> = entries_f1
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_f1.iter().any(|label| label.contains("Fuzzy Find [f]")));
        assert!(entry_labels_f1.iter().any(|label| label.contains("Expand/Collapse [←/→]")));
        assert!(entry_labels_f1.iter().any(|label| label.contains("History [⇧H]")));
        assert!(!entry_labels_f1.iter().any(|label| label.contains("Open in Editor [e/o]")));

        // Add a file tree item that is a directory
        app.file_tree.visible_files.push(crate::app::FileTreeItem {
            name: "src".to_string(),
            full_path: "src".to_string(),
            is_dir: true,
            depth: 0,
            is_expanded: true,
        });
        app.file_tree.file_list_selection = 0;
        let (_, entries_f1_dir) = detail_dismiss_entries(&app);
        let entry_labels_f1_dir: Vec<String> = entries_f1_dir
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(!entry_labels_f1_dir.iter().any(|label| label.contains("Open in Editor [e/o]")));

        // Add a file tree item that is a file
        app.file_tree.visible_files.push(crate::app::FileTreeItem {
            name: "main.rs".to_string(),
            full_path: "src/main.rs".to_string(),
            is_dir: false,
            depth: 1,
            is_expanded: false,
        });
        app.file_tree.file_list_selection = 1;
        let (_, entries_f1_file) = detail_dismiss_entries(&app);
        let entry_labels_f1_file: Vec<String> = entries_f1_file
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_f1_file.iter().any(|label| label.contains("Open in Editor [e/o]")));

        // Tab 1: Files - FileContent Focus
        app.detail_focus = DetailSection::FileContent;
        let (_, entries_f2) = detail_dismiss_entries(&app);
        let entry_labels_f2: Vec<String> = entries_f2
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(!entry_labels_f2.iter().any(|label| label.contains("Fuzzy Find [f]")));
        assert!(!entry_labels_f2.iter().any(|label| label.contains("Expand/Collapse [←/→]")));
        assert!(!entry_labels_f2.iter().any(|label| label.contains("History [⇧H]")));
        assert!(entry_labels_f2.iter().any(|label| label.contains("Full Screen [→]")));
        assert!(entry_labels_f2.iter().any(|label| label.contains("Open in Editor [e/o]")));

        app.inspect_full_diff = true;
        let (_, entries_f2_full) = detail_dismiss_entries(&app);
        let entry_labels_f2_full: Vec<String> = entries_f2_full
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(
            entry_labels_f2_full.iter().any(|label| label.contains("Exit Full Screen [←/⎋/q]"))
        );
        app.inspect_full_diff = false;

        // Tab 3: Branches - LocalBranches Focus
        app.detail_tab = 3;
        app.detail_focus = DetailSection::LocalBranches;
        let (_, entries_b1) = detail_dismiss_entries(&app);
        let entry_labels_b1: Vec<String> = entries_b1
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_b1.iter().any(|label| label.contains("Fetch [f/F]")));
        assert!(entry_labels_b1.iter().any(|label| label.contains("Pull [p]")));
        assert!(entry_labels_b1.iter().any(|label| label.contains("Push [⇧P]")));

        // Tab 3: Branches - RemoteBranches Focus
        app.detail_focus = DetailSection::RemoteBranches;
        let (_, entries_b2) = detail_dismiss_entries(&app);
        let entry_labels_b2: Vec<String> = entries_b2
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(!entry_labels_b2.iter().any(|label| label.contains("Fetch [f/F]")));
        assert!(!entry_labels_b2.iter().any(|label| label.contains("Pull [p]")));
        assert!(!entry_labels_b2.iter().any(|label| label.contains("Push [⇧P]")));

        // Tab 6: Stashes - Stashes Focus
        app.detail_tab = 6;
        app.detail_focus = DetailSection::Stashes;
        let (_, entries_s1) = detail_dismiss_entries(&app);
        let entry_labels_s1: Vec<String> = entries_s1
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_s1.iter().any(|label| label.contains("Apply [a]")));
        assert!(entry_labels_s1.iter().any(|label| label.contains("Delete [D]")));

        // Tab 6: Stashes - StashedFiles Focus
        app.detail_focus = DetailSection::StashedFiles;
        let (_, entries_s2) = detail_dismiss_entries(&app);
        let entry_labels_s2: Vec<String> = entries_s2
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(!entry_labels_s2.iter().any(|label| label.contains("Apply [a]")));
        assert!(!entry_labels_s2.iter().any(|label| label.contains("Delete [D]")));

        // Tab 4: Tags
        app.detail_tab = 4;
        let (_, entries_tags) = detail_dismiss_entries(&app);
        let entry_labels_tags: Vec<String> = entries_tags
            .iter()
            .map(|entry| {
                entry.spans.iter().map(|s| s.content.as_ref()).collect::<Vec<&str>>().join("")
            })
            .collect();
        assert!(entry_labels_tags.iter().any(|label| label.contains("Fetch [f/F]")));
    }

    #[test]
    fn test_wrap_str() {
        let wrapped = wrap_str("node_modules,target,build,dist", 10);
        assert_eq!(
            wrapped,
            vec!["node_modul".to_string(), "es,target,".to_string(), "build,dist".to_string(),]
        );

        let wrapped_empty = wrap_str("", 10);
        assert_eq!(wrapped_empty, vec!["".to_string()]);
    }

    #[test]
    fn test_wrap_excludes() {
        // Items that fit together on one line stay together
        let w = wrap_excludes("node_modules,target", 30);
        assert_eq!(w, vec!["node_modules,target"]);

        // Items that overflow wrap to the next line
        let w = wrap_excludes("node_modules,target,build,dist", 20);
        // "node_modules," = 13, "target," = 7 → 20 fits on line 1
        // "build," = 6, "dist" = 4 → fits on line 2
        assert_eq!(w, vec!["node_modules,target,", "build,dist"]);

        // Single very long item gets hard-wrapped
        let w = wrap_excludes("averylongnamehere", 10);
        assert_eq!(w, vec!["averylongn", "amehere"]);

        // Empty string returns a single empty line
        let w = wrap_excludes("", 20);
        assert_eq!(w, vec![""]);
    }

    #[test]
    fn test_settings_val_str() {
        let config = Config {
            editor: "vim".to_string(),
            ssh_strict_host_checking: true,
            ..Default::default()
        };
        let mut app = App::new(config, PathBuf::from("dummy.toml"));

        // Setting index 55: SSH Strict Host Checking
        let val_ssh = crate::popups::settings::get_val_str(&app, 55);
        assert_eq!(val_ssh, "true");

        // Setting index 56: Editor Command
        app.settings_selected_index = 56;
        app.settings_editing = false;
        let val_editor = crate::popups::settings::get_val_str(&app, 56);
        assert_eq!(val_editor, "vim");

        // During editing
        app.settings_editing = true;
        app.input_buffer = "nano".to_string();
        let val_editor_edit = crate::popups::settings::get_val_str(&app, 56);
        assert_eq!(val_editor_edit, "nano█");
    }

    #[test]
    fn test_repo_indicator_line_divergence() {
        let config = Config::default();
        let app = App::new(config, PathBuf::from("dummy.toml"));

        // Case 1: Ahead <= 3 (Green)
        let summary1 = RepoSummary {
            ahead: 2,
            behind: 0,
            staged: 0,
            modified: 0,
            untracked: 0,
            conflicted: 0,
            branch: None,
            state: RepoState::Clean,
            last_commit_time: None,
        };
        let line1 = repo_indicator_line(&app, &summary1);
        let ahead_span = line1.spans.iter().find(|s| s.content.contains(app.sym("up"))).unwrap();
        assert_eq!(ahead_span.style.fg, Some(SUCCESS()));

        // Case 2: Ahead <= 10 (Yellow)
        let summary2 = RepoSummary {
            ahead: 7,
            behind: 0,
            staged: 0,
            modified: 0,
            untracked: 0,
            conflicted: 0,
            branch: None,
            state: RepoState::Clean,
            last_commit_time: None,
        };
        let line2 = repo_indicator_line(&app, &summary2);
        let ahead_span2 = line2.spans.iter().find(|s| s.content.contains(app.sym("up"))).unwrap();
        assert_eq!(ahead_span2.style.fg, Some(WARNING()));

        // Case 3: Behind <= 5 (Yellow)
        let summary3 = RepoSummary {
            ahead: 0,
            behind: 3,
            staged: 0,
            modified: 0,
            untracked: 0,
            conflicted: 0,
            branch: None,
            state: RepoState::Clean,
            last_commit_time: None,
        };
        let line3 = repo_indicator_line(&app, &summary3);
        let behind_span = line3.spans.iter().find(|s| s.content.contains(app.sym("down"))).unwrap();
        assert_eq!(behind_span.style.fg, Some(WARNING()));

        // Case 4: Behind > 5 (Red)
        let summary4 = RepoSummary {
            ahead: 0,
            behind: 8,
            staged: 0,
            modified: 0,
            untracked: 0,
            conflicted: 0,
            branch: None,
            state: RepoState::Clean,
            last_commit_time: None,
        };
        let line4 = repo_indicator_line(&app, &summary4);
        let behind_span2 =
            line4.spans.iter().find(|s| s.content.contains(app.sym("down"))).unwrap();
        assert_eq!(behind_span2.style.fg, Some(DANGER()));
    }

    #[test]
    fn test_draw_global_summary_bar() {
        let config = Config {
            items: vec!["/path/to/repo_a".to_string(), "/path/to/repo_b".to_string()],
            ..Default::default()
        };
        let mut app = App::new(config, PathBuf::from("dummy_path.toml"));

        let summary_clean = RepoSummary {
            branch: Some("main".to_string()),
            staged: 0,
            modified: 0,
            untracked: 0,
            conflicted: 0,
            ahead: 0,
            behind: 0,
            state: RepoState::Clean,
            last_commit_time: Some(
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_secs() as i64,
            ),
        };

        let summary_dirty = RepoSummary {
            branch: Some("main".to_string()),
            staged: 1,
            modified: 0,
            untracked: 0,
            conflicted: 0,
            ahead: 2,
            behind: 0,
            state: RepoState::Clean,
            last_commit_time: Some(0), // very stale
        };

        app.statuses = vec![
            ItemStatus::GitRepo(Some(summary_clean)),
            ItemStatus::GitRepo(Some(summary_dirty)),
        ];

        let backend = ratatui::backend::TestBackend::new(80, 1);
        let mut terminal = ratatui::Terminal::new(backend).unwrap();
        terminal
            .draw(|f| {
                draw_global_summary_bar(f, Rect::new(0, 0, 80, 1), &app);
            })
            .unwrap();

        // Verify correct rendering of stats in the buffer
        let buffer = terminal.backend().buffer();
        let text: String = (0..80).map(|x| buffer[(x, 0)].symbol()).collect();
        let trimmed = text.trim();
        assert!(trimmed.contains("2 repos"), "Buffer contents: {}", trimmed);
        assert!(trimmed.contains("1 dirty"), "Buffer contents: {}", trimmed);
        assert!(trimmed.contains("1 ahead"), "Buffer contents: {}", trimmed);
        assert!(trimmed.contains("1 stale"), "Buffer contents: {}", trimmed);
    }

    #[test]
    fn test_draw_partial_uncommitted_badge() {
        let config = Config { items: vec!["/path/to/repo_a".to_string()], ..Default::default() };
        let mut app = App::new(config, PathBuf::from("dummy_path.toml"));

        // Has both staged and unstaged (modified) changes -> should show PARTIAL badge
        let summary_partial = RepoSummary {
            branch: Some("main".to_string()),
            staged: 1,
            modified: 1,
            untracked: 0,
            conflicted: 0,
            ahead: 0,
            behind: 0,
            state: RepoState::Clean,
            last_commit_time: None,
        };

        app.statuses = vec![ItemStatus::GitRepo(Some(summary_partial))];

        let backend = ratatui::backend::TestBackend::new(80, 5);
        let mut terminal = ratatui::Terminal::new(backend).unwrap();
        terminal
            .draw(|f| {
                let chunks = vec![Rect::new(0, 0, 80, 4)];
                draw_items(f, &app, &chunks);
            })
            .unwrap();

        let buffer = terminal.backend().buffer();
        let mut found_partial = false;
        for y in 0..5 {
            let row_text: String = (0..80).map(|x| buffer[(x, y)].symbol()).collect();
            if row_text.contains("PARTIAL") {
                found_partial = true;
                break;
            }
        }
        assert!(found_partial, "Expected to find PARTIAL badge in card rendering");
    }
}