konoma 0.23.0

Terminal file browser built for AI pair-programming — full-screen previews (Markdown, images, PDF, CSV), git suite, and an agent-watch mode that follows your AI's edits (macOS and Linux)
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
//! File-manager actions: create / rename / delete (trash) and multi-select / visual range — methods on `App`.

use super::*;

impl App {
    // --- File operations: create/rename/delete (M7 Phase B, confirm + trash) -------------
    /// Whether a confirm/input dialog is showing (while true, main intercepts keys).
    pub fn is_dialog(&self) -> bool {
        self.dialog.is_some()
    }
    /// Whether it is a confirm (y/n) dialog (false = text input dialog). For render/key branching.
    pub fn dialog_is_confirm(&self) -> bool {
        matches!(
            self.dialog.as_ref().map(|d| &d.kind),
            Some(DialogKind::Confirm { .. })
        )
    }
    /// Render view for the dialog: (is_confirm, heading/message, text being entered, cursor character position).
    /// Preview is rendered via a separate path (`dialog_preview_view`), so this returns None for it.
    pub fn dialog_view(&self) -> Option<(bool, &str, &str, usize)> {
        match &self.dialog.as_ref()?.kind {
            DialogKind::Confirm { message, .. } => Some((true, message.as_str(), "", 0)),
            DialogKind::Input {
                title,
                buffer,
                cursor,
            } => Some((false, title.as_str(), buffer.as_str(), *cursor)),
            DialogKind::Preview { .. } => None,
        }
    }
    /// Whether the batch-rename preview is showing (keys: y apply / Esc cancel / j k scroll).
    pub fn dialog_is_preview(&self) -> bool {
        matches!(
            self.dialog.as_ref().map(|d| &d.kind),
            Some(DialogKind::Preview { .. })
        )
    }
    /// For rendering the preview: (heading, the "old → new" list, first displayed row).
    pub fn dialog_preview_view(&self) -> Option<(&str, &[String], usize)> {
        match &self.dialog.as_ref()?.kind {
            DialogKind::Preview {
                title,
                lines,
                scroll,
            } => Some((title.as_str(), lines.as_slice(), *scroll)),
            _ => None,
        }
    }
    /// Scroll the preview up/down.
    pub fn dialog_preview_scroll(&mut self, delta: i32) {
        if let Some(Dialog {
            kind: DialogKind::Preview { lines, scroll, .. },
            ..
        }) = self.dialog.as_mut()
        {
            let max = lines.len().saturating_sub(1) as i32;
            *scroll = (*scroll as i32 + delta).clamp(0, max) as usize;
        }
    }
    /// Whether the confirm dialog offers `!`=permanent delete (unrecoverable) (true only for delete confirmation).
    pub fn dialog_allow_permanent(&self) -> bool {
        matches!(
            self.dialog.as_ref().map(|d| &d.kind),
            Some(DialogKind::Confirm {
                allow_permanent: true,
                ..
            })
        )
    }
    /// Whether the current confirm dialog is a branch deletion (so the y/!/n wording is for a branch, not a file deletion).
    pub fn confirm_is_branch_delete(&self) -> bool {
        matches!(
            self.dialog.as_ref().map(|d| &d.op),
            Some(PendingOp::GitDeleteBranch { .. })
        )
    }
    /// Whether the current confirm dialog is a drag-and-drop transfer (so the keys/wording become c=copy / m=move).
    pub fn confirm_is_drop(&self) -> bool {
        matches!(
            self.dialog.as_ref().map(|d| &d.op),
            Some(PendingOp::DropTransfer { .. })
        )
    }
    /// Whether the current confirm dialog is the app-quit confirmation (so `q`/`y`/Enter quit and the chip/footer say "quit").
    pub fn confirm_is_quit(&self) -> bool {
        matches!(self.dialog.as_ref().map(|d| &d.op), Some(PendingOp::Quit))
    }
    /// Whether the current confirm dialog is a bookmark-overwrite confirmation (own chip/footer wording).
    pub fn confirm_is_bookmark(&self) -> bool {
        matches!(
            self.dialog.as_ref().map(|d| &d.op),
            Some(PendingOp::BookmarkOverwrite { .. })
        )
    }

    /// Receive a paste from the terminal (including drag-and-drop). While input is active (dialog input / filter /
    /// search / branch filter), **insert text**; in Tree mode, if the dropped content is an existing path,
    /// open a **copy/move dialog** (drop target = the cursor's base directory). Control characters are stripped.
    pub fn handle_paste(&mut self, text: String) {
        // 1) While text input is active: feed it straight into the input field (control
        // characters are dropped).
        if self.is_text_input_active() {
            for ch in text.chars().filter(|c| !c.is_control()) {
                self.input_push_char(ch);
            }
            return;
        }
        // 2) Only Tree mode treats this as a "drop" (ignored in Preview, etc.).
        if !matches!(self.tab.mode, Mode::Tree) {
            return;
        }
        let sources = parse_dropped_paths(&text);
        if sources.is_empty() {
            return; // do nothing if there's no drop target (an existing path)
        }
        let dir = self.op_base_dir();
        let message = format!(
            "{} {}{}",
            sources.len(),
            crate::i18n::tr(self.lang, crate::i18n::Msg::DroppedItems),
            self.format_path(&dir)
        );
        self.dialog = Some(Dialog {
            op: PendingOp::DropTransfer { sources, dir },
            kind: DialogKind::Confirm {
                message,
                allow_permanent: false,
            },
        });
    }

    /// Whether text input is active (a state where a paste should be inserted as characters).
    fn is_text_input_active(&self) -> bool {
        self.dialog_is_text_input()
            || self.is_filtering()
            || self.is_searching()
            || self.git_branch_filtering()
    }
    /// Whether the currently open dialog is the Input (text input) kind.
    fn dialog_is_text_input(&self) -> bool {
        matches!(
            self.dialog.as_ref().map(|d| &d.kind),
            Some(DialogKind::Input { .. })
        )
    }
    /// Feed one character to the active input field (priority: dialog input > filter > search > branch filter).
    fn input_push_char(&mut self, ch: char) {
        if self.dialog_is_text_input() {
            self.dialog_input_push(ch);
        } else if self.is_filtering() {
            self.filter_input_push(ch);
        } else if self.is_searching() {
            self.search_input_push(ch);
        } else if self.git_branch_filtering() {
            self.git_branch_filter_push(ch);
        }
    }

    /// Execute the drop confirmation's `c`=copy / `m`=move. Transfers each source to the drop target and flashes the result.
    /// Runs in the background (design principle #4) — see `start_file_op`. Stops at the first
    /// error (unlike paste/duplicate), matching the original synchronous implementation.
    pub fn drop_apply(&mut self, move_it: bool) -> Result<()> {
        let Some(dialog) = self.dialog.take() else {
            return Ok(());
        };
        let PendingOp::DropTransfer { sources, dir } = dialog.op else {
            return Ok(());
        };
        let job = FileOpJob {
            kind: if move_it {
                FileOpKind::DropMove
            } else {
                FileOpKind::DropCopy
            },
            targets: sources,
            dest: Some(dir),
            root: PathBuf::new(), // filled in by start_file_op with tab.root at dispatch time
            err_self_paste: String::new(), // Drop doesn't use a dedicated self-paste message (relies on copy_dir_all's generic error = existing behavior)
            err_failed: crate::i18n::tr(self.lang, crate::i18n::Msg::OperationFailed).to_string(),
        };
        self.start_file_op(job);
        Ok(())
    }

    /// Attach the Sender of the worker that runs background filesystem operations (called by main at startup).
    pub fn attach_fileop_runner(&mut self, tx: std::sync::mpsc::Sender<crate::app::FileOpResult>) {
        self.fileop_tx = Some(tx);
    }

    /// Whether watcher-driven refreshes should be held back right now.
    ///
    /// A background copy/move/delete of a large directory makes konoma's own watcher fire tens of
    /// thousands of events; refreshing per burst starves the run loop so keystrokes are not processed
    /// until the storm ends. While the operation runs, the run loop accumulates the bursts and applies
    /// **one** refresh afterwards — nothing is missed, because `apply_file_op` refreshes on completion
    /// and the deferred burst is flushed right after it. Same idea as `is_content_event`/the `.git`
    /// lock filter: never react to filesystem noise konoma itself caused.
    pub fn should_defer_fs_events(&self) -> bool {
        self.fileop_pending.is_some()
    }

    /// Start a filesystem operation in the background, or run it synchronously when no runner is
    /// attached (unit tests / no run loop) so the result is observable immediately (same contract
    /// as `spawn_or_sync_statuses`/`spawn_or_sync_ignored`). Returns false and flashes when another
    /// operation is already in flight (only one runs at a time).
    pub(super) fn start_file_op(&mut self, mut job: FileOpJob) -> bool {
        if self.fileop_pending.is_some() {
            self.flash = Some(crate::i18n::tr(self.lang, crate::i18n::Msg::FileOpBusy).into());
            return false;
        }
        // Bake in "which tab this was dispatched from" here at dispatch time (the caller passes
        // it empty). The active tab when it completes may not be the same one, so apply_file_op
        // uses this to decide.
        job.root = self.tab.root.clone();
        self.fileop_gen = self.fileop_gen.wrapping_add(1);
        let gen = self.fileop_gen;
        self.fileop_pending = Some(job.kind);
        self.fileop_total = job.targets.len();
        let progress = Arc::new(crate::fileops::Progress::default());
        self.fileop_progress = Some(progress.clone());

        let Some(tx) = self.fileop_tx.clone() else {
            let res = Self::run_file_op(gen, job, &progress);
            self.apply_file_op(res);
            return true;
        };
        let kind = job.kind;
        let root = job.root.clone();
        // Translate the fallback panic error using self.lang here (on the UI thread) ahead of
        // time (the worker thread has no &self, so it cannot call tr()).
        let panic_err = crate::i18n::tr(self.lang, crate::i18n::Msg::OperationFailed).to_string();
        std::thread::spawn(move || {
            // If the worker panics, no result comes back and `fileop_pending` stays set forever,
            // leaving the spinner spinning. Use the same safety net as the other workers to
            // always return a result (design principle #3).
            let res =
                crate::preview::markdown::catch_silent(|| Self::run_file_op(gen, job, &progress))
                    .unwrap_or(FileOpResult {
                        gen,
                        kind,
                        root,
                        ok: 0,
                        last: None,
                        err: Some(panic_err),
                    });
            let _ = tx.send(res);
        });
        true
    }

    /// The actual work (pure: no `&self`), shared by the worker thread and the synchronous fallback.
    /// `PasteCopy`/`PasteMove`/`Duplicate` continue past a failing target (existing behaviour);
    /// `DropCopy`/`DropMove` stop at the first error (existing `drop_apply` behaviour).
    fn run_file_op(gen: u64, job: FileOpJob, p: &crate::fileops::Progress) -> FileOpResult {
        let FileOpJob {
            kind,
            targets,
            dest,
            root,
            err_self_paste,
            err_failed,
        } = job;
        let (mut ok, mut last, mut err) = (0usize, None, None);
        match kind {
            FileOpKind::PasteCopy | FileOpKind::PasteMove => {
                let Some(dest) = dest else {
                    return FileOpResult {
                        gen,
                        kind,
                        root,
                        ok,
                        last,
                        err: Some(err_failed),
                    };
                };
                for src in &targets {
                    // Reject pasting into itself (or inside itself), since that would become an
                    // infinite copy.
                    if dest.starts_with(src) {
                        err = Some(err_self_paste.clone());
                        p.bump_item();
                        continue;
                    }
                    let res = if kind == FileOpKind::PasteCopy {
                        crate::fileops::copy_into_with_progress(&dest, src, p)
                    } else {
                        crate::fileops::move_into_with_progress(&dest, src, p)
                    };
                    match res {
                        Ok(path) => {
                            ok += 1;
                            last = Some(path);
                        }
                        Err(e) => err = Some(e.to_string()),
                    }
                    p.bump_item();
                }
            }
            FileOpKind::DropCopy | FileOpKind::DropMove => {
                let Some(dest) = dest else {
                    return FileOpResult {
                        gen,
                        kind,
                        root,
                        ok,
                        last,
                        err: Some(err_failed),
                    };
                };
                for src in &targets {
                    let res = if kind == FileOpKind::DropCopy {
                        crate::fileops::copy_into_with_progress(&dest, src, p)
                    } else {
                        crate::fileops::move_into_with_progress(&dest, src, p)
                    };
                    match res {
                        Ok(path) => {
                            ok += 1;
                            last = Some(path);
                            p.bump_item();
                        }
                        Err(e) => {
                            err = Some(e.to_string());
                            break; // a drop stops at the first failure (same as the existing drop_apply)
                        }
                    }
                }
            }
            FileOpKind::Duplicate => {
                for src in &targets {
                    // Parent directory = the duplication target (duplicate in place). If there's
                    // no parent (e.g. root), treat it as a failure (no crash).
                    let Some(parent) = src.parent() else {
                        err = Some(err_failed.clone());
                        p.bump_item();
                        continue;
                    };
                    match crate::fileops::copy_into_with_progress(parent, src, p) {
                        Ok(path) => {
                            ok += 1;
                            last = Some(path);
                        }
                        Err(e) => err = Some(e.to_string()),
                    }
                    p.bump_item();
                }
            }
            // Sending to trash is **a single call** to `trash::delete_all`, so no progress can be
            // read mid-flight. It only advances by the full count once done (progress stays at
            // 0/M until it jumps to M/M at the end).
            FileOpKind::Trash => match crate::fileops::move_to_trash(&targets) {
                Ok(()) => {
                    ok = targets.len();
                    for _ in 0..ok {
                        p.bump_item();
                    }
                }
                Err(e) => err = Some(e.to_string()),
            },
            // Permanent deletion loops per path, so progress can advance one item at a time
            // (`_with_progress`).
            FileOpKind::DeletePermanent => {
                match crate::fileops::delete_permanently_with_progress(&targets, p) {
                    Ok(()) => ok = targets.len(),
                    Err(e) => err = Some(e.to_string()),
                }
            }
        }
        FileOpResult {
            gen,
            kind,
            root,
            ok,
            last,
            err,
        }
    }

    /// Apply a finished filesystem operation: refresh the tree, reveal the newest item and flash the
    /// same summary the synchronous implementation used to produce.
    ///
    /// The generation check is belt-and-braces only: `fileop_gen` advances solely in
    /// `start_file_op`, which refuses to start while `fileop_pending` is set, so the generation is
    /// frozen for as long as an operation is in flight and a stale result cannot actually occur
    /// today. The guard that matters is the **root comparison**: the user can switch tabs during a
    /// long copy, so the tab active when the result arrives may not be the one that dispatched it.
    /// The tree cursor is only touched when the roots match.
    pub fn apply_file_op(&mut self, res: FileOpResult) -> bool {
        if res.gen != self.fileop_gen {
            return false;
        }
        self.fileop_pending = None;
        self.fileop_progress = None;
        self.fileop_total = 0;
        // Reload unconditionally (the disk has actually changed regardless of which tab is being
        // viewed).
        let mut post = self.refresh();
        // Reveal (= move the cursor) only **when we're back on the tab that dispatched it**.
        // `reveal_and_select` unconditionally calls rebuild_tree, so doing it on another tab
        // would collapse its filter (`/`) or changed view (`C`) and even move that tab's cursor.
        // Compare by root, not tab number (tabs can be closed or reordered mid-operation, so a
        // number is not grounds for identity).
        if post.is_ok() && res.root == self.tab.root {
            if let Some(p) = &res.last {
                post = self.reveal_and_select(p);
            }
        }
        // Clear the selection for delete-type operations **only on success** (same as the old
        // synchronous version). If a partial failure — such as a permission error on the 3rd of
        // 12 items — also wiped the selection, the user would have to start over from scratch.
        if res.err.is_none() && matches!(res.kind, FileOpKind::Trash | FileOpKind::DeletePermanent)
        {
            self.clear_selection();
        }
        let lang = self.lang;
        // If the operation itself succeeded but a later step (reload/reveal) fails, surface that
        // failure. The old synchronous version propagated `refresh()?` / `reveal_and_select(p)?`
        // up to handle_key, where `resolve_key_result` flashed it — so don't paper over it with a
        // success flash (principle: don't call something a success unverified).
        // If the operation itself failed, that error is the real story, so leave it to the branch
        // below.
        if res.err.is_none() {
            if let Err(e) = post {
                self.flash = Some(format!(
                    "{}: {e}",
                    crate::i18n::tr(lang, crate::i18n::Msg::Failed)
                ));
                return true;
            }
        }
        self.flash = Some(match res.kind {
            FileOpKind::PasteCopy | FileOpKind::PasteMove => match &res.err {
                Some(e) => format!(
                    "{} {} / {}: {e}",
                    crate::i18n::tr(lang, crate::i18n::Msg::Pasted),
                    res.ok,
                    crate::i18n::tr(lang, crate::i18n::Msg::Failed),
                ),
                None => format!(
                    "{} ({})",
                    crate::i18n::tr(lang, crate::i18n::Msg::Pasted),
                    res.ok
                ),
            },
            FileOpKind::Duplicate => match &res.err {
                Some(e) => format!(
                    "{} {} / {}: {e}",
                    crate::i18n::tr(lang, crate::i18n::Msg::Duplicated),
                    res.ok,
                    crate::i18n::tr(lang, crate::i18n::Msg::Failed)
                ),
                None => format!(
                    "{} ({})",
                    crate::i18n::tr(lang, crate::i18n::Msg::Duplicated),
                    res.ok
                ),
            },
            FileOpKind::DropCopy | FileOpKind::DropMove => {
                let verb = if res.kind == FileOpKind::DropMove {
                    crate::i18n::tr(lang, crate::i18n::Msg::Moved)
                } else {
                    crate::i18n::tr(lang, crate::i18n::Msg::Copied)
                };
                match &res.err {
                    Some(e) => format!("{}: {e}", crate::i18n::tr(lang, crate::i18n::Msg::Failed)),
                    None => format!("{verb} ({})", res.ok),
                }
            }
            FileOpKind::Trash => match &res.err {
                Some(e) => format!("{}: {e}", crate::i18n::tr(lang, crate::i18n::Msg::Failed)),
                None => format!(
                    "{} ({})",
                    crate::i18n::tr(lang, crate::i18n::Msg::MovedToTrash),
                    res.ok
                ),
            },
            FileOpKind::DeletePermanent => match &res.err {
                Some(e) => format!("{}: {e}", crate::i18n::tr(lang, crate::i18n::Msg::Failed)),
                None => format!(
                    "{} ({})",
                    crate::i18n::tr(lang, crate::i18n::Msg::DeletedPermanently),
                    res.ok
                ),
            },
        });
        true
    }

    /// `N/M` (targets finished) plus the running leaf-file count once it exceeds the target count,
    /// e.g. `2/3` or `1/1 · 3120`. `None` when no operation is running.
    pub fn fileop_progress_text(&self) -> Option<String> {
        let p = self.fileop_progress.as_ref()?;
        let items = p.items();
        let files = p.files();
        let total = self.fileop_total;
        let mut s = format!("{items}/{total}");
        if files > total {
            s.push_str(&format!(" · {files}"));
        }
        Some(s)
    }

    /// The operation target directory relative to the cursor (selection is a directory = inside it / a file = its parent / none = root).
    pub(super) fn op_base_dir(&self) -> PathBuf {
        match self.tab.entries.get(self.tab.selected) {
            Some(e) if e.is_dir => e.path.clone(),
            Some(e) => e
                .path
                .parent()
                .map(|p| p.to_path_buf())
                .unwrap_or_else(|| self.tab.root.clone()),
            None => self.tab.root.clone(),
        }
    }

    /// Deep reveal: expand **every** collapsed ancestor of `target` under root (rebuilding as each level
    /// appears), then select it. Unlike `reveal_and_select` (one level), this reaches into collapsed
    /// subtrees — used by the changed-file jump (`n`/`N`) and follow mode. Returns whether the target
    /// became visible and selected (false = e.g. hidden by the dotfile filter).
    pub(super) fn reveal_path_deep(&mut self, target: &Path) -> Result<bool> {
        // Shallow-to-deep, from directly under root to target's parent. Setting expanded and
        // rebuilding reveals the next level.
        let mut ancestors: Vec<PathBuf> = Vec::new();
        let mut p = target.parent();
        while let Some(a) = p {
            if a == self.tab.root || !a.starts_with(&self.tab.root) {
                break;
            }
            ancestors.push(a.to_path_buf());
            p = a.parent();
        }
        ancestors.reverse();
        for anc in ancestors {
            if let Some(e) = self.tab.entries.iter_mut().find(|e| e.path == anc) {
                if e.is_dir && !e.expanded {
                    e.expanded = true;
                    self.rebuild_tree()?;
                }
            }
        }
        if let Some(i) = self.tab.entries.iter().position(|e| e.path == target) {
            self.tab.selected = i;
            return Ok(true);
        }
        Ok(false)
    }

    /// After an operation, reveal and select `target`. If the parent is a collapsed dir, expand it before rebuilding.
    pub(super) fn reveal_and_select(&mut self, target: &Path) -> Result<()> {
        if let Some(parent) = target.parent() {
            if let Some(e) = self.tab.entries.iter_mut().find(|e| e.path == parent) {
                if e.is_dir && !e.expanded {
                    e.expanded = true;
                }
            }
        }
        self.rebuild_tree()?;
        if let Some(i) = self.tab.entries.iter().position(|e| e.path == target) {
            self.tab.selected = i;
        }
        Ok(())
    }

    // --- Multi-select + visual (range) selection (M7 Phase B) ------------------------
    /// Whether there are any selected items (the committed set).
    pub fn has_selection(&self) -> bool {
        !self.tab.selection.is_empty()
    }
    /// Whether `path` is in the committed selection (for the render's marker check).
    pub fn is_selected(&self, path: &Path) -> bool {
        self.tab.selection.contains(path)
    }
    /// Clear the entire selection (Esc / after a batch operation). If in visual mode, also clears the range.
    pub fn clear_selection(&mut self) {
        self.tab.selection.clear();
        self.tab.visual_anchor = None;
    }
    /// `V`=toggle the selection of the single item at the cursor and move down one (for picking scattered items / consecutive selection).
    pub fn toggle_select(&mut self) {
        if let Some(e) = self.tab.entries.get(self.tab.selected) {
            let p = e.path.clone();
            if !self.tab.selection.remove(&p) {
                self.tab.selection.insert(p);
            }
            self.tree_next();
        }
    }

    /// Whether visual (range) selection mode is active.
    pub fn is_visual(&self) -> bool {
        self.tab.visual_anchor.is_some()
    }
    /// `v`=start visual mode. Places the anchor at the current cursor (does nothing on an empty tree).
    pub fn enter_visual(&mut self) {
        if !self.tab.entries.is_empty() {
            self.tab.visual_anchor = Some(self.tab.selected);
        }
    }
    /// The visual range [lo, hi] (anchor to cursor, ascending). None if not in visual mode.
    fn visual_bounds(&self) -> Option<(usize, usize)> {
        self.tab
            .visual_anchor
            .map(|a| (a.min(self.tab.selected), a.max(self.tab.selected)))
    }
    /// Whether row `idx` is within the visual range (for the render's live preview).
    pub fn is_in_visual_range(&self, idx: usize) -> bool {
        matches!(self.visual_bounds(), Some((lo, hi)) if idx >= lo && idx <= hi)
    }
    /// Commit the range with `v`/Esc etc.: add the paths in range to the selection set and leave visual mode.
    pub fn exit_visual_commit(&mut self) {
        if let Some((lo, hi)) = self.visual_bounds() {
            let paths: Vec<PathBuf> = (lo..=hi)
                .filter_map(|i| self.tab.entries.get(i).map(|e| e.path.clone()))
                .collect();
            for p in paths {
                self.tab.selection.insert(p);
            }
        }
        self.tab.visual_anchor = None;
    }
    /// Esc=leave visual mode without taking in the range (keeps the committed selection).
    pub fn exit_visual_cancel(&mut self) {
        self.tab.visual_anchor = None;
    }
    /// `a`/`A` during visual mode = scope bulk selection. `all_displayed`=everything displayed / otherwise=the same parent level as the cursor.
    /// Also takes in the in-progress range, adds it to the selection, and leaves visual mode.
    pub fn visual_select_scope(&mut self, all_displayed: bool) {
        // Commit the in-progress range.
        let mut paths: Vec<PathBuf> = self
            .visual_bounds()
            .map(|(lo, hi)| {
                (lo..=hi)
                    .filter_map(|i| self.tab.entries.get(i).map(|e| e.path.clone()))
                    .collect()
            })
            .unwrap_or_default();
        // Add the scope (everything displayed, or the same parent as the cursor).
        let parent = self
            .tab
            .entries
            .get(self.tab.selected)
            .and_then(|e| e.path.parent().map(|p| p.to_path_buf()));
        for e in &self.tab.entries {
            let same_parent = e.path.parent().map(|p| p.to_path_buf()) == parent;
            if all_displayed || same_parent {
                paths.push(e.path.clone());
            }
        }
        for p in paths {
            self.tab.selection.insert(p);
        }
        self.tab.visual_anchor = None;
    }
    /// Whether to render the marker column (leftmost 2 cells): there is a committed selection, or visual mode is active.
    pub fn show_selection_gutter(&self) -> bool {
        !self.tab.selection.is_empty() || self.is_visual()
    }
    /// The count currently marked (committed ∪ visual range). For the context's `sel: N` display.
    pub fn marked_count(&self) -> usize {
        match self.visual_bounds() {
            Some((lo, hi)) => {
                let mut n = self.tab.selection.len();
                for i in lo..=hi {
                    if let Some(e) = self.tab.entries.get(i) {
                        if !self.tab.selection.contains(&e.path) {
                            n += 1;
                        }
                    }
                }
                n
            }
            None => self.tab.selection.len(),
        }
    }
    /// Target paths for a batch operation: if there is a selection, **all selected items** (path ascending); otherwise, the single item at the cursor.
    pub(super) fn op_targets(&self) -> Vec<PathBuf> {
        if self.tab.selection.is_empty() {
            self.tab
                .entries
                .get(self.tab.selected)
                .map(|e| vec![e.path.clone()])
                .unwrap_or_default()
        } else {
            self.tab.selection.iter().cloned().collect()
        }
    }
}