From 6da85246c65fb65f0aa32e8feef4a7e0a909e510 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 13 Sep 2011 10:13:27 -0500 Subject: [PATCH] Proof-of-concept external completions --- completions/rbenv.bash | 31 ++++++++----------------------- libexec/rbenv-completions | 15 +++++++++++++++ libexec/rbenv-local | 7 +++++++ 3 files changed, 30 insertions(+), 23 deletions(-) create mode 100755 libexec/rbenv-completions diff --git a/completions/rbenv.bash b/completions/rbenv.bash index d548a859..e3276016 100644 --- a/completions/rbenv.bash +++ b/completions/rbenv.bash @@ -1,29 +1,14 @@ -_rbenv_commands() { - COMPREPLY=() - local cur="${COMP_WORDS[COMP_CWORD]}" - COMPREPLY=( $( compgen -W "$(rbenv commands)" -- $cur ) ) -} - -_rbenv_versions() { - COMPREPLY=() - local cur="${COMP_WORDS[COMP_CWORD]}" - local versions="$(echo system; rbenv versions --bare)" - COMPREPLY=( $( compgen -W "$versions" -- $cur ) ) -} - _rbenv() { COMPREPLY=() - local cur="${COMP_WORDS[COMP_CWORD]}" - local prev="${COMP_WORDS[COMP_CWORD-1]}" + local word="${COMP_WORDS[COMP_CWORD]}" - case "$prev" in - set-* | global | local | shell | prefix ) - _rbenv_versions - ;; - * ) - _rbenv_commands - ;; - esac + if [ "$COMP_CWORD" -eq 1 ]; then + COMPREPLY=( $(compgen -W "$(rbenv commands)" -- "$word") ) + else + local command="${COMP_WORDS[1]}" + local completions="$(rbenv completions "$command")" + COMPREPLY=( $(compgen -W "$completions" -- "$word") ) + fi } complete -F _rbenv rbenv diff --git a/libexec/rbenv-completions b/libexec/rbenv-completions new file mode 100755 index 00000000..e83690dc --- /dev/null +++ b/libexec/rbenv-completions @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -e +[ -n "$RBENV_DEBUG" ] && set -x + +COMMAND="$1" +if [ -z "$COMMAND" ]; then + echo "usage: rbenv completions COMMAND [arg1 arg2...]" >&2 + exit 1 +fi + +COMMAND_PATH="$(command -v "rbenv-$COMMAND")" +if grep -i "^# provide rbenv completions" "$COMMAND_PATH" >/dev/null; then + shift + exec "$COMMAND_PATH" --complete "$@" +fi diff --git a/libexec/rbenv-local b/libexec/rbenv-local index e8f1c96d..d261564c 100755 --- a/libexec/rbenv-local +++ b/libexec/rbenv-local @@ -2,6 +2,13 @@ set -e [ -n "$RBENV_DEBUG" ] && set -x +# Provide rbenv completions +if [ "$1" = "--complete" ]; then + shift + echo system + exec rbenv-versions --bare +fi + RBENV_VERSION="$1" RBENV_VERSION_FILE=".rbenv-version"