Enable "quiet mode"

This commit is contained in:
Andrew Radev 2021-02-16 18:43:01 +02:00
parent 5254362522
commit acd8f875ec
3 changed files with 38 additions and 14 deletions

View File

@ -9,7 +9,11 @@ use id3_image::embed_image;
struct Opt { struct Opt {
/// Verbose mode (-v, -vv, -vvv, etc.) /// Verbose mode (-v, -vv, -vvv, etc.)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))] #[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 /// Music file to embed image into
#[structopt(name = "music-file.mp3", required = true, parse(from_os_str))] #[structopt(name = "music-file.mp3", required = true, parse(from_os_str))]
@ -22,15 +26,18 @@ struct Opt {
fn main() { fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
let verbosity = if opt.quiet { -1 } else { opt.verbose };
let music_filename = opt.music_filename; let music_filename = opt.music_filename;
let image_filename = opt.image_filename. let image_filename = opt.image_filename.
unwrap_or_else(|| music_filename.with_extension("jpg")); unwrap_or_else(|| music_filename.with_extension("jpg"));
if let Err(e) = embed_image(&music_filename, &image_filename) { if let Err(e) = embed_image(&music_filename, &image_filename) {
if verbosity >= 0 {
eprintln!("{}", e); eprintln!("{}", e);
}
process::exit(1); process::exit(1);
} }
if opt.verbose >= 1 { if verbosity >= 1 {
println!("Embedded {:?} into {:?}", image_filename, music_filename); println!("Embedded {:?} into {:?}", image_filename, music_filename);
} }
} }

View File

@ -9,7 +9,11 @@ use id3_image::extract_first_image;
struct Opt { struct Opt {
/// Verbose mode (-v, -vv, -vvv, etc.) /// Verbose mode (-v, -vv, -vvv, etc.)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))] #[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 /// Music file to extract image from
#[structopt(name = "music-file.mp3", required = true, parse(from_os_str))] #[structopt(name = "music-file.mp3", required = true, parse(from_os_str))]
@ -22,19 +26,22 @@ struct Opt {
fn main() { fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
let verbosity = if opt.quiet { -1 } else { opt.verbose };
let image_filename = opt.image_filename.clone(). let image_filename = opt.image_filename.clone().
unwrap_or_else(|| opt.music_filename.with_extension("jpg")); unwrap_or_else(|| opt.music_filename.with_extension("jpg"));
if let Err(e) = extract_first_image(&opt.music_filename, &image_filename) { if let Err(e) = extract_first_image(&opt.music_filename, &image_filename) {
if verbosity >= 0 {
eprintln!("{}", e); eprintln!("{}", e);
}
process::exit(1); process::exit(1);
} }
if opt.verbose == 1 { if verbosity == 1 {
// then just print the output filename for scripting purposes: // then just print the output filename for scripting purposes:
println!("{}", image_filename.display()); println!("{}", image_filename.display());
} else if opt.verbose >= 2 { } else if verbosity >= 2 {
// show a longer status message: // show a longer status message:
println!("Extracted cover art from {:?} to {:?}", opt.music_filename, image_filename); println!("Extracted cover art from {:?} to {:?}", opt.music_filename, image_filename);
} }

View File

@ -10,7 +10,11 @@ use id3_image::remove_images;
struct Opt { struct Opt {
/// Verbose mode (-v, -vv, -vvv, etc.) /// Verbose mode (-v, -vv, -vvv, etc.)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))] #[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 /// Don't ask for confirmation before removing
#[structopt(long = "no-confirm")] #[structopt(long = "no-confirm")]
@ -23,29 +27,35 @@ struct Opt {
fn main() { fn main() {
let opt = Opt::from_args(); 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(); print_prompt();
let mut input = String::new(); let mut input = String::new();
while let Err(_) = io::stdin().read_line(&mut input) { 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(); print_prompt();
} }
let choice = input.to_lowercase().trim().chars().next().unwrap_or('y'); let choice = input.to_lowercase().trim().chars().next().unwrap_or('y');
if choice != 'y' { if choice != 'y' {
if opt.verbose >= 1 { if verbosity >= 1 {
println!("Exiting without removing images"); println!("Exiting without removing images");
} }
process::exit(0); return;
} }
} }
if let Err(e) = remove_images(&opt.music_filename) { if let Err(e) = remove_images(&opt.music_filename) {
if verbosity >= 0 {
eprintln!("{}", e); eprintln!("{}", e);
}
process::exit(1); process::exit(1);
} }
if opt.verbose >= 1 { if verbosity >= 1 {
println!("Removed images on {:?}", opt.music_filename); println!("Removed images on {:?}", opt.music_filename);
} }
} }