tui-file-explorer 4.0.1

Embeddable keyboard-driven multi-pane file explorer for Ratatui
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
//! Application-level features built on the panes: undo, bookmarks, the fuzzy
//! finder, bulk rename, archive extraction and the git column.
//!
//! Everything slow runs on a worker thread and is collected by
//! [`App::poll_background`], which `draw` calls once per frame.

use std::collections::VecDeque;

use super::*;
use crate::{
    bulk_rename,
    filesystem::FileSystem,
    picker::{Picker, PickerAction, PickerItem, PickerKind},
    worker::Task,
};

/// Maximum number of undoable steps kept.
const UNDO_LIMIT: usize = 100;

// ── Undo ──────────────────────────────────────────────────────────────────────

/// One reversible user action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UndoEntry {
    /// `from` was renamed to `to`.
    Rename {
        /// The old path.
        from: PathBuf,
        /// The new path.
        to: PathBuf,
    },
    /// A file or folder was created at `path`.
    Create {
        /// The new file or folder.
        path: PathBuf,
        /// `true` for a folder.
        is_dir: bool,
    },
    /// Items were moved: each `(from, to)`.
    Move {
        /// Old and new path of each moved item.
        pairs: Vec<(PathBuf, PathBuf)>,
    },
    /// Items were copied: each `(source, new_copy)`.
    Copy {
        /// Source and new copy of each copied item.
        pairs: Vec<(PathBuf, PathBuf)>,
    },
    /// Items were deleted *to the trash* (restorable only on some platforms).
    Delete {
        /// The deleted items.
        paths: Vec<PathBuf>,
    },
    /// `archive` was extracted into the new folder `dest`.
    Extract {
        /// The archive that was extracted.
        archive: PathBuf,
        /// The folder it was extracted into.
        dest: PathBuf,
    },
    /// Several entries undone together (bulk rename).
    Batch(Vec<UndoEntry>),
}

impl UndoEntry {
    /// Short description for the status line.
    #[must_use]
    pub fn describe(&self) -> String {
        let name = |p: &Path| {
            p.file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .into_owned()
        };
        match self {
            Self::Rename { from, to } => format!("rename {} → {}", name(from), name(to)),
            Self::Create { path, .. } => format!("create {}", name(path)),
            Self::Move { pairs } => format!("move of {} item(s)", pairs.len()),
            Self::Copy { pairs } => format!("copy of {} item(s)", pairs.len()),
            Self::Delete { paths } => format!("delete of {} item(s)", paths.len()),
            Self::Extract { dest, .. } => format!("extract into {}", name(dest)),
            Self::Batch(items) => format!("{} change(s)", items.len()),
        }
    }
}

/// Reverse `entry` on `fs`. Returns the number of items restored.
fn apply_undo(fs: &dyn FileSystem, entry: &UndoEntry) -> Result<usize, String> {
    let remove = |p: &Path| {
        if fs.is_dir(p) {
            fs.remove_dir_all(p)
        } else {
            fs.remove_file(p)
        }
    };
    match entry {
        UndoEntry::Rename { from, to } => {
            if !fs.exists(to) {
                return Err(format!("'{}' no longer exists", to.display()));
            }
            if fs.exists(from) {
                return Err(format!("'{}' already exists", from.display()));
            }
            fs.rename(to, from).map_err(|e| e.to_string())?;
            Ok(1)
        }
        UndoEntry::Create { path, is_dir } => {
            if !fs.exists(path) {
                return Err(format!("'{}' no longer exists", path.display()));
            }
            // Only remove what is still the empty thing we created.
            let parent = path.parent().unwrap_or_else(|| Path::new(""));
            let empty = if *is_dir {
                fs.read_dir(path).map_err(|e| e.to_string())?.is_empty()
            } else {
                fs.read_dir(parent)
                    .map_err(|e| e.to_string())?
                    .iter()
                    .any(|e| e.path == *path && e.size == Some(0))
            };
            if !empty {
                return Err(format!(
                    "'{}' is no longer empty — left in place",
                    path.display()
                ));
            }
            remove(path).map_err(|e| e.to_string())?;
            Ok(1)
        }
        UndoEntry::Move { pairs } => {
            let mut done = 0;
            for (from, to) in pairs.iter().rev() {
                if !fs.exists(to) {
                    return Err(format!("'{}' no longer exists", to.display()));
                }
                if fs.exists(from) {
                    return Err(format!("'{}' already exists", from.display()));
                }
                let copied = if fs.is_dir(to) {
                    fs.copy_dir(to, from)
                } else {
                    fs.copy_file(to, from)
                };
                copied.map_err(|e| e.to_string())?;
                remove(to).map_err(|e| e.to_string())?;
                done += 1;
            }
            Ok(done)
        }
        UndoEntry::Copy { pairs } => {
            let mut done = 0;
            for (_, p) in pairs.iter().rev() {
                if fs.exists(p) {
                    remove(p).map_err(|e| e.to_string())?;
                    done += 1;
                }
            }
            Ok(done)
        }
        UndoEntry::Delete { paths } => {
            let mut done = 0;
            for p in paths {
                fs.restore_deleted(p)
                    .map_err(|e| format!("cannot restore '{}': {e}", p.display()))?;
                done += 1;
            }
            Ok(done)
        }
        UndoEntry::Extract { dest, .. } => {
            if fs.exists(dest) {
                remove(dest).map_err(|e| e.to_string())?;
            }
            Ok(1)
        }
        UndoEntry::Batch(items) => {
            // A batch of renames may contain swaps/chains: undo it with the
            // same two-phase strategy used to apply it.
            let renames: Option<Vec<bulk_rename::RenameStep>> = items
                .iter()
                .map(|e| match e {
                    UndoEntry::Rename { from, to } => Some(bulk_rename::RenameStep {
                        from: to.clone(),
                        to: from.clone(),
                    }),
                    _ => None,
                })
                .collect();
            if let Some(steps) = renames {
                if let Some(missing) = steps.iter().find(|s| !fs.exists(&s.from)) {
                    return Err(format!("'{}' no longer exists", missing.from.display()));
                }
                // Targets must be free unless they are themselves moving.
                let moving: Vec<&Path> = steps.iter().map(|s| s.from.as_path()).collect();
                if let Some(taken) = steps
                    .iter()
                    .find(|s| fs.exists(&s.to) && !moving.contains(&s.to.as_path()))
                {
                    return Err(format!("'{}' already exists", taken.to.display()));
                }
                return bulk_rename::apply(fs, &steps)
                    .map(|done| done.len())
                    .map_err(|e| e.to_string());
            }
            let mut done = 0;
            for e in items.iter().rev() {
                done += apply_undo(fs, e)?;
            }
            Ok(done)
        }
    }
}

/// What re-doing an entry asks of the caller.
#[derive(Debug, PartialEq, Eq)]
enum Redone {
    /// Done; this many items were affected.
    Items(usize),
    /// The extraction must be re-run (in the background) by the caller.
    Extract { archive: PathBuf, dest: PathBuf },
}

/// Re-apply `entry` (the inverse of [`apply_undo`]) on `fs`.
fn apply_redo(fs: &dyn FileSystem, entry: &UndoEntry) -> Result<Redone, String> {
    let remove = |p: &Path| {
        if fs.is_dir(p) {
            fs.remove_dir_all(p)
        } else {
            fs.remove_file(p)
        }
    };
    let copy = |from: &Path, to: &Path| {
        if fs.is_dir(from) {
            fs.copy_dir(from, to)
        } else {
            fs.copy_file(from, to)
        }
    };
    let need = |p: &Path| -> Result<(), String> {
        if fs.exists(p) {
            Ok(())
        } else {
            Err(format!("'{}' no longer exists", p.display()))
        }
    };
    let free = |p: &Path| -> Result<(), String> {
        if fs.exists(p) {
            Err(format!("'{}' already exists", p.display()))
        } else {
            Ok(())
        }
    };
    match entry {
        UndoEntry::Rename { from, to } => {
            need(from)?;
            free(to)?;
            fs.rename(from, to).map_err(|e| e.to_string())?;
            Ok(Redone::Items(1))
        }
        UndoEntry::Create { path, is_dir } => {
            free(path)?;
            let made = if *is_dir {
                fs.create_dir_all(path)
            } else {
                fs.create_file(path)
            };
            made.map_err(|e| e.to_string())?;
            Ok(Redone::Items(1))
        }
        UndoEntry::Move { pairs } => {
            for (from, to) in pairs {
                need(from)?;
                free(to)?;
                copy(from, to).map_err(|e| e.to_string())?;
                remove(from).map_err(|e| e.to_string())?;
            }
            Ok(Redone::Items(pairs.len()))
        }
        UndoEntry::Copy { pairs } => {
            for (from, to) in pairs {
                need(from)?;
                free(to)?;
                copy(from, to).map_err(|e| e.to_string())?;
            }
            Ok(Redone::Items(pairs.len()))
        }
        UndoEntry::Delete { paths } => {
            for p in paths {
                need(p)?;
                remove(p).map_err(|e| e.to_string())?;
            }
            Ok(Redone::Items(paths.len()))
        }
        UndoEntry::Extract { archive, dest } => {
            need(archive)?;
            free(dest)?;
            Ok(Redone::Extract {
                archive: archive.clone(),
                dest: dest.clone(),
            })
        }
        UndoEntry::Batch(items) => {
            let renames: Option<Vec<bulk_rename::RenameStep>> = items
                .iter()
                .map(|e| match e {
                    UndoEntry::Rename { from, to } => Some(bulk_rename::RenameStep {
                        from: from.clone(),
                        to: to.clone(),
                    }),
                    _ => None,
                })
                .collect();
            if let Some(steps) = renames {
                for s in &steps {
                    need(&s.from)?;
                }
                let moving: Vec<&Path> = steps.iter().map(|s| s.from.as_path()).collect();
                if let Some(taken) = steps
                    .iter()
                    .find(|s| fs.exists(&s.to) && !moving.contains(&s.to.as_path()))
                {
                    return Err(format!("'{}' already exists", taken.to.display()));
                }
                return bulk_rename::apply(fs, &steps)
                    .map(|done| Redone::Items(done.len()))
                    .map_err(|e| e.to_string());
            }
            let mut n = 0;
            for e in items {
                match apply_redo(fs, e)? {
                    Redone::Items(k) => n += k,
                    Redone::Extract { .. } => {
                        return Err("cannot redo an extraction inside a batch".into());
                    }
                }
            }
            Ok(Redone::Items(n))
        }
    }
}

// ── Bulk rename session ───────────────────────────────────────────────────────

/// An in-progress bulk rename: the temp file being edited and the entries it
/// stands for.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BulkRenameSession {
    /// The temporary file the user edits, one name per line.
    pub file: PathBuf,
    /// The entries being renamed, in the file's line order.
    pub sources: Vec<PathBuf>,
}

// ── Background results ────────────────────────────────────────────────────────

/// A running archive extraction.
#[cfg(feature = "archive")]
#[derive(Debug)]
pub(crate) struct ExtractJob {
    archive: PathBuf,
    dest: PathBuf,
    is_redo: bool,
    task: Task<Result<usize, String>>,
}

/// Messages from the fuzzy-finder tree walk.
#[derive(Debug)]
pub(crate) enum WalkMsg {
    Items(Vec<PickerItem>),
    Truncated,
}

/// Cap on files visited by the fuzzy finder.
const WALK_MAX_ENTRIES: usize = 200_000;
/// Wall-clock cap for the fuzzy finder walk.
const WALK_MAX_DURATION: Duration = Duration::from_secs(8);

/// Breadth-first walk of `root`, streaming batches of picker rows (paths
/// relative to `root`). Skips VCS metadata always, and `node_modules` /
/// `target` unless hidden entries are shown. Returns `true` when a cap cut
/// the walk short.
pub(crate) fn walk_tree(
    fs: &dyn FileSystem,
    root: &Path,
    show_hidden: bool,
    emit: &mut dyn FnMut(Vec<PickerItem>) -> bool,
) -> bool {
    let started = Instant::now();
    let mut queue: VecDeque<PathBuf> = VecDeque::from([root.to_path_buf()]);
    let mut batch: Vec<PickerItem> = Vec::new();
    let mut visited = 0usize;

    while let Some(dir) = queue.pop_front() {
        let Ok(mut entries) = fs.read_dir(&dir) else {
            continue;
        };
        entries.sort_by(|a, b| a.name.cmp(&b.name));
        for e in entries {
            let hidden = e.name.starts_with('.');
            if hidden && !show_hidden {
                continue;
            }
            if e.is_dir
                && (matches!(e.name.as_str(), ".git" | ".hg" | ".svn")
                    || (!show_hidden && matches!(e.name.as_str(), "node_modules" | "target")))
            {
                continue;
            }
            visited += 1;
            let rel = e.path.strip_prefix(root).unwrap_or(&e.path);
            let mut label = rel.to_string_lossy().replace('\\', "/");
            if e.is_dir {
                label.push('/');
                queue.push_back(e.path.clone());
            }
            batch.push(PickerItem {
                label,
                detail: String::new(),
                path: e.path,
                is_dir: e.is_dir,
            });
            if batch.len() >= 512 && !emit(std::mem::take(&mut batch)) {
                return false;
            }
            if visited >= WALK_MAX_ENTRIES || started.elapsed() >= WALK_MAX_DURATION {
                let _ = emit(std::mem::take(&mut batch));
                return true;
            }
        }
    }
    let _ = emit(batch);
    false
}

impl App {
    // ── Undo ──────────────────────────────────────────────────────────────

    /// Remember `entry` so [`undo`](Self::undo) can reverse it.
    ///
    /// A new action invalidates anything that could have been redone.
    pub fn record_undo(&mut self, entry: UndoEntry) {
        self.redo_stack.clear();
        self.push_undo(entry);
    }

    fn push_undo(&mut self, entry: UndoEntry) {
        self.undo_stack.push(entry);
        if self.undo_stack.len() > UNDO_LIMIT {
            self.undo_stack.remove(0);
        }
    }

    /// Reverse the most recent undoable action.
    pub fn undo(&mut self) {
        let Some(entry) = self.undo_stack.pop() else {
            self.notify("Nothing to undo");
            return;
        };
        let fs = self.active_pane().filesystem.clone();
        match apply_undo(fs.as_ref(), &entry) {
            Ok(_) => {
                let msg = format!("Undid {}", entry.describe());
                self.redo_stack.push(entry);
                for p in &mut self.panes {
                    p.reload();
                    p.clear_dir_size_cache();
                }
                self.preview_state.invalidate();
                self.status_msg = msg.clone();
                self.notify(msg);
            }
            Err(e) => {
                let msg = format!("Undo failed: {e}");
                self.status_msg = format!("Error: {msg}");
                self.notify_error(msg);
            }
        }
    }

    /// Re-apply the action most recently reversed by [`undo`](Self::undo).
    pub fn redo(&mut self) {
        let Some(entry) = self.redo_stack.pop() else {
            self.notify("Nothing to redo");
            return;
        };
        let fs = self.active_pane().filesystem.clone();
        match apply_redo(fs.as_ref(), &entry) {
            Ok(Redone::Items(_)) => {
                let msg = format!("Redid {}", entry.describe());
                self.push_undo(entry);
                for p in &mut self.panes {
                    p.reload();
                    p.clear_dir_size_cache();
                }
                self.preview_state.invalidate();
                self.status_msg = msg.clone();
                self.notify(msg);
            }
            #[cfg(feature = "archive")]
            Ok(Redone::Extract { archive, dest }) => {
                // Runs in the background; the finished extraction goes back
                // on the undo stack (without discarding the rest of redo).
                self.start_extract(archive, dest, true);
            }
            #[cfg(not(feature = "archive"))]
            Ok(Redone::Extract { .. }) => {
                self.notify_error("Redo failed: archive support disabled")
            }
            Err(e) => {
                let msg = format!("Redo failed: {e}");
                self.status_msg = format!("Error: {msg}");
                self.notify_error(msg);
            }
        }
    }

    // ── Bookmarks ─────────────────────────────────────────────────────────

    /// Add the active pane's directory to the bookmarks, or remove it if it
    /// is already bookmarked.
    pub fn toggle_bookmark(&mut self) {
        let dir = self.active_pane().current_dir.clone();
        if let Some(i) = self.bookmarks.iter().position(|b| *b == dir) {
            self.bookmarks.remove(i);
            self.notify(format!("Removed bookmark '{}'", dir.display()));
        } else {
            self.notify(format!("Bookmarked '{}'", dir.display()));
            self.bookmarks.push(dir);
        }
    }

    /// Open the bookmark list.
    pub fn open_bookmarks(&mut self) {
        if self.bookmarks.is_empty() {
            self.notify("No bookmarks yet — press b to bookmark this folder");
            return;
        }
        let items = self
            .bookmarks
            .iter()
            .map(|p| PickerItem {
                label: p.display().to_string(),
                detail: String::new(),
                path: p.clone(),
                is_dir: true,
            })
            .collect();
        self.picker_task = None;
        self.picker = Some(Picker::new(PickerKind::Bookmarks, "Bookmarks", items));
    }

    // ── Fuzzy finder ──────────────────────────────────────────────────────

    /// Open the recursive fuzzy finder rooted at the active pane's directory.
    pub fn open_fuzzy_find(&mut self) {
        let pane = self.active_pane();
        let root = pane.current_dir.clone();
        let fs = pane.filesystem.clone();
        let show_hidden = pane.show_hidden;

        let mut picker = Picker::new(
            PickerKind::Fuzzy,
            format!("Find in {}", root.display()),
            Vec::new(),
        );
        picker.loading = true;
        self.picker = Some(picker);
        self.picker_task = Some(Task::spawn(move |emit| {
            let truncated = walk_tree(fs.as_ref(), &root, show_hidden, &mut |batch| {
                emit.send(WalkMsg::Items(batch))
            });
            if truncated {
                emit.send(WalkMsg::Truncated);
            }
        }));
    }

    /// Route a key to the open picker. Returns `true` when the app should
    /// exit (never, currently) — kept for symmetry with `handle_key`.
    pub(super) fn handle_picker_key(&mut self, key: crossterm::event::KeyEvent) {
        let Some(picker) = self.picker.as_mut() else {
            return;
        };
        match picker.handle_key(key) {
            PickerAction::Pending => {}
            PickerAction::Cancel => self.close_picker(),
            PickerAction::Choose(item) => {
                self.close_picker();
                self.jump_to(&item.path);
            }
            PickerAction::Delete(item) => {
                self.bookmarks.retain(|b| *b != item.path);
                if let Some(p) = self.picker.as_mut() {
                    p.remove(&item);
                }
                if self.bookmarks.is_empty() {
                    self.close_picker();
                }
            }
        }
    }

    fn close_picker(&mut self) {
        self.picker = None;
        self.picker_task = None;
    }

    /// Navigate the active pane to `path` (a folder, or a file's folder with
    /// the file selected).
    pub fn jump_to(&mut self, path: &Path) {
        if !self.active_pane().filesystem.exists(path) {
            self.notify_error(format!("'{}' no longer exists", path.display()));
            return;
        }
        self.active_pane_mut().go_to(path);
        self.preview_state.invalidate();
        self.status_msg = format!("Jumped to '{}'", path.display());
    }

    // ── Bulk rename ───────────────────────────────────────────────────────

    /// Start a bulk rename of the marked entries (or every listed entry when
    /// nothing is marked): the names are opened in the configured editor —
    /// or the built-in one — one per line.
    pub fn start_bulk_rename(&mut self) {
        let pane = self.active_pane();
        let sources: Vec<PathBuf> = if pane.marked.is_empty() {
            pane.entries.iter().map(|e| e.path.clone()).collect()
        } else {
            pane.entries
                .iter()
                .filter(|e| pane.marked.contains(&e.path))
                .map(|e| e.path.clone())
                .collect()
        };
        if sources.is_empty() {
            self.notify_error("Nothing to rename");
            return;
        }
        let content = match bulk_rename::names_file_content(&sources) {
            Ok(c) => c,
            Err(name) => {
                self.notify_error(format!(
                    "Cannot bulk-rename '{}': newline in name",
                    name.escape_debug()
                ));
                return;
            }
        };
        // Unique per session so concurrent instances (and tests) never collide.
        static SESSION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let n = SESSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let file =
            std::env::temp_dir().join(format!("tfe-bulk-rename-{}-{n}.txt", std::process::id()));
        if let Err(e) = std::fs::write(&file, content) {
            self.notify_error(format!("Cannot write temp file: {e}"));
            return;
        }
        self.bulk_rename = Some(BulkRenameSession {
            file: file.clone(),
            sources,
        });
        if self.editor == Editor::None {
            match InlineEditor::open(&file) {
                Ok(ed) => self.inline_editor = Some(ed),
                Err(e) => {
                    self.bulk_rename = None;
                    self.notify_error(format!("Cannot edit: {e}"));
                }
            }
        } else {
            self.open_with_editor = Some(file);
        }
        self.status_msg = "Edit the names, save, and quit the editor to apply.".into();
    }

    /// Tell the app an external editor session on `path` has ended.
    ///
    /// Hosts that handle [`AppOutcome::OpenEditor`](super::AppOutcome) (or
    /// [`App::take_editor_request`]) themselves must call this afterwards so
    /// a pending bulk rename is applied.
    pub fn editor_finished(&mut self, path: &Path) {
        if self.bulk_rename.as_ref().is_some_and(|s| s.file == path) {
            self.finish_bulk_rename();
        }
    }

    /// Apply the edited name list of the pending bulk rename, if any.
    pub(super) fn finish_bulk_rename(&mut self) {
        let Some(session) = self.bulk_rename.take() else {
            return;
        };
        let edited = std::fs::read_to_string(&session.file);
        let _ = std::fs::remove_file(&session.file);
        let edited = match edited {
            Ok(t) => t,
            Err(e) => {
                self.notify_error(format!("Bulk rename aborted: {e}"));
                return;
            }
        };
        let fs = self.active_pane().filesystem.clone();
        let steps = match bulk_rename::plan(&session.sources, &edited, |p| fs.exists(p)) {
            Ok(s) => s,
            Err(e) => {
                self.notify_error(format!("Bulk rename aborted: {e}"));
                return;
            }
        };
        if steps.is_empty() {
            self.notify("Bulk rename: no changes");
            return;
        }
        match bulk_rename::apply(fs.as_ref(), &steps) {
            Ok(done) => {
                let n = done.len();
                self.record_undo(UndoEntry::Batch(
                    done.into_iter()
                        .map(|s| UndoEntry::Rename {
                            from: s.from,
                            to: s.to,
                        })
                        .collect(),
                ));
                for p in &mut self.panes {
                    p.clear_marks();
                    p.reload();
                }
                self.preview_state.invalidate();
                self.notify(format!("Renamed {n} item(s)"));
            }
            Err(e) => self.notify_error(format!("Bulk rename failed (rolled back): {e}")),
        }
    }

    // ── Archives ──────────────────────────────────────────────────────────

    /// Extract the highlighted archive into a sibling folder named after it.
    #[cfg(feature = "archive")]
    pub fn extract_current_archive(&mut self) {
        let pane = self.active_pane();
        let Some(entry) = pane.current_entry() else {
            return;
        };
        let path = entry.path.clone();
        if entry.is_dir || crate::archive::ArchiveFormat::detect(&path).is_none() {
            self.notify_error("Not a supported archive (.zip, .tar, .tar.gz, .tgz)");
            return;
        }
        if !pane.filesystem.is_local() {
            self.notify_error("Extraction needs a local filesystem");
            return;
        }
        let dest = crate::archive::default_destination(&path);
        if dest.exists() {
            self.notify_error(format!("'{}' already exists", dest.display()));
            return;
        }
        self.start_extract(path, dest, false);
    }

    /// Run an extraction on a worker thread. `is_redo` keeps the redo stack.
    #[cfg(feature = "archive")]
    fn start_extract(&mut self, archive: PathBuf, dest: PathBuf, is_redo: bool) {
        self.status_msg = format!("Extracting '{}'…", archive.display());
        let target = dest.clone();
        let source = archive.clone();
        self.extract_task = Some(ExtractJob {
            archive,
            dest,
            is_redo,
            task: Task::spawn(move |emit| {
                let result = crate::archive::extract(&source, &target).map_err(|e| e.to_string());
                if result.is_err() {
                    // Don't leave a half-extracted folder behind.
                    let _ = std::fs::remove_dir_all(&target);
                }
                emit.send(result);
            }),
        });
    }

    // ── Git ───────────────────────────────────────────────────────────────

    /// Show or hide git change markers in every pane.
    pub fn toggle_git(&mut self) {
        let show = !self.active_pane().show_git;
        for p in &mut self.panes {
            p.set_show_git(show);
        }
        self.notify(format!("git markers: {}", if show { "on" } else { "off" }));
    }

    // ── Background polling ────────────────────────────────────────────────

    /// Collect finished background work. Returns `true` when the view should
    /// be redrawn. Called by [`draw`](crate::draw) once per frame.
    pub fn poll_background(&mut self) -> bool {
        let mut changed = false;
        for p in &mut self.panes {
            changed |= p.poll_background();
        }

        if let Some(task) = &mut self.picker_task {
            for msg in task.poll() {
                if let Some(picker) = &mut self.picker {
                    match msg {
                        WalkMsg::Items(items) => picker.extend(items),
                        WalkMsg::Truncated => picker.truncated = true,
                    }
                    changed = true;
                }
            }
            if task.is_done() {
                self.picker_task = None;
                if let Some(picker) = &mut self.picker {
                    picker.loading = false;
                }
                changed = true;
            }
        }

        #[cfg(feature = "archive")]
        if let Some(job) = &mut self.extract_task {
            let results = job.task.poll();
            let done = job.task.is_done();
            let (archive, dest, is_redo) = (job.archive.clone(), job.dest.clone(), job.is_redo);
            for result in results {
                match result {
                    Ok(n) => {
                        let entry = UndoEntry::Extract {
                            archive: archive.clone(),
                            dest: dest.clone(),
                        };
                        if is_redo {
                            self.push_undo(entry);
                        } else {
                            self.record_undo(entry);
                        }
                        for p in &mut self.panes {
                            p.reload();
                        }
                        self.notify(format!("Extracted {n} item(s) into '{}'", dest.display()));
                    }
                    Err(e) => self.notify_error(format!("Extract failed: {e}")),
                }
                changed = true;
            }
            if done {
                self.extract_task = None;
            }
        }

        if self.preview_state.poll() {
            changed = true;
        }
        changed
    }

    /// `true` while anything is still computing in the background — the
    /// event loop should wake periodically to redraw.
    #[must_use]
    pub fn has_background_work(&self) -> bool {
        #[cfg(feature = "archive")]
        if self.extract_task.is_some() {
            return true;
        }
        self.picker_task.is_some()
            || self.preview_state.is_loading()
            || self.panes.iter().any(FileExplorer::has_pending_work)
    }
}

#[cfg(test)]
mod tests;