[torrent stats] Pretty print torrents if --print is passed

type: added
This commit is contained in:
Casey Rodarmor 2020-01-23 21:53:24 -08:00
parent 7d5e68f194
commit b83d8d7ef5
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0

View File

@ -8,6 +8,8 @@ pub(crate) struct Stats {
extract_patterns: Vec<Regex>, extract_patterns: Vec<Regex>,
#[structopt(name = "INPUT", long = "input", short = "i")] #[structopt(name = "INPUT", long = "input", short = "i")]
input: PathBuf, input: PathBuf,
#[structopt(long = "print", short = "p")]
print: bool,
} }
impl Stats { impl Stats {
@ -20,7 +22,7 @@ impl Stats {
let path = env.resolve(self.input); let path = env.resolve(self.input);
let mut extractor = Extractor::new(&self.extract_patterns); let mut extractor = Extractor::new(self.print, &self.extract_patterns);
for result in WalkDir::new(path).sort_by(|a, b| a.file_name().cmp(b.file_name())) { for result in WalkDir::new(path).sort_by(|a, b| a.file_name().cmp(b.file_name())) {
if extractor.torrents >= self.limit.unwrap_or(u64::max_value()) { if extractor.torrents >= self.limit.unwrap_or(u64::max_value()) {
@ -85,16 +87,17 @@ impl Stats {
struct Extractor { struct Extractor {
bencode_decode_errors: u64, bencode_decode_errors: u64,
current_path: String,
io_errors: u64, io_errors: u64,
paths: HashMap<String, u64>, paths: HashMap<String, u64>,
print: bool,
regex_set: RegexSet, regex_set: RegexSet,
torrents: u64, torrents: u64,
values: HashMap<String, Vec<String>>, values: HashMap<String, Vec<String>>,
current_path: String,
} }
impl Extractor { impl Extractor {
fn new(regexes: &[Regex]) -> Self { fn new(print: bool, regexes: &[Regex]) -> Self {
let regex_set = RegexSet::new(regexes.iter().map(Regex::as_str)) let regex_set = RegexSet::new(regexes.iter().map(Regex::as_str))
.expect("Validated regex pattern failed to recompile in regex set"); .expect("Validated regex pattern failed to recompile in regex set");
@ -105,6 +108,7 @@ impl Extractor {
torrents: 0, torrents: 0,
values: HashMap::new(), values: HashMap::new(),
current_path: String::new(), current_path: String::new(),
print,
regex_set, regex_set,
} }
} }
@ -134,6 +138,10 @@ impl Extractor {
return; return;
}; };
if self.print {
eprintln!("{}: {}", path.display(), value);
}
self.extract(&value); self.extract(&value);
} }