From 83bf1f7af673bc2cdd78f63c167f9551b1949952 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Wed, 6 Mar 2024 15:41:45 -0600 Subject: [PATCH] new: Autocomplete files generation --- Cargo.lock | 10 ++++++++++ Cargo.toml | 5 +++++ Makefile | 2 +- build.rs | 21 +++++++++++++++++++++ src/args.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/functions.rs | 40 ---------------------------------------- src/main.rs | 5 +++-- 7 files changed, 86 insertions(+), 43 deletions(-) create mode 100644 build.rs create mode 100644 src/args.rs diff --git a/Cargo.lock b/Cargo.lock index 86ce8be..60e3aee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "885e4d7d5af40bfb99ae6f9433e292feac98d452dcb3ec3d25dfe7552b77da8c" +dependencies = [ + "clap", +] + [[package]] name = "clap_lex" version = "0.7.0" @@ -550,6 +559,7 @@ name = "movie-rename" version = "2.2.2" dependencies = [ "clap", + "clap_complete", "inquire", "load_file", "tmdb-api", diff --git a/Cargo.toml b/Cargo.toml index 6ca35da..7191ed2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "movie-rename" version = "2.2.2" +build = "build.rs" edition = "2021" authors = ["Sayantan Santra Result<(), Error> { + let target = "./target/autocomplete"; + remove_dir_all(target).ok(); + create_dir(target)?; + let outdir = OsString::from(target); + + let mut cmd = get_command(); + generate_to(Bash, &mut cmd, "movie-rename", &outdir)?; + generate_to(Fish, &mut cmd, "movie-rename", &outdir)?; + generate_to(Zsh, &mut cmd, "movie-rename", &outdir)?; + Ok(()) +} diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..21610f3 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,46 @@ +use clap::{arg, command, ArgAction, Command, ValueHint}; +use std::collections::HashMap; + +// Bare command generation function to help with autocompletion +pub fn get_command() -> Command { + command!() + .name("movie-rename") + .author("Sayantan Santra ") + .about("A simple tool to rename movies, written in Rust.") + .arg(arg!(-d --directory "Run in directory mode").action(ArgAction::SetTrue)) + .arg(arg!(-n --"dry-run" "Do a dry run").action(ArgAction::SetTrue)) + .arg(arg!(-l --"i-feel-lucky" "Always choose the first option").action(ArgAction::SetTrue)) + .arg( + arg!([entries] "The files/directories to be processed") + .trailing_var_arg(true) + .num_args(1..) + .value_hint(ValueHint::AnyPath) + .required(true), + ) + // Use -v instead of -V for version + .disable_version_flag(true) + .arg(arg!(-v --version "Print version").action(ArgAction::Version)) + .arg_required_else_help(true) +} + +// Function to process the passed arguments +pub fn process_args() -> (Vec, HashMap) { + let matches = get_command().get_matches(); + + // Generate the settings HashMap from read flags + let mut settings = HashMap::new(); + for id in matches.ids().map(|x| x.as_str()) { + if id != "entries" { + settings.insert(id.to_string(), matches.get_flag(id)); + } + } + + // Every unmatched argument should be treated as a file entry + let entries: Vec = matches + .get_many::("entries") + .expect("No entries provided!") + .cloned() + .collect(); + + (entries, settings) +} diff --git a/src/functions.rs b/src/functions.rs index 2246e64..aab057a 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -1,4 +1,3 @@ -use clap::{arg, command, ArgAction}; use inquire::{ ui::{Color, IndexPrefix, RenderConfig, Styled}, InquireError, Select, @@ -206,45 +205,6 @@ pub async fn process_file( (filename_without_ext, Some(new_name_base), true) } -// Function to process the passed arguments -pub fn process_args() -> (Vec, HashMap) { - let matches = command!() - .name("movie-rename") - .author("Sayantan Santra ") - .about("A simple tool to rename movies, written in Rust.") - .arg(arg!(-d --directory "Run in directory mode").action(ArgAction::SetTrue)) - .arg(arg!(-n --"dry-run" "Do a dry run").action(ArgAction::SetTrue)) - .arg(arg!(-l --"i-feel-lucky" "Always choose the first option").action(ArgAction::SetTrue)) - .arg( - arg!([entries] "The files/directories to be processed") - .trailing_var_arg(true) - .num_args(1..) - .required(true), - ) - // Use -v instead of -V for version - .disable_version_flag(true) - .arg(arg!(-v --version "Print version").action(ArgAction::Version)) - .arg_required_else_help(true) - .get_matches(); - - // Generate the settings HashMap from read flags - let mut settings = HashMap::new(); - for id in matches.ids().map(|x| x.as_str()) { - if id != "entries" { - settings.insert(id.to_string(), matches.get_flag(id)); - } - } - - // Every unmatched argument should be treated as a file entry - let entries: Vec = matches - .get_many::("entries") - .expect("No entries provided!") - .cloned() - .collect(); - - (entries, settings) -} - // RenderConfig for the menu items fn get_render_config() -> RenderConfig { let mut render_config = RenderConfig::default(); diff --git a/src/main.rs b/src/main.rs index fc182fc..227484f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,14 @@ use tmdb_api::Client; // Import all the modules mod functions; -use functions::{process_args, process_file}; +use functions::process_file; +mod args; mod structs; #[tokio::main] async fn main() { // Process the passed arguments - let (entries, settings) = process_args(); + let (entries, settings) = args::process_args(); let flag_dry_run = settings["dry-run"]; let flag_directory = settings["directory"]; let flag_lucky = settings["i-feel-lucky"];