Skip to content

A treemap for finding and removing what fills your disk, for Omarchy. Rust + GPUI.

Rust

scan: escape or Cancel stops a scan without showing a partial tree

Tyler South8a3f8ae9c19f
diff --git a/README.md b/README.md
index 7ee7533..ee62ad2 100644
--- a/README.md
+++ b/README.md
@@ -211,6 +211,7 @@ and shows how much free space was actually gained.
| `d` | disk usage or apparent size |
| `i` | include or skip hidden entries |
| `r` | scan again |
+| `esc` while scanning | stop the scan |
| `ctrl o` (`⌘O` on macOS) | choose another directory to scan |
| `g` | the whole disk |
| `p` | show or hide the selection line |
diff --git a/crates/disktree-app/src/state.rs b/crates/disktree-app/src/state.rs
index 27e1846..92c9789 100644
--- a/crates/disktree-app/src/state.rs
+++ b/crates/disktree-app/src/state.rs
@@ -540,6 +540,11 @@ impl Disktree {
cx: &mut Context<'_, Self>,
) -> Self {
let mut app = Self::new(root_path, options, depth, cx);
+ // `new` started a walk; this tree stands in for its result.
+ if let Some(scan) = app.scan.take() {
+ scan.cancel();
+ }
+ app.scan_epoch += 1;
app.marks.refresh(&app.root_path, &tree, app.options.metric);
app.tree = Some(Arc::new(tree));
app.cache = None;
@@ -658,6 +663,22 @@ impl Disktree {
// ── scanning ────────────────────────────────────────────────────────
+ /// Stop the walk in progress. A partial tree is never shown as if it
+ /// were the whole one: a first scan leaves the panel saying it stopped,
+ /// and a widening scan leaves the tree it started from on screen.
+ pub fn cancel_scan(&mut self, cx: &mut Context<'_, Self>) {
+ let Some(scan) = self.scan.take() else {
+ return;
+ };
+ scan.cancel();
+ // The poller stops at its next tick instead of taking the result.
+ self.scan_epoch += 1;
+ self.progress = scan.progress.snapshot();
+ self.scan_elapsed = self.scan_started.map(|started| started.elapsed());
+ self.scan_root.clone_from(&self.root_path);
+ cx.notify();
+ }
+
/// Start a fresh scan, abandoning any walk still in progress.
pub fn start_scan(&mut self, cx: &mut Context<'_, Self>) {
if let Some(scan) = &self.scan {
@@ -2381,6 +2402,7 @@ impl Disktree {
"backspace" | "u" if !control => self.ascend(cx),
// A filter is the first thing Escape takes away.
"escape" if self.matches.is_some() => self.clear_filter(),
+ "escape" if self.scan.is_some() => self.cancel_scan(cx),
"escape" => {
if self.selected.is_some() {
self.selected = None;
diff --git a/crates/disktree-app/src/tests.rs b/crates/disktree-app/src/tests.rs
index af151ec..83dcac2 100644
--- a/crates/disktree-app/src/tests.rs
+++ b/crates/disktree-app/src/tests.rs
@@ -1322,3 +1322,67 @@ fn back_and_forward_retrace_where_you_have_been(cx: &mut TestAppContext) {
Vec::<usize>::new()
);
}
+
+/// Escape stops a first scan. What the walk found so far is not shown as a
+/// tree, a late result is ignored, and `r` starts over.
+#[gpui_kit::test]
+fn escape_cancels_the_first_scan_and_r_starts_it_again(
+ cx: &mut TestAppContext,
+) {
+ cx.update(gpui_omarchy::init);
+ let temp = fixture();
+ let root = temp.path().to_path_buf();
+ let (view, cx) =
+ cx.add_window_view(move |_, cx| Disktree::new(root, options(), 3, cx));
+ let focus = view.read_with(cx, |app, _| app.focus.clone());
+ cx.update(|window, cx| window.focus(&focus, cx));
+ draw(cx);
+ let epoch = read(&view, cx, |app| app.scan_epoch);
+
+ press(cx, "escape");
+ let (scanning, cancelled) = read(&view, cx, |app| {
+ (app.scan.is_some(), app.progress.cancelled)
+ });
+ assert!(!scanning, "the walk was dropped");
+ assert!(cancelled, "the panel can say it stopped");
+ std::thread::sleep(std::time::Duration::from_millis(50));
+ let polling = update(&view, cx, |app, cx| app.poll_scan_once(epoch, cx));
+ assert!(!polling, "the old poller stops");
+ assert!(read(&view, cx, |app| app.tree().is_none()));
+ draw(cx);
+
+ press(cx, "r");
+ finish_scan(&view, cx);
+ assert!(read(&view, cx, |app| app.tree().is_some()));
+}
+
+/// Escape stops a widening scan and keeps the tree it started from.
+#[gpui_kit::test]
+fn escape_cancels_widening_and_keeps_the_tree_on_screen(
+ cx: &mut TestAppContext,
+) {
+ cx.update(gpui_omarchy::init);
+ let temp = fixture();
+ let inner = temp.path().join("junk");
+ let (view, cx) = view_over(&inner, cx);
+ update(&view, cx, |app, _| {
+ app.disk_root = Some(temp.path().to_path_buf());
+ });
+ let before = read(&view, cx, |app| app.tree().map(|tree| tree.files));
+
+ press(cx, "g");
+ assert!(read(&view, cx, |app| app.scan.is_some()));
+ press(cx, "escape");
+ let (scanning, root, scan_root, files) = read(&view, cx, |app| {
+ (
+ app.scan.is_some(),
+ app.root_path.clone(),
+ app.scan_root.clone(),
+ app.tree().map(|tree| tree.files),
+ )
+ });
+ assert!(!scanning);
+ assert_eq!(root, inner);
+ assert_eq!(scan_root, inner, "the trail stops showing a widening");
+ assert_eq!(files, before, "the tree on screen is unchanged");
+}
diff --git a/crates/disktree-app/src/views.rs b/crates/disktree-app/src/views.rs
index 8f75253..0e39799 100644
--- a/crates/disktree-app/src/views.rs
+++ b/crates/disktree-app/src/views.rs
@@ -1800,6 +1800,11 @@ fn key_bar(app: &Disktree, theme: &Theme, cx: &App) -> Div {
widgets::human_count(app.progress.files),
human_bytes(app.progress.bytes)
)
+ } else if app.progress.cancelled {
+ format!(
+ "scan cancelled \u{00b7} {} entries \u{00b7} r scans again",
+ widgets::human_count(app.progress.files)
+ )
} else {
let elapsed = app.scan_elapsed.map_or_else(String::new, |time| {
format!(" \u{00b7} {:.1} s", time.as_secs_f32())
@@ -1820,8 +1825,13 @@ fn key_bar(app: &Disktree, theme: &Theme, cx: &App) -> Div {
}
/// What the viewport shows while the first scan is running.
-fn scanning_panel(app: &Disktree, theme: &Theme, cx: &gpui_kit::App) -> Div {
+fn scanning_panel(
+ app: &Disktree,
+ theme: &Theme,
+ cx: &Context<'_, Disktree>,
+) -> Div {
let progress = &app.progress;
+ let cancelled = app.scan.is_none() && progress.cancelled;
let mut panel = div()
.flex()
.flex_col()
@@ -1831,17 +1841,23 @@ fn scanning_panel(app: &Disktree, theme: &Theme, cx: &gpui_kit::App) -> Div {
.justify_center()
.gap(space::LG)
.bg(theme.inset)
- .child(
- gpui_omarchy::icon(gpui_omarchy::IconName::Loader)
- .size(icon::LG)
- .text_color(theme.accent),
- )
+ .when(!cancelled, |panel| {
+ panel.child(
+ gpui_omarchy::icon(gpui_omarchy::IconName::Loader)
+ .size(icon::LG)
+ .text_color(theme.accent),
+ )
+ })
.child(
div()
.text_size(text::TITLE)
.font_weight(FontWeight::SEMIBOLD)
.text_color(theme.bright)
- .child(format!("Reading {}", widgets::display_root(app))),
+ .child(if cancelled {
+ format!("Stopped reading {}", widgets::display_root(app))
+ } else {
+ format!("Reading {}", widgets::display_root(app))
+ }),
)
.child(
div()
@@ -1866,23 +1882,43 @@ fn scanning_panel(app: &Disktree, theme: &Theme, cx: &gpui_kit::App) -> Div {
cx,
)),
)
- .child(
- div()
- .w(size::SCANNING_METER)
- .child(widgets::meter_row(
- "",
- "",
- progress_estimate(progress.files),
- theme.accent,
- cx,
- )),
- )
+ .when(!cancelled, |panel| {
+ panel.child(
+ div()
+ .w(size::SCANNING_METER)
+ .child(widgets::meter_row(
+ "",
+ "",
+ progress_estimate(progress.files),
+ theme.accent,
+ cx,
+ )),
+ )
+ })
.child(
div()
.text_size(text::BODY)
.text_color(theme.secondary)
- .child("Marking, zooming and the free-space meter all work as soon as it lands."),
- );
+ .child(if cancelled {
+ "Nothing is shown from a scan that did not finish."
+ } else {
+ "Marking, zooming and the free-space meter all work as soon as it lands."
+ }),
+ )
+ // A failed scan has nothing left to cancel either.
+ .child(if app.scan.is_none() {
+ button("scan-again", "Scan again", ButtonVariant::Outline, cx)
+ .on_click(cx.listener(|this, _, window, cx| {
+ this.start_scan(cx);
+ window.focus(&this.focus, cx);
+ }))
+ } else {
+ button("cancel-scan", "Cancel", ButtonVariant::Outline, cx)
+ .on_click(cx.listener(|this, _, window, cx| {
+ this.cancel_scan(cx);
+ window.focus(&this.focus, cx);
+ }))
+ });
if let Some(error) = &app.scan_error {
panel = panel.child(
@@ -2995,7 +3031,7 @@ fn help_overlay(app: &Disktree, cx: &gpui_kit::App) -> Div {
let theme = cx.omarchy();
// Sentence case, and the tile a key acts on is always the one under the
// pointer if the pointer moved last, else the keyboard selection.
- let rows: [(&str, &str); 27] = [
+ let rows: [(&str, &str); 28] = [
("space / x", "Mark or unmark the tile you point at"),
(MODIFIER_CLICK, "Mark without moving the selection"),
("enter", "Open that directory, at any depth"),
@@ -3021,6 +3057,7 @@ fn help_overlay(app: &Disktree, cx: &gpui_kit::App) -> Div {
("c", "Review the marked list"),
("t", "Size, files or age: what areas and colours say"),
("r", "Scan again from the same root"),
+ ("esc", "Stop a scan in progress"),
(MODIFIER_OPEN, "Choose another directory to scan"),
("g", "The whole disk; click any directory above to widen"),
("d", "Disk usage or apparent size"),