From ad33cc76817ca05e81b9453a5e8ee0cee960798f Mon Sep 17 00:00:00 2001 From: Janik von Rotz Date: Mon, 20 Jul 2026 16:22:05 +0200 Subject: [PATCH] feat: refactor setup completion script --- bin/setup-completion | 83 ++++++++++++++++++++++++++++---------------- task | 1 - 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/bin/setup-completion b/bin/setup-completion index 78bc902..81a10b5 100755 --- a/bin/setup-completion +++ b/bin/setup-completion @@ -3,19 +3,29 @@ setup-completion() { local shell_type="$1" - # Set default shell type if not provided - if [[ -z "$shell_type" ]]; then - shell_type="bash" # Default to bash + # "help" is a pseudo-shell-type: print setup instructions for every + # supported shell and skip emitting any completion script at all. + if [[ "$shell_type" == "help" ]]; then + echo "" + show-completion-instructions "bash" + show-completion-instructions "zsh" + show-completion-instructions "fish" + return 0 fi + # Remember whether the caller actually passed a shell type, + # since we only want to print setup instructions when they didn't. + local shell_type_given=1 + [[ -z "$shell_type" ]] && shell_type_given=0 + # Validate shell type case "$shell_type" in bash|zsh|fish) # Valid shell type ;; *) - echo "Error: Unsupported shell type '$shell_type'. Use 'bash', 'zsh', or 'fish'." - echo "Usage: task setup-completion [bash|zsh|fish]" + echo "Error: Unsupported shell type '$shell_type'. Use 'bash', 'zsh', 'fish', or 'help'." + echo "Usage: task setup-completion [bash|zsh|fish|help]" return 1 ;; esac @@ -23,24 +33,35 @@ setup-completion() { # Output the actual completion script output_completion_script "$shell_type" - # Add instructions as comments at the end + # Only show setup instructions when no shell type was explicitly given. + # When the caller specifies a shell type (e.g. for piping/redirecting + # straight into a rc file), keep the output clean of comment noise. + if [[ "$shell_type_given" == "0" ]]; then + echo "" + show-completion-instructions "$shell_type" + fi +} + +show-completion-instructions() { + local shell_type="$1" + + echo "Taskfile completion setup instructions ($shell_type):" echo "" - echo "# ============================================" - echo "# Taskfile completion setup instructions" - echo "# ============================================" - echo "# To enable completion, add this script to your shell configuration:" + echo "To enable completion, add this script to your shell configuration:" if [[ "$shell_type" == "fish" ]]; then - echo "# task setup-completion fish >> ~/.config/fish/config.fish" - echo "# source ~/.config/fish/config.fish" + echo " task setup-completion fish >> ~/.config/fish/config.fish" + echo " source ~/.config/fish/config.fish" elif [[ "$shell_type" == "zsh" ]]; then - echo "# task setup-completion zsh >> ~/.zshrc" - echo "# source ~/.zshrc" + echo " task setup-completion zsh >> ~/.zshrc" + echo " source ~/.zshrc" else - echo "# task setup-completion bash >> ~/.bashrc" - echo "# source ~/.bashrc" + echo " task setup-completion bash >> ~/.bashrc" + echo " source ~/.bashrc" fi - echo "# ============================================" + echo "" + echo "---" + echo "" } output_completion_script() { @@ -73,24 +94,26 @@ EOF cat << 'EOF' # Taskfile completion for zsh _task_completions() { - local reply + local -a reply local si=$IFS - IFS=$' -' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" ./task help | grep -A999 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$')) - IFS=$si - if [[ ${#reply} -gt 0 ]]; then - _describe 'values' reply + + # $CURRENT is zsh's 1-indexed equivalent of bash's COMP_CWORD. + # CURRENT == 2 means we're completing the first argument after `task`/`t`. + if [[ $CURRENT -eq 2 ]]; then + IFS=$'\n' reply=($(./task help | grep -A999 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$')) + IFS=$si + if (( ${#reply} > 0 )); then + _describe 'task' reply + else + _default + fi else - _default + _files fi } -if [[ "${zsh_eval_context[-1]}" == "loadautofunc" ]]; then - _task_completions "$@" -else - compdef _task_completions task - compdef _task_completions t -fi +compdef _task_completions task +compdef _task_completions t EOF else # Bash completion script diff --git a/task b/task index c04230e..0001985 100755 --- a/task +++ b/task @@ -7,7 +7,6 @@ help-table() { local max_cmd=7 max_opt=6 max_desc=11 rows+=("Command|Option|Description") - rows+=("all||Run all tasks.") rows+=("build-11ty||Build the 11ty website.") rows+=("build-image|[output]|Build Docker image with buildx.") rows+=("dev-11ty||Run 11ty server.")