feat: Added an option to automatically choose the first option

This commit is contained in:
Sayantan Santra 2023-12-04 17:47:55 -06:00
parent 790ec225ea
commit 16118a8d5a
Signed by: SinTan1729
GPG key ID: EB3E68BFBA25C85F
3 changed files with 40 additions and 19 deletions

View file

@ -30,6 +30,7 @@ sudo make install
- Default pattern is `{title} ({year}) - {director}`. Extension is always kept. - 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 `--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 `--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 `-nd` or `-dn` does a dry run in directory mode.
- Passing `--help` or `-h` shows help and exits. - Passing `--help` or `-h` shows help and exits.
- Passing `--version` or `-v` shows version and exits. - Passing `--version` or `-v` shows version and exits.

View file

@ -21,6 +21,7 @@ pub async fn process_file(
tmdb: &Client, tmdb: &Client,
pattern: &str, pattern: &str,
dry_run: bool, dry_run: bool,
lucky: bool,
movie_list: Option<&HashMap<String, Option<String>>>, movie_list: Option<&HashMap<String, Option<String>>>,
// The last bool tells whether the entry should be added to the movie_list or not // 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 // 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); return (filename_without_ext, None, true);
} }
// Choose from the possible entries let choice;
let choice = match Select::new( if lucky {
format!(" Possible choices for {file_base}:").as_str(), // Take first choice if in lucky mode
movie_list, choice = movie_list.into_iter().next().unwrap();
) } else {
.prompt() // Choose from the possible entries
{ choice = match Select::new(
Ok(movie) => movie, format!(" Possible choices for {file_base}:").as_str(),
Err(error) => { movie_list,
println!(" {error}"); )
let flag = matches!( .prompt()
error, {
InquireError::OperationCanceled | InquireError::OperationInterrupted Ok(movie) => movie,
); Err(error) => {
return (filename_without_ext, None, flag); println!(" {error}");
} let flag = matches!(
error,
InquireError::OperationCanceled | InquireError::OperationInterrupted
);
return (filename_without_ext, None, flag);
}
};
}; };
// Create the new name // Create the new name
@ -206,13 +213,13 @@ pub fn process_args(mut args: Vec<String>) -> (Vec<String>, HashMap<&'static str
// Remove the entry corresponding to the running process // Remove the entry corresponding to the running process
args.remove(0); args.remove(0);
let mut entries = Vec::new(); 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 { for arg in args {
match arg.as_str() { match arg.as_str() {
"--help" | "-h" => { "--help" | "-h" => {
println!(" The expected syntax is:"); println!(" The expected syntax is:");
println!( println!(
" movie-rename <filename(s)> [-n|--dry-run] [-d|--directory] [-v|--version]" " movie-rename <filename(s)> [-n|--dry-run] [-d|--directory] [-l|--i-feel-lucky] [-v|--version]"
); );
println!( println!(
" There needs to be a config file named config in the $XDG_CONFIG_HOME/movie-rename/ directory." " 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<String>) -> (Vec<String>, HashMap<&'static str
println!("Doing a dry run..."); println!("Doing a dry run...");
settings.entry("dry_run").and_modify(|x| *x = true); 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" => { "--directory" | "-d" => {
println!("Running in directory mode..."); println!("Running in directory mode...");
settings.entry("directory").and_modify(|x| *x = true); settings.entry("directory").and_modify(|x| *x = true);

View file

@ -48,7 +48,15 @@ async fn main() {
false => { false => {
if Path::new(entry.as_str()).is_file() { if Path::new(entry.as_str()).is_file() {
// Process the filename for movie entries // 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 { } else {
eprintln!("The file {entry} wasn't found on disk, skipping..."); eprintln!("The file {entry} wasn't found on disk, skipping...");
continue; continue;
@ -70,6 +78,7 @@ async fn main() {
&tmdb, &tmdb,
pattern, pattern,
settings["dry_run"], settings["dry_run"],
settings["lucky"],
Some(&movie_list), Some(&movie_list),
) )
.await; .await;