From c9495134690a1818fccfec0b8a5717dc31ea5633 Mon Sep 17 00:00:00 2001 From: Dominik Heidler Date: Tue, 15 May 2012 11:25:11 +0200 Subject: [PATCH] make nova bash-complete faster and more accurate - cache output of "nova bash-complete" - distinguish between flags and commands (based on already typed text) Change-Id: I85bd1c2198eef222540cf12063a3b233b0d6db12 --- AUTHORS | 1 + tools/nova.bash_completion | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/AUTHORS b/AUTHORS index b8b92ab5c..e0eca44ed 100644 --- a/AUTHORS +++ b/AUTHORS @@ -18,6 +18,7 @@ Dan Prince Dan Wendlandt Dave Walker Dean Troyer +Dominik Heidler Ed Leafe Edouard Thuleau Eldar Nugaev diff --git a/tools/nova.bash_completion b/tools/nova.bash_completion index 90d16c0cd..43390052b 100644 --- a/tools/nova.bash_completion +++ b/tools/nova.bash_completion @@ -1,15 +1,27 @@ +_nova_opts="" # lazy init +_nova_flags="" # lazy init +_nova_opts_exp="" # lazy init _nova() { - local cur prev opts - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" + local cur prev nbc cflags + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" - opts="$(nova bash_completion)" + if [ "x$_nova_opts" == "x" ] ; then + nbc="`nova bash-completion`" + _nova_opts="`echo "$nbc" | sed -e "s/--[a-z0-9_-]*//g" -e "s/\s\s*/ /g"`" + _nova_flags="`echo " $nbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/\s\s*/ /g"`" + _nova_opts_exp="`echo "$_nova_opts" | sed -e "s/\s/|/g"`" + fi - COMPLETION_CACHE=~/.novaclient/*/*-cache - opts+=" "$(cat $COMPLETION_CACHE 2> /dev/null | tr '\n' ' ') - - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + if [[ " ${COMP_WORDS[@]} " =~ " "($_nova_opts_exp)" " && "$prev" != "help" ]] ; then + COMPLETION_CACHE=~/.novaclient/*/*-cache + cflags="$_nova_flags "$(cat $COMPLETION_CACHE 2> /dev/null | tr '\n' ' ') + COMPREPLY=($(compgen -W "${cflags}" -- ${cur})) + else + COMPREPLY=($(compgen -W "${_nova_opts}" -- ${cur})) + fi + return 0 } complete -F _nova nova