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.
- 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.

View file

@ -21,6 +21,7 @@ pub async fn process_file(
tmdb: &Client,
pattern: &str,
dry_run: bool,
lucky: bool,
movie_list: Option<&HashMap<String, Option<String>>>,
// 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,8 +130,13 @@ pub async fn process_file(
return (filename_without_ext, None, true);
}
let choice;
if lucky {
// Take first choice if in lucky mode
choice = movie_list.into_iter().next().unwrap();
} else {
// Choose from the possible entries
let choice = match Select::new(
choice = match Select::new(
format!(" Possible choices for {file_base}:").as_str(),
movie_list,
)
@ -146,6 +152,7 @@ pub async fn process_file(
return (filename_without_ext, None, flag);
}
};
};
// Create the new name
new_name_base = choice.rename_format(String::from(pattern));
@ -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
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 <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!(
" 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...");
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);

View file

@ -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;