Handle terminal signals (#128)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #87
authored by Dock · committed by David Peter · parent 44ee5a0 · Browse files · 4 files·+59 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+59 added−6 removed
- other:+59 added−6 removed
Provenance
- Commit
c0bfc65- Tree
8d41164- Author
Dock <dockconnellyoneal@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
44ee5a0
4 files changed+59 added−6 removed
modified:src/output.rs+15 added-3 removed
11 unmodified lines12 use std::{fs, process};13 use std::io::{self, Write};14 use std::ops::Deref;15 use std::path::{self, Path, PathBuf, Component};16+use std::sync::Arc;17+use std::sync::atomic::{AtomicBool, Ordering};18 #[cfg(any(unix, target_os = "redox"))]19 use std::os::unix::fs::PermissionsExt;20 21 use ansi_term;22 21-pub fn print_entry(entry: &PathBuf, config: &FdOptions) {23+pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) {24 let path = entry.strip_prefix(".").unwrap_or(entry);25 26 let r = if let Some(ref ls_colors) = config.ls_colors {25- print_entry_colorized(path, config, ls_colors)27+ print_entry_colorized(path, config, ls_colors, &wants_to_quit)28 } else {29 print_entry_uncolorized(path, config)30 };31 2 unmodified lines34 process::exit(0);35 }36 }37 36-fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) -> io::Result<()> {38+fn print_entry_colorized(39+ path: &Path,40+ config: &FdOptions,41+ ls_colors: &LsColors,42+ wants_to_quit: &Arc<AtomicBool>,43+) -> io::Result<()> {44 let default_style = ansi_term::Style::default();45 46 let stdout = io::stdout();47 let mut handle = stdout.lock();21 unmodified lines69 Component::RootDir => String::new(),70 // Everything else uses a separator that is painted the same way as the component.71 _ => style.paint(path::MAIN_SEPARATOR.to_string()).to_string(),72 };73+74+ if wants_to_quit.load(Ordering::Relaxed) {75+ write!(handle, "\n")?;76+ process::exit(0);77+ }78 }79 80 if config.null_separator {81 write!(handle, "\0")52 unmodified linesmodified:src/walk.rs+12 added-3 removed
4 unmodified lines5 // at your option. All files in the project carrying such6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9+extern crate ctrlc;10+11 use exec;12 use fshelper;13 use internal::{error, FdOptions};14 use output;15 16 use std::path::Path;17 use std::sync::{Arc, Mutex};18+use std::sync::atomic::{AtomicBool, Ordering};19 use std::sync::mpsc::channel;20 use std::thread;21 use std::time;22 53 unmodified lines76 .max_depth(config.max_depth)77 .threads(threads)78 .build_parallel();79 80+ let wants_to_quit = Arc::new(AtomicBool::new(false));81+82+ if let Some(_) = config.ls_colors {83+ let wq = Arc::clone(&wants_to_quit);84+ ctrlc::set_handler(move || { wq.store(true, Ordering::Relaxed); }).unwrap();85+ }86 // Spawn the thread that receives all results through the channel.87 let rx_config = Arc::clone(&config);88 let receiver_thread = thread::spawn(move || {89 // This will be set to `Some` if the `--exec` argument was supplied.47 unmodified lines137 // Have we reached the maximum time?138 if time::Instant::now() - start > max_buffer_time {139 // Flush the buffer140 for v in &buffer {132- output::print_entry(v, &rx_config);141+ output::print_entry(&v, &rx_config, &wants_to_quit);142 }143 buffer.clear();144 145 // Start streaming146 mode = ReceiverMode::Streaming;147 }148 }149 ReceiverMode::Streaming => {141- output::print_entry(&value, &rx_config);150+ output::print_entry(&value, &rx_config, &wants_to_quit);151 }152 }153 }154 1 unmodified line156 // anything to the console, yet. In this case, sort the results and print them:157 if !buffer.is_empty() {158 buffer.sort();159 for value in buffer {151- output::print_entry(&value, &rx_config);160+ output::print_entry(&value, &rx_config, &wants_to_quit);161 }162 }163 }164 });77 unmodified linesmodified:Cargo.lock+30 added
3 unmodified lines4 dependencies = [5 "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",6 "atty 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",7 "clap 2.26.2 (registry+https://github.com/rust-lang/crates.io-index)",8+ "ctrlc 3.0.3 (registry+https://github.com/rust-lang/crates.io-index)",9 "diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",10 "ignore 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",11 "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",12 "lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",40 unmodified lines53 version = "0.9.1"54 source = "registry+https://github.com/rust-lang/crates.io-index"55 56 [[package]]57+name = "cfg-if"58+version = "0.1.2"59+source = "registry+https://github.com/rust-lang/crates.io-index"60+61+[[package]]62 name = "clap"63 version = "2.26.2"64 source = "registry+https://github.com/rust-lang/crates.io-index"65 dependencies = [20 unmodified lines86 version = "0.2.10"87 source = "registry+https://github.com/rust-lang/crates.io-index"88 89 [[package]]90+name = "ctrlc"91+version = "3.0.3"92+source = "registry+https://github.com/rust-lang/crates.io-index"93+dependencies = [94+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",95+ "nix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",96+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",97+]98+99+[[package]]100 name = "custom_derive"101 version = "0.1.7"102 source = "registry+https://github.com/rust-lang/crates.io-index"103 83 unmodified lines187 "libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",188 ]189 190 [[package]]191+name = "nix"192+version = "0.8.1"193+source = "registry+https://github.com/rust-lang/crates.io-index"194+dependencies = [195+ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",196+ "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",197+ "libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",198+ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",199+]200+201+[[package]]202 name = "num_cpus"203 version = "1.7.0"204 source = "registry+https://github.com/rust-lang/crates.io-index"205 dependencies = [157 unmodified lines363 "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"364 "checksum atty 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "21e50800ec991574876040fff8ee46b136a53e985286fbe6a3bdfe6421b78860"365 "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"366 "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"367+"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"368 "checksum clap 2.26.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3451e409013178663435d6f15fdb212f14ee4424a3d74f979d081d0a66b6f1f2"369 "checksum conv 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "78ff10625fd0ac447827aa30ea8b861fead473bb60aeb73af6c1c58caf0d1299"370 "checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97"371+"checksum ctrlc 3.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "df391ea008fca636e41e40863a0b39a850e2ab26b0cdeed0c3657fd05a66d44c"372 "checksum custom_derive 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ef8ae57c4978a2acd8b869ce6b9ca1dfe817bff704c220209fdef2c0b75a01b9"373 "checksum diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0a515461b6c8c08419850ced27bc29e86166dcdcde8fbe76f8b1f0589bb49472"374 "checksum fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6cc484842f1e2884faf56f529f960cc12ad8c71ce96cc7abba0a067c98fee344"375 "checksum globset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "feeb1b6840809ef5efcf7a4a990bc4e1b7ee3df8cf9e2379a75aeb2ba42ac9c3"4 unmodified lines380 "checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"381 "checksum magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf0336886480e671965f794bc9b6fce88503563013d1bfb7a502c81fe3ac527"382 "checksum magenta-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40d014c7011ac470ae28e2f76a02bfea4a8480f73e701353b49ad7a8d75f4699"383 "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"384+"checksum nix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "47e49f6982987135c5e9620ab317623e723bd06738fd85377e8d55f57c8b6487"385 "checksum num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "514f0d73e64be53ff320680ca671b64fe3fb91da01e1ae2ddc99eb51d453b20d"386 "checksum rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "eb250fd207a4729c976794d03db689c9be1d634ab5a1c9da9492a13d8fecbcdf"387 "checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"388 "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"18 unmodified linesmodified:Cargo.toml+2 added
38 unmodified lines394041424344454647487 unmodified lines38 unmodified lineslazy_static = "0.2.9"num_cpus = "1.6.2"regex = "0.2"regex-syntax = "0.4"ctrlc = "3.0"shell-escape = "0.1.3"
[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]libc = "0.2"
7 unmodified lines