Skip to content

Commit 6d25458

Browse files
committed
Restructure
1 parent bb81df3 commit 6d25458

File tree

3 files changed

+180
-15
lines changed

3 files changed

+180
-15
lines changed

Cargo.lock

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ repository = "https://github.com/sharkdp/dup"
1010
version = "0.2.0"
1111

1212
[[bin]]
13-
name = "fd"
13+
name = "dup"
1414
path = "src/main.rs"
1515

1616
[dependencies]
1717
ignore = "0.4.3"
1818
num_cpus = "1.0"
1919
humansize = "1.1"
2020

21+
[dependencies.clap]
22+
version = "2"
23+
features = ["suggestions", "color", "wrap_help"]
24+
2125
[profile.release]
2226
lto = true
2327
codegen-units = 1

src/main.rs

+51-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
extern crate humansize;
22
extern crate ignore;
33
extern crate num_cpus;
4+
#[macro_use]
5+
extern crate clap;
46

57
use std::collections::HashSet;
68
use std::os::unix::fs::MetadataExt;
9+
use std::path::Path;
710
use std::sync::mpsc::channel;
811
use std::thread;
912

13+
use clap::{App, AppSettings, Arg};
1014
use humansize::{file_size_opts, FileSize};
1115
use ignore::WalkBuilder;
1216

13-
fn main() {
14-
let mut builder = WalkBuilder::new("./");
17+
fn get_size<P: AsRef<Path>>(p: P, num_threads: usize) -> u64 {
18+
let mut builder = WalkBuilder::new(p);
1519
builder.hidden(false);
1620
builder.parents(false);
1721
builder.ignore(false);
@@ -20,12 +24,7 @@ fn main() {
2024
builder.git_exclude(false);
2125
builder.follow_links(false);
2226

23-
// Setting the number of threads to 3x the number of cores is a good tradeoff between
24-
// cold-cache and warm-cache runs. For a cold disk cache, we are limited by disk IO and
25-
// therefore want the number of threads to be rather large in order for the IO scheduler to
26-
// plan ahead. On the other hand, the number of threads shouldn't be too high for warm disk
27-
// caches where we would otherwise pay a higher synchronization overhead.
28-
builder.threads(3 * num_cpus::get());
27+
builder.threads(num_threads);
2928

3029
let walker = builder.build_parallel();
3130

@@ -44,11 +43,8 @@ fn main() {
4443
total += size;
4544
}
4645
}
47-
println!(
48-
"{} ({} bytes)",
49-
total.file_size(file_size_opts::DECIMAL).unwrap(),
50-
total
51-
);
46+
47+
total
5248
});
5349

5450
walker.run(|| {
@@ -86,5 +82,46 @@ fn main() {
8682
});
8783

8884
drop(tx);
89-
receiver_thread.join().ok();
85+
receiver_thread.join().unwrap()
86+
}
87+
88+
fn print_result(size: u64) {
89+
println!(
90+
"{} ({} bytes)",
91+
size.file_size(file_size_opts::DECIMAL).unwrap(),
92+
size
93+
);
94+
}
95+
96+
fn main() {
97+
let app = App::new(crate_name!())
98+
.setting(AppSettings::ColorAuto)
99+
.setting(AppSettings::ColoredHelp)
100+
.setting(AppSettings::DeriveDisplayOrder)
101+
.setting(AppSettings::UnifiedHelpMessage)
102+
.version(crate_version!())
103+
.about("Compute disk usage for the current directory")
104+
.arg(
105+
Arg::with_name("threads")
106+
.long("threads")
107+
.short("j")
108+
.value_name("N")
109+
.takes_value(true)
110+
.help("Set the number of threads (default: 3 x num cores)"),
111+
);
112+
113+
let matches = app.get_matches();
114+
115+
// Setting the number of threads to 3x the number of cores is a good tradeoff between
116+
// cold-cache and warm-cache runs. For a cold disk cache, we are limited by disk IO and
117+
// therefore want the number of threads to be rather large in order for the IO scheduler to
118+
// plan ahead. On the other hand, the number of threads shouldn't be too high for warm disk
119+
// caches where we would otherwise pay a higher synchronization overhead.
120+
let num_threads = matches
121+
.value_of("threads")
122+
.and_then(|t| t.parse().ok())
123+
.unwrap_or(3 * num_cpus::get());
124+
125+
let size = get_size(".", num_threads);
126+
print_result(size);
90127
}

0 commit comments

Comments
 (0)