#!/usr/bin/env bash setup-completion() { local shell_type="$1" # Set default shell type if not provided if [[ -z "$shell_type" ]]; then shell_type="bash" # Default to bash fi # 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]" return 1 ;; esac # Output the actual completion script output_completion_script "$shell_type" # Add instructions as comments at the end echo "" echo "# ============================================" echo "# Taskfile completion setup instructions" echo "# ============================================" 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" elif [[ "$shell_type" == "zsh" ]]; then echo "# task setup-completion zsh >> ~/.zshrc" echo "# source ~/.zshrc" else echo "# task setup-completion bash >> ~/.bashrc" echo "# source ~/.bashrc" fi echo "# ============================================" } output_completion_script() { local shell_type="$1" # Default to bash if no shell type provided if [[ -z "$shell_type" ]]; then shell_type="bash" fi if [[ "$shell_type" == "fish" ]]; then # Fish completion script cat << 'EOF' # Taskfile completion for fish function _task_completions # Get the current word being typed set cur (commandline -p) # Get the previous word set prev (commandline -oprev) # Get the available commands from `task help` set commands (./task help | grep -A100 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$' | tr '\n' ' ') if test (count (commandline -opc)) -eq 1 # Provide completions for the command part (second word) complete -c task -f -n "__fish_seen_subcommand_from $prev" -a "$commands" else if test (count (commandline -opc)) -eq 2 # Provide file path completions for the third parameter complete -c task -f -n "__fish_seen_subcommand_from $prev" -a (ls) end end EOF elif [[ "$shell_type" == "zsh" ]]; then # ZSH completion script cat << 'EOF' # Taskfile completion for zsh _task_completions() { local 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 '^$' | tr ' ' ' ')) IFS=$si if [[ ${#reply} -gt 0 ]]; then _describe 'values' reply else _default fi } if [[ "${zsh_eval_context[-1]}" == "loadautofunc" ]]; then _task_completions "$@" else compdef _task_completions task compdef _task_completions t fi EOF else # Bash completion script cat << 'EOF' # Taskfile completion for bash _task_completions() { local cur prev commands cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" commands=$(./task help | grep -A999 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$' | tr '\n' ' ') if [[ ${COMP_CWORD} == 1 ]]; then COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) elif [[ ${COMP_CWORD} == 2 ]]; then COMPREPLY=( $(compgen -f -- "${cur}") ) else COMPREPLY=() fi } complete -F _task_completions task complete -F _task_completions t EOF fi }