From acd8f875ec1701d31490d9767fb61320b1f8f3ef Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Tue, 16 Feb 2021 18:43:01 +0200 Subject: [PATCH] Enable "quiet mode" --- src/bin/id3-image-embed.rs | 13 ++++++++++--- src/bin/id3-image-extract.rs | 15 +++++++++++---- src/bin/id3-image-remove.rs | 24 +++++++++++++++++------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/bin/id3-image-embed.rs b/src/bin/id3-image-embed.rs index 87dc466..c1a3043 100644 --- a/src/bin/id3-image-embed.rs +++ b/src/bin/id3-image-embed.rs @@ -9,7 +9,11 @@ use id3_image::embed_image; struct Opt { /// Verbose mode (-v, -vv, -vvv, etc.) #[structopt(short = "v", long = "verbose", parse(from_occurrences))] - verbose: u8, + verbose: i8, + + /// Quiet mode, implies no verbosity, and also no error explanations + #[structopt(short = "q", long = "quiet")] + quiet: bool, /// Music file to embed image into #[structopt(name = "music-file.mp3", required = true, parse(from_os_str))] @@ -22,15 +26,18 @@ struct Opt { fn main() { let opt = Opt::from_args(); + let verbosity = if opt.quiet { -1 } else { opt.verbose }; let music_filename = opt.music_filename; let image_filename = opt.image_filename. unwrap_or_else(|| music_filename.with_extension("jpg")); if let Err(e) = embed_image(&music_filename, &image_filename) { - eprintln!("{}", e); + if verbosity >= 0 { + eprintln!("{}", e); + } process::exit(1); } - if opt.verbose >= 1 { + if verbosity >= 1 { println!("Embedded {:?} into {:?}", image_filename, music_filename); } } diff --git a/src/bin/id3-image-extract.rs b/src/bin/id3-image-extract.rs index f9b9266..96f80fc 100644 --- a/src/bin/id3-image-extract.rs +++ b/src/bin/id3-image-extract.rs @@ -9,7 +9,11 @@ use id3_image::extract_first_image; struct Opt { /// Verbose mode (-v, -vv, -vvv, etc.) #[structopt(short = "v", long = "verbose", parse(from_occurrences))] - verbose: u8, + verbose: i8, + + /// Quiet mode, implies no verbosity, and also no error explanations + #[structopt(short = "q", long = "quiet")] + quiet: bool, /// Music file to extract image from #[structopt(name = "music-file.mp3", required = true, parse(from_os_str))] @@ -22,19 +26,22 @@ struct Opt { fn main() { let opt = Opt::from_args(); + let verbosity = if opt.quiet { -1 } else { opt.verbose }; let image_filename = opt.image_filename.clone(). unwrap_or_else(|| opt.music_filename.with_extension("jpg")); if let Err(e) = extract_first_image(&opt.music_filename, &image_filename) { - eprintln!("{}", e); + if verbosity >= 0 { + eprintln!("{}", e); + } process::exit(1); } - if opt.verbose == 1 { + if verbosity == 1 { // then just print the output filename for scripting purposes: println!("{}", image_filename.display()); - } else if opt.verbose >= 2 { + } else if verbosity >= 2 { // show a longer status message: println!("Extracted cover art from {:?} to {:?}", opt.music_filename, image_filename); } diff --git a/src/bin/id3-image-remove.rs b/src/bin/id3-image-remove.rs index 30736a0..a71aa51 100644 --- a/src/bin/id3-image-remove.rs +++ b/src/bin/id3-image-remove.rs @@ -10,7 +10,11 @@ use id3_image::remove_images; struct Opt { /// Verbose mode (-v, -vv, -vvv, etc.) #[structopt(short = "v", long = "verbose", parse(from_occurrences))] - verbose: u8, + verbose: i8, + + /// Quiet mode, implies no verbosity, and also no error explanations + #[structopt(short = "q", long = "quiet")] + quiet: bool, /// Don't ask for confirmation before removing #[structopt(long = "no-confirm")] @@ -23,29 +27,35 @@ struct Opt { fn main() { let opt = Opt::from_args(); + let verbosity = if opt.quiet { -1 } else { opt.verbose }; + let confirm = !opt.no_confirm; - if !opt.no_confirm { + if verbosity >= 0 && confirm { print_prompt(); let mut input = String::new(); while let Err(_) = io::stdin().read_line(&mut input) { - println!("Could not read your input, please try again."); + if verbosity >= 0 { + eprintln!("Could not read your input, please try again."); + } print_prompt(); } let choice = input.to_lowercase().trim().chars().next().unwrap_or('y'); if choice != 'y' { - if opt.verbose >= 1 { + if verbosity >= 1 { println!("Exiting without removing images"); } - process::exit(0); + return; } } if let Err(e) = remove_images(&opt.music_filename) { - eprintln!("{}", e); + if verbosity >= 0 { + eprintln!("{}", e); + } process::exit(1); } - if opt.verbose >= 1 { + if verbosity >= 1 { println!("Removed images on {:?}", opt.music_filename); } }