From 16118a8d5a056ed55c14172a1ec2e5cd93f455f5 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 4 Dec 2023 17:47:55 -0600 Subject: [PATCH] feat: Added an option to automatically choose the first option --- README.md | 1 + src/functions.rs | 47 +++++++++++++++++++++++++++++------------------ src/main.rs | 11 ++++++++++- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index cc9cf4c..cf83155 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ sudo make install - Default pattern is `{title} ({year}) - {director}`. Extension is always kept. - Passing `--directory` or `-d` assumes that the arguments are directory names, which contain exactly one movie and optionally subtitles. - Passing `--dry-run` or `-n` does a dry tun and only prints out the new names, without actually doing anything. +- Passing `--i-feel-lucky` or `-l` automatically chooses the first option. Useful when you use the program as part of a script. - Passing `-nd` or `-dn` does a dry run in directory mode. - Passing `--help` or `-h` shows help and exits. - Passing `--version` or `-v` shows version and exits. diff --git a/src/functions.rs b/src/functions.rs index 8880a54..1a69309 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -21,6 +21,7 @@ pub async fn process_file( tmdb: &Client, pattern: &str, dry_run: bool, + lucky: bool, movie_list: Option<&HashMap>>, // The last bool tells whether the entry should be added to the movie_list or not // The first String is filename without extension, and the second String is @@ -129,22 +130,28 @@ pub async fn process_file( return (filename_without_ext, None, true); } - // Choose from the possible entries - let choice = match Select::new( - format!(" Possible choices for {file_base}:").as_str(), - movie_list, - ) - .prompt() - { - Ok(movie) => movie, - Err(error) => { - println!(" {error}"); - let flag = matches!( - error, - InquireError::OperationCanceled | InquireError::OperationInterrupted - ); - return (filename_without_ext, None, flag); - } + let choice; + if lucky { + // Take first choice if in lucky mode + choice = movie_list.into_iter().next().unwrap(); + } else { + // Choose from the possible entries + choice = match Select::new( + format!(" Possible choices for {file_base}:").as_str(), + movie_list, + ) + .prompt() + { + Ok(movie) => movie, + Err(error) => { + println!(" {error}"); + let flag = matches!( + error, + InquireError::OperationCanceled | InquireError::OperationInterrupted + ); + return (filename_without_ext, None, flag); + } + }; }; // Create the new name @@ -206,13 +213,13 @@ pub fn process_args(mut args: Vec) -> (Vec, HashMap<&'static str // Remove the entry corresponding to the running process args.remove(0); let mut entries = Vec::new(); - let mut settings = HashMap::from([("dry_run", false), ("directory", false)]); + let mut settings = HashMap::from([("dry_run", false), ("directory", false), ("lucky", false)]); for arg in args { match arg.as_str() { "--help" | "-h" => { println!(" The expected syntax is:"); println!( - " movie-rename [-n|--dry-run] [-d|--directory] [-v|--version]" + " movie-rename [-n|--dry-run] [-d|--directory] [-l|--i-feel-lucky] [-v|--version]" ); println!( " There needs to be a config file named config in the $XDG_CONFIG_HOME/movie-rename/ directory." @@ -240,6 +247,10 @@ pub fn process_args(mut args: Vec) -> (Vec, HashMap<&'static str println!("Doing a dry run..."); settings.entry("dry_run").and_modify(|x| *x = true); } + "--i-feel-lucky" | "-l" => { + println!("Choosing the first option automatically..."); + settings.entry("lucky").and_modify(|x| *x = true); + } "--directory" | "-d" => { println!("Running in directory mode..."); settings.entry("directory").and_modify(|x| *x = true); diff --git a/src/main.rs b/src/main.rs index 3b70013..f21dfde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,15 @@ async fn main() { false => { if Path::new(entry.as_str()).is_file() { // Process the filename for movie entries - process_file(&entry, &tmdb, pattern, settings["dry_run"], None).await; + process_file( + &entry, + &tmdb, + pattern, + settings["dry_run"], + settings["lucky"], + None, + ) + .await; } else { eprintln!("The file {entry} wasn't found on disk, skipping..."); continue; @@ -70,6 +78,7 @@ async fn main() { &tmdb, pattern, settings["dry_run"], + settings["lucky"], Some(&movie_list), ) .await;