|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Bash/Zsh tab completion for phpvm |
| 3 | +# Source this file or place it in your completions directory |
| 4 | + |
| 5 | +_phpvm_completions() { |
| 6 | + local cur prev commands |
| 7 | + cur="${COMP_WORDS[COMP_CWORD]}" |
| 8 | + prev="${COMP_WORDS[COMP_CWORD - 1]}" |
| 9 | + |
| 10 | + commands="install uninstall use current which list ls ls-remote alias unalias exec run resolve cache deactivate system auto info version help unload" |
| 11 | + |
| 12 | + case "$prev" in |
| 13 | + use | uninstall | which | exec | run | resolve) |
| 14 | + # Complete with installed versions + aliases |
| 15 | + local versions="" |
| 16 | + local phpvm_dir="${PHPVM_DIR:-$HOME/.phpvm}" |
| 17 | + |
| 18 | + # Installed versions from active_version tracking |
| 19 | + if [ -d "$phpvm_dir/alias" ]; then |
| 20 | + local alias_file |
| 21 | + for alias_file in "$phpvm_dir/alias"/*; do |
| 22 | + [ -f "$alias_file" ] && versions="$versions ${alias_file##*/}" |
| 23 | + done |
| 24 | + fi |
| 25 | + |
| 26 | + # Try to get installed versions from package manager |
| 27 | + if command -v brew > /dev/null 2>&1; then |
| 28 | + local formula |
| 29 | + while IFS= read -r formula; do |
| 30 | + local ver="${formula#php@}" |
| 31 | + [ "$formula" = "php" ] && continue |
| 32 | + versions="$versions $ver" |
| 33 | + done < <(brew list --formula 2> /dev/null | command grep -E '^php(@[0-9]+\.[0-9]+)?$') |
| 34 | + fi |
| 35 | + |
| 36 | + versions="$versions system" |
| 37 | + # shellcheck disable=SC2207 # Intentional: word splitting for COMPREPLY |
| 38 | + COMPREPLY=($(compgen -W "$versions" -- "$cur")) |
| 39 | + return |
| 40 | + ;; |
| 41 | + alias) |
| 42 | + # Complete with alias subcommands or existing aliases |
| 43 | + if [ "$COMP_CWORD" -eq 2 ]; then |
| 44 | + # shellcheck disable=SC2207 |
| 45 | + COMPREPLY=($(compgen -W "default" -- "$cur")) |
| 46 | + fi |
| 47 | + return |
| 48 | + ;; |
| 49 | + unalias) |
| 50 | + # Complete with existing alias names |
| 51 | + local aliases="" |
| 52 | + local phpvm_dir="${PHPVM_DIR:-$HOME/.phpvm}" |
| 53 | + if [ -d "$phpvm_dir/alias" ]; then |
| 54 | + local alias_file |
| 55 | + for alias_file in "$phpvm_dir/alias"/*; do |
| 56 | + [ -f "$alias_file" ] && aliases="$aliases ${alias_file##*/}" |
| 57 | + done |
| 58 | + fi |
| 59 | + # shellcheck disable=SC2207 |
| 60 | + COMPREPLY=($(compgen -W "$aliases" -- "$cur")) |
| 61 | + return |
| 62 | + ;; |
| 63 | + cache) |
| 64 | + # shellcheck disable=SC2207 |
| 65 | + COMPREPLY=($(compgen -W "clear" -- "$cur")) |
| 66 | + return |
| 67 | + ;; |
| 68 | + phpvm) |
| 69 | + # shellcheck disable=SC2207 |
| 70 | + COMPREPLY=($(compgen -W "$commands" -- "$cur")) |
| 71 | + return |
| 72 | + ;; |
| 73 | + esac |
| 74 | + |
| 75 | + # Default: complete commands |
| 76 | + # shellcheck disable=SC2207 |
| 77 | + COMPREPLY=($(compgen -W "$commands" -- "$cur")) |
| 78 | +} |
| 79 | + |
| 80 | +complete -F _phpvm_completions phpvm |
0 commit comments