gravityfile_tui/
lib.rs

1//! Terminal user interface for gravityfile.
2//!
3//! This crate provides an interactive TUI for exploring disk usage,
4//! built with ratatui.
5//!
6//! # Overview
7//!
8//! `gravityfile-tui` provides a feature-rich terminal interface:
9//!
10//! - **Explorer view** - Navigate directory trees sorted by size
11//! - **Duplicates view** - Browse duplicate file groups
12//! - **Age view** - Analyze file age distribution
13//! - **Errors view** - Review scan warnings
14//!
15//! # Usage
16//!
17//! ```rust,no_run
18//! use gravityfile_tui::{self, TuiConfig};
19//! use std::path::PathBuf;
20//!
21//! // Run the TUI on a directory
22//! let config = TuiConfig::default();
23//! gravityfile_tui::run_with_config(PathBuf::from("/path/to/explore"), config).unwrap();
24//! ```
25//!
26//! # Keyboard Navigation
27//!
28//! - `j`/`k` - Move down/up
29//! - `h`/`l` - Collapse/expand directories
30//! - `Enter` - Drill into directory
31//! - `Backspace` - Navigate back
32//! - `Tab` - Switch view
33//! - `d` - Mark for deletion
34//! - `:` - Command palette
35//! - `?` - Help
36//! - `q` - Quit
37
38pub mod app;
39mod event;
40mod opener;
41mod preview;
42mod search;
43mod theme;
44mod ui;
45
46pub use app::{App, AppResult};
47pub use theme::Theme;
48
49/// Configuration for the TUI application.
50#[derive(Debug, Clone, Default)]
51pub struct TuiConfig {
52    /// Whether to automatically start scanning on startup.
53    /// If false (default), only a quick directory listing is shown
54    /// and the user must press 'R' to start a full scan.
55    pub scan_on_startup: bool,
56}
57
58impl TuiConfig {
59    /// Create a new config with default values.
60    pub fn new() -> Self {
61        Self::default()
62    }
63
64    /// Enable auto-scan on startup.
65    pub fn with_scan_on_startup(mut self, scan: bool) -> Self {
66        self.scan_on_startup = scan;
67        self
68    }
69}
70
71/// Run the TUI application with default configuration.
72pub fn run(path: std::path::PathBuf) -> AppResult<()> {
73    run_with_config(path, TuiConfig::default())
74}
75
76/// Run the TUI application with custom configuration.
77pub fn run_with_config(path: std::path::PathBuf, config: TuiConfig) -> AppResult<()> {
78    // Create tokio runtime for async operations
79    let rt = tokio::runtime::Runtime::new()?;
80
81    let terminal = ratatui::init();
82    let result = rt.block_on(App::with_config(path, config).run(terminal));
83    ratatui::restore();
84
85    // Shutdown runtime immediately to cancel background tasks
86    rt.shutdown_timeout(std::time::Duration::from_millis(100));
87
88    result
89}