From 6c430e28694730f7e1868d25259e40a188465443 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Sun, 23 Feb 2025 18:32:12 +0100 Subject: [PATCH] feat: added folder selection loop to make it easier to use --- src/files/mod.rs | 14 ++++++++++++++ src/main.rs | 41 +++++++++++++++++++++++++++++++++++++---- src/parsers/mod.rs | 1 - 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/files/mod.rs b/src/files/mod.rs index a944494..c16ed6b 100644 --- a/src/files/mod.rs +++ b/src/files/mod.rs @@ -20,6 +20,20 @@ pub fn get_template_options(base_paths: Vec) -> Vec { type_options } +pub fn get_sub_dirs_paths(dir: &str) -> Vec { + let paths = fs::read_dir(dir).unwrap(); + + let mut valid_dirs: Vec = vec![]; + + for path in paths { + let _path = path.unwrap().path(); + if _path.is_dir() { + valid_dirs.push(_path.display().to_string()); + } + } + return valid_dirs; +} + pub fn get_valid_dirs_paths(dir: &str) -> Vec { // FIXME: this panics when the directory does not exist let paths = fs::read_dir(dir).unwrap(); diff --git a/src/main.rs b/src/main.rs index ad43bd0..1b96e46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,11 +30,44 @@ fn main() { let target_name_result = Text::new("Insert the desired name:").prompt(); if let Ok(target_name) = target_name_result { - // TODO: decide if the path should be inserted automatically of with a loop of selections -> Maybe better the loop - let target_path_result = Text::new("Insert the target path:").prompt(); + let mut target_path_heap = "./".to_string(); + const CONFIRM_OPTION: &str = "-- Current --"; + const CANCEL_OPTION: &str = "-- Cancel --"; - if let Ok(target_path) = target_path_result { - create_template(template_path, target_name, target_path).unwrap(); + loop { + let mut sub_directory_list = get_sub_dirs_paths(&target_path_heap); + sub_directory_list.insert(0, CONFIRM_OPTION.to_string()); + sub_directory_list.push(CANCEL_OPTION.to_string()); + + let target_path_result = Select::new( + &format!( + "Select a target directory\n Current is: {}", + &target_path_heap + ), + sub_directory_list, + ) + .prompt(); + if let Ok(target_path) = target_path_result { + // TODO: also check if dir has no subdirs + if target_path == CANCEL_OPTION { + break; + } + + if target_path == CONFIRM_OPTION { + create_template( + template_path.clone(), + target_name.clone(), + target_path_heap.clone(), + ) + .unwrap(); + break; + } else { + target_path_heap = target_path; + } + } else { + // TODO: manage Error + break; + } } } } diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs index 4eb225f..ad29e99 100644 --- a/src/parsers/mod.rs +++ b/src/parsers/mod.rs @@ -19,7 +19,6 @@ pub fn apply_name_template(template: &str, filename: &str) -> String { } } -// FIXME: admit space between `{}` and name|upperCase name... pub fn apply_all_templates_to_string(mut input: String, replacement: &str) -> String { let get_template_names_regex = Regex::new(r"(\{\{[\s]*(name|upperCase name|lowerCase name|camelCase name|pascalCase name|snakeCase name|upperSnakeCase name|kebabCase name|lowerDotCase name)[\s]*\}\})").unwrap(); input = get_template_names_regex