From 0ed957b59e2724b0c6899cf43b8fe914fc8b82e6 Mon Sep 17 00:00:00 2001 From: Kenny Parnell Date: Mon, 29 Feb 2016 02:53:43 -0500 Subject: [PATCH] Add bash completion --- README.md | 2 ++ completions/asdf.bash | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 completions/asdf.bash diff --git a/README.md b/README.md index ec4ba221..bced1123 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,11 @@ Depending on your OS, run the following ```bash # For Ubuntu or other linux distros echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc +echo '. $HOME/.asdf/completions/asdf.bash >> ~/.bashrc' # OR for Max OSX echo '. $HOME/.asdf/asdf.sh' >> ~/.bash_profile +echo '. $HOME/.asdf/completions/asdf.bash >> ~/.bash_profile' ``` If you use zsh or any other shell, replace `.bashrc` with the config file for the respective shell. diff --git a/completions/asdf.bash b/completions/asdf.bash new file mode 100644 index 00000000..16b0d905 --- /dev/null +++ b/completions/asdf.bash @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +_asdf () { + local cur=${COMP_WORDS[COMP_CWORD]} + local cmd=${COMP_WORDS[1]} + local prev=${COMP_WORDS[COMP_CWORD-1]} + local plugins=$(asdf plugin-list | tr '\n' ' ') + + COMPREPLY=() + + case "$cmd" in + plugin-update) + COMPREPLY=($(compgen -W "$plugins --all" -- $cur)) + ;; + plugin-remove|which|list|list-all) + COMPREPLY=($(compgen -W "$plugins" -- $cur)) + ;; + install) + if [[ "$plugins" == *"$prev"* ]] ; then + local versions=$(asdf list-all $prev) + COMPREPLY=($(compgen -W "$versions" -- $cur)) + else + COMPREPLY=($(compgen -W "$plugins" -- $cur)) + fi + ;; + uninstall|where|reshim) + if [[ "$plugins" == *"$prev"* ]] ; then + local versions=$(asdf list $prev) + COMPREPLY=($(compgen -W "$versions" -- $cur)) + else + COMPREPLY=($(compgen -W "$plugins" -- $cur)) + fi + ;; + *) + local cmds='plugin-add plugin-list plugin-remove plugin-update install uninstall which where list list-all reshim' + COMPREPLY=($(compgen -W "$cmds" -- $cur)) + ;; + esac + + return 0 +} + +complete -F _asdf asdf