From e0910f5c7c6f1ba4cd49fca04c1aeaf7c8ca56ab Mon Sep 17 00:00:00 2001 From: Tom Grennan Date: Fri, 30 Nov 2007 15:36:37 -0800 Subject: cache completion list(s) between first and second tab/alt-? --- etc/bash_completion.d/10vyatta-op | 139 ++++++++++++++++++++------------------ 1 file changed, 75 insertions(+), 64 deletions(-) (limited to 'etc/bash_completion.d/10vyatta-op') diff --git a/etc/bash_completion.d/10vyatta-op b/etc/bash_completion.d/10vyatta-op index dacf57f..0b5055f 100644 --- a/etc/bash_completion.d/10vyatta-op +++ b/etc/bash_completion.d/10vyatta-op @@ -22,7 +22,8 @@ # # **** End License **** -declare -r _vyatta_default_pager="less\ +test -z "$_vyatta_default_pager" && \ + declare -r _vyatta_default_pager="less\ --QUIT-AT-EOF\ --quit-if-one-screen\ --LONG-PROMPT\ @@ -40,6 +41,8 @@ test ! -d "$vyatta_op_templates" && \ return 0 declare _vyatta_op_last_comp +declare _vyatta_op_node_path +declare -a _vyatta_op_noncompletions _vyatta_op_completions # $1: label # #2...: strings @@ -107,19 +110,17 @@ _vyatta_op_print_help () } # $1: $cur -# $2: $node_path -# $3...: possible completions +# $2...: possible completions _vyatta_op_help () { local cur=$1; shift - local node_path=$1; shift local ndef node_tag_help node_run help last_help - ndef=$node_path/node.tag/node.def + ndef=${_vyatta_op_node_path}/node.tag/node.def [ -f $ndef ] && \ node_tag_help=$( _vyatta_op_get_node_def_field $ndef help ) - ndef=$node_path/node.def + ndef=${_vyatta_op_node_path}/node.def [ -f $ndef ] && \ node_run=$( _vyatta_op_get_node_def_field $ndef run ) @@ -137,7 +138,7 @@ _vyatta_op_help () fi _vyatta_op_print_help '*' "$help" elif [[ -z "$cur" || $comp == ${cur}* ]] ; then - ndef=$node_path/$comp/node.def + ndef=${_vyatta_op_node_path}/$comp/node.def if [ -f $ndef ] ; then help=$( _vyatta_op_get_node_def_field $ndef help ) else @@ -153,25 +154,76 @@ _vyatta_op_help () done } -# $1: cur -# $2...: comps -_vyatta_op_compreply () +_vyatta_op_set_node_path () { - local cur=$1; shift - local -a justhelp comps scomps + _vyatta_op_node_path=$vyatta_op_templates + for (( i=0 ; i or - for allow ; do - if [[ "$allow" == \<*\> ]] ; then - justhelp+=( $allow ) + _vyatta_op_noncompletions=( ) + completions=( ) + for (( i=0 ; i<${#allowed[@]} ; i++ )) ; do + if [[ "${allowed[i]}" == \<*\> ]] ; then + _vyatta_op_noncompletions+=( ${allowed[i]} ) else - comps+=( $allow ) + completions+=( ${allowed[i]} ) fi done - scomps=($( printf "%s\n" ${comps[@]} | sort -u )) + _vyatta_op_completions=($( printf "%s\n" ${completions[@]} | sort -u )) +} + +_vyatta_op_expand () +{ + local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; ) + shopt -s extglob nullglob + local cur="" + + if (( ${#COMP_WORDS[@]} > 0 )); then + cur=${COMP_WORDS[COMP_CWORD]} + else + (( COMP_CWORD = ${#COMP_WORDS[@]} )) + fi + + if [ "${COMP_WORDS[*]}" != "$_vyatta_op_last_comp" ] ; then + if ! _vyatta_op_set_node_path ; then + echo -e \\a + COMPREPLY=( "" " " ) + eval "$restore_shopts" + return 1 + fi + _vyatta_op_set_completions + fi - COMPREPLY=($( compgen -W "${scomps[*]}" -- $cur )) + COMPREPLY=($( compgen -W "${_vyatta_op_completions[*]}" -- $cur )) # if the last command line arg is empty and we have # an empty completion option (meaning wild card), @@ -189,58 +241,17 @@ _vyatta_op_compreply () fi if [ "${COMP_WORDS[*]}" == "$_vyatta_op_last_comp" ] ; then - _vyatta_op_help "$cur" "$node_path" ${justhelp[@]} ${scomps[@]} | \ - eval ${VYATTA_PAGER:-cat} + _vyatta_op_help "$cur" \ + ${_vyatta_op_noncompletions[@]} \ + ${_vyatta_op_completions[@]} \ + | eval ${VYATTA_PAGER:-cat} COMPREPLY=( "" " " ) _vyatta_op_last_comp="" else _vyatta_op_last_comp="${COMP_WORDS[*]}" fi -} - -_vyatta_op_expand () -{ - local cur="" - if (( ${#COMP_WORDS[@]} > 0 )); then - cur=${COMP_WORDS[COMP_CWORD]} - else - (( COMP_CWORD = ${#COMP_WORDS[@]} )) - fi - local node_path=$vyatta_op_templates - local -a allowed - local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; ) - shopt -s extglob nullglob - for (( i=0 ; i