From f90adca156cb9ab4640164292ec7293bfc59611a Mon Sep 17 00:00:00 2001 From: Wessel Tip Date: Mon, 10 Nov 2025 11:42:50 +0100 Subject: [PATCH] feat: Add support for directory templates --- fish/completions/env.fish | 24 ++++++++++++++ fish/functions/env.fish | 68 +++++++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/fish/completions/env.fish b/fish/completions/env.fish index 1bee2ad..9cab375 100644 --- a/fish/completions/env.fish +++ b/fish/completions/env.fish @@ -49,6 +49,7 @@ end function __env_complete_templates set -l template_dir "$HOME/.config/fish/environments/templates" if test -d "$template_dir" + # List file templates (*.fish) for template_file in "$template_dir"/*.fish if test -f "$template_file" set template_name (basename "$template_file" .fish) @@ -61,6 +62,29 @@ function __env_complete_templates end end end + + # List directory templates + for template_path in "$template_dir"/* + if test -d "$template_path" + set template_name (basename "$template_path") + set description "Multi-file template" + + # Try to get description from readme.norg or activate.fish + if test -f "$template_path/readme.norg" + set first_line (head -n 1 "$template_path/readme.norg" | sed 's/^# *//') + if test -n "$first_line" + set description "$first_line" + end + else if test -f "$template_path/activate.fish" + set first_comment (head -n 5 "$template_path/activate.fish" | grep -E "^#[^!]" | head -n 1 | sed 's/^# *//') + if test -n "$first_comment" + set description "$first_comment" + end + end + + echo -e "$template_name\t$description" + end + end end end diff --git a/fish/functions/env.fish b/fish/functions/env.fish index 358f56c..968b04f 100644 --- a/fish/functions/env.fish +++ b/fish/functions/env.fish @@ -90,13 +90,23 @@ function __env_create -d "Create a new environment" set -l env_dir "$__ENV_CONFIGS_DIR/$env_name" set -l template_file "$__ENV_TEMPLATES_DIR/$template_type.fish" + set -l template_dir "$__ENV_TEMPLATES_DIR/$template_type" if test -d "$env_dir" echo (set_color yellow)"'$env_name': environment already exists"(set_color normal) return 1 end - if not test -f "$template_file" + # Check if template exists as file or directory + set -l template_path "" + set -l is_dir_template 0 + + if test -f "$template_file" + set template_path "$template_file" + else if test -d "$template_dir" + set template_path "$template_dir" + set is_dir_template 1 + else echo (set_color red)"'$template_type': template not found"(set_color normal) echo "Available templates:" __env_list_templates @@ -106,9 +116,27 @@ function __env_create -d "Create a new environment" # Create environment directory mkdir -p "$env_dir" - # Copy template and customize - sed "s/{{ENV_NAME}}/$env_name/g" "$template_file" >"$env_dir/activate.fish" - chmod +x "$env_dir/activate.fish" + if test $is_dir_template -eq 1 + # Directory template: copy all files and substitute {{ENV_NAME}} + for file in "$template_path"/* + set file_name (basename "$file") + if test -f "$file" + # Substitute {{ENV_NAME}} in file content + sed "s/{{ENV_NAME}}/$env_name/g" "$file" >"$env_dir/$file_name" + # Preserve executable permissions + test -x "$file" && chmod +x "$env_dir/$file_name" + else if test -d "$file" + # Copy subdirectories recursively + cp -r "$file" "$env_dir/$file_name" + # Substitute {{ENV_NAME}} in all files within subdirectories + find "$env_dir/$file_name" -type f -exec sed -i "s/{{ENV_NAME}}/$env_name/g" {} \; + end + end + else + # File template: copy and customize activate.fish + sed "s/{{ENV_NAME}}/$env_name/g" "$template_path" >"$env_dir/activate.fish" + chmod +x "$env_dir/activate.fish" + end # Create environment config file echo "# Environment: $env_name" >"$env_dir/config.toml" @@ -409,7 +437,7 @@ function __env_export -d "Export environment to a self-contained directory" set -l fish_dir "$target_dir/.fish" set -l target_activate "$fish_dir/activate.fish" - set -l target_readme "$fish_dir/README.md" + set -l target_readme "$fish_dir/readme.norg" # Create .fish directory if not test -d "$fish_dir" @@ -439,7 +467,7 @@ function __env_export -d "Export environment to a self-contained directory" chmod +x "$target_activate" # Create a README from template - set -l readme_template "$__ENV_TEMPLATES_DIR/README.md" + set -l readme_template "$__ENV_TEMPLATES_DIR/readme.norg" if test -f "$readme_template" sed "s/{{ENV_NAME}}/$env_name/g" "$readme_template" >"$target_readme" else @@ -466,6 +494,7 @@ function __env_list_templates -d "List available templates with descriptions" return 0 end + # List file templates (*.fish) for template_file in "$template_dir"/*.fish if test -f "$template_file" set template_name (basename "$template_file" .fish) @@ -482,4 +511,31 @@ function __env_list_templates -d "List available templates with descriptions" printf " %-12s %s\n" "$template_name" "$description" end end + + # List directory templates + for template_path in "$template_dir"/* + if test -d "$template_path" + set template_name (basename "$template_path") + + # Look for readme.norg or activate.fish for description + set description "Multi-file template" + + if test -f "$template_path/readme.norg" + # Extract first line from README + set first_line (head -n 1 "$template_path/readme.norg" | sed 's/^# *//') + if test -n "$first_line" + set description "$first_line" + end + else if test -f "$template_path/activate.fish" + # Extract description from activate.fish + set first_comment_line (head -n 5 "$template_path/activate.fish" | grep -E "^#[^!]" | head -n 1 | sed 's/^# *//') + if test -n "$first_comment_line" + set description "$first_comment_line" + end + end + + # Format with proper alignment + printf " %-12s %s\n" "$template_name" "$description" + end + end end