asdf/completions/asdf.bash
Ville Skyttä 7c802c3fc9
feat(completions): bash improvements (#1329)
* fix(completions): bash `list` and `list-all` version completions
* feat(completions): add bash `help` one
* feat(completions): add bash `which` one
* feat(completions): add bash `plugin-list/-all`, `info` non-completions
2022-10-18 09:54:03 -04:00

90 lines
2.6 KiB
Bash

_asdf() {
local cur
cur=${COMP_WORDS[COMP_CWORD]}
local cmd
cmd=${COMP_WORDS[1]}
local prev
prev=${COMP_WORDS[COMP_CWORD - 1]}
local plugins
plugins=$(asdf plugin-list 2>/dev/null | tr '\n' ' ')
# We can safely ignore warning SC2207 since it warns that it will uses the
# shell's sloppy word splitting and globbing. The possible commands here are
# all single words, and most likely won't contain special chars the shell will
# expand.
COMPREPLY=()
case "$cmd" in
plugin-update)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$plugins --all" -- "$cur"))
;;
plugin-remove | current)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
;;
plugin-add)
local available_plugins
available_plugins=$(asdf plugin-list-all 2>/dev/null | awk '{ if ($2 !~ /^\*/) print $1}')
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$available_plugins" -- "$cur"))
;;
install | list | list-all | help)
if [[ "$plugins" == *"$prev"* ]]; then
local versions
versions=$(asdf list-all "$prev" 2>/dev/null)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$versions" -- "$cur"))
else
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
fi
;;
update)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "--head" -- "$cur"))
;;
uninstall | where | reshim)
if [[ "$plugins" == *"$prev"* ]]; then
local versions
versions=$(asdf list "$prev" 2>/dev/null)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$versions" -- "$cur"))
else
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
fi
;;
local | global | shell)
if [[ "$plugins" == *"$prev"* ]]; then
local versions
versions=$(asdf list "$prev" 2>/dev/null)
versions+=" system"
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$versions" -- "$cur"))
else
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
fi
;;
latest)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "--all" -- "$cur"))
;;
which)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -c -- "$cur"))
;;
plugin-list | plugin-list-all | info) ;;
*)
local cmds='current global help install list list-all local plugin-add plugin-list plugin-list-all plugin-remove plugin-update reshim shell uninstall update where which info'
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
;;
esac
return 0
}
complete -F _asdf asdf