Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/interactive/app/deletion_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,20 @@ impl FilesystemDeletion {

impl AppState {
pub fn is_deleting(&self) -> bool {
self.deletion.is_some()
self.deletion.is_some() || self.queued_deletion.is_some()
}

pub(super) fn block_deletion_changes(&mut self) -> bool {
if self.is_deleting() {
self.message = Some(self.language.ui_text().deletion_running.into());
let text = self.language.ui_text();
self.message = Some(
if self.queued_deletion.is_some() {
text.deletion_queued
} else {
text.deletion_running
}
.into(),
);
true
} else {
false
Expand All @@ -113,12 +121,18 @@ impl AppState {
if self.read_only {
anyhow::bail!(self.language.ui_text().snapshots_read_only);
}
if self.scan.is_some() {
anyhow::bail!(self.language.ui_text().traversal_running);
}
let Some(pane) = &window.mark else {
return Ok(());
};
if pane.is_empty() {
return Ok(());
}
if self.scan.is_some() {
self.queued_deletion = Some(mode);
self.pending_exit = false;
self.reset_message();
return Ok(());
}
let mut targets: Vec<_> = pane
.marked()
.keys()
Expand Down Expand Up @@ -171,6 +185,7 @@ impl AppState {

/// Turn an actual exit request into cancellation followed by draining the worker.
pub(super) fn exit_after_deletion(&mut self) -> Option<WalkResult> {
self.queued_deletion = None;
if let Some(deletion) = &self.deletion {
deletion.task.cancel.store(true, Ordering::Relaxed);
self.pending_exit = false;
Expand Down
5 changes: 5 additions & 0 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ impl AppState {
crossbeam::select! {
recv(events) -> event => {
let Ok(event) = event else {
self.queued_deletion = None;
return Ok(Some(WalkResult { num_errors: self.stats.io_errors }));
};
let res = self.process_terminal_event(
Expand Down Expand Up @@ -561,6 +562,10 @@ impl AppState {
self.navigation_mut().view_root = view;
}
}
if is_finished && let Some(mode) = self.queued_deletion.take() {
let tree = self.tree_view(traversal);
self.start_deletion(window, &tree, *display, mode)?;
}
self.update_state_during_traversal(traversal, previous_selection.as_ref(), is_finished);
self.refresh_screen(window, traversal, display, terminal, config)?;
if is_finished {
Expand Down
2 changes: 2 additions & 0 deletions src/interactive/app/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ impl AppState {
pub fn reset_message(&mut self) {
if let Some(deletion) = &self.deletion {
self.message = Some(deletion.message(self.language));
} else if self.queued_deletion.is_some() {
self.message = Some(self.language.ui_text().deletion_queued.into());
} else if self.scan.is_some() {
self.message = Some(self.language.ui_text().scanning.into());
} else if let Some(hub) = self.clean_hub.as_ref().filter(|hub| hub.root.is_none()) {
Expand Down
5 changes: 4 additions & 1 deletion src/interactive/app/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use dua::WalkOptions;
use dua::traverse::{BackgroundTraversal, TraversalStats};

use crate::interactive::widgets::{Column, Language};
use crate::interactive::widgets::{Column, Language, MarkMode};

use super::{EntryDataBundle, SortMode, input::TerminalFocus, navigation::Navigation};
use super::{clean_hub::CleanHub, deletion_progress::FilesystemDeletion};
Expand Down Expand Up @@ -68,6 +68,8 @@ pub struct AppState {
pub scan: Option<FilesystemScan>,
/// The frozen deletion batch, kept alive until all workers have stopped.
pub(super) deletion: Option<FilesystemDeletion>,
/// Confirmed action on the frozen marks, waiting for the current scan to finish.
pub queued_deletion: Option<MarkMode>,
/// Latest traversal progress and error counters.
pub stats: TraversalStats,
/// Options used when starting filesystem walks.
Expand Down Expand Up @@ -113,6 +115,7 @@ impl AppState {
received_events: false,
scan: None,
deletion: None,
queued_deletion: None,
stats: TraversalStats::default(),
walk_options,
root_paths: input,
Expand Down
2 changes: 2 additions & 0 deletions src/interactive/app/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ impl TerminalApp {
);
if result.is_err() {
self.state.deletion = None;
self.state.queued_deletion = None;
}
result
}
Expand All @@ -235,6 +236,7 @@ impl TerminalApp {
);
if result.is_err() {
self.state.deletion = None;
self.state.queued_deletion = None;
}
result
}
Expand Down
51 changes: 51 additions & 0 deletions src/interactive/app/tests/journeys_deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,57 @@ fn key(app: &mut TerminalApp, terminal: &mut Terminal<TestBackend>, key: KeyEven
Ok(())
}

#[test]
fn quitting_cancels_queued_delete_and_trash_without_starting_a_worker() -> Result<()> {
use crate::interactive::{app::state::FilesystemScan, widgets::MarkMode};
use dua::traverse::BackgroundTraversal;

for mode in [
MarkMode::Delete,
#[cfg(feature = "trash-move")]
MarkMode::Trash,
] {
let (fixture, mut terminal, mut app) = prepared()?;
app.state.scan = Some(FilesystemScan {
active_traversal: BackgroundTraversal::start(
app.traversal.root_index,
&app.state.walk_options,
Vec::new(),
None,
false,
false,
)?,
previous_selection: None,
previous_cleanup_view: None,
snapshot_export: None,
});
let scan_events = std::mem::replace(
&mut app.state.scan.as_mut().unwrap().active_traversal.event_rx,
never(),
);
let tree = app.state.tree_view(&mut app.traversal);
app.state
.start_deletion(&app.window, &tree, app.display, mode)?;
assert!(app.state.queued_deletion.is_some());
assert!(app.state.deletion.is_none());

let result = step(
&mut app,
&mut terminal,
&into_events([Event::Key(KeyEvent::new(
KeyCode::Char('c'),
KeyModifiers::CONTROL,
))]),
)?;
assert_eq!(result.unwrap().num_errors, 0);
assert!(!app.state.is_deleting());
app.state.scan.as_mut().unwrap().active_traversal.event_rx = scan_events;
app.process_events_once(&mut terminal, into_events([]))?;
assert!(fixture.path().join("delete/remove.bin").exists());
}
Ok(())
}

#[test]
fn deletion_ticks_update_deleted_bytes_without_input_and_keep_failed_marks() -> Result<()> {
let (fixture, mut terminal, mut app) = prepared()?;
Expand Down
24 changes: 17 additions & 7 deletions src/interactive/app/tests/journeys_with_writes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::interactive::app::tests::utils::{
WritableFixture, initialized_app_and_terminal_from_paths, into_codes, into_events,

Check warning on line 2 in src/interactive/app/tests/journeys_with_writes.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

unused import: `WritableFixture`

Check warning on line 2 in src/interactive/app/tests/journeys_with_writes.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

unused import: `WritableFixture`
new_test_terminal,
};
use crate::interactive::terminal::TerminalApp;
use crate::interactive::widgets::Language;

Check warning on line 6 in src/interactive/app/tests/journeys_with_writes.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

unused import: `crate::interactive::widgets::Language`

Check warning on line 6 in src/interactive/app/tests/journeys_with_writes.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

unused import: `crate::interactive::widgets::Language`
use anyhow::Result;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use dua::{ByteFormat, Config, WalkOptions};
Expand Down Expand Up @@ -149,7 +149,7 @@
}

#[test]
fn deletion_and_trash_are_blocked_until_scan_finishes() -> Result<()> {
fn deletion_is_queued_and_runs_automatically_after_the_scan() -> Result<()> {
use crate::interactive::app::{state::FilesystemScan, tree_view::TreeView};
use dua::traverse::BackgroundTraversal;

Expand Down Expand Up @@ -186,6 +186,7 @@
let delete_key = KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL);
for key in [
delete_key,
KeyCode::Char('x').into(),
#[cfg(feature = "trash-move")]
KeyEvent::new(KeyCode::Char('t'), KeyModifiers::CONTROL),
] {
Expand All @@ -203,24 +204,33 @@
);
assert_eq!(
app.state.message.as_deref(),
Some("Traversal already running")
Some("Deletion queued; waiting for the scan to finish")
);
assert!(
app.state.deletion.is_none(),
"no worker starts during the scan"
);
assert!(matches!(
app.state.queued_deletion,
Some(crate::interactive::widgets::MarkMode::Delete)
));
assert!(root.join("file").exists());
assert_eq!(app.traversal.tree.len(), node_count);
assert_eq!(
marked_file_names(&app, "marks survive the blocked action"),
marked_file_names(&app, "queued marks cannot be changed"),
marked
);
assert!(app.state.scan.is_some());
}

let (_key_send, key_receive) = crossbeam::channel::bounded(0);
app.run_until_traversed(&mut terminal, key_receive)?;
app.process_events(&mut terminal, into_events([Event::Key(delete_key)]))?;
let result = app.process_events_once(&mut terminal, into_events([]))?;
assert_eq!(result.num_errors, 0);
assert!(
!root.exists(),
"deletion succeeds when retried after the scan"
"the queued deletion runs without another confirmation"
);
assert!(app.state.scan.is_none());
assert!(!app.state.is_deleting());
assert!(app.window.mark.is_none());
assert_eq!(app.traversal.tree.len(), 1);
Ok(())
Expand Down
6 changes: 6 additions & 0 deletions src/interactive/widgets/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
pub mark_trash_2: &'static str,

pub app_title: &'static str,
pub app_suspend: &'static str,

Check warning on line 162 in src/interactive/widgets/i18n.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

field `app_suspend` is never read

Check warning on line 162 in src/interactive/widgets/i18n.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

field `app_suspend` is never read

Check warning on line 162 in src/interactive/widgets/i18n.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

field `app_suspend` is never read

Check warning on line 162 in src/interactive/widgets/i18n.rs

View workflow job for this annotation

GitHub Actions / Windows (nightly)

field `app_suspend` is never read
pub app_repaint: &'static str,
pub app_quit: &'static str,
}
Expand Down Expand Up @@ -543,6 +543,7 @@
pub snapshots_read_only: &'static str,
pub traversal_running: &'static str,
pub deletion_running: &'static str,
pub deletion_queued: &'static str,
pub cancelling_deletion: &'static str,
pub no_cleanup_candidates: &'static str,
pub cleanup_candidates_already_marked: &'static str,
Expand Down Expand Up @@ -864,6 +865,7 @@
snapshots_read_only: "Snapshots are read-only",
traversal_running: "Traversal already running",
deletion_running: "Deletion in progress; changes are disabled",
deletion_queued: "Deletion queued; waiting for the scan to finish",
cancelling_deletion: "Cancelling deletion...",
no_cleanup_candidates: "No cleanup candidates in view",
cleanup_candidates_already_marked: "Cleanup candidates are already marked",
Expand Down Expand Up @@ -927,6 +929,7 @@
snapshots_read_only: "スナップショットは読み取り専用です",
traversal_running: "スキャンはすでに実行中です",
deletion_running: "削除中のため変更できません",
deletion_queued: "削除を予約しました。スキャン完了を待っています",
cancelling_deletion: "削除を中止しています...",
no_cleanup_candidates: "現在の表示にクリーンアップ候補はありません",
cleanup_candidates_already_marked: "クリーンアップ候補はすでにマーク済みです",
Expand Down Expand Up @@ -990,6 +993,7 @@
snapshots_read_only: "스냅샷은 읽기 전용입니다",
traversal_running: "스캔이 이미 실행 중입니다",
deletion_running: "삭제 중에는 변경할 수 없습니다",
deletion_queued: "삭제 예약됨; 스캔이 끝날 때까지 기다리는 중",
cancelling_deletion: "삭제 취소 중...",
no_cleanup_candidates: "현재 보기에 정리 후보가 없습니다",
cleanup_candidates_already_marked: "정리 후보가 이미 표시되어 있습니다",
Expand Down Expand Up @@ -1053,6 +1057,7 @@
snapshots_read_only: "快照为只读",
traversal_running: "扫描已在进行",
deletion_running: "正在删除,暂时无法更改",
deletion_queued: "删除已排队,等待扫描完成",
cancelling_deletion: "正在取消删除...",
no_cleanup_candidates: "当前视图中没有清理候选项",
cleanup_candidates_already_marked: "清理候选项已标记",
Expand Down Expand Up @@ -1116,6 +1121,7 @@
snapshots_read_only: "Snapshots sind schreibgeschützt",
traversal_running: "Scan läuft bereits",
deletion_running: "Löschung läuft; Änderungen sind gesperrt",
deletion_queued: "Löschung vorgemerkt; wartet auf das Scan-Ende",
cancelling_deletion: "Löschung wird abgebrochen...",
no_cleanup_candidates: "Keine Bereinigungskandidaten in der Ansicht",
cleanup_candidates_already_marked: "Bereinigungskandidaten sind bereits markiert",
Expand Down
7 changes: 5 additions & 2 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ impl MainWindow {
let (entries_style, help_style, mark_style, glob_style) = pane_border_style(state.focussed);
let (header_area, content_area, footer_area) = main_window_layout(area);

let safety_notice =
mark_safety_notice(state.read_only, state.is_deleting(), &config.keys, language);
let safety_notice = if state.queued_deletion.is_some() {
Some(language.ui_text().deletion_queued)
} else {
mark_safety_notice(state.read_only, state.is_deleting(), &config.keys, language)
};

let header_bg_color =
header_background_color(self.has_marks() && safety_notice.is_none(), state.focussed);
Expand Down
Loading