feat: added folder selection loop to make it easier to use

This commit is contained in:
2025-02-23 18:32:12 +01:00
parent 727d8d770b
commit 6c430e2869
3 changed files with 51 additions and 5 deletions

View File

@@ -20,6 +20,20 @@ pub fn get_template_options(base_paths: Vec<String>) -> Vec<String> {
type_options type_options
} }
pub fn get_sub_dirs_paths(dir: &str) -> Vec<String> {
let paths = fs::read_dir(dir).unwrap();
let mut valid_dirs: Vec<String> = 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<String> { pub fn get_valid_dirs_paths(dir: &str) -> Vec<String> {
// FIXME: this panics when the directory does not exist // FIXME: this panics when the directory does not exist
let paths = fs::read_dir(dir).unwrap(); let paths = fs::read_dir(dir).unwrap();

View File

@@ -30,11 +30,44 @@ fn main() {
let target_name_result = Text::new("Insert the desired name:").prompt(); let target_name_result = Text::new("Insert the desired name:").prompt();
if let Ok(target_name) = target_name_result { 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 mut target_path_heap = "./".to_string();
let target_path_result = Text::new("Insert the target path:").prompt(); const CONFIRM_OPTION: &str = "-- Current --";
const CANCEL_OPTION: &str = "-- Cancel --";
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 { if let Ok(target_path) = target_path_result {
create_template(template_path, target_name, target_path).unwrap(); // 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;
}
} }
} }
} }

View File

@@ -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 { 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(); 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 input = get_template_names_regex