From b83d8d7ef53bac73e7066cfd3f9497a62fc9233b Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 23 Jan 2020 21:53:24 -0800 Subject: [PATCH] [torrent stats] Pretty print torrents if `--print` is passed type: added --- src/torrent/stats.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/torrent/stats.rs b/src/torrent/stats.rs index 6b2b64b..30634fe 100644 --- a/src/torrent/stats.rs +++ b/src/torrent/stats.rs @@ -8,6 +8,8 @@ pub(crate) struct Stats { extract_patterns: Vec, #[structopt(name = "INPUT", long = "input", short = "i")] input: PathBuf, + #[structopt(long = "print", short = "p")] + print: bool, } impl Stats { @@ -20,7 +22,7 @@ impl Stats { 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())) { if extractor.torrents >= self.limit.unwrap_or(u64::max_value()) { @@ -85,16 +87,17 @@ impl Stats { struct Extractor { bencode_decode_errors: u64, + current_path: String, io_errors: u64, paths: HashMap, + print: bool, regex_set: RegexSet, torrents: u64, values: HashMap>, - current_path: String, } 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)) .expect("Validated regex pattern failed to recompile in regex set"); @@ -105,6 +108,7 @@ impl Extractor { torrents: 0, values: HashMap::new(), current_path: String::new(), + print, regex_set, } } @@ -134,6 +138,10 @@ impl Extractor { return; }; + if self.print { + eprintln!("{}: {}", path.display(), value); + } + self.extract(&value); }