acme-disk-use 0.3.0

Fast disk usage analyzer with intelligent caching for incremental write workloads
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
//! TUI module for displaying cached disk usage statistics
//!
//! Provides an ncdu-like interface for navigating and viewing directory sizes

use std::io::{self, stdout};
use std::path::PathBuf;

use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind},
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
    ExecutableCommand,
};
use ratatui::{
    prelude::*,
    widgets::{Block, Borders, List, ListItem, ListState, Paragraph},
};

use crate::format_size;
use crate::scanner::DirStat;

/// Entry in the TUI directory list
struct DirEntry {
    path: PathBuf,
    name: String,
    size: u64,
    file_count: u64,
    has_children: bool,
}

/// State for the TUI application
struct App<'a> {
    /// Stored roots for lookup when at root level
    roots: Vec<&'a DirStat>,
    /// Stack of directory stats for navigation (parent directories)
    path_stack: Vec<&'a DirStat>,
    /// Current directory being viewed (None means we're at the roots list)
    current: Option<&'a DirStat>,
    /// List of entries in the current directory
    entries: Vec<DirEntry>,
    /// Currently selected index
    list_state: ListState,
    /// Should quit
    should_quit: bool,
}

impl<'a> App<'a> {
    /// Convert a DirStat to a DirEntry
    fn make_entry(stat: &DirStat) -> DirEntry {
        DirEntry {
            path: stat.path().to_path_buf(),
            name: stat
                .path()
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_else(|| stat.path().display().to_string()),
            size: stat.total_size(),
            file_count: stat.file_count(),
            has_children: !stat.children().is_empty(),
        }
    }

    /// Sort entries by size (descending) and select first item
    fn finalize_entries(&mut self) {
        self.entries.sort_by(|a, b| b.size.cmp(&a.size));
        if !self.entries.is_empty() {
            self.list_state.select(Some(0));
        } else {
            self.list_state.select(None);
        }
    }

    fn new(roots: Vec<&'a DirStat>) -> Self {
        let entries: Vec<DirEntry> = roots.iter().map(|stat| Self::make_entry(stat)).collect();

        let mut app = Self {
            roots,
            path_stack: Vec::new(),
            current: None,
            entries,
            list_state: ListState::default(),
            should_quit: false,
        };

        app.finalize_entries();
        app
    }

    fn from_stat(stat: &'a DirStat) -> Self {
        let mut app = Self {
            roots: vec![stat],
            path_stack: Vec::new(),
            current: Some(stat),
            entries: Vec::new(),
            list_state: ListState::default(),
            should_quit: false,
        };

        app.populate_entries_from_current();
        app
    }

    fn populate_entries_from_current(&mut self) {
        if let Some(stat) = self.current {
            self.entries = stat.children().values().map(Self::make_entry).collect();
        }
        self.finalize_entries();
    }

    fn populate_entries_from_roots(&mut self) {
        self.entries = self
            .roots
            .iter()
            .map(|stat| Self::make_entry(stat))
            .collect();
        self.finalize_entries();
    }

    fn get_current_path(&self) -> String {
        if let Some(stat) = self.current {
            stat.path().display().to_string()
        } else {
            "Cached Roots".to_string()
        }
    }

    fn get_current_total_size(&self) -> u64 {
        if let Some(stat) = self.current {
            stat.total_size()
        } else {
            self.entries.iter().map(|e| e.size).sum()
        }
    }

    fn move_up(&mut self) {
        if let Some(selected) = self.list_state.selected() {
            if selected > 0 {
                self.list_state.select(Some(selected - 1));
            }
        }
    }

    fn move_down(&mut self) {
        if let Some(selected) = self.list_state.selected() {
            if selected < self.entries.len().saturating_sub(1) {
                self.list_state.select(Some(selected + 1));
            }
        }
    }

    fn enter_selected(&mut self) {
        if let Some(selected) = self.list_state.selected() {
            if selected < self.entries.len() && self.entries[selected].has_children {
                let selected_path = &self.entries[selected].path;

                // Find the DirStat for the selected entry
                let child_stat = if let Some(current) = self.current {
                    // We're inside a directory, look in its children
                    current.children().get(selected_path)
                } else {
                    // We're at root level, look in the roots
                    self.roots
                        .iter()
                        .find(|r| r.path() == selected_path)
                        .copied()
                };

                if let Some(stat) = child_stat {
                    // Push current to stack and navigate to child
                    if let Some(current) = self.current {
                        self.path_stack.push(current);
                    }
                    self.current = Some(stat);
                    self.populate_entries_from_current();
                }
            }
        }
    }

    fn go_back(&mut self) {
        if let Some(parent) = self.path_stack.pop() {
            self.current = Some(parent);
            self.populate_entries_from_current();
        } else if self.current.is_some() {
            // We're at a root, go back to roots list
            self.current = None;
            self.populate_entries_from_roots();
        }
    }

    fn handle_key(&mut self, code: KeyCode) {
        match code {
            KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
            KeyCode::Up | KeyCode::Char('k') => self.move_up(),
            KeyCode::Down | KeyCode::Char('j') => self.move_down(),
            KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => self.enter_selected(),
            KeyCode::Backspace | KeyCode::Left | KeyCode::Char('h') => self.go_back(),
            _ => {}
        }
    }
}

/// Run the TUI with a single DirStat (the root of a scanned directory)
pub fn run_tui(stat: &DirStat) -> io::Result<()> {
    // Setup terminal
    enable_raw_mode()?;
    stdout().execute(EnterAlternateScreen)?;

    let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
    let mut app = App::from_stat(stat);

    // Main loop
    loop {
        terminal.draw(|frame| render(&mut app, frame))?;

        if let Event::Key(key) = event::read()? {
            if key.kind == KeyEventKind::Press {
                app.handle_key(key.code);
            }
        }

        if app.should_quit {
            break;
        }
    }

    // Cleanup
    disable_raw_mode()?;
    stdout().execute(LeaveAlternateScreen)?;

    Ok(())
}

/// Run the TUI with multiple cached roots
pub fn run_tui_with_roots(roots: Vec<&DirStat>) -> io::Result<()> {
    if roots.is_empty() {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "No cached directories found",
        ));
    }

    // Setup terminal
    enable_raw_mode()?;
    stdout().execute(EnterAlternateScreen)?;

    let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
    let mut app = App::new(roots);

    // Main loop
    loop {
        terminal.draw(|frame| render(&mut app, frame))?;

        if let Event::Key(key) = event::read()? {
            if key.kind == KeyEventKind::Press {
                app.handle_key(key.code);
            }
        }

        if app.should_quit {
            break;
        }
    }

    // Cleanup
    disable_raw_mode()?;
    stdout().execute(LeaveAlternateScreen)?;

    Ok(())
}

fn render(app: &mut App, frame: &mut Frame) {
    let area = frame.area();

    // Create layout with header, main content, and footer
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(0),    // Content
            Constraint::Length(3), // Footer
        ])
        .split(area);

    // Header with current path and total size
    let total_size = format_size(app.get_current_total_size(), true);
    let header_text = format!("{} (Total: {})", app.get_current_path(), total_size);
    let header = Paragraph::new(header_text)
        .style(
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("acme-disk-use"),
        );
    frame.render_widget(header, chunks[0]);

    // Content - directory listing
    let items: Vec<ListItem> = app
        .entries
        .iter()
        .map(|entry| {
            let size_str = format_size(entry.size, true);
            let indicator = if entry.has_children { "/" } else { "" };
            let line = format!(
                "{:>12}  {:>6} files  {}{}",
                size_str, entry.file_count, entry.name, indicator
            );
            ListItem::new(line)
        })
        .collect();

    let list = List::new(items)
        .block(Block::default().borders(Borders::ALL).title("Directories"))
        .highlight_style(
            Style::default()
                .bg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("> ");

    frame.render_stateful_widget(list, chunks[1], &mut app.list_state);

    // Footer with help text
    let help_text = "↑/k: Up | ↓/j: Down | Enter/→/l: Open | Backspace/←/h: Back | q/Esc: Quit";
    let footer = Paragraph::new(help_text)
        .style(Style::default().fg(Color::DarkGray))
        .block(Block::default().borders(Borders::ALL).title("Help"));
    frame.render_widget(footer, chunks[2]);
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::time::SystemTime;

    fn create_test_stat() -> DirStat {
        DirStat {
            path: PathBuf::from("/test"),
            total_size: 1000,
            file_count: 10,
            last_scan: SystemTime::now(),
            children: HashMap::new(),
        }
    }

    #[test]
    fn test_dir_entry_sorting() {
        // Create test stats with different sizes
        let stat1 = DirStat {
            path: PathBuf::from("/test/small"),
            total_size: 100,
            file_count: 1,
            last_scan: SystemTime::now(),
            children: HashMap::new(),
        };

        let stat2 = DirStat {
            path: PathBuf::from("/test/large"),
            total_size: 1000,
            file_count: 10,
            last_scan: SystemTime::now(),
            children: HashMap::new(),
        };

        let roots: Vec<&DirStat> = vec![&stat1, &stat2];
        let app = App::new(roots);

        // Verify entries are sorted by size descending
        assert_eq!(app.entries.len(), 2);
        assert_eq!(app.entries[0].size, 1000); // Large first
        assert_eq!(app.entries[1].size, 100); // Small second
    }

    #[test]
    fn test_app_navigation() {
        let stat = create_test_stat();
        let mut app = App::from_stat(&stat);

        // Test that navigation doesn't crash with empty entries
        app.move_up();
        app.move_down();
        app.enter_selected();
        app.go_back();
    }

    #[test]
    fn test_go_back_to_roots() {
        // Create a root stat with children
        let child = DirStat {
            path: PathBuf::from("/test/child"),
            total_size: 500,
            file_count: 5,
            last_scan: SystemTime::now(),
            children: HashMap::new(),
        };

        let mut children = HashMap::new();
        children.insert(PathBuf::from("/test/child"), child);

        let root = DirStat {
            path: PathBuf::from("/test"),
            total_size: 1000,
            file_count: 10,
            last_scan: SystemTime::now(),
            children,
        };

        let roots: Vec<&DirStat> = vec![&root];
        let mut app = App::new(roots);

        // Start at roots list
        assert!(app.current.is_none());
        assert_eq!(app.get_current_path(), "Cached Roots");

        // Navigate into root
        app.enter_selected();
        assert!(app.current.is_some());
        assert_eq!(app.get_current_path(), "/test");

        // Go back to roots
        app.go_back();
        assert!(app.current.is_none());
        assert_eq!(app.get_current_path(), "Cached Roots");
    }
}