disktree 0.2.0

Disk usage TUI: scan a drive, see what's taking space, and delete it safely
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
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
use std::path::Path;
use std::sync::atomic::Ordering;
use std::time::Duration;

use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Color, Modifier, Style, Stylize};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Cell, Clear, Paragraph, Row, Table};

use crate::app::{App, Mode, format_count, format_size};
use crate::index::ScanProgress;

const SPINNER: [char; 10] = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
const BAR_WIDTH: usize = 10;
const PARTIAL_BLOCKS: [char; 8] = [' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉'];

pub fn draw(f: &mut Frame, app: &mut App) {
    let [header_area, table_area, footer_area] = Layout::vertical([
        Constraint::Length(1),
        Constraint::Min(0),
        Constraint::Length(2),
    ])
    .areas(f.area());

    draw_header(f, app, header_area);
    draw_table(f, app, table_area);
    draw_footer(f, app, footer_area);

    match &app.mode {
        Mode::Confirm { permanent, targets } => draw_confirm(f, app, *permanent, targets),
        Mode::Deleting { count } => draw_deleting(f, *count),
        Mode::Browse => {}
    }
}

fn draw_header(f: &mut Frame, app: &App, area: Rect) {
    let node = &app.tree.nodes[app.cwd];
    let mut text = format!(
        " {}  —  {}, {} files",
        app.tree.path_of(app.cwd).display(),
        format_size(node.size),
        format_count(node.n_files),
    );
    if app.tree.errors > 0 {
        text.push_str(&format!("  ({} unreadable)", format_count(app.tree.errors)));
    }
    // Keep the tail (current dir + totals) visible when the path is long.
    let text = truncate_left(&text, area.width as usize);
    f.render_widget(
        Paragraph::new(text).style(
            Style::new()
                .add_modifier(Modifier::BOLD)
                .bg(Color::DarkGray),
        ),
        area,
    );
}

fn draw_table(f: &mut Frame, app: &mut App, area: Rect) {
    let total = app.tree.nodes[app.cwd].size.max(1);
    // One line of the area is the header, so it cannot hold data rows.
    let window = app.visible_range(area.height.saturating_sub(1) as usize);

    let rows: Vec<Row> = app.rows[window.clone()]
        .iter()
        .map(|&idx| {
            let node = &app.tree.nodes[idx];
            let marked = app.marked.contains(&idx);
            let pct = node.size as f64 / total as f64 * 100.0;

            let mark_cell =
                Cell::from(if marked { "●" } else { " " }).style(Style::new().fg(Color::Yellow));
            let size_cell = Cell::from(format!("{:>10}", format_size(node.size)));
            let pct_cell = Cell::from(format!("{pct:>5.1}%"));
            let bar_cell = Cell::from(bar(pct)).style(Style::new().fg(Color::Green));
            let items_cell = Cell::from(if node.is_dir {
                format!("{:>10}", format_count(node.n_files))
            } else {
                String::new()
            });
            let name = node.name.to_string_lossy().into_owned();
            let name_cell = if node.is_dir {
                Cell::from(format!("{name}\\"))
                    .style(Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD))
            } else {
                Cell::from(name)
            };

            let row = Row::new(vec![
                mark_cell, size_cell, pct_cell, bar_cell, items_cell, name_cell,
            ]);
            if marked {
                row.style(Style::new().fg(Color::Yellow))
            } else {
                row
            }
        })
        .collect();

    let table = Table::new(
        rows,
        [
            Constraint::Length(1),
            Constraint::Length(10),
            Constraint::Length(6),
            Constraint::Length(BAR_WIDTH as u16),
            Constraint::Length(10),
            Constraint::Min(10),
        ],
    )
    .header(
        Row::new(vec!["", "      Size", "     %", "", "     Items", "Name"]).style(
            Style::new()
                .add_modifier(Modifier::UNDERLINED)
                .fg(Color::Gray),
        ),
    )
    .row_highlight_style(Style::new().bg(Color::Blue));

    // The table only received the visible slice, so the highlight index must be
    // relative to the window, not to the full row list.
    app.table_state.select(if window.is_empty() {
        None
    } else {
        Some(app.selected.saturating_sub(window.start))
    });
    f.render_stateful_widget(table, area, &mut app.table_state);

    if app.rows.is_empty() {
        let msg = Paragraph::new("  (empty directory)").style(Style::new().fg(Color::DarkGray));
        let inner = Rect {
            y: area.y + 1,
            height: 1,
            ..area
        };
        f.render_widget(msg, inner);
    }
}

fn draw_footer(f: &mut Frame, app: &App, area: Rect) {
    let [info_area, keys_area] =
        Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);

    let info: Line = if let Some(status) = &app.status {
        Line::from(Span::styled(
            format!(" {status}"),
            Style::new().fg(Color::Green),
        ))
    } else if !app.marked.is_empty() {
        let marked_size: u64 = app.marked.iter().map(|&i| app.tree.nodes[i].size).sum();
        Line::from(Span::styled(
            format!(
                " {} marked — {}",
                app.marked.len(),
                format_size(marked_size)
            ),
            Style::new().fg(Color::Yellow),
        ))
    } else {
        Line::default()
    };
    f.render_widget(Paragraph::new(info), info_area);

    let keys = " ↑↓ move  ⏎ open  ⌫ up  space mark  c clear  d → recycle bin  D delete forever  r rescan  q quit";
    f.render_widget(
        Paragraph::new(keys).style(Style::new().fg(Color::DarkGray)),
        keys_area,
    );
}

fn draw_confirm(f: &mut Frame, app: &App, permanent: bool, targets: &[usize]) {
    let total: u64 = targets.iter().map(|&t| app.tree.nodes[t].size).sum();
    let (title, color) = if permanent {
        (" Permanent delete ", Color::Red)
    } else {
        (" Move to Recycle Bin ", Color::Yellow)
    };

    let mut lines: Vec<Line> = Vec::new();
    let question = if permanent {
        format!(
            "PERMANENTLY delete {} item(s) ({})? This cannot be undone.",
            targets.len(),
            format_size(total)
        )
    } else {
        format!(
            "Move {} item(s) ({}) to the Recycle Bin?",
            targets.len(),
            format_size(total)
        )
    };
    lines.push(Line::from(question));
    lines.push(Line::default());

    const MAX_SHOWN: usize = 6;
    for &t in targets.iter().take(MAX_SHOWN) {
        // Full path, not just the name: marks are held across navigation, so a
        // bare name list renders three unrelated "bin" folders as "bin, bin, bin"
        // with nothing to distinguish what is about to be deleted.
        let size = format_size(app.tree.nodes[t].size);
        let path = app.tree.path_of(t).display().to_string();
        let room = 66usize.saturating_sub(size.len() + 6);
        lines.push(Line::from(format!(
            "  {}  ({size})",
            truncate_left(&path, room)
        )));
    }
    if targets.len() > MAX_SHOWN {
        lines.push(Line::from(format!(
            "  … and {} more",
            targets.len() - MAX_SHOWN
        )));
    }
    lines.push(Line::default());
    lines.push(Line::from(vec![
        Span::styled("[Y]", Style::new().add_modifier(Modifier::BOLD)),
        Span::raw("es    "),
        Span::styled("[N]", Style::new().add_modifier(Modifier::BOLD)),
        Span::raw("o"),
    ]));

    let height = lines.len() as u16 + 2;
    let area = centered(70, height, f.area());
    f.render_widget(Clear, area);
    f.render_widget(
        Paragraph::new(lines).block(
            Block::bordered()
                .title(title)
                .border_style(Style::new().fg(color)),
        ),
        area,
    );
}

fn draw_deleting(f: &mut Frame, count: usize) {
    let area = centered(40, 3, f.area());
    f.render_widget(Clear, area);
    f.render_widget(
        Paragraph::new(format!("Deleting {count} item(s)…"))
            .centered()
            .block(Block::bordered().border_style(Style::new().fg(Color::Yellow))),
        area,
    );
}

/// Which scanner is running, since the two report progress in different units.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ScanMode {
    /// Walking directories: counts folders and files as it goes.
    Walk,
    /// Reading the NTFS MFT: counts record slots and bytes of `$MFT`.
    Mft,
}

pub fn draw_scanning(
    f: &mut Frame,
    root: &Path,
    progress: &ScanProgress,
    elapsed: Duration,
    mode: ScanMode,
) {
    let area = centered(72, 9, f.area());
    let spinner = SPINNER[(elapsed.as_millis() / 80) as usize % SPINNER.len()];

    let dirs = progress.dirs.load(Ordering::Relaxed);
    let files = progress.files.load(Ordering::Relaxed);
    let bytes = progress.bytes.load(Ordering::Relaxed);
    let errors = progress.errors.load(Ordering::Relaxed);
    let current = progress
        .current
        .lock()
        .map(|c| c.clone())
        .unwrap_or_default();

    let max_path = area.width.saturating_sub(14) as usize;
    let shown_path = truncate_left(&current, max_path);

    let heading = match mode {
        ScanMode::Walk => format!("{spinner}  Scanning {}", root.display()),
        ScanMode::Mft => format!("{spinner}  Reading $MFT for {}", root.display()),
    };
    // An MFT scan has no notion of "folders visited"; it streams record slots, so
    // reusing the walk's labels would misreport what is happening.
    let counts = match mode {
        ScanMode::Walk => format!(
            "{} folders    {} files    {}",
            format_count(dirs),
            format_count(files),
            format_size(bytes)
        ),
        ScanMode::Mft => format!(
            "{} records    {} read",
            format_count(dirs),
            format_size(bytes)
        ),
    };
    let errors_label = match mode {
        ScanMode::Walk => format!("unreadable: {}", format_count(errors)),
        ScanMode::Mft => format!("skipped records: {}", format_count(errors)),
    };

    let mut lines = vec![
        Line::from(heading).bold(),
        Line::default(),
        Line::from(counts),
        Line::from(errors_label).style(Style::new().fg(Color::DarkGray)),
        Line::from(format!("in: {shown_path}")).style(Style::new().fg(Color::DarkGray)),
        Line::from(format!("elapsed: {:.1} s", elapsed.as_secs_f64())),
        Line::default(),
    ];
    lines.push(Line::from("press q to cancel").style(Style::new().fg(Color::DarkGray)));

    f.render_widget(Clear, area);
    f.render_widget(
        Paragraph::new(lines).block(Block::bordered().title(" disktree ")),
        area,
    );
}

fn bar(pct: f64) -> String {
    let eighths = (pct / 100.0 * BAR_WIDTH as f64 * 8.0).round() as usize;
    let full = (eighths / 8).min(BAR_WIDTH);
    let remainder = eighths % 8;
    let mut s = "█".repeat(full);
    if full < BAR_WIDTH && remainder > 0 {
        s.push(PARTIAL_BLOCKS[remainder]);
    }
    while s.chars().count() < BAR_WIDTH {
        s.push(' ');
    }
    s
}

fn truncate_left(s: &str, max: usize) -> String {
    let count = s.chars().count();
    if count <= max {
        return s.to_owned();
    }
    let skip = count - max.saturating_sub(1);
    format!("…{}", s.chars().skip(skip).collect::<String>())
}

fn centered(width: u16, height: u16, area: Rect) -> Rect {
    let w = width.min(area.width);
    let h = height.min(area.height);
    Rect {
        x: area.x + (area.width - w) / 2,
        y: area.y + (area.height - h) / 2,
        width: w,
        height: h,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::{ScanProgress, scan};
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use std::fs::{self, File};
    use std::io::Write as _;

    #[test]
    fn bar_widths() {
        assert_eq!(bar(0.0), "          ");
        assert_eq!(bar(100.0), "██████████");
        assert_eq!(bar(50.0), "█████     ");
    }

    #[test]
    fn table_renders_bars_left_aligned() {
        let root = std::env::temp_dir().join(format!("rustree_ui_{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(&root).unwrap();
        File::create(root.join("big.bin"))
            .unwrap()
            .write_all(&[0u8; 8000])
            .unwrap();
        File::create(root.join("small.bin"))
            .unwrap()
            .write_all(&[0u8; 2000])
            .unwrap();

        let progress = ScanProgress::default();
        let tree = scan(root.clone(), &progress);
        let mut app = crate::app::App::new(tree);

        let backend = TestBackend::new(80, 10);
        let mut terminal = Terminal::new(backend).unwrap();
        terminal.draw(|f| draw(f, &mut app)).unwrap();

        let buffer = terminal.backend().buffer().clone();
        let line = |y: u16| -> String {
            (0..80)
                .map(|x| buffer[(x, y)].symbol().to_string())
                .collect::<Vec<_>>()
                .join("")
        };
        // Largest file first, bar left-aligned right after the % column.
        assert!(line(2).contains("80.0% ████████   "), "row: {:?}", line(2));
        assert!(line(2).contains("big.bin"));
        assert!(line(3).contains("20.0% ██        "), "row: {:?}", line(3));
        assert!(line(3).contains("small.bin"));

        fs::remove_dir_all(&root).unwrap();
    }

    /// An `App` over a synthetic tree with `n` equal children, no filesystem.
    fn synthetic_app(n: usize) -> crate::app::App {
        use crate::index::{Node, Tree};
        let mut nodes = vec![Node {
            name: std::ffi::OsString::from("root"),
            is_dir: true,
            size: n as u64 * 10,
            n_files: n as u64,
            parent: None,
            children: (1..=n).collect(),
            deleted: false,
        }];
        for i in 0..n {
            nodes.push(Node {
                name: std::ffi::OsString::from(format!("f{i:06}")),
                is_dir: false,
                // Descending so sort order is predictable and stable.
                size: (n - i) as u64 * 10,
                n_files: 1,
                parent: Some(0),
                children: Vec::new(),
                deleted: false,
            });
        }
        crate::app::App::new(Tree {
            nodes,
            root: 0,
            root_path: std::path::PathBuf::from("T:\\synthetic"),
            errors: 0,
            scan_secs: 0.0,
        })
    }

    /// Rendered data rows, in order, as trimmed name text.
    fn rendered_names(app: &mut crate::app::App, w: u16, h: u16) -> Vec<String> {
        let mut terminal = Terminal::new(TestBackend::new(w, h)).unwrap();
        terminal.draw(|f| draw(f, app)).unwrap();
        let buf = terminal.backend().buffer().clone();
        let mut out = Vec::new();
        // Row 0 is the header line, then data rows until the footer.
        for y in 2..h.saturating_sub(2) {
            let text: String = (0..w).map(|x| buf[(x, y)].symbol().to_string()).collect();
            if let Some(tok) = text.split_whitespace().find(|t| t.starts_with('f')) {
                out.push(tok.to_owned());
            }
        }
        out
    }

    /// The highlight index is relative to the rendered window, so a scrolled view
    /// must still paint the selected row and no other.
    #[test]
    fn selected_row_is_highlighted_within_the_window() {
        for (n, selected) in [(5usize, 0usize), (5, 3), (1000, 0), (1000, 999)] {
            let mut app = synthetic_app(n);
            app.selected = selected;
            let mut terminal = Terminal::new(TestBackend::new(80, 12)).unwrap();
            terminal.draw(|f| draw(f, &mut app)).unwrap();
            let buf = terminal.backend().buffer().clone();

            // Table occupies y=1 (header) then data rows from y=2.
            let highlighted: Vec<u16> = (2..10)
                .filter(|&y| buf[(2, y)].style().bg == Some(Color::Blue))
                .collect();
            assert_eq!(
                highlighted.len(),
                1,
                "n={n} selected={selected}: {} highlighted rows, expected 1",
                highlighted.len()
            );
            // It must be the row holding the selection.
            let expected_y = 2 + (app.selected - app.offset) as u16;
            assert_eq!(
                highlighted[0], expected_y,
                "n={n} selected={selected}: highlight on wrong row"
            );
        }
    }

    /// The proportional bar is the at-a-glance cue; it must actually be coloured.
    #[test]
    fn bars_render_in_green() {
        let mut app = synthetic_app(4);
        let mut terminal = Terminal::new(TestBackend::new(80, 12)).unwrap();
        terminal.draw(|f| draw(f, &mut app)).unwrap();
        let buf = terminal.backend().buffer().clone();

        // Find any full-block glyph and check its foreground.
        let mut found = false;
        for y in 2..8u16 {
            for x in 0..80u16 {
                if buf[(x, y)].symbol() == "█" {
                    assert_eq!(
                        buf[(x, y)].style().fg,
                        Some(Color::Green),
                        "bar glyph at ({x},{y}) is not green"
                    );
                    found = true;
                }
            }
        }
        assert!(found, "no bar glyphs rendered at all");
    }

    #[test]
    fn window_empty_directory() {
        let mut app = synthetic_app(0);
        assert_eq!(app.visible_range(10), 0..0);
        assert_eq!(app.offset, 0);
        // Must not panic with no rows.
        let _ = rendered_names(&mut app, 80, 12);
    }

    #[test]
    fn window_single_entry() {
        let mut app = synthetic_app(1);
        assert_eq!(app.visible_range(10), 0..1);
        assert_eq!(rendered_names(&mut app, 80, 12), vec!["f000000"]);
    }

    #[test]
    fn window_selection_at_top_shows_first_rows() {
        let mut app = synthetic_app(1000);
        app.selected = 0;
        assert_eq!(app.visible_range(5), 0..5);
        assert_eq!(app.offset, 0);
    }

    #[test]
    fn window_selection_at_end_scrolls_to_bottom() {
        let mut app = synthetic_app(1000);
        app.selected = 999;
        let r = app.visible_range(5);
        assert_eq!(r, 995..1000);
        // The selection must land inside the drawn window.
        assert!(r.contains(&app.selected));
    }

    #[test]
    fn window_shrinks_when_delete_removes_rows() {
        let mut app = synthetic_app(20);
        app.selected = 19;
        app.visible_range(5);
        assert_eq!(app.offset, 15);
        // Simulate the list shrinking under a scrolled-down view.
        app.rows.truncate(6);
        let r = app.visible_range(5);
        assert!(r.end <= 6, "range {r:?} exceeds row count");
        assert!(app.selected < 6, "selection {} out of range", app.selected);
        assert!(r.contains(&app.selected));
    }

    #[test]
    fn window_reclamps_after_resize() {
        let mut app = synthetic_app(100);
        app.selected = 99;
        app.visible_range(5);
        assert_eq!(app.offset, 95);
        // A taller terminal must not leave blank space below the last row.
        let r = app.visible_range(40);
        assert_eq!(r, 60..100);
        assert!(r.contains(&app.selected));
        // And a shorter one must bring the selection back into view.
        let r = app.visible_range(3);
        assert!(r.contains(&app.selected), "range {r:?} lost selection");
    }

    #[test]
    fn window_renders_only_visible_slice() {
        let mut app = synthetic_app(100_000);
        // Sorted by size descending, so f000000 is first.
        let names = rendered_names(&mut app, 80, 12);
        assert!(names.len() <= 12, "drew {} rows into 12 lines", names.len());
        assert_eq!(names.first().map(String::as_str), Some("f000000"));
        assert_eq!(names.get(1).map(String::as_str), Some("f000001"));
    }

    /// The pre-windowing renderer built one widget per child, so a frame cost
    /// grew with directory size (~0.5 s at 100k children). Only the visible slice
    /// is built now, so a 100x larger directory must not cost meaningfully more.
    #[test]
    fn window_render_cost_is_independent_of_directory_size() {
        fn per_frame_secs(n: usize) -> f64 {
            let mut app = synthetic_app(n);
            let mut terminal = Terminal::new(TestBackend::new(120, 40)).unwrap();
            // Draw once so lazy setup is not charged to the measurement.
            terminal.draw(|f| draw(f, &mut app)).unwrap();
            let start = std::time::Instant::now();
            for _ in 0..20 {
                terminal.draw(|f| draw(f, &mut app)).unwrap();
            }
            start.elapsed().as_secs_f64() / 20.0
        }

        let small = per_frame_secs(1_000);
        let large = per_frame_secs(100_000);
        eprintln!(
            "MEASURED: {:.3} ms/frame at 1k children, {:.3} ms/frame at 100k",
            small * 1000.0,
            large * 1000.0
        );
        // 100x the children must not cost 5x the frame. Generous, because these
        // are sub-millisecond timings on a shared CI box.
        assert!(
            large < small * 5.0 + 0.002,
            "frame cost scales with directory size: {:.3} ms at 1k vs {:.3} ms at 100k",
            small * 1000.0,
            large * 1000.0
        );
        assert!(large < 0.010, "{:.3} ms per frame at 100k", large * 1000.0);
    }
}