diff --git a/Cargo.lock b/Cargo.lock index 7a54006..c0f9756 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ dependencies = [ "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.26.0 (registry+https://github.com/rust-lang/crates.io-index)", "ignore 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -122,6 +123,14 @@ dependencies = [ "libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num_cpus" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.2.2" @@ -249,6 +258,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)" = "2370ca07ec338939e356443dac2296f581453c35fe1e3a3ed06023c49435f915" "checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b" "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4" +"checksum num_cpus 1.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aec53c34f2d0247c5ca5d32cca1478762f301740468ee9ee6dcb7a0dd7a0c584" "checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b" "checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db" "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7" diff --git a/Cargo.toml b/Cargo.toml index 5c68238..f747681 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ clap = "2.26.0" atty = "0.2" regex = "0.2" ignore = "0.2" +num_cpus = "1.6.2" diff --git a/src/main.rs b/src/main.rs index d917ec9..268b499 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate ansi_term; extern crate atty; extern crate regex; extern crate ignore; +extern crate num_cpus; pub mod lscolors; pub mod fshelper; @@ -67,7 +68,7 @@ struct FdOptions { max_depth: Option, /// The number of threads to use. - threads: Option, + threads: usize, /// Time to buffer results internally before streaming to the console. This is useful to /// provide a sorted output, in case the total execution time is shorter than @@ -209,7 +210,7 @@ fn scan(root: &Path, pattern: Arc, base: &Path, config: Arc) { .git_exclude(config.read_ignore) .follow_links(config.follow_links) .max_depth(config.max_depth) - .threads(config.threads.unwrap_or(0)) + .threads(config.threads) .build_parallel(); // Spawn the thread that receives all results through the channel. @@ -433,8 +434,12 @@ fn main() { null_separator: matches.is_present("null_separator"), max_depth: matches.value_of("depth") .and_then(|n| usize::from_str_radix(n, 10).ok()), - threads: matches.value_of("threads") - .and_then(|n| usize::from_str_radix(n, 10).ok()), + threads: std::cmp::max( + matches.value_of("threads") + .and_then(|n| usize::from_str_radix(n, 10).ok()) + .unwrap_or(num_cpus::get()), + 1 + ), max_buffer_time: matches.value_of("max-buffer-time") .and_then(|n| u64::from_str_radix(n, 10).ok()) .map(time::Duration::from_millis),