From c93ddd7709088fcd119b1f91b788284e1e661347 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 30 Jul 2020 16:18:40 +0100 Subject: [PATCH] added quiet mode --- Cargo.toml | 2 +- src/args.rs | 20 +++++++++--- src/config.rs | 17 ++++++++++ src/work_async.rs | 20 +++++++++--- src/work_async/progress.rs | 63 ++++++++++++++++++++++++++++++++++++-- 5 files changed, 110 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16daf9c..175b9e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lolistealer" -version = "1.2.0" +version = "1.3.0" authors = ["Avril "] edition = "2018" diff --git a/src/args.rs b/src/args.rs index 9bd5dee..05cf093 100644 --- a/src/args.rs +++ b/src/args.rs @@ -17,14 +17,24 @@ lazy_static! { /// Print usage then exit with code `1` pub fn usage() -> ! { - println!("lolistealer version {}\n", env!("CARGO_PKG_VERSION")); - println!("Usage: {} [--rating ] [--tags ][]", &PROGRAM_NAME[..]); + println!("lolistealer version {}", env!("CARGO_PKG_VERSION")); + println!(" written by {} with <3", env!("CARGO_PKG_AUTHORS")); + println!(" licensed with GNU GPL 3.0 or later\n"); + println!("Usage: {} [-q] [--rating ] [--tags ] []", &PROGRAM_NAME[..]); println!("Usage: {} --help", &PROGRAM_NAME[..]); - println!("Filter expression:"); + println!("Options:"); + println!(" -q\t\t\tQuiet mode, do not output progress"); + println!(" --rating \tEither `s`afe, `q`uestionable, or `e`xplicit"); + println!(" --tags \tFilter downloaded images out by tags, see below for format."); + println!("Tags filter expreession:"); println!(" tag_name\tMust contain this tag"); println!(" -tag_name\tMust not contains this tag"); println!(" +tag_name\tMust contains at least one tag prepended with `+`"); + println!("Note:"); + println!(" Tag filter expreession is a single argument, tags must be seperated with spaces. e.g. 'tag1 -tag2 +tag3 +tag4'"); + #[cfg(feature="async")] + println!("\n (compiled with threading support)"); std::process::exit(1) } @@ -64,6 +74,7 @@ where I: IntoIterator let mut one = String::default(); let mut reading = true; let mut tags = Vec::new(); + let mut verbose = Default::default(); macro_rules! take_one { () => { @@ -84,6 +95,7 @@ where I: IntoIterator match arg.to_lowercase().trim() { "-" => reading = false, "--help" => return Ok(Mode::Help), + "-q" => verbose = config::Verbosity::Silent, "--rating" if take_one!() => rating = one.parse::()?, "--tags" if take_one!() => tags = tags::parse(&one), _ => paths.push(try_dir(arg)?), @@ -97,7 +109,7 @@ where I: IntoIterator return Err(Error::NoOutput); } - Ok(Mode::Normal(config::Config{rating, output: paths, tags})) + Ok(Mode::Normal(config::Config{rating, output: paths, tags, verbose})) } #[derive(Debug)] diff --git a/src/config.rs b/src/config.rs index 3e4fbb9..54db10c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,21 @@ impl Rating } } +#[derive(Debug, PartialEq, Eq, Hash)] +pub enum Verbosity +{ + Full, + Silent, +} + +impl Default for Verbosity +{ + fn default() -> Self + { + Self::Full + } +} + #[derive(Debug, PartialEq, Eq, Hash)] pub enum OutputType { @@ -37,6 +52,7 @@ pub struct Config pub rating: Rating, pub output: Vec, pub tags: Vec, + pub verbose: Verbosity, } impl Default for Rating @@ -54,6 +70,7 @@ impl Default for Config rating: Rating::default(), output: Vec::new(), tags: Vec::new(), + verbose: Default::default(), } } } diff --git a/src/work_async.rs b/src/work_async.rs index 83317f8..7fb8182 100644 --- a/src/work_async.rs +++ b/src/work_async.rs @@ -86,15 +86,27 @@ pub async fn perform(url: impl AsRef, path: impl AsRef, progress: &mu Ok(()) } + pub async fn work(conf: config::Config) -> Result<(), Box> { let rating = conf.rating; let mut children = Vec::with_capacity(conf.output.len()); - let prog = progress::AsyncProgressCounter::new("Initialising...", 1); - let mut prog_writer = prog.writer(); - - let prog = prog.host(); + let (mut prog_writer, prog) = match conf.verbose { + config::Verbosity::Full => { + let prog = progress::AsyncProgressCounter::::new("Initialising...", 1); + let prog_writer = prog.writer(); + let prog = prog.host(); + (prog_writer, prog) + }, + config::Verbosity::Silent => { + let prog = progress::AsyncProgressCounter::::new("Initialising...", 1); + let prog_writer = prog.writer(); + let prog = prog.host(); + (prog_writer, prog) + }, + }; + for path in conf.output.into_iter() { diff --git a/src/work_async/progress.rs b/src/work_async/progress.rs index 6072647..f0cf6e5 100644 --- a/src/work_async/progress.rs +++ b/src/work_async/progress.rs @@ -47,7 +47,8 @@ impl std::ops::Drop for Command } } -pub struct AsyncProgressCounter +pub struct AsyncProgressCounter + where T: termprogress::ProgressBar + WithTitle + std::marker::Send + std::marker::Sync +'static, { writer: Sender, reader: Receiver, @@ -55,6 +56,8 @@ pub struct AsyncProgressCounter small: u64, large: u64, + + _phantom: std::marker::PhantomData, } #[derive(Clone)] @@ -226,7 +229,8 @@ impl CommandCallback } } -impl AsyncProgressCounter +impl AsyncProgressCounter + where T: termprogress::ProgressBar + WithTitle + std::marker::Send + std::marker::Sync +'static, { /// Create a new `AsyncProgressCounter` pub fn new(title: impl Into, max: u64) -> Self @@ -239,6 +243,8 @@ impl AsyncProgressCounter title: title.into(), small: 0, large: max, + + _phantom: std::marker::PhantomData, } } @@ -257,7 +263,7 @@ impl AsyncProgressCounter pub fn host(mut self) -> JoinHandle<()> { //let mut spin = spinner::Spin::with_title(&self.title[..], Default::default()); - let mut bar = termprogress::progress::Bar::with_title(50, &self.title[..]); + let mut bar = T::with_title(50, &self.title[..]); bar.update(); @@ -317,3 +323,54 @@ impl AsyncProgressCounter }) } } + +pub trait WithTitle: Sized + ProgressBar + Display +{ + fn with_title(len: usize, string: impl AsRef) -> Self; + fn update(&mut self); + fn complete(self); +} + +impl WithTitle for termprogress::progress::Bar +{ + fn with_title(len: usize, string: impl AsRef) -> Self + { + Self::with_title(len, string) + } + fn update(&mut self) + { + self.update(); + } + fn complete(self) + { + self.complete(); + } +} + +#[derive(Debug)] +pub struct Silent; + + +impl termprogress::Display for Silent +{ + fn println(&self, _: &str){} + fn refresh(&self){} + fn blank(&self){} + fn get_title(&self) -> &str{""} + fn set_title(&mut self, _: &str){} + fn update_dimensions(&mut self, _:usize){} +} + +impl termprogress::ProgressBar for Silent +{ + fn set_progress(&mut self, _:f64){} + fn get_progress(&self) -> f64{0.0} +} + +impl WithTitle for Silent +{ + fn with_title(_: usize, _: impl AsRef) -> Self{Self} + fn update(&mut self) {} + fn complete(self) {} +} +