rustdupe 0.2.0

Smart duplicate file finder with interactive TUI
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
//! TUI main loop.
//!
//! This module provides the main entry point for running the interactive TUI.
//! It handles terminal setup, the event loop, and cleanup on exit.
//!
//! # Terminal Management
//!
//! The TUI takes over the terminal by:
//! - Enabling raw mode (unbuffered input, no echo)
//! - Entering the alternate screen buffer
//! - Hiding the cursor
//!
//! All these changes are reverted on exit, including on panic.
//!
//! # Event Loop
//!
//! The main loop follows this pattern:
//! 1. Poll for events with a timeout
//! 2. Handle any event that occurred
//! 3. Render the current state
//! 4. Limit frame rate to ~60 FPS
//!
//! # Example
//!
//! ```no_run
//! use rustdupe::tui::{run_tui, App};
//! use rustdupe::duplicates::DuplicateGroup;
//! use std::path::PathBuf;
//!
//! let groups = vec![
//!     DuplicateGroup::new(
//!         [0u8; 32],
//!         1000,
//!         vec![
//!             rustdupe::scanner::FileEntry::new(PathBuf::from("/a.txt"), 1000, std::time::SystemTime::now()),
//!             rustdupe::scanner::FileEntry::new(PathBuf::from("/b.txt"), 1000, std::time::SystemTime::now()),
//!         ],
//!         vec![],
//!     ),
//! ];
//! let mut app = App::with_groups(groups);
//!
//! // Run the TUI
//! run_tui(&mut app, None).unwrap();
//! ```

use std::io::{self, Stdout};
use std::panic;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use crossterm::{
    cursor,
    event::{DisableMouseCapture, EnableMouseCapture},
    execute,
    terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use thiserror::Error;

use super::app::{Action, App, AppMode};
use super::events::EventHandler;
use super::ui::render;
use crate::actions::delete::{delete_batch, validate_preserves_copy, DeleteConfig};
use crate::actions::preview::preview_file_simple;

/// Frame rate limit: 60 FPS = ~16.67ms per frame.
/// Using 16ms for slightly conservative timing.
const FRAME_DURATION: Duration = Duration::from_millis(16);

/// Event poll timeout: Use the frame duration for responsive rendering.
const POLL_TIMEOUT: Duration = Duration::from_millis(16);

/// Error type for TUI operations.
#[derive(Debug, Error)]
pub enum TuiError {
    /// I/O error from terminal operations.
    #[error("terminal I/O error: {0}")]
    Io(#[from] io::Error),

    /// Event handling error.
    #[error("event error: {0}")]
    Event(#[from] super::events::EventError),

    /// The TUI was interrupted by a shutdown signal.
    #[error("interrupted by shutdown signal")]
    Interrupted,

    /// Error during file deletion.
    #[error("deletion error: {0}")]
    DeleteError(String),
}

/// Result type for TUI operations.
pub type TuiResult<T> = Result<T, TuiError>;

/// Type alias for the terminal backend.
type Terminal = ratatui::Terminal<CrosstermBackend<Stdout>>;

/// Run the interactive TUI.
///
/// This function takes over the terminal and runs the interactive interface
/// until the user quits or an error occurs.
///
/// # Arguments
///
/// * `app` - The application state, typically pre-loaded with duplicate groups
/// * `shutdown_flag` - Optional flag for external shutdown signaling (e.g., Ctrl+C handler)
///
/// # Returns
///
/// Returns `Ok(())` on normal exit, or `Err` on error.
///
/// # Terminal Restoration
///
/// The terminal is always restored to its original state, even on error or panic.
///
/// # Errors
///
/// Returns `TuiError::Io` for terminal I/O errors.
/// Returns `TuiError::Event` for event handling errors.
/// Returns `TuiError::Interrupted` if shutdown was requested.
///
/// # Example
///
/// ```no_run
/// use rustdupe::tui::{run_tui, App};
///
/// let mut app = App::new();
/// if let Err(e) = run_tui(&mut app, None) {
///     eprintln!("TUI error: {}", e);
/// }
/// ```
pub fn run_tui(app: &mut App, shutdown_flag: Option<Arc<AtomicBool>>) -> TuiResult<()> {
    // Set up panic hook to restore terminal on panic
    let original_hook = panic::take_hook();
    panic::set_hook(Box::new(move |panic_info| {
        // Restore terminal before showing panic message
        let _ = restore_terminal();
        original_hook(panic_info);
    }));

    // Run the TUI with proper cleanup
    let result = run_tui_inner(app, shutdown_flag);

    // Restore the original panic hook
    let _ = panic::take_hook();

    result
}

/// Inner function that runs the TUI loop.
///
/// This is separated from `run_tui` to ensure cleanup happens correctly.
fn run_tui_inner(app: &mut App, shutdown_flag: Option<Arc<AtomicBool>>) -> TuiResult<()> {
    // Set up the terminal
    let mut terminal = setup_terminal()?;

    // Create event handler
    let event_handler = EventHandler::new();

    // Track frame timing for rate limiting
    let mut last_render = Instant::now();

    // Main loop
    loop {
        // Check for external shutdown signal
        if let Some(ref flag) = shutdown_flag {
            if flag.load(Ordering::SeqCst) {
                log::info!("Shutdown signal received, exiting TUI");
                break;
            }
        }

        // Check if app wants to quit
        if app.should_quit() {
            log::debug!("App requested quit");
            break;
        }

        // Render the current state
        terminal.draw(|frame| render(frame, app))?;

        // Poll for events with timeout
        if let Some(action) = event_handler.poll(POLL_TIMEOUT)? {
            handle_action(app, action, &shutdown_flag)?;
        }

        // Frame rate limiting
        let elapsed = last_render.elapsed();
        if elapsed < FRAME_DURATION {
            std::thread::sleep(FRAME_DURATION - elapsed);
        }
        last_render = Instant::now();
    }

    // Cleanup - restore terminal
    restore_terminal()?;

    log::info!("TUI exited normally");
    Ok(())
}

/// Handle a user action.
///
/// This processes actions that require more than simple state updates,
/// like file deletion or preview loading.
fn handle_action(
    app: &mut App,
    action: Action,
    _shutdown_flag: &Option<Arc<AtomicBool>>,
) -> TuiResult<()> {
    // First, let the app handle the action for state updates
    let was_handled = app.handle_action(action);

    // Handle special actions that require additional processing
    match action {
        Action::Confirm => {
            if app.mode() == AppMode::Confirming {
                // Perform the actual deletion
                let result = perform_deletion(app);
                match result {
                    Ok(deleted_count) => {
                        log::info!("Deleted {} files", deleted_count);
                        app.set_mode(AppMode::Reviewing);
                    }
                    Err(e) => {
                        app.set_error(&format!("Deletion failed: {}", e));
                        app.set_mode(AppMode::Reviewing);
                    }
                }
            }
        }
        Action::Preview => {
            if app.mode() == AppMode::Previewing {
                // Load preview content for the current file
                if let Some(path) = app.current_file() {
                    let content = preview_file_simple(path);
                    app.set_preview(content);
                }
            }
        }
        Action::Cancel => {
            // Clear any error message on cancel
            if app.error_message().is_some() {
                app.clear_error();
            }
        }
        _ => {
            // Other actions are handled by app.handle_action()
            if !was_handled {
                log::trace!("Action not handled: {:?}", action);
            }
        }
    }

    Ok(())
}

/// Perform file deletion for selected files.
fn perform_deletion(app: &mut App) -> Result<usize, TuiError> {
    let selected_files = app.selected_files_vec();

    if selected_files.is_empty() {
        return Ok(0);
    }

    // Validate that we're not deleting all copies
    // We need to check for each group
    for group in app.groups() {
        let group_paths = group.paths();
        if let Err(_e) = validate_preserves_copy(&selected_files, &group_paths) {
            return Err(TuiError::DeleteError(
                "Cannot delete all copies - at least one file must be preserved".to_string(),
            ));
        }
    }

    // Use trash deletion by default
    let config = DeleteConfig::trash();

    // Perform deletion
    let result = delete_batch(&selected_files, &config, None::<&NoOpProgress>);

    // Update app state with deleted files
    let deleted_paths: Vec<_> = result.successes.iter().map(|r| r.path.clone()).collect();
    app.remove_deleted_files(&deleted_paths);

    // Report any failures
    if !result.failures.is_empty() {
        let (failed_path, error_msg) = &result.failures[0];
        log::warn!(
            "Some files failed to delete: {} - {}",
            failed_path.display(),
            error_msg
        );
        if result.successes.is_empty() {
            return Err(TuiError::DeleteError(format!(
                "Failed to delete files: {}",
                error_msg
            )));
        }
    }

    Ok(result.success_count())
}

/// Placeholder progress callback that does nothing.
struct NoOpProgress;

impl crate::actions::delete::DeleteProgressCallback for NoOpProgress {
    fn on_before_delete(&self, _path: &std::path::Path, _index: usize, _total: usize) {}
    fn on_delete_success(&self, _path: &std::path::Path, _size: u64) {}
    fn on_delete_failure(&self, _path: &std::path::Path, _error: &str) {}
    fn on_complete(&self, _result: &crate::actions::delete::BatchDeleteResult) {}
}

/// Set up the terminal for TUI mode.
fn setup_terminal() -> TuiResult<Terminal> {
    log::debug!("Setting up terminal for TUI");

    // Enable raw mode (no line buffering, no echo)
    terminal::enable_raw_mode()?;

    // Set up stdout with crossterm commands
    let mut stdout = io::stdout();
    execute!(
        stdout,
        EnterAlternateScreen,
        EnableMouseCapture,
        cursor::Hide
    )?;

    // Create the ratatui terminal
    let backend = CrosstermBackend::new(stdout);
    let terminal = Terminal::new(backend)?;

    log::debug!("Terminal setup complete");
    Ok(terminal)
}

/// Restore the terminal to its original state.
fn restore_terminal() -> TuiResult<()> {
    log::debug!("Restoring terminal");

    // Disable raw mode
    let _ = terminal::disable_raw_mode();

    // Restore stdout
    let mut stdout = io::stdout();
    let _ = execute!(
        stdout,
        LeaveAlternateScreen,
        DisableMouseCapture,
        cursor::Show
    );

    log::debug!("Terminal restored");
    Ok(())
}

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

    #[test]
    fn test_tui_error_display() {
        let io_err = io::Error::other("test error");
        let tui_err = TuiError::Io(io_err);
        assert!(format!("{}", tui_err).contains("terminal I/O error"));

        let interrupted = TuiError::Interrupted;
        assert!(format!("{}", interrupted).contains("interrupted"));
    }

    #[test]
    fn test_preview_file_simple_nonexistent() {
        let content = preview_file_simple(std::path::Path::new("/nonexistent/file.txt"));
        assert!(content.to_lowercase().contains("error") || content.contains("not found"));
    }

    #[test]
    fn test_preview_file_simple_with_temp_file() {
        use std::io::Write;

        // Create a temporary file
        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join("rustdupe_test_preview.txt");

        {
            let mut file = std::fs::File::create(&temp_path).unwrap();
            writeln!(file, "Line 1").unwrap();
            writeln!(file, "Line 2").unwrap();
            writeln!(file, "Line 3").unwrap();
        }

        let content = preview_file_simple(&temp_path);
        assert!(content.contains("Line 1"));
        assert!(content.contains("Line 2"));
        assert!(content.contains("Line 3"));

        // Cleanup
        let _ = std::fs::remove_file(&temp_path);
    }

    #[test]
    fn test_preview_file_simple_empty_file() {
        use std::fs::File;

        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join("rustdupe_test_empty.txt");

        // Create empty file
        File::create(&temp_path).unwrap();

        let content = preview_file_simple(&temp_path);
        assert!(content.contains("empty"));

        // Cleanup
        let _ = std::fs::remove_file(&temp_path);
    }

    #[test]
    fn test_frame_duration() {
        // Verify the frame duration is reasonable for 60 FPS
        assert_eq!(FRAME_DURATION.as_millis(), 16);
    }

    #[test]
    fn test_poll_timeout() {
        // Poll timeout should match frame duration for responsive updates
        assert_eq!(POLL_TIMEOUT.as_millis(), 16);
    }

    // Note: We can't easily test the actual TUI without a real terminal,
    // but we can test the supporting functions.

    #[test]
    fn test_noop_progress_callback() {
        use crate::actions::delete::DeleteProgressCallback;

        // Verify NoOpProgress implements the trait correctly
        let progress = NoOpProgress;
        progress.on_before_delete(std::path::Path::new("/test"), 0, 1);
        progress.on_delete_success(std::path::Path::new("/test"), 100);
        progress.on_delete_failure(std::path::Path::new("/test"), "error");

        // Create a mock result for on_complete
        let result = crate::actions::delete::BatchDeleteResult {
            successes: vec![],
            failures: vec![],
            bytes_freed: 0,
        };
        progress.on_complete(&result);
    }

    mod perform_deletion_tests {
        use super::*;
        use crate::duplicates::DuplicateGroup;
        use crate::tui::App;

        fn make_group(size: u64, paths: Vec<&str>) -> DuplicateGroup {
            DuplicateGroup::new(
                [0u8; 32],
                size,
                paths
                    .into_iter()
                    .map(|p| {
                        crate::scanner::FileEntry::new(
                            PathBuf::from(p),
                            size,
                            std::time::SystemTime::now(),
                        )
                    })
                    .collect(),
                Vec::new(),
            )
        }

        #[test]
        fn test_perform_deletion_empty_selection() {
            let groups = vec![make_group(100, vec!["/a.txt", "/b.txt"])];
            let mut app = App::with_groups(groups);

            // No files selected
            let result = perform_deletion(&mut app);
            assert!(result.is_ok());
            assert_eq!(result.unwrap(), 0);
        }

        #[test]
        fn test_perform_deletion_prevents_deleting_all_copies() {
            let groups = vec![make_group(100, vec!["/a.txt", "/b.txt"])];
            let mut app = App::with_groups(groups);

            // Select all files (which should be prevented)
            app.select(PathBuf::from("/a.txt"));
            app.select(PathBuf::from("/b.txt"));

            let result = perform_deletion(&mut app);
            assert!(result.is_err());
        }
    }
}