-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.rs
65 lines (56 loc) · 1.83 KB
/
cli.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;
use clap::Parser;
use indicatif::ProgressBar;
use num_cpus;
use std::sync::mpsc::*;
use walkdir::WalkDir;
use crate::pipeline::{file_run, PreprocOpts};
lazy_static! {
static ref NUM_CPUS: String = num_cpus::get().to_string();
}
#[derive(Parser, Debug, Clone)]
pub struct CommandLine {
#[clap(short='t', default_value=NUM_CPUS.as_str(), parse(try_from_str = usize::from_str), help = "Number of threads to use")]
threads: usize,
#[clap(flatten)]
preproc_opts: PreprocOpts,
#[clap(parse(from_os_str))]
input: PathBuf,
#[clap(parse(from_os_str))]
output: PathBuf,
}
pub fn run(opts: CommandLine) {
if !opts.input.exists() {
panic!("Given input file or directory does not exist.");
}
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(num_cpus::get())
.build()
.unwrap();
let walker = WalkDir::new(opts.input).into_iter();
let mut output = fs::File::create(opts.output).unwrap();
// TODO: Use https://github.com/fosskers/linya instead, as indicatif concurrency is inneficient
let progress = ProgressBar::new(0);
pool.install(|| {
let (sender, receiver): (Sender<String>, Receiver<String>) = channel();
for entry in walker {
let entry = entry.unwrap();
if entry.path().is_file() {
progress.inc_length(1);
let tx = sender.clone();
let preproc_opts = opts.preproc_opts.clone();
pool.spawn(move || {
tx.send(file_run(preproc_opts, entry)).unwrap();
});
}
}
drop(sender);
while let Ok(text) = receiver.recv() {
progress.inc(1);
output.write_all(text.as_bytes()).unwrap();
}
});
}