mnml-rs 0.2.20

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
//! Renders a [`crate::file_browser::FileBrowserPane`] — path header, then
//! `icon name … size … modified` rows.
//!
//! Column layout degrades by width rather than truncating names: the
//! modified column drops first, then size, so a narrow pane still shows
//! full filenames. Names are what you navigate by; a date you cannot read
//! is worth less than a name you can.

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;

use crate::app::App;
use crate::layout::PaneId;
use crate::ui::theme;

/// Width at which the modified column appears, and then the size column.
const W_FOR_MODIFIED: u16 = 46;
const W_FOR_SIZE: u16 = 30;
const SIZE_COL: usize = 8;
/// Entry index used for the pinned `..` row's click rect. Not a real
/// index into `entries` — the click handler maps it to `go_parent`.
pub const PARENT_ROW: usize = usize::MAX;
const MOD_COL: usize = 12;

/// Human-readable size in the same idiom as `ls -h`.
///
/// One decimal place below 10 units and none above, so the column stays
/// narrow while `4.1 M` and `210 K` both read exactly.
pub fn human_size(bytes: u64) -> String {
    const UNITS: [&str; 5] = ["B", "K", "M", "G", "T"];
    let mut v = bytes as f64;
    let mut u = 0usize;
    while v >= 1024.0 && u + 1 < UNITS.len() {
        v /= 1024.0;
        u += 1;
    }
    if u == 0 {
        format!("{bytes} {}", UNITS[0])
    } else if v < 10.0 {
        format!("{v:.1} {}", UNITS[u])
    } else {
        format!("{v:.0} {}", UNITS[u])
    }
}

/// `MM/DD HH:MM`, matching the git graph's date column so the two panes
/// read consistently.
pub fn short_time(secs: u64) -> String {
    crate::ui::git_graph_view::format_commit_datetime(secs as i64)
}

pub fn draw(frame: &mut Frame, app: &mut App, pane_id: PaneId, area: Rect) {
    let t = theme::cur();
    let nerd = !app.config.ui.ascii_icons;
    frame.render_widget(
        Paragraph::new("").style(Style::default().bg(t.bg_dark)),
        area,
    );
    if area.height < 2 || area.width < 8 {
        return;
    }

    // ── path header ──
    let header = Rect {
        x: area.x,
        y: area.y,
        width: area.width,
        height: 1,
    };
    // #files item 3 — CLICKABLE BREADCRUMB instead of a flat path.
    //
    // Each segment navigates to that ancestor, and a trailing `▾` opens
    // the destinations picker. The old header was an elided string: it
    // told you where you were and gave you nothing to do about it, so the
    // only way to move up several levels was pressing `h` repeatedly.
    //
    // Segments are dropped from the LEFT when they do not fit, because the
    // tail identifies where you are. The dropped prefix renders as `…`,
    // which stays clickable and jumps to the deepest hidden ancestor —
    // otherwise narrowing the pane would silently remove navigation.
    // #files item 5 — git status per row.
    //
    // `GitStatus::snapshot().files` is a path -> state map that mnml
    // already maintains on a TTL for the tree tint and statusline chips,
    // keyed ABSOLUTELY. So badges cost one hash lookup per visible row and
    // nothing else — no extra `git` invocation, no new cache.
    //
    // This is the thing a standalone file manager cannot do, and the
    // strongest argument for a browser INSIDE the editor rather than
    // beside one.
    //
    // BORROWED, not cloned. All three reviewers flagged the clone: it
    // copied the entire repo-wide `HashMap<PathBuf, FileState>` — every
    // key a fresh heap allocation — on EVERY draw, while only the handful
    // of visible rows ever look into it. In a monorepo with thousands of
    // dirty files that is an allocation storm per frame, doubled in the
    // dual-pane commander layout.
    //
    // `tree_view` does the identical badge lookup by reference in a
    // function with the same interleaved-borrow shape, which is what
    // proved the clone was habit rather than necessity. The snapshot is
    // cloned into a local `Rc`-free owned map only where the borrow
    // checker genuinely demands it — here it does not, because the
    // lookups all happen before `app.rects` is touched.
    let git_files = &app.git.snapshot().files;
    let (cwd, sort_kind, filter_q, filter_on) = match app.panes.get(pane_id) {
        Some(crate::pane::Pane::Files(f)) => (
            f.cwd.clone(),
            f.sort,
            f.filter.clone(),
            f.filter_focused || !f.filter.is_empty(),
        ),
        _ => return,
    };
    let chain = crate::places::breadcrumb(&cwd);
    let chevron = if nerd { "\u{25BE}" } else { "v" };
    let sort_label = match sort_kind {
        crate::file_browser::Sort::DirsFirstName => "name",
        crate::file_browser::Sort::Size => "size",
        crate::file_browser::Sort::Modified => "modified",
    };
    let sort_text = format!(" {sort_label} \u{25BE} ");
    // Reserve the chevron AND the sort label before laying out the
    // breadcrumb.
    //
    // The first version appended the sort label afterwards using whatever
    // width was left, so on a long path there was none left and the label
    // silently vanished — which is precisely when you have most trouble
    // telling why the listing is ordered as it is. Fixed-width chrome
    // gets its space first; the PATH is the elastic part, and it already
    // knows how to elide.
    let avail = (area.width as usize)
        .saturating_sub(3)
        .saturating_sub(sort_text.chars().count());
    // Widest suffix of the chain that fits, always keeping the last
    // segment even if it alone overflows.
    let mut first = 0usize;
    loop {
        let width: usize = chain[first..]
            .iter()
            .map(|(l, _)| l.chars().count() + 1)
            .sum::<usize>()
            + if first > 0 { 2 } else { 0 };
        if width <= avail || first + 1 >= chain.len() {
            break;
        }
        first += 1;
    }
    let mut spans: Vec<Span<'static>> = Vec::new();
    let mut x = area.x;
    let hdr_bg = t.bg_darker;
    if first > 0 {
        let lbl = "".to_string();
        let w = lbl.chars().count() as u16;
        spans.push(Span::styled(lbl, Style::default().fg(t.comment).bg(hdr_bg)));
        // Clicking `…` goes to the deepest ancestor that was dropped.
        app.rects.file_pane_breadcrumbs.push((
            Rect {
                x,
                y: area.y,
                width: w,
                height: 1,
            },
            pane_id,
            chain[first - 1].1.clone(),
        ));
        x += w;
    }
    for (i, (label, path)) in chain.iter().enumerate().skip(first) {
        let is_last = i + 1 == chain.len();
        let text = if label == "/" {
            "/".to_string()
        } else if is_last {
            label.clone()
        } else {
            format!("{label}/")
        };
        let w = text.chars().count() as u16;
        spans.push(Span::styled(
            text,
            Style::default()
                .fg(if is_last { t.blue } else { t.comment })
                .bg(hdr_bg)
                .add_modifier(if is_last {
                    Modifier::BOLD
                } else {
                    Modifier::empty()
                }),
        ));
        app.rects.file_pane_breadcrumbs.push((
            Rect {
                x,
                y: area.y,
                width: w,
                height: 1,
            },
            pane_id,
            path.clone(),
        ));
        x += w;
    }
    spans.push(Span::styled(
        format!(" {chevron} "),
        Style::default().fg(t.comment).bg(hdr_bg),
    ));
    // #files — the active SORT, right-aligned in the header.
    //
    // `s` cycles name → size → modified and used to announce itself with
    // a toast only, so a second later the active sort was invisible and
    // the listing order looked arbitrary. A gap introduced by the
    // foundation commit; state a mode is in, do not just announce the
    // transition.
    let used: usize = spans.iter().map(|sp| sp.content.chars().count()).sum();
    let pad = (area.width as usize)
        .saturating_sub(used)
        .saturating_sub(sort_text.chars().count());
    if pad > 0 {
        spans.push(Span::styled(" ".repeat(pad), Style::default().bg(hdr_bg)));
    }
    let sort_w = sort_text.chars().count() as u16;
    let sort_x = area.x + (area.width.saturating_sub(sort_w));
    spans.push(Span::styled(
        sort_text,
        Style::default().fg(t.comment).bg(hdr_bg),
    ));
    // Clickable. It paints the same `▾` as the destinations chevron, so
    // painting it inert made it a dead click — flagged in review.
    app.rects.file_pane_sort_label = Some((
        Rect {
            x: sort_x,
            y: area.y,
            width: sort_w,
            height: 1,
        },
        pane_id,
    ));
    app.rects.file_pane_places_chevron = Some((
        Rect {
            x,
            y: area.y,
            width: 3,
            height: 1,
        },
        pane_id,
    ));
    frame.render_widget(
        Paragraph::new(Line::from(spans)).style(Style::default().bg(hdr_bg)),
        header,
    );

    let mut body = Rect {
        x: area.x,
        y: area.y + 1,
        width: area.width,
        height: area.height - 1,
    };
    // #files item 2 — a footer row while anything is marked. Only then:
    // a permanent status row would cost a listing row for information
    // that is usually "nothing selected".
    let marked_count = match app.panes.get(pane_id) {
        Some(crate::pane::Pane::Files(f)) => f.marked.len(),
        _ => 0,
    };
    let footer = if marked_count > 0 && body.height > 2 {
        let r = Rect {
            x: body.x,
            y: body.y + body.height - 1,
            width: body.width,
            height: 1,
        };
        body.height -= 1;
        Some(r)
    } else {
        None
    };
    let Some(crate::pane::Pane::Files(f)) = app.panes.get_mut(pane_id) else {
        return;
    };

    // ── filter row ──
    //
    // Only while filtering. A permanent row would cost a listing row to
    // say "not filtering", the same reasoning as the marked footer.
    let body = if filter_on && body.height > 2 {
        let r = Rect {
            x: body.x,
            y: body.y,
            width: body.width,
            height: 1,
        };
        let glyph = crate::ui::search_glyph::for_ascii(!nerd);
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled(format!(" {glyph} "), Style::default().fg(t.cyan).bg(t.bg2)),
                Span::styled(
                    if filter_q.is_empty() {
                        crate::ui::filter_placeholder::for_state(true).to_string()
                    } else {
                        filter_q.clone()
                    },
                    Style::default().fg(t.fg).bg(t.bg2),
                ),
            ]))
            .style(Style::default().bg(t.bg2)),
            r,
        );
        Rect {
            x: body.x,
            y: body.y + 1,
            width: body.width,
            height: body.height - 1,
        }
    } else {
        body
    };

    // ── `..` parent row ──
    //
    // User, having navigated into assets/: "how do i go up a level i wnt
    // into this folder and coudlnt get back up to mnml folder".
    //
    // Backspace / Left / h already worked — verified by probe — so this is
    // purely a DISCOVERABILITY failure: I shipped a file browser with no
    // visible way up. Every file manager has this row (ranger, mc,
    // superfile), and a keybinding nobody can see is not an affordance.
    //
    // Rendered by the VIEW and PINNED above the listing rather than
    // injected into `entries`, for two reasons: a synthetic entry in the
    // model would be a target for file operations ("delete .."), and
    // pinning means the way out never scrolls off the top of a long
    // directory. The cursor therefore only ever sits on real entries.
    let has_parent = f.cwd.parent().is_some();
    let (parent_row, body) = if has_parent && body.height > 1 {
        (
            Some(Rect {
                x: body.x,
                y: body.y,
                width: body.width,
                height: 1,
            }),
            Rect {
                x: body.x,
                y: body.y + 1,
                width: body.width,
                height: body.height - 1,
            },
        )
    } else {
        (None, body)
    };
    if let Some(r) = parent_row {
        let (icon, _) = crate::ui::icons::for_path(&f.cwd, true, false, nerd);
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled(" ", Style::default().bg(t.bg_dark)),
                Span::styled(
                    format!("{icon} "),
                    Style::default().fg(t.blue).bg(t.bg_dark),
                ),
                Span::styled(
                    "..",
                    Style::default()
                        .fg(t.blue)
                        .bg(t.bg_dark)
                        .add_modifier(Modifier::BOLD),
                ),
                // Name the keys inline. The row itself is the affordance;
                // the hint teaches the keyboard route from it.
                Span::styled(
                    "   (⌫ / ← / h)",
                    Style::default().fg(t.comment).bg(t.bg_dark),
                ),
            ])),
            r,
        );
        app.rects.file_pane_rows.push((r, pane_id, PARENT_ROW));
    }

    // Error / empty states render BELOW the `..` row, not instead of it.
    //
    // These two guards used to `return` before the parent row was drawn,
    // so browsing into an empty or unreadable directory left a pane with
    // no visible way out — reintroducing exactly the bug the `..` row was
    // added to fix, for its most likely trigger. Caught in review.
    if let Some(err) = f.error.clone() {
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled(
                format!("  {err}"),
                Style::default().fg(t.red).bg(t.bg_dark),
            ))),
            body,
        );
        return;
    }
    if f.entries.is_empty() {
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled(
                "  (empty directory)",
                Style::default().fg(t.comment).bg(t.bg_dark),
            ))),
            body,
        );
        return;
    }

    // ── scroll ──
    // Same two-policy split as the git graph (#1229): a jump gets context,
    // a step scrolls minimally.
    let h = body.height as usize;
    let want_center = std::mem::take(&mut f.center_on_next_draw);
    f.scroll = reveal_scroll(f.selected, f.scroll, h, want_center);
    let max_scroll = f.entries.len().saturating_sub(h.min(f.entries.len()));
    f.scroll = f.scroll.min(max_scroll);

    let show_mod = area.width >= W_FOR_MODIFIED;
    let show_size = area.width >= W_FOR_SIZE;
    let sb_w: u16 = if f.entries.len() > h { 1 } else { 0 };
    let badge_w = if git_files.is_empty() { 0 } else { 2 };
    let name_w = (area.width.saturating_sub(sb_w) as usize)
        .saturating_sub(4) // icon + pads
        .saturating_sub(badge_w)
        .saturating_sub(if show_size { SIZE_COL + 1 } else { 0 })
        .saturating_sub(if show_mod { MOD_COL + 1 } else { 0 });

    let selected = f.selected;
    let scroll = f.scroll;
    // Same story as `git_files`: borrowed, not cloned. `entries` and
    // `marked` are disjoint fields of the same pane, so the row loop can
    // read both. Bounded by the user's selection rather than the repo, so
    // smaller — but the same unforced per-frame allocation.
    let f_marked = &f.marked;
    let rows: Vec<(Rect, usize)> = {
        let mut out = Vec::new();
        for (row, idx) in (scroll..f.entries.len()).take(h).enumerate() {
            let e = &f.entries[idx];
            let is_cur = idx == selected;
            let row_bg = if is_cur { t.bg2 } else { t.bg_dark };
            let (icon, icon_fg) = crate::ui::icons::for_path(&e.path, e.is_dir, false, nerd);
            let mut name: String = e.name.chars().take(name_w).collect();
            if e.is_symlink {
                name.push('@');
            }
            let pad = name_w.saturating_sub(name.chars().count());
            // Mark glyph occupies the leading cell, which otherwise keeps
            // the pane bg so a selected row never butts against the pane's
            // left edge (#970 / #1229). A marked row shows `▌` in green
            // there — visible at a glance down the column, and it cannot be
            // confused with the cursor highlight, which is a background.
            let is_marked = f_marked.contains(&e.path);
            let lead = if is_marked {
                Span::styled("\u{258C}", Style::default().fg(t.green).bg(t.bg_dark))
            } else {
                Span::styled(" ", Style::default().bg(t.bg_dark))
            };
            let mut spans = vec![
                lead,
                Span::styled(format!("{icon} "), Style::default().fg(icon_fg).bg(row_bg)),
                Span::styled(
                    name,
                    Style::default()
                        .fg(if e.is_dir { t.blue } else { t.fg })
                        .bg(row_bg)
                        .add_modifier(if e.is_dir {
                            Modifier::BOLD
                        } else {
                            Modifier::empty()
                        }),
                ),
                Span::styled(" ".repeat(pad), Style::default().bg(row_bg)),
            ];
            // Badge: one cell, always reserved when the repo has ANY
            // changes, so rows do not shift horizontally as files change
            // state underneath the cursor.
            if !git_files.is_empty() {
                let (ch, fg) = match git_files.get(&e.path) {
                    Some(crate::git::status::FileState::Conflicted) => ("!", t.red),
                    Some(crate::git::status::FileState::Staged) => ("+", t.green),
                    Some(crate::git::status::FileState::Modified) => ("~", t.yellow),
                    Some(crate::git::status::FileState::Untracked) => ("?", t.comment),
                    None => (" ", t.comment),
                };
                spans.push(Span::styled(
                    ch.to_string(),
                    Style::default()
                        .fg(fg)
                        .bg(row_bg)
                        .add_modifier(Modifier::BOLD),
                ));
                spans.push(Span::styled(" ", Style::default().bg(row_bg)));
            }
            if show_size {
                let s = e.size.map(human_size).unwrap_or_else(|| "".into());
                spans.push(Span::styled(
                    format!("{s:>SIZE_COL$} "),
                    Style::default().fg(t.comment).bg(row_bg),
                ));
            }
            if show_mod {
                let m = e.modified.map(short_time).unwrap_or_else(|| "".into());
                spans.push(Span::styled(
                    format!("{m:>MOD_COL$} "),
                    Style::default().fg(t.comment).bg(row_bg),
                ));
            }
            let r = Rect {
                x: body.x,
                y: body.y + row as u16,
                width: body.width.saturating_sub(sb_w),
                height: 1,
            };
            frame.render_widget(Paragraph::new(Line::from(spans)), r);
            out.push((r, idx));
        }
        out
    };

    if let Some(r) = footer {
        let (n, bytes) = f.marked_summary();
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled(
                    format!(" {n} selected"),
                    Style::default()
                        .fg(t.bg_darker)
                        .bg(t.green)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(
                    format!("  {}  ", human_size(bytes)),
                    Style::default().fg(t.fg).bg(t.bg2),
                ),
                // Name the clearing key: a mode you cannot get out of is
                // worse than no mode.
                Span::styled(
                    "Esc clears · Ctrl+C/X acts on the set ",
                    Style::default().fg(t.comment).bg(t.bg2),
                ),
            ]))
            .style(Style::default().bg(t.bg2)),
            r,
        );
    }
    let total = f.entries.len();
    if sb_w > 0 {
        let sb = Rect {
            x: body.x + body.width - 1,
            y: body.y,
            width: 1,
            height: body.height,
        };
        crate::ui::scrollbar::paint_simple_scrollbar(frame, sb, &t, total, h, scroll);
        app.rects.scrollbars.push(crate::app::ScrollbarHit {
            area: sb,
            pane_id,
            total,
            viewport: h,
            kind: crate::app::ScrollbarKind::FilesPane,
        });
    }
    for (r, idx) in rows {
        app.rects.file_pane_rows.push((r, pane_id, idx));
    }
    app.rects.editor_panes.push((area, pane_id));
}

/// Where the listing should scroll to. See
/// [`crate::ui::git_graph_view`]'s equivalent — a jump gets context, a
/// step scrolls minimally, and centring only applies when the target is
/// off-screen.
fn reveal_scroll(selected: usize, scroll: usize, h: usize, want_center: bool) -> usize {
    if h == 0 {
        return scroll;
    }
    let visible = selected >= scroll && selected < scroll + h;
    if want_center && !visible {
        return selected.saturating_sub(h / 3);
    }
    if selected < scroll {
        selected
    } else if selected >= scroll + h {
        selected + 1 - h
    } else {
        scroll
    }
}

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

    #[test]
    fn human_size_reads_exactly_at_every_scale() {
        assert_eq!(human_size(0), "0 B");
        assert_eq!(human_size(512), "512 B");
        assert_eq!(human_size(1024), "1.0 K");
        assert_eq!(human_size(4_300_000), "4.1 M");
        // Above 10 units, drop the decimal so the column stays narrow.
        assert_eq!(human_size(210 * 1024), "210 K");
    }

    /// The column budget must never exceed the pane, or names overflow
    /// into the scrollbar column.
    #[test]
    fn narrow_panes_drop_columns_rather_than_names() {
        // (No assertion that W_FOR_SIZE < W_FOR_MODIFIED — both are
        // consts, so it is a compile-time truth and clippy rightly calls
        // it noise. The real invariant is the name-column budget below.)
        // At the narrowest width that still shows both, the fixed columns
        // must leave room for a usable name.
        let fixed = 4 + SIZE_COL + 1 + MOD_COL + 1;
        assert!(
            (W_FOR_MODIFIED as usize) > fixed + 8,
            "at {W_FOR_MODIFIED} cells the fixed columns ({fixed}) leave under \
             8 cells for the name"
        );
    }

    #[test]
    fn a_jump_is_revealed_with_context_and_a_step_is_not() {
        // Jump far below the viewport → not pinned to the last row.
        let s = reveal_scroll(500, 0, 30, true);
        assert!(500 - s > 2 && 500 - s < 28);
        // Already visible → no movement.
        assert_eq!(reveal_scroll(105, 100, 30, true), 100);
        // Step off the bottom → exactly one row.
        assert_eq!(reveal_scroll(130, 100, 30, false), 101);
    }
}

#[cfg(test)]
mod render_tests {
    use super::*;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::crossterm::event::KeyModifiers;

    /// End-to-end: a Files pane opened through the App renders its
    /// directory. Compiling is not the same as rendering — this is the
    /// check that the pane is actually wired into the draw dispatch.
    #[test]
    fn a_files_pane_renders_its_directory_listing() {
        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::fs::create_dir(d.path().join("subdir")).unwrap();
        std::fs::write(d.path().join("readme.md"), "hello").unwrap();

        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(None);
        let pid = app.active.expect("open_files_pane did not focus the pane");

        let mut term = Terminal::new(TestBackend::new(60, 10)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 60,
                    height: 10,
                },
            )
        })
        .unwrap();
        let buf = term.backend().buffer();
        let screen: String = (0..10)
            .map(|y| (0..60).map(|x| buf[(x, y)].symbol()).collect::<String>())
            .collect::<Vec<_>>()
            .join("\n");

        assert!(
            screen.contains("subdir"),
            "directory row missing:\n{screen}"
        );
        assert!(screen.contains("readme.md"), "file row missing:\n{screen}");
        assert!(
            screen.contains("5 B"),
            "size column missing (readme.md is 5 bytes):\n{screen}"
        );
        // Row rects must be registered, or clicks land nowhere.
        assert!(
            app.rects.file_pane_rows.iter().any(|(_, p, _)| *p == pid),
            "no click rects registered for the Files pane"
        );
    }

    /// #files — the active sort must be VISIBLE, not just announced by a
    /// toast that vanishes.
    #[test]
    fn the_header_names_the_active_sort() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::fs::write(d.path().join("a.txt"), "a").unwrap();
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(None);
        let pid = app.active.unwrap();

        let header = |app: &mut crate::app::App| -> String {
            let mut term = Terminal::new(TestBackend::new(70, 6)).unwrap();
            term.draw(|f| {
                draw(
                    f,
                    app,
                    pid,
                    Rect {
                        x: 0,
                        y: 0,
                        width: 70,
                        height: 6,
                    },
                )
            })
            .unwrap();
            let buf = term.backend().buffer();
            (0..70).map(|x| buf[(x, 0)].symbol()).collect()
        };

        assert!(header(&mut app).contains("name"), "default sort not shown");
        if let Some(crate::pane::Pane::Files(f)) = app.panes.get_mut(pid) {
            f.set_sort(crate::file_browser::Sort::Size);
        }
        let h = header(&mut app);
        assert!(h.contains("size"), "sort change not reflected: {h:?}");
        assert!(!h.contains("name "), "stale sort label left behind: {h:?}");
    }

    /// #files item 5 — git state per row, from the snapshot mnml already
    /// maintains. Uses a REAL repo, because the whole value is that the
    /// states come from git rather than from a fixture.
    #[test]
    fn rows_show_their_git_state() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let run = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(d.path())
                .output()
                .expect("git");
        };
        run(&["init", "-q", "-b", "main"]);
        run(&["config", "user.email", "t@t"]);
        run(&["config", "user.name", "T"]);
        std::fs::write(d.path().join("tracked.txt"), "one").unwrap();
        run(&["add", "."]);
        run(&["commit", "-qm", "init"]);
        // Now: one modified, one untracked, one unchanged.
        std::fs::write(d.path().join("tracked.txt"), "two").unwrap();
        std::fs::write(d.path().join("brand_new.txt"), "x").unwrap();
        std::fs::write(d.path().join("staged.txt"), "s").unwrap();
        run(&["add", "staged.txt"]);

        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.git.refresh();
        app.open_files_pane(None);
        let pid = app.active.unwrap();

        let mut term = Terminal::new(TestBackend::new(70, 10)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 70,
                    height: 10,
                },
            )
        })
        .unwrap();
        let buf = term.backend().buffer();
        let rows: Vec<String> = (0..10)
            .map(|y| (0..70).map(|x| buf[(x, y)].symbol()).collect())
            .collect();

        let row_for = |name: &str| -> String {
            rows.iter()
                .find(|r| r.contains(name))
                .unwrap_or_else(|| panic!("no row for {name}:\n{}", rows.join("\n")))
                .clone()
        };
        assert!(
            row_for("tracked.txt").contains('~'),
            "modified file has no `~` badge: {:?}",
            row_for("tracked.txt")
        );
        assert!(
            row_for("brand_new.txt").contains('?'),
            "untracked file has no `?` badge: {:?}",
            row_for("brand_new.txt")
        );
        assert!(
            row_for("staged.txt").contains('+'),
            "staged file has no `+` badge: {:?}",
            row_for("staged.txt")
        );
    }

    /// Outside a repo — or in a clean one — no badge column, and no
    /// crash. A file browser must work on any directory on the machine.
    #[test]
    fn a_directory_with_no_git_changes_shows_no_badges() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::fs::write(d.path().join("plain.txt"), "x").unwrap();
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(None);
        let pid = app.active.unwrap();

        let mut term = Terminal::new(TestBackend::new(70, 8)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 70,
                    height: 8,
                },
            )
        })
        .unwrap();
        let buf = term.backend().buffer();
        let row: String = (0..70).map(|x| buf[(x, 2)].symbol()).collect::<String>();
        assert!(
            row.contains("plain.txt"),
            "listing did not render outside a repo: {row:?}"
        );
    }

    /// #files item 3 — every breadcrumb segment must be a real click
    /// target pointing at the RIGHT ancestor. A breadcrumb that renders
    /// but does not navigate is decoration.
    #[test]
    fn breadcrumb_segments_are_clickable_and_point_at_their_ancestor() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let deep = d.path().join("a").join("b");
        std::fs::create_dir_all(&deep).unwrap();
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(Some(deep.clone()));
        let pid = app.active.unwrap();

        let mut term = Terminal::new(TestBackend::new(80, 8)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 80,
                    height: 8,
                },
            )
        })
        .unwrap();

        let crumbs = &app.rects.file_pane_breadcrumbs;
        assert!(!crumbs.is_empty(), "no breadcrumb segments registered");
        // The last segment is the current directory...
        let last = crumbs.last().unwrap();
        assert_eq!(last.2, deep, "final segment is not the cwd");
        // ...and its predecessor is the real parent, not some prefix
        // string that happens to render correctly.
        let parent = crumbs[crumbs.len() - 2].2.clone();
        assert_eq!(
            parent,
            deep.parent().unwrap(),
            "segment before the cwd points at {parent:?}, not its parent"
        );
        // Every segment must be an ancestor of the cwd.
        for (_, _, p) in crumbs {
            assert!(
                deep.starts_with(p),
                "segment {p:?} is not an ancestor of {deep:?}"
            );
        }
        // And the `▾` must be registered, or the destinations list is
        // keyboard-only.
        assert!(
            app.rects.file_pane_places_chevron.is_some(),
            "no chevron rect — the destinations picker has no mouse route"
        );
    }

    /// A narrow pane must drop segments from the LEFT and keep the current
    /// directory visible — the tail is what tells you where you are.
    #[test]
    fn a_narrow_breadcrumb_keeps_the_current_directory() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let deep = d
            .path()
            .join("aaaaaaaaaa")
            .join("bbbbbbbbbb")
            .join("cccccccccc");
        std::fs::create_dir_all(&deep).unwrap();
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(Some(deep.clone()));
        let pid = app.active.unwrap();

        let mut term = Terminal::new(TestBackend::new(30, 8)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 30,
                    height: 8,
                },
            )
        })
        .unwrap();
        let buf = term.backend().buffer();
        let header: String = (0..30).map(|x| buf[(x, 0)].symbol()).collect();

        assert!(
            header.contains("cccccccccc"),
            "the current directory was elided away: {header:?}"
        );
        assert!(
            header.contains('\u{2026}'),
            "no ellipsis marking the dropped prefix: {header:?}"
        );
        // The ellipsis must still navigate somewhere, or narrowing the
        // pane silently removes navigation.
        assert!(
            app.rects.file_pane_breadcrumbs.len() >= 2,
            "narrow breadcrumb registered too few targets"
        );
    }

    /// #files item 2 — a mark has to be VISIBLE, and the footer has to
    /// say how many. A selection you cannot see is a way to delete the
    /// wrong thing.
    #[test]
    fn marked_rows_show_a_marker_and_a_footer_count() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        for n in ["one.txt", "two.txt"] {
            std::fs::write(d.path().join(n), "xx").unwrap();
        }
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(None);
        let pid = app.active.unwrap();

        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 10,
        };
        let render = |app: &mut crate::app::App| -> String {
            let mut term = Terminal::new(TestBackend::new(60, 10)).unwrap();
            term.draw(|f| draw(f, app, pid, area)).unwrap();
            let buf = term.backend().buffer();
            (0..10)
                .map(|y| (0..60).map(|x| buf[(x, y)].symbol()).collect::<String>())
                .collect::<Vec<_>>()
                .join("\n")
        };

        let before = render(&mut app);
        assert!(
            !before.contains("selected"),
            "footer showed with nothing marked — it costs a listing row:\n{before}"
        );

        if let Some(crate::pane::Pane::Files(f)) = app.panes.get_mut(pid) {
            f.selected = 0;
            f.toggle_mark();
        }
        let after = render(&mut app);
        assert!(
            after.contains("\u{258C}"),
            "no mark glyph on the marked row:\n{after}"
        );
        assert!(
            after.contains("1 selected"),
            "footer does not report the count:\n{after}"
        );
        assert!(
            after.contains("Esc clears"),
            "footer does not name the way out of the selection:\n{after}"
        );
    }

    /// #1229 f/u — the way out must be VISIBLE.
    ///
    /// User: "how do i go up a level i wnt into this folder and coudlnt
    /// get back up to mnml folder". Backspace / Left / h all worked
    /// (verified by probe), so this was purely discoverability: a file
    /// browser with no `..` row.
    #[test]
    fn a_parent_row_is_rendered_and_clickable() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::fs::create_dir(d.path().join("assets")).unwrap();
        std::fs::write(d.path().join("assets").join("demo.gif"), "x").unwrap();

        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(Some(d.path().join("assets")));
        let pid = app.active.unwrap();

        let mut term = Terminal::new(TestBackend::new(60, 8)).unwrap();
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 8,
        };
        term.draw(|f| draw(f, &mut app, pid, area)).unwrap();
        let screen: String = {
            let buf = term.backend().buffer();
            (0..8)
                .map(|y| (0..60).map(|x| buf[(x, y)].symbol()).collect::<String>())
                .collect::<Vec<_>>()
                .join("\n")
        };

        assert!(
            screen.contains(".."),
            "no `..` row — the only way up is an invisible keybinding:\n{screen}"
        );
        // And it must be a click target, not just paint.
        assert!(
            app.rects
                .file_pane_rows
                .iter()
                .any(|(_, p, idx)| *p == pid && *idx == PARENT_ROW),
            "the `..` row registered no click rect"
        );
        // The real entry rows must still be registered and NOT collide
        // with the sentinel.
        assert!(
            app.rects
                .file_pane_rows
                .iter()
                .any(|(_, p, idx)| *p == pid && *idx == 0),
            "entry rows lost their rects when the parent row was added"
        );
    }

    /// #files — the mouse tester's sharpest finding: marking is the
    /// pane's headline feature and had NO mouse path at all. Ctrl-click,
    /// shift-click, middle-click and clicking the green mark gutter were
    /// all dead, and the only guidance on screen was three chords in a
    /// footer that does not appear until something is already marked.
    ///
    /// These four tests drive the real `dispatch_mouse`, so they fail if
    /// the handler is reordered behind an earlier `return` — which is how
    /// the `+ dock` chip already shadows this pane's last row.
    fn marking_fixture() -> (tempfile::TempDir, crate::app::App, crate::layout::PaneId) {
        let d = tempfile::tempdir().unwrap();
        for n in ["a.txt", "b.txt", "c.txt", "d.txt"] {
            std::fs::write(d.path().join(n), "x").unwrap();
        }
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(Some(d.path().to_path_buf()));
        let pid = app.active.unwrap();
        (d, app, pid)
    }

    /// Render once so `file_pane_rows` is populated, then return the
    /// click point for listing row `idx`.
    fn row_point(app: &mut crate::app::App, pid: crate::layout::PaneId, idx: usize) -> (u16, u16) {
        // Full-chrome draw, not a pane-local one: the point is that the
        // click survives the real `PaneRects`, where other widgets'
        // hit-rects compete for the same cell.
        let mut term = Terminal::new(TestBackend::new(120, 40)).unwrap();
        term.draw(|f| crate::ui::draw(f, app)).unwrap();
        let (r, _, _) = *app
            .rects
            .file_pane_rows
            .iter()
            .find(|(_, p, i)| *p == pid && *i == idx)
            .unwrap_or_else(|| panic!("no click rect for row {idx}"));
        (r.x + 1, r.y)
    }

    fn click(app: &mut crate::app::App, at: (u16, u16), mods: KeyModifiers) {
        use ratatui::crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
        crate::tui::dispatch_mouse(
            app,
            MouseEvent {
                kind: MouseEventKind::Down(MouseButton::Left),
                column: at.0,
                row: at.1,
                modifiers: mods,
            },
        );
    }

    fn marked_names(app: &crate::app::App, pid: crate::layout::PaneId) -> Vec<String> {
        let Some(crate::pane::Pane::Files(f)) = app.panes.get(pid) else {
            panic!("not a Files pane");
        };
        let mut v: Vec<String> = f
            .marked
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
            .collect();
        v.sort();
        v
    }

    #[test]
    fn ctrl_click_toggles_a_mark() {
        let (_d, mut app, pid) = marking_fixture();
        let at = row_point(&mut app, pid, 1);

        click(&mut app, at, KeyModifiers::CONTROL);
        assert_eq!(
            marked_names(&app, pid).len(),
            1,
            "ctrl-click did not mark — marking is still keyboard-only"
        );

        // And it must TOGGLE, not just accumulate: a mouse user with no
        // way to unmark is worse off than before.
        let at = row_point(&mut app, pid, 1);
        click(&mut app, at, KeyModifiers::CONTROL);
        assert!(
            marked_names(&app, pid).is_empty(),
            "ctrl-click on a marked row did not unmark it: {:?}",
            marked_names(&app, pid)
        );
    }

    /// A plain click must still OPEN. If the modifier branch swallowed
    /// every click the pane would be unusable, and a test that only
    /// checked marking would not notice.
    #[test]
    fn a_plain_click_still_activates_and_marks_nothing() {
        let (_d, mut app, pid) = marking_fixture();
        let at = row_point(&mut app, pid, 1);
        click(&mut app, at, KeyModifiers::empty());
        assert!(
            marked_names(&app, pid).is_empty(),
            "an unmodified click marked a row: {:?}",
            marked_names(&app, pid)
        );
    }

    #[test]
    fn shift_click_extends_a_range_from_the_cursor() {
        let (_d, mut app, pid) = marking_fixture();
        // Put the cursor on row 0 the way a user would — by clicking it.
        let at = row_point(&mut app, pid, 0);
        click(&mut app, at, KeyModifiers::CONTROL);
        let at = row_point(&mut app, pid, 2);
        click(&mut app, at, KeyModifiers::SHIFT);

        let names = marked_names(&app, pid);
        assert_eq!(
            names.len(),
            3,
            "shift-click marked {} rows, not the 3 spanned: {names:?}",
            names.len()
        );
    }

    /// The other direction. The `lo`/`hi` swap is the only thing standing
    /// between an upward shift-click and an empty range, and the
    /// downward-only test above would not notice it going wrong.
    #[test]
    fn shift_click_above_the_anchor_spans_the_same_rows() {
        let (_d, mut app, pid) = marking_fixture();
        let at = row_point(&mut app, pid, 3);
        click(&mut app, at, KeyModifiers::CONTROL);
        let at = row_point(&mut app, pid, 1);
        click(&mut app, at, KeyModifiers::SHIFT);

        let names = marked_names(&app, pid);
        assert_eq!(
            names.len(),
            3,
            "an upward shift-click spanned {} rows, not 3: {names:?}",
            names.len()
        );
    }

    /// Review finding — the range used to only ever GROW, and the anchor
    /// drifted to wherever you last clicked, so shift-clicking a nearer
    /// row could not take the selection back. Finder, Explorer and VS Code
    /// all recompute from a stable anchor; a user who overshoots by one
    /// row otherwise has to clear and start again.
    #[test]
    fn a_second_shift_click_shrinks_the_range_instead_of_only_growing_it() {
        let (_d, mut app, pid) = marking_fixture();
        let at = row_point(&mut app, pid, 0);
        click(&mut app, at, KeyModifiers::CONTROL);
        let at = row_point(&mut app, pid, 3);
        click(&mut app, at, KeyModifiers::SHIFT);
        assert_eq!(marked_names(&app, pid).len(), 4, "setup: expected 0..=3");

        // Overshot by two — pull it back.
        let at = row_point(&mut app, pid, 1);
        click(&mut app, at, KeyModifiers::SHIFT);
        let names = marked_names(&app, pid);
        assert_eq!(
            names.len(),
            2,
            "shift-click could not shrink the range — still {} marked: {names:?}",
            names.len()
        );
    }

    /// A mark made deliberately, outside the shift range, must survive a
    /// later shift-click that spans it. Taking back "what the last
    /// shift-click added" must mean exactly that.
    #[test]
    fn shrinking_a_shift_range_keeps_marks_made_separately() {
        let (_d, mut app, pid) = marking_fixture();
        // Mark row 3 on its own first.
        let at = row_point(&mut app, pid, 3);
        click(&mut app, at, KeyModifiers::CONTROL);
        // Anchor at 0, sweep 0..=3 (which spans the separate mark), then
        // pull back to 0..=1.
        let at = row_point(&mut app, pid, 0);
        click(&mut app, at, KeyModifiers::CONTROL);
        let at = row_point(&mut app, pid, 3);
        click(&mut app, at, KeyModifiers::SHIFT);
        let at = row_point(&mut app, pid, 1);
        click(&mut app, at, KeyModifiers::SHIFT);

        let names = marked_names(&app, pid);
        assert!(
            names.iter().any(|n| n == "d.txt"),
            "shrinking the range swept away a mark the user made separately: {names:?}"
        );
    }

    /// Finding #6 — the right-click menu reused the tree's, which is
    /// path-based, so "mark five, right-click, Copy" copied exactly one
    /// file with no indication the other four were ignored.
    #[test]
    fn the_context_menu_acts_on_the_mark_set_when_the_clicked_row_is_in_it() {
        use ratatui::crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
        let (_d, mut app, pid) = marking_fixture();
        for idx in [0usize, 1, 2] {
            let at = row_point(&mut app, pid, idx);
            click(&mut app, at, KeyModifiers::CONTROL);
        }
        let at = row_point(&mut app, pid, 1);
        crate::tui::dispatch_mouse(
            &mut app,
            MouseEvent {
                kind: MouseEventKind::Down(MouseButton::Right),
                column: at.0,
                row: at.1,
                modifiers: KeyModifiers::empty(),
            },
        );
        let labels: Vec<String> = app
            .context_menu
            .as_ref()
            .expect("no context menu opened")
            .items
            .iter()
            .map(|i| i.label.clone())
            .collect();
        assert!(
            labels.iter().any(|l| l == "Copy 3 selected"),
            "menu does not offer the mark set — it would act on one row: {labels:?}"
        );
        assert!(
            labels.iter().any(|l| l == "Unmark"),
            "no mark toggle in the menu: {labels:?}"
        );

        // And choosing it must stage all three, not just the clicked row.
        let idx = labels.iter().position(|l| l == "Copy 3 selected").unwrap();
        let action = app.context_menu.as_ref().unwrap().items[idx].action.clone();
        app.run_menu_action(action);
        assert_eq!(
            app.file_clipboard.len(),
            3,
            "staged {} paths from a 3-file selection",
            app.file_clipboard.len()
        );
    }

    /// The other half of the same rule: right-clicking a row you did NOT
    /// mark is you pointing at that row. Silently acting on a set aimed
    /// elsewhere is the bug, not a feature to generalise.
    #[test]
    fn the_context_menu_ignores_marks_when_the_clicked_row_is_not_one() {
        use ratatui::crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
        let (_d, mut app, pid) = marking_fixture();
        let at = row_point(&mut app, pid, 0);
        click(&mut app, at, KeyModifiers::CONTROL);
        let at = row_point(&mut app, pid, 2);
        crate::tui::dispatch_mouse(
            &mut app,
            MouseEvent {
                kind: MouseEventKind::Down(MouseButton::Right),
                column: at.0,
                row: at.1,
                modifiers: KeyModifiers::empty(),
            },
        );
        let labels: Vec<String> = app
            .context_menu
            .as_ref()
            .expect("no context menu opened")
            .items
            .iter()
            .map(|i| i.label.clone())
            .collect();
        assert!(
            !labels.iter().any(|l| l.ends_with("selected")),
            "offered the mark set on a row that is not marked: {labels:?}"
        );
        assert!(
            labels.iter().any(|l| l == "Mark"),
            "no way to add this row to the set: {labels:?}"
        );
    }

    /// Review finding (critical) — an EMPTY directory used to return
    /// before the `..` row was drawn, so browsing into one left a pane
    /// with no visible way out. That is the exact bug the row exists to
    /// fix, reintroduced for its most likely trigger: a directory you
    /// just created, or one filtered to nothing.
    #[test]
    fn an_empty_directory_still_shows_the_parent_row() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let empty = d.path().join("empty");
        std::fs::create_dir(&empty).unwrap();
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(Some(empty));
        let pid = app.active.unwrap();

        let mut term = Terminal::new(TestBackend::new(60, 8)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 60,
                    height: 8,
                },
            )
        })
        .unwrap();
        let buf = term.backend().buffer();
        let screen: String = (0..8)
            .map(|y| (0..60).map(|x| buf[(x, y)].symbol()).collect::<String>())
            .collect::<Vec<_>>()
            .join("\n");

        assert!(
            screen.contains("(empty directory)"),
            "empty state missing:\n{screen}"
        );
        assert!(
            screen.contains(".."),
            "no `..` row in an empty directory — the user is stuck:\n{screen}"
        );
        assert!(
            app.rects
                .file_pane_rows
                .iter()
                .any(|(_, p, idx)| *p == pid && *idx == PARENT_ROW),
            "the `..` row has no click rect in an empty directory"
        );
    }

    /// Same for an unreadable directory — the error must not cost the way
    /// out either.
    #[test]
    fn an_unreadable_directory_still_shows_the_parent_row() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.config.ui.ascii_icons = true;
        app.open_files_pane(Some(d.path().join("does-not-exist")));
        let pid = app.active.unwrap();

        let mut term = Terminal::new(TestBackend::new(60, 8)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 60,
                    height: 8,
                },
            )
        })
        .unwrap();
        assert!(
            app.rects
                .file_pane_rows
                .iter()
                .any(|(_, p, idx)| *p == pid && *idx == PARENT_ROW),
            "no `..` row when the directory could not be read"
        );
    }

    /// The root of the filesystem has no parent, so no row — otherwise it
    /// is a dead click.
    #[test]
    fn the_filesystem_root_shows_no_parent_row() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.open_files_pane(Some(std::path::PathBuf::from("/")));
        let pid = app.active.unwrap();
        let mut term = Terminal::new(TestBackend::new(60, 8)).unwrap();
        term.draw(|f| {
            draw(
                f,
                &mut app,
                pid,
                Rect {
                    x: 0,
                    y: 0,
                    width: 60,
                    height: 8,
                },
            )
        })
        .unwrap();
        assert!(
            !app.rects
                .file_pane_rows
                .iter()
                .any(|(_, p, idx)| *p == pid && *idx == PARENT_ROW),
            "`/` has no parent, so a `..` row there would be a dead click"
        );
    }

    /// Enter on a directory descends; Enter on a file opens it as an
    /// editor pane rather than descending.
    #[test]
    fn activate_descends_a_directory_and_opens_a_file() {
        let d = tempfile::tempdir().unwrap();
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::fs::create_dir(d.path().join("subdir")).unwrap();
        std::fs::write(d.path().join("subdir").join("inner.txt"), "x").unwrap();

        let mut app =
            crate::app::App::new(d.path().to_path_buf(), crate::config::Config::default()).unwrap();
        app.open_files_pane(None);
        let pid = app.active.unwrap();

        // Cursor is on `subdir` (the only entry) — activate descends.
        app.files_pane_activate(pid);
        let cwd = match app.panes.get(pid) {
            Some(crate::pane::Pane::Files(f)) => f.cwd.clone(),
            _ => panic!("pane is no longer a Files pane"),
        };
        // Canonicalise BOTH sides: `App::new` canonicalises the
        // workspace, so on macOS the pane's cwd is `/private/var/...`
        // while `tempdir()` hands back `/var/...`. Comparing raw paths
        // reported "did not descend" when the descent had worked
        // perfectly.
        let want = d.path().join("subdir").canonicalize().unwrap();
        assert_eq!(
            cwd.canonicalize().unwrap(),
            want,
            "did not descend into subdir"
        );

        // Now the cursor is on `inner.txt` — activate opens it.
        let panes_before = app.panes.len();
        app.files_pane_activate(pid);
        assert!(
            app.panes.len() > panes_before,
            "activating a file opened no editor pane"
        );
        assert!(
            app.panes.iter().any(|p| match p {
                crate::pane::Pane::Editor(b) =>
                    b.path.as_ref().and_then(|p| p.canonicalize().ok())
                        == d.path()
                            .join("subdir")
                            .join("inner.txt")
                            .canonicalize()
                            .ok(),
                _ => false,
            }),
            "the opened pane is not an editor on inner.txt"
        );
    }
}