feat: added folder selection loop to make it easier to use
This commit is contained in:
@@ -20,6 +20,20 @@ pub fn get_template_options(base_paths: Vec<String>) -> Vec<String> {
|
||||
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> {
|
||||
// FIXME: this panics when the directory does not exist
|
||||
let paths = fs::read_dir(dir).unwrap();
|
||||
|
||||
41
src/main.rs
41
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user