Skip to content

Commit

Permalink
feat: Listen for ctrl-c
Browse files Browse the repository at this point in the history
before ctrl-c would not cancel a running job.
  • Loading branch information
bootandy committed Mar 14, 2024
1 parent a8bf76c commit f806147
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 49 deletions.
125 changes: 77 additions & 48 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ config-file = "0.2"
serde = { version = "1.0", features = ["derive"] }
directories = "4"
sysinfo = "0.27"
ctrlc = "3.4"

[target.'cfg(windows)'.dependencies]
winapi-util = "0.1"
Expand Down
3 changes: 3 additions & 0 deletions src/dir_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ fn ignore_file(entry: &DirEntry, walk_data: &WalkData) -> bool {
fn walk(dir: PathBuf, walk_data: &WalkData, depth: usize) -> Option<Node> {
let prog_data = &walk_data.progress_data;
let errors = &walk_data.errors;
if errors.lock().unwrap().abort{
return None
}

let children = if dir.is_dir() {
let read_dir = fs::read_dir(&dir);
Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use std::path::PathBuf;
use terminal_size::{terminal_size, Height, Width};
use utils::get_filesystem_devices;
use utils::simplify_dir_names;
use ctrlc;

Check failure on line 40 in src/main.rs

View workflow job for this annotation

GitHub Actions / Style (ubuntu-latest)

this import is redundant

Check failure on line 40 in src/main.rs

View workflow job for this annotation

GitHub Actions / Style (macos-latest)

this import is redundant

Check failure on line 40 in src/main.rs

View workflow job for this annotation

GitHub Actions / Style (windows-latest)

this import is redundant

Check failure on line 40 in src/main.rs

View workflow job for this annotation

GitHub Actions / Style (ubuntu-latest)

this import is redundant

Check failure on line 40 in src/main.rs

View workflow job for this annotation

GitHub Actions / Style (macos-latest)

this import is redundant

Check failure on line 40 in src/main.rs

View workflow job for this annotation

GitHub Actions / Style (windows-latest)

this import is redundant

static DEFAULT_NUMBER_OF_LINES: usize = 30;
static DEFAULT_TERMINAL_WIDTH: usize = 80;
Expand Down Expand Up @@ -105,6 +106,17 @@ fn main() {
let options = build_cli().get_matches();
let config = get_config();

let errors = RuntimeErrors::default();
let error_ctrlc = Arc::new(Mutex::new(errors));
let error_main = error_ctrlc.clone();
let error_last = error_ctrlc.clone();

ctrlc::set_handler(move || {
println!("\nAborting");
error_ctrlc.lock().unwrap().abort = true;
})
.expect("Error setting Ctrl-C handler");

let target_dirs = match options.get_many::<String>("params") {
Some(values) => values.map(|v| v.as_str()).collect::<Vec<&str>>(),
None => vec!["."],
Expand Down Expand Up @@ -197,7 +209,7 @@ fn main() {
ignore_hidden,
follow_links,
progress_data: indicator.data.clone(),
errors: Arc::new(Mutex::new(RuntimeErrors::default())),
errors: error_main
};
let stack_size = config.get_custom_stack_size(&options);
init_rayon(&stack_size);
Expand All @@ -222,6 +234,10 @@ fn main() {
// Must have stopped indicator before we print to stderr
indicator.stop();

if error_last.lock().unwrap().abort{
return
}

let final_errors = walk_data.errors.lock().unwrap();
let failed_permissions = final_errors.no_permissions;
if !final_errors.file_not_found.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct RuntimeErrors {
pub no_permissions: bool,
pub file_not_found: HashSet<String>,
pub unknown_error: HashSet<String>,
pub abort: bool,
}

/* -------------------------------------------------------------------------- */
Expand Down

0 comments on commit f806147

Please sign in to comment.