Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ctrlc #126

Closed
wants to merge 4 commits into from
Closed
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
30 changes: 30 additions & 0 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 @@ -27,6 +27,7 @@ atty = "0.2"
regex = "0.2"
ignore = "0.2"
num_cpus = "1.6.2"
ctrlc = "3.0"

[build-dependencies]
clap = "2.26.0"
Expand Down
8 changes: 7 additions & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use std::ops::Deref;
use std::path::{self, Path, PathBuf};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::sync::Arc;
use std::sync::atomic::{Ordering, AtomicBool};

use ansi_term;

pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) {
let path_full = base.join(entry);

let path_str = entry.to_string_lossy();
Expand Down Expand Up @@ -79,6 +81,10 @@ pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
let sep = path::MAIN_SEPARATOR.to_string();
write!(handle, "{}", style.paint(sep)).ok();
}

if wants_to_quit.load(Ordering::Relaxed) {
process::exit(0);
}
}

let r = if config.null_separator {
Expand Down
24 changes: 21 additions & 3 deletions src/walk.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
extern crate ctrlc;

use internal::{error, FdOptions};
use fshelper;
use output;

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{Ordering, AtomicBool};
use std::sync::mpsc::channel;
use std::thread;
use std::time;
Expand Down Expand Up @@ -46,6 +49,21 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions
.threads(config.threads)
.build_parallel();

let wants_to_quit = Arc::new(AtomicBool::new(false));

// Only set ctrl-c handler if output is colorized.
// When ctr-c is recieved enter a quitng state where
// output will finish printing but will exit right after
match config.ls_colors {
Some(_) => {
let r = Arc::clone(&wants_to_quit);
ctrlc::set_handler(move || {
r.store(true, Ordering::Relaxed);
}).unwrap();
},
None => (),
}

// Spawn the thread that receives all results through the channel.
let rx_config = Arc::clone(&config);
let rx_base = base.to_owned();
Expand All @@ -71,7 +89,7 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions
if time::Instant::now() - start > max_buffer_time {
// Flush the buffer
for v in &buffer {
output::print_entry(&rx_base, v, &rx_config);
output::print_entry(&rx_base, v, &rx_config, &wants_to_quit);
}
buffer.clear();

Expand All @@ -80,7 +98,7 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions
}
}
ReceiverMode::Streaming => {
output::print_entry(&rx_base, &value, &rx_config);
output::print_entry(&rx_base, &value, &rx_config, &wants_to_quit);
}
}
}
Expand All @@ -90,7 +108,7 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions
if !buffer.is_empty() {
buffer.sort();
for value in buffer {
output::print_entry(&rx_base, &value, &rx_config);
output::print_entry(&rx_base, &value, &rx_config, &wants_to_quit);
}
}
});
Expand Down