120 lines
3.6 KiB
Bash
Executable File
120 lines
3.6 KiB
Bash
Executable File
#!/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_commands
|
|
./task help 2>/dev/null | grep -A100 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$'
|
|
end
|
|
|
|
# First argument position: offer the list of task commands.
|
|
complete -c task -f -n '__fish_use_subcommand' -a '(__task_commands)'
|
|
complete -c t -f -n '__fish_use_subcommand' -a '(__task_commands)'
|
|
|
|
# Once a command has already been typed, fall back to file completion.
|
|
complete -c task -f -n 'not __fish_use_subcommand' -a '(__fish_complete_path)'
|
|
complete -c t -f -n 'not __fish_use_subcommand' -a '(__fish_complete_path)'
|
|
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 '^$'))
|
|
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]}"
|
|
|
|
# compgen -W wants a single space-separated string, so tr is fine here.
|
|
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
|
|
} |