Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b54a215

Browse files
committed
feat: Listen for ctrl-c
before ctrl-c would not cancel a running job.
1 parent 0364cf7 commit b54a215

5 files changed

Lines changed: 98 additions & 49 deletions

File tree

Cargo.lock

Lines changed: 77 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ config-file = "0.2"
4040
serde = { version = "1.0", features = ["derive"] }
4141
directories = "4"
4242
sysinfo = "0.27"
43+
ctrlc = "3.4"
4344

4445
[target.'cfg(windows)'.dependencies]
4546
winapi-util = "0.1"

src/dir_walker.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ fn ignore_file(entry: &DirEntry, walk_data: &WalkData) -> bool {
130130
fn walk(dir: PathBuf, walk_data: &WalkData, depth: usize) -> Option<Node> {
131131
let prog_data = &walk_data.progress_data;
132132
let errors = &walk_data.errors;
133+
if errors.lock().unwrap().abort {
134+
return None;
135+
}
133136

134137
let children = if dir.is_dir() {
135138
let read_dir = fs::read_dir(&dir);

src/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ fn main() {
105105
let options = build_cli().get_matches();
106106
let config = get_config();
107107

108+
let errors = RuntimeErrors::default();
109+
let error_listen_for_ctrlc = Arc::new(Mutex::new(errors));
110+
let errors_for_rayon = error_listen_for_ctrlc.clone();
111+
let errors_final = error_listen_for_ctrlc.clone();
112+
113+
ctrlc::set_handler(move || {
114+
error_listen_for_ctrlc.lock().unwrap().abort = true;
115+
println!("\nAborting");
116+
})
117+
.expect("Error setting Ctrl-C handler");
118+
108119
let target_dirs = match options.get_many::<String>("params") {
109120
Some(values) => values.map(|v| v.as_str()).collect::<Vec<&str>>(),
110121
None => vec!["."],
@@ -197,7 +208,7 @@ fn main() {
197208
ignore_hidden,
198209
follow_links,
199210
progress_data: indicator.data.clone(),
200-
errors: Arc::new(Mutex::new(RuntimeErrors::default())),
211+
errors: errors_for_rayon,
201212
};
202213
let stack_size = config.get_custom_stack_size(&options);
203214
init_rayon(&stack_size);
@@ -222,6 +233,10 @@ fn main() {
222233
// Must have stopped indicator before we print to stderr
223234
indicator.stop();
224235

236+
if errors_final.lock().unwrap().abort {
237+
return;
238+
}
239+
225240
let final_errors = walk_data.errors.lock().unwrap();
226241
let failed_permissions = final_errors.no_permissions;
227242
if !final_errors.file_not_found.is_empty() {

src/progress.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub struct RuntimeErrors {
7373
pub no_permissions: bool,
7474
pub file_not_found: HashSet<String>,
7575
pub unknown_error: HashSet<String>,
76+
pub abort: bool,
7677
}
7778

7879
/* -------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)