Skip to content
Myr

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

4 files+59 added−6 removed

modified:src/output.rs+15 added-3 removed
11 unmodified lines
12use std::{fs, process};13use std::io::{self, Write};14use std::ops::Deref;15use std::path::{self, Path, PathBuf, Component};16use std::sync::Arc;17use std::sync::atomic::{AtomicBool, Ordering};18#[cfg(any(unix, target_os = "redox"))]19use std::os::unix::fs::PermissionsExt;2021use ansi_term;2221pub fn print_entry(entry: &PathBuf, config: &FdOptions) {23pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) {24 let path = entry.strip_prefix(".").unwrap_or(entry);2526 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 lines
34 process::exit(0);35 }36}3736fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) -> io::Result<()> {38fn 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();4546 let stdout = io::stdout();47 let mut handle = stdout.lock();
21 unmodified lines
69 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 };7374 if wants_to_quit.load(Ordering::Relaxed) {75 write!(handle, "\n")?;76 process::exit(0);77 }78 }7980 if config.null_separator {81 write!(handle, "\0")
52 unmodified lines
modified:src/walk.rs+12 added-3 removed
4 unmodified lines
5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89extern crate ctrlc;1011use exec;12use fshelper;13use internal::{error, FdOptions};14use output;1516use std::path::Path;17use std::sync::{Arc, Mutex};18use std::sync::atomic::{AtomicBool, Ordering};19use std::sync::mpsc::channel;20use std::thread;21use std::time;22
53 unmodified lines
76 .max_depth(config.max_depth)77 .threads(threads)78 .build_parallel();7980 let wants_to_quit = Arc::new(AtomicBool::new(false));8182 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 lines
137 // 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();144145 // 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 line
156 // 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 lines
modified:Cargo.lock+30 added
3 unmodified lines
4dependencies = [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 lines
53version = "0.9.1"54source = "registry+https://github.com/rust-lang/crates.io-index"5556[[package]]57name = "cfg-if"58version = "0.1.2"59source = "registry+https://github.com/rust-lang/crates.io-index"6061[[package]]62name = "clap"63version = "2.26.2"64source = "registry+https://github.com/rust-lang/crates.io-index"65dependencies = [
20 unmodified lines
86version = "0.2.10"87source = "registry+https://github.com/rust-lang/crates.io-index"8889[[package]]90name = "ctrlc"91version = "3.0.3"92source = "registry+https://github.com/rust-lang/crates.io-index"93dependencies = [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]9899[[package]]100name = "custom_derive"101version = "0.1.7"102source = "registry+https://github.com/rust-lang/crates.io-index"103
83 unmodified lines
187 "libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",188]189190[[package]]191name = "nix"192version = "0.8.1"193source = "registry+https://github.com/rust-lang/crates.io-index"194dependencies = [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]200201[[package]]202name = "num_cpus"203version = "1.7.0"204source = "registry+https://github.com/rust-lang/crates.io-index"205dependencies = [
157 unmodified lines
363"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 lines
380"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 lines
modified:Cargo.toml+2 added