duodiff 0.6.0

A fast, cross-platform terminal user interface (TUI) directory comparison tool
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
//! Shared actions: scan, copy, palette, external tools, and pure key-outcome builders.
use crate::app::{self, App};
use crate::diff_tool::{self, ExternalDiffTool};
use crate::event::AppEvent;
use crossterm::{
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::Terminal;
use std::path::PathBuf;
use std::str::FromStr;

/// Pure IO intent from a key press — built without performing IO.
/// [`dispatch_key_outcome`] performs the process spawn / terminal handoff.
#[derive(Clone, Debug, PartialEq)]
pub enum KeyOutcome {
    /// No IO needed — the key was fully handled by pure state mutation (or ignored).
    None,
    LaunchDiff {
        tool: ExternalDiffTool,
        left: PathBuf,
        right: PathBuf,
    },
    LaunchEditor {
        path: PathBuf,
    },
}

/// Build the diff-launch intent for the currently selected row (the `D` key).
pub fn diff_launch_outcome(app: &App) -> KeyOutcome {
    let Some(row) = app.selected_row() else {
        return KeyOutcome::None;
    };
    if row.is_dir() || row.left.is_none() || row.right.is_none() {
        return KeyOutcome::None;
    }
    let Some(tool_str) = app.settings().external_diff_tool.as_ref() else {
        return KeyOutcome::None;
    };
    let Ok(tool) = ExternalDiffTool::from_str(tool_str) else {
        return KeyOutcome::None;
    };
    KeyOutcome::LaunchDiff {
        tool,
        left: app.left_path().join(&row.relative_path),
        right: app.right_path().join(&row.relative_path),
    }
}

/// Build the editor-launch intent for the active side's selected file (the `E` key).
pub fn editor_launch_outcome(app: &App) -> KeyOutcome {
    let Some(row) = app.selected_row() else {
        return KeyOutcome::None;
    };
    let side = if app.active_side_left() {
        &row.left
    } else {
        &row.right
    };
    if side.as_ref().is_none_or(|f| f.is_dir) {
        return KeyOutcome::None;
    }
    let root = if app.active_side_left() {
        app.left_path()
    } else {
        app.right_path()
    };
    KeyOutcome::LaunchEditor {
        path: root.join(&row.relative_path),
    }
}

/// Leaves raw mode + the alternate screen on construction (unless stdout isn't a real
/// terminal — see the "TTY recovery" invariant in AGENTS.md), and restores both on `Drop`.
/// Callers hold this across the external process and drop it **before** re-clearing the TUI.
pub(crate) struct RealTerminalHandoff {
    mouse_enabled: bool,
    is_terminal: bool,
}

impl RealTerminalHandoff {
    pub(crate) fn new(mouse_enabled: bool) -> std::io::Result<Self> {
        use std::io::IsTerminal;
        let is_terminal = std::io::stdout().is_terminal();
        if is_terminal {
            Self::suspend(mouse_enabled)?;
        }
        Ok(Self {
            mouse_enabled,
            is_terminal,
        })
    }

    fn suspend(mouse_enabled: bool) -> std::io::Result<()> {
        disable_raw_mode()?;
        if mouse_enabled {
            execute!(
                std::io::stdout(),
                LeaveAlternateScreen,
                crossterm::event::DisableMouseCapture
            )
        } else {
            execute!(std::io::stdout(), LeaveAlternateScreen)
        }
    }

    fn resume(mouse_enabled: bool) -> std::io::Result<()> {
        enable_raw_mode()?;
        if mouse_enabled {
            execute!(
                std::io::stdout(),
                EnterAlternateScreen,
                crossterm::event::EnableMouseCapture
            )
        } else {
            execute!(std::io::stdout(), EnterAlternateScreen)
        }
    }
}

impl Drop for RealTerminalHandoff {
    fn drop(&mut self) {
        if !self.is_terminal {
            return;
        }
        // Drop can't propagate a Result, so a restore failure is reported rather than
        // silently swallowed — but there's nothing more this guard can do about it.
        if let Err(e) = Self::resume(self.mouse_enabled) {
            eprintln!("Failed to restore terminal after external process: {e}");
        }
    }
}

fn wait_for_enter() {
    let mut buf = String::new();
    let _ = std::io::stdin().read_line(&mut buf);
}

/// Spawn an external diff tool. Caller must keep the TUI suspended (e.g. hold
/// [`RealTerminalHandoff`]) across this call and only clear after that guard drops.
pub(crate) fn run_external_diff(
    tool: &diff_tool::ExternalDiffTool,
    left: &std::path::Path,
    right: &std::path::Path,
) {
    match diff_tool::open_diff(tool, left, right) {
        Err(e) => {
            eprintln!(
                "Error launching external diff: {}. Press Enter to continue...",
                e
            );
            wait_for_enter();
        }
        Ok(()) if matches!(tool, diff_tool::ExternalDiffTool::Difftastic) => {
            println!("\nPress Enter to return to duodiff...");
            wait_for_enter();
        }
        Ok(()) => {}
    }
}

/// Spawn `$EDITOR`/`$VISUAL`. Same handoff contract as [`run_external_diff`].
pub(crate) fn run_external_editor(file_path: &std::path::Path) {
    if let Err(e) = diff_tool::open_editor(file_path) {
        eprintln!(
            "Error launching external editor: {}. Press Enter to continue...",
            e
        );
        wait_for_enter();
    }
}

/// Suspend the TUI, run `body`, restore the TUI, then clear the alt-screen buffer.
fn with_terminal_handoff<B: ratatui::backend::Backend>(
    terminal: &mut ratatui::Terminal<B>,
    mouse_enabled: bool,
    body: impl FnOnce(),
) -> Result<(), Box<dyn std::error::Error>>
where
    B::Error: 'static,
{
    {
        let _handoff = RealTerminalHandoff::new(mouse_enabled)?;
        body();
    }
    terminal.clear()?;
    Ok(())
}

/// Perform the IO a [`KeyOutcome`] describes. Pure key-handling code only builds a
/// `KeyOutcome`; process spawn and terminal mode toggling live here.
pub fn dispatch_key_outcome<B: ratatui::backend::Backend>(
    outcome: KeyOutcome,
    terminal: &mut ratatui::Terminal<B>,
    mouse_enabled: bool,
) -> Result<(), Box<dyn std::error::Error>>
where
    B::Error: 'static,
{
    match outcome {
        KeyOutcome::None => Ok(()),
        KeyOutcome::LaunchDiff { tool, left, right } => {
            with_terminal_handoff(terminal, mouse_enabled, || {
                run_external_diff(&tool, &left, &right);
            })
        }
        KeyOutcome::LaunchEditor { path } => with_terminal_handoff(terminal, mouse_enabled, || {
            run_external_editor(&path);
        }),
    }
}

pub async fn execute_confirm_action(
    app: &mut App,
    tx: tokio::sync::mpsc::Sender<AppEvent>,
) -> Result<(), Box<dyn std::error::Error>> {
    if let Some(action) = app.take_confirmed_action() {
        if let Some(row) = app.selected_row() {
            let relative_path = row.relative_path.clone();
            let name = row.name.clone();

            let src = match action {
                app::ConfirmAction::CopyLeftToRight => app.left_path().join(&relative_path),
                app::ConfirmAction::CopyRightToLeft => app.right_path().join(&relative_path),
            };
            let dst = match action {
                app::ConfirmAction::CopyLeftToRight => app.right_path().join(&relative_path),
                app::ConfirmAction::CopyRightToLeft => app.left_path().join(&relative_path),
            };
            let dst_root = match action {
                app::ConfirmAction::CopyLeftToRight => app.right_path().to_path_buf(),
                app::ConfirmAction::CopyRightToLeft => app.left_path().to_path_buf(),
            };

            // Perform copy — all errors are captured uniformly in `res`
            let res = copy_entry_checked(&src, &dst, &dst_root);

            match res {
                Ok(()) => {
                    app.set_status(format!("Copied '{}'", name), false);
                    app.leave_file_diff();
                    // Prefer a targeted subtree re-align; fall back to full scan
                    // for root-level copies or missing tree paths.
                    let copied_is_dir = std::fs::symlink_metadata(&dst)
                        .map(|m| {
                            let ft = m.file_type();
                            ft.is_dir() && !ft.is_symlink()
                        })
                        .unwrap_or(false);
                    if app
                        .apply_incremental_rescan(&relative_path, copied_is_dir)
                        .is_err()
                    {
                        kick_scan(app, tx);
                    }
                }
                Err(e) => {
                    app.set_status(format!("Copy failed: {}", e), true);
                }
            }
        }
    }
    Ok(())
}

pub(crate) fn normalize_lexically(path: &std::path::Path) -> std::path::PathBuf {
    use std::path::{Component, PathBuf};
    let mut out = PathBuf::new();
    for c in path.components() {
        match c {
            Component::Prefix(_) | Component::RootDir => out.push(c.as_os_str()),
            Component::CurDir => {}
            Component::ParentDir => {
                out.pop();
            }
            Component::Normal(s) => out.push(s),
        }
    }
    out
}

pub(crate) fn path_is_under(path: &std::path::Path, root: &std::path::Path) -> bool {
    let path = normalize_lexically(path);
    let root = normalize_lexically(root);
    path.starts_with(&root)
}

pub(crate) fn copy_entry_checked(
    src: &std::path::Path,
    dst: &std::path::Path,
    dst_root: &std::path::Path,
) -> std::io::Result<()> {
    if !path_is_under(dst, dst_root) {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "copy destination escapes the target root",
        ));
    }

    let meta = std::fs::symlink_metadata(src)?;
    let file_type = meta.file_type();
    if file_type.is_symlink() {
        if let Some(parent) = dst.parent() {
            std::fs::create_dir_all(parent)?;
        }
        recreate_symlink(src, dst)
    } else if file_type.is_dir() {
        copy_dir_recursive(src, dst, dst_root)
    } else if file_type.is_file() {
        if let Some(parent) = dst.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::copy(src, dst).map(|_| ())
    } else {
        Err(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "Source path not found on disk",
        ))
    }
}

fn recreate_symlink(src: &std::path::Path, dst: &std::path::Path) -> std::io::Result<()> {
    let target = std::fs::read_link(src)?;
    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(target, dst)
    }
    #[cfg(windows)]
    {
        // Prefer recreating the link; Windows may require elevated privileges.
        let meta = std::fs::symlink_metadata(src)?;
        // `is_dir` on symlink metadata reports the *target* type on Windows.
        if meta.file_type().is_dir() {
            std::os::windows::fs::symlink_dir(target, dst)
        } else {
            std::os::windows::fs::symlink_file(target, dst)
        }
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = (src, dst, target);
        Err(std::io::Error::new(
            std::io::ErrorKind::Unsupported,
            "symlink copy is not supported on this platform",
        ))
    }
}

pub(crate) fn copy_dir_recursive(
    src: &std::path::Path,
    dst: &std::path::Path,
    dst_root: &std::path::Path,
) -> std::io::Result<()> {
    if !path_is_under(dst, dst_root) {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "copy destination escapes the target root",
        ));
    }
    std::fs::create_dir_all(dst)?;
    for entry in std::fs::read_dir(src)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());
        if !path_is_under(&dst_path, dst_root) {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "copy destination escapes the target root",
            ));
        }
        if file_type.is_symlink() {
            recreate_symlink(&src_path, &dst_path)?;
        } else if file_type.is_dir() {
            copy_dir_recursive(&src_path, &dst_path, dst_root)?;
        } else {
            std::fs::copy(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

pub fn kick_scan(app: &mut App, tx: tokio::sync::mpsc::Sender<AppEvent>) {
    let generation = app.begin_scan();
    start_scan_task(
        app.left_path().to_path_buf(),
        app.right_path().to_path_buf(),
        app.precise_mode(),
        app.ignore_matcher().clone(),
        generation,
        tx,
    );
}

pub fn start_scan_task(
    left: PathBuf,
    right: PathBuf,
    precise: bool,
    ignore: crate::ignore::IgnoreMatcher,
    generation: u64,
    tx: tokio::sync::mpsc::Sender<crate::event::AppEvent>,
) {
    tokio::spawn(async move {
        let root = tokio::task::spawn_blocking(move || {
            crate::diff::align_directories(
                &left,
                &right,
                std::path::Path::new(""),
                precise,
                &ignore,
            )
        })
        .await;

        match root {
            Ok(Ok(node)) => {
                let _ = tx
                    .send(crate::event::AppEvent::ScanFinished { generation, node })
                    .await;
            }
            Ok(Err(err)) => {
                let _ = tx
                    .send(crate::event::AppEvent::Error {
                        generation,
                        message: err.to_string(),
                    })
                    .await;
            }
            Err(err) => {
                let _ = tx
                    .send(crate::event::AppEvent::Error {
                        generation,
                        message: err.to_string(),
                    })
                    .await;
            }
        }
    });
}

pub async fn execute_palette_action<B: ratatui::backend::Backend>(
    action: &crate::ui::PaletteAction,
    app: &mut App,
    terminal: &mut Terminal<B>,
    tx: tokio::sync::mpsc::Sender<AppEvent>,
) -> Result<(), Box<dyn std::error::Error>>
where
    B::Error: 'static,
{
    match action.action_id {
        crate::ui::PaletteActionId::ExternalDiff => {
            dispatch_key_outcome(diff_launch_outcome(app), terminal, app.mouse_enabled())?;
        }
        crate::ui::PaletteActionId::ExternalEdit => {
            dispatch_key_outcome(editor_launch_outcome(app), terminal, app.mouse_enabled())?;
        }
        crate::ui::PaletteActionId::CopyLeftToRight => {
            app.request_copy(app::ConfirmAction::CopyLeftToRight);
        }
        crate::ui::PaletteActionId::CopyRightToLeft => {
            app.request_copy(app::ConfirmAction::CopyRightToLeft);
        }
        crate::ui::PaletteActionId::BuiltinDiff => {
            app.enter_file_diff();
        }
        crate::ui::PaletteActionId::SwapPaths => {
            app.swap_paths();
            kick_scan(app, tx);
        }
        crate::ui::PaletteActionId::ToggleScan => {
            app.toggle_precise_mode();
            kick_scan(app, tx);
        }
        crate::ui::PaletteActionId::Refresh => {
            kick_scan(app, tx);
        }
        crate::ui::PaletteActionId::Config => {
            app.open_config();
        }
        crate::ui::PaletteActionId::Help => {
            app.open_help();
        }
        crate::ui::PaletteActionId::Filter => {
            app.filter_mut().open();
        }
        crate::ui::PaletteActionId::Quit => {
            app.request_quit();
        }
        crate::ui::PaletteActionId::ToggleWrap => {
            app.diff_mut().toggle_wrap();
        }
        crate::ui::PaletteActionId::ToggleFullDiff => {
            if let Err(e) = app.toggle_diff_show_full() {
                app.set_status(format!("Cannot refresh diff: {e}"), true);
            }
        }
        crate::ui::PaletteActionId::NextChange => {
            app.jump_to_next_change();
        }
        crate::ui::PaletteActionId::PrevChange => {
            app.jump_to_prev_change();
        }
        crate::ui::PaletteActionId::CopyHunkLeftToRight => {
            match app.copy_hunk_at_cursor(crate::diff_view::HunkCopyDirection::LeftToRight) {
                Ok(()) => app.set_status("Copied change block to right".to_string(), false),
                Err(e) => app.set_status(format!("Hunk copy failed: {}", e), true),
            }
        }
        crate::ui::PaletteActionId::CopyHunkRightToLeft => {
            match app.copy_hunk_at_cursor(crate::diff_view::HunkCopyDirection::RightToLeft) {
                Ok(()) => app.set_status("Copied change block to left".to_string(), false),
                Err(e) => app.set_status(format!("Hunk copy failed: {}", e), true),
            }
        }
        crate::ui::PaletteActionId::Back => {
            if app.view_mode() == app::ViewMode::FileDiff {
                app.leave_file_diff();
            } else {
                app.close_help();
            }
        }
    }
    Ok(())
}

pub fn open_repo_url(app: &mut App) {
    app.set_status("Opening GitHub repository in the browser...", false);
    let url = env!("CARGO_PKG_REPOSITORY");
    std::thread::spawn(move || {
        let _ = match std::env::consts::OS {
            "macos" => std::process::Command::new("open").arg(url).status(),
            "windows" => std::process::Command::new("cmd")
                .args(["/c", "start", url])
                .status(),
            _ => std::process::Command::new("xdg-open").arg(url).status(),
        };
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::FlatRow;
    use crate::diff::{DiffState, FileInfo};
    use crate::test_support::{lock_env_tests, RecordingTerminalHandoff};
    use std::cell::RefCell;
    use std::path::PathBuf;
    use std::rc::Rc;
    use std::time::SystemTime;

    // `run_external_diff` isn't exercised directly here: `ExternalDiffTool::as_str()`
    // names a real GUI/CLI tool binary with no env-var override (unlike `$EDITOR`), so
    // there's no fast, portable way to make it succeed in CI. The editor path covers the
    // same handoff scope pattern used by `dispatch_key_outcome` (guard around the spawn).
    #[test]
    fn test_run_external_editor_suspends_then_resumes_around_the_spawn() {
        let _guard = lock_env_tests();
        std::env::remove_var("VISUAL");
        #[cfg(not(target_os = "windows"))]
        std::env::set_var("EDITOR", "true");
        #[cfg(target_os = "windows")]
        std::env::set_var("EDITOR", "cargo --version");

        let log = Rc::new(RefCell::new(Vec::new()));
        {
            let _handoff = RecordingTerminalHandoff::new(log.clone());
            run_external_editor(std::path::Path::new("dummy.txt"));
        }
        assert_eq!(*log.borrow(), vec!["suspend", "resume"]);
    }

    #[test]
    fn test_terminal_handoff_resumes_even_if_the_scope_panics() {
        let log = Rc::new(RefCell::new(Vec::new()));
        let log_for_closure = log.clone();

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
            let _handoff = RecordingTerminalHandoff::new(log_for_closure);
            panic!("simulated failure mid external-process handoff");
        }));

        assert!(result.is_err());
        assert_eq!(*log.borrow(), vec!["suspend", "resume"]);
    }

    fn file_row(name: &str, left: bool, right: bool, is_dir: bool) -> FlatRow {
        let info = FileInfo {
            is_dir,
            size: 10,
            modified: SystemTime::UNIX_EPOCH,
        };
        FlatRow {
            depth: 0,
            relative_path: PathBuf::from(name),
            name: name.to_string(),
            state: DiffState::DifferentNewerLeft,
            left: left.then_some(info.clone()),
            right: right.then_some(info),
        }
    }

    #[test]
    fn diff_launch_outcome_none_without_configured_tool() {
        let mut app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        app.set_external_diff_tool(None);
        app.filter_mut()
            .set_rows(vec![file_row("a.txt", true, true, false)]);
        app.set_selected_idx(0);
        assert_eq!(diff_launch_outcome(&app), KeyOutcome::None);
    }

    #[test]
    fn diff_launch_outcome_none_for_directory() {
        let mut app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        app.set_external_diff_tool(Some("vim".to_string()));
        app.filter_mut()
            .set_rows(vec![file_row("dir", true, true, true)]);
        app.set_selected_idx(0);
        assert_eq!(diff_launch_outcome(&app), KeyOutcome::None);
    }

    #[test]
    fn diff_launch_outcome_none_for_single_sided_file() {
        let mut app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        app.set_external_diff_tool(Some("vim".to_string()));
        app.filter_mut()
            .set_rows(vec![file_row("a.txt", true, false, false)]);
        app.set_selected_idx(0);
        assert_eq!(diff_launch_outcome(&app), KeyOutcome::None);
    }

    #[test]
    fn diff_launch_outcome_builds_paths_for_both_sided_file() {
        let mut app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        app.set_external_diff_tool(Some("vim".to_string()));
        app.filter_mut()
            .set_rows(vec![file_row("a.txt", true, true, false)]);
        app.set_selected_idx(0);
        assert_eq!(
            diff_launch_outcome(&app),
            KeyOutcome::LaunchDiff {
                tool: ExternalDiffTool::Vim,
                left: PathBuf::from("/left/a.txt"),
                right: PathBuf::from("/right/a.txt"),
            }
        );
    }

    #[test]
    fn editor_launch_outcome_none_for_directory() {
        let mut app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        app.focus_left_pane();
        app.filter_mut()
            .set_rows(vec![file_row("dir", true, false, true)]);
        app.set_selected_idx(0);
        assert_eq!(editor_launch_outcome(&app), KeyOutcome::None);
    }

    #[test]
    fn editor_launch_outcome_follows_active_side() {
        let mut app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        app.filter_mut()
            .set_rows(vec![file_row("a.txt", true, true, false)]);
        app.set_selected_idx(0);

        app.focus_left_pane();
        assert_eq!(
            editor_launch_outcome(&app),
            KeyOutcome::LaunchEditor {
                path: PathBuf::from("/left/a.txt"),
            }
        );

        app.focus_right_pane();
        assert_eq!(
            editor_launch_outcome(&app),
            KeyOutcome::LaunchEditor {
                path: PathBuf::from("/right/a.txt"),
            }
        );
    }

    #[test]
    fn editor_launch_outcome_none_when_missing_on_active_side() {
        let mut app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        app.focus_right_pane();
        app.filter_mut()
            .set_rows(vec![file_row("a.txt", true, false, false)]);
        app.set_selected_idx(0);
        assert_eq!(editor_launch_outcome(&app), KeyOutcome::None);
    }

    #[test]
    fn outcomes_are_none_when_selection_out_of_range() {
        let app = App::new(PathBuf::from("/left"), PathBuf::from("/right"));
        assert_eq!(diff_launch_outcome(&app), KeyOutcome::None);
        assert_eq!(editor_launch_outcome(&app), KeyOutcome::None);
    }
}