duodiff 0.4.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
//! Shared actions: scan, copy, palette, and external tools.
use crate::app::{self, App};
use crate::diff_tool;
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;

pub fn run_external_diff<B: ratatui::backend::Backend>(
    tool: &diff_tool::ExternalDiffTool,
    left: &std::path::Path,
    right: &std::path::Path,
    terminal: &mut ratatui::Terminal<B>,
) -> Result<(), Box<dyn std::error::Error>>
where
    B::Error: 'static,
{
    use std::io::IsTerminal;
    let is_terminal = std::io::stdout().is_terminal();
    if is_terminal {
        disable_raw_mode()?;
        execute!(
            std::io::stdout(),
            LeaveAlternateScreen,
            crossterm::event::DisableMouseCapture
        )?;
    }

    let res = diff_tool::open_diff(tool, left, right);
    if let Err(e) = res {
        eprintln!(
            "Error launching external diff: {}. Press Enter to continue...",
            e
        );
        let mut buf = String::new();
        let _ = std::io::stdin().read_line(&mut buf);
    } else if matches!(tool, diff_tool::ExternalDiffTool::Difftastic) {
        println!("\nPress Enter to return to duodiff...");
        let mut buf = String::new();
        let _ = std::io::stdin().read_line(&mut buf);
    }

    if is_terminal {
        enable_raw_mode()?;
        execute!(
            std::io::stdout(),
            EnterAlternateScreen,
            crossterm::event::EnableMouseCapture
        )?;
    }
    terminal.clear()?;
    Ok(())
}

pub fn run_external_editor<B: ratatui::backend::Backend>(
    file_path: &std::path::Path,
    terminal: &mut ratatui::Terminal<B>,
) -> Result<(), Box<dyn std::error::Error>>
where
    B::Error: 'static,
{
    use std::io::IsTerminal;
    let is_terminal = std::io::stdout().is_terminal();
    if is_terminal {
        disable_raw_mode()?;
        execute!(
            std::io::stdout(),
            LeaveAlternateScreen,
            crossterm::event::DisableMouseCapture
        )?;
    }

    let res = diff_tool::open_editor(file_path);
    if let Err(e) = res {
        eprintln!(
            "Error launching external editor: {}. Press Enter to continue...",
            e
        );
        let mut buf = String::new();
        let _ = std::io::stdin().read_line(&mut buf);
    }

    if is_terminal {
        enable_raw_mode()?;
        execute!(
            std::io::stdout(),
            EnterAlternateScreen,
            crossterm::event::EnableMouseCapture
        )?;
    }
    terminal.clear()?;
    Ok(())
}

pub async fn execute_confirm_action(
    app: &mut App,
    tx: tokio::sync::mpsc::Sender<AppEvent>,
) -> Result<(), Box<dyn std::error::Error>> {
    app.show_confirm_modal = false;
    if let Some(action) = app.confirm_modal_action.take() {
        if app.selected_idx < app.filtered_rows.len() {
            let row = &app.filtered_rows[app.selected_idx];
            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.clone(),
                app::ConfirmAction::CopyRightToLeft => app.left_path.clone(),
            };

            // 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.view_mode = app::ViewMode::DirectoryTree;
                    // 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.clone(),
        app.right_path.clone(),
        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::app::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 {
        "ext_diff" => {
            if app.selected_idx < app.filtered_rows.len() {
                let row = &app.filtered_rows[app.selected_idx];
                if let Some(ref tool_str) = app.settings.external_diff_tool {
                    if let Ok(tool) = diff_tool::ExternalDiffTool::from_str(tool_str) {
                        let left_file = app.left_path.join(&row.relative_path);
                        let right_file = app.right_path.join(&row.relative_path);
                        run_external_diff(&tool, &left_file, &right_file, terminal)?;
                    }
                }
            }
        }
        "ext_edit" => {
            if app.selected_idx < app.filtered_rows.len() {
                let row = &app.filtered_rows[app.selected_idx];
                let file_exists = if app.active_side_left {
                    row.left.as_ref().map(|f| !f.is_dir).unwrap_or(false)
                } else {
                    row.right.as_ref().map(|f| !f.is_dir).unwrap_or(false)
                };
                if file_exists {
                    let file_path = if app.active_side_left {
                        app.left_path.join(&row.relative_path)
                    } else {
                        app.right_path.join(&row.relative_path)
                    };
                    run_external_editor(&file_path, terminal)?;
                }
            }
        }
        "copy_l2r" => {
            if app.selected_idx < app.filtered_rows.len() {
                let row = &app.filtered_rows[app.selected_idx];
                if row.left.is_some() {
                    app.show_confirm_modal = true;
                    app.confirm_modal_message = format!("Copy '{}' to right side?", row.name);
                    app.confirm_modal_action = Some(app::ConfirmAction::CopyLeftToRight);
                }
            }
        }
        "copy_r2l" => {
            if app.selected_idx < app.filtered_rows.len() {
                let row = &app.filtered_rows[app.selected_idx];
                if row.right.is_some() {
                    app.show_confirm_modal = true;
                    app.confirm_modal_message = format!("Copy '{}' to left side?", row.name);
                    app.confirm_modal_action = Some(app::ConfirmAction::CopyRightToLeft);
                }
            }
        }
        "builtin_diff" => {
            app.enter_file_diff();
        }
        "swap_paths" => {
            app.swap_paths();
            kick_scan(app, tx);
        }
        "toggle_scan" => {
            app.precise_mode = !app.precise_mode;
            kick_scan(app, tx);
        }
        "refresh" => {
            kick_scan(app, tx);
        }
        "config" => {
            app.open_config();
        }
        "help" => {
            app.open_help();
        }
        "filter" => {
            app.filter_active = true;
            app.filter_input.clear();
        }
        "quit" => {
            app.should_quit = true;
        }
        "toggle_wrap" => {
            app.diff_wrap = !app.diff_wrap;
            app.diff_scroll = 0;
            app.diff_h_scroll = 0;
        }
        "toggle_full" => {
            app.diff_show_full = !app.diff_show_full;
            if let Err(e) = app.refresh_file_diff() {
                app.diff_show_full = !app.diff_show_full;
                app.set_status(format!("Cannot refresh diff: {e}"), true);
            } else {
                app.diff_scroll = 0;
                app.diff_h_scroll = 0;
            }
        }
        "next_change" => {
            app.jump_to_next_change();
        }
        "prev_change" => {
            app.jump_to_prev_change();
        }
        "copy_hunk_l2r" => {
            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),
            }
        }
        "copy_hunk_r2l" => {
            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),
            }
        }
        "back" => {
            if app.view_mode == app::ViewMode::FileDiff {
                app.view_mode = app::ViewMode::DirectoryTree;
            } else {
                app.view_mode = app.help_return_view;
            }
        }
        _ => {}
    }
    Ok(())
}

pub fn open_repo_url(app: &mut App) {
    app.set_status("Opening GitHub repository in the browser...", false);
    let url = "https://github.com/akunzai/duodiff";
    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(),
        };
    });
}