diff options
author | John Southworth <john.southworth@vyatta.com> | 2011-10-03 13:18:25 -0500 |
---|---|---|
committer | John Southworth <john.southworth@vyatta.com> | 2011-10-03 13:18:25 -0500 |
commit | 1612e4510ad21d14589da469bd937c871a182c3e (patch) | |
tree | a53c390e9e1b50332fa76946b07df1f1f75b33e1 /etc | |
parent | a852c603969a100b7d086dc2ad2df7a391971a1e (diff) | |
download | vyatta-op-1612e4510ad21d14589da469bd937c871a182c3e.tar.gz vyatta-op-1612e4510ad21d14589da469bd937c871a182c3e.zip |
Make completion properly handle non-last-word completions
Diffstat (limited to 'etc')
-rw-r--r-- | etc/bash_completion.d/vyatta-op | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/etc/bash_completion.d/vyatta-op b/etc/bash_completion.d/vyatta-op index 833d4a3..d76874a 100644 --- a/etc/bash_completion.d/vyatta-op +++ b/etc/bash_completion.d/vyatta-op @@ -302,6 +302,9 @@ _vyatta_op_invalid_completion () _vyatta_op_expand () { + # We need nospace here and we have to append our own spaces + compopt -o nospace + local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; ) shopt -s extglob nullglob local cur="" @@ -315,13 +318,17 @@ _vyatta_op_expand () fi if _vyatta_pipe_completion "${COMP_WORDS[@]}"; then - if [ "${COMP_WORDS[*]}" == "$_vyatta_op_last_comp" ] ; then + if [ "${COMP_WORDS[*]}" == "$_vyatta_op_last_comp" ] || + [ ${#_vyatta_pipe_completions[@]} -eq 0 ]; then _vyatta_do_pipe_help COMPREPLY=( "" " " ) _vyatta_op_last_comp=${_vyatta_op_last_comp_init} else COMPREPLY=( "${_vyatta_pipe_completions[@]}" ) _vyatta_op_last_comp="${COMP_WORDS[*]}" + if [ ${#COMPREPLY[@]} -eq 1 ]; then + COMPREPLY=( "${COMPREPLY[0]} " ) + fi fi eval "$restore_shopts" return @@ -339,7 +346,7 @@ _vyatta_op_expand () return 1 fi - if [ "${COMP_WORDS[*]}" != "$_vyatta_op_last_comp" ] ; then + if [ "${COMP_WORDS[*]:0:$[$COMP_CWORD+1]}" != "$_vyatta_op_last_comp" ] ; then _vyatta_set_comptype case $_vyatta_comptype in 'imagefiles') @@ -361,11 +368,12 @@ _vyatta_op_expand () # if the last command line arg is empty and we have # an empty completion option (meaning wild card), # append a blank(s) to the completion array to force ambiguity - if [ -z "$cur" ] ; then + if [ -z "$cur" ] || + [[ "${COMPREPLY[0]}" =~ "$cur" ]]; then for comp ; do if [ -z "$comp" ] ; then if [ ${#COMPREPLY[@]} -eq 0 ] ; then - COMPREPLYu=( " " "" ) + COMPREPLY=( " " "" ) elif _vyatta_op_comprely_needs_ambiguity ; then COMPREPLY+=( " " ) fi @@ -374,11 +382,21 @@ _vyatta_op_expand () fi # Set this environment to enable and disable debugging on the fly if [[ $DBG_OP_COMPS -eq 1 ]]; then - echo "Current: $cur" - echo "Number of comps: ${#_vyatta_op_completions[@]}" - echo "Number of non-comps: ${#_vyatta_op_noncompletions[@]}" - echo "Last comp: $_vyatta_op_last_comp" - echo -e "Current comp: ${COMP_WORDS[*]}\n" + echo -e "\nCurrent: '$cur'" + echo "Number of comps: ${#_vyatta_op_completions[*]}" + echo "Number of non-comps: ${#_vyatta_op_noncompletions[*]}" + echo "_vyatta_op_completions: '${_vyatta_op_completions[*]}'" + echo "COMPREPLY: '${COMPREPLY[@]}'" + echo "CWORD: $COMP_CWORD" + echo "Last comp: '$_vyatta_op_last_comp'" + echo -e "Current comp: '${COMP_WORDS[*]:0:$[$COMP_CWORD+1]}'\n" + fi + + # This is non obvious... + # To have completion continue to work when working with words that aren't the last word, + # we have to set nospace at the beginning of this script and then append the spaces here. + if [ ${#COMPREPLY[@]} -eq 1 ];then + COMPREPLY=( "${COMPREPLY[0]} " ) fi # if there are no completions then handle invalid commands if [ ${#_vyatta_op_noncompletions[@]} -eq 0 ] && @@ -390,9 +408,9 @@ _vyatta_op_expand () # Stop completions from getting stuck elif [ ${#_vyatta_op_completions[@]} -eq 1 ] && [[ "${COMPREPLY[0]}" =~ "$cur" ]]; then - _vyatta_op_last_comp="${COMP_WORDS[*]}" + _vyatta_op_last_comp=${_vyatta_op_last_comp_init} # if there are no completions then always show the non-comps - elif [ "${COMP_WORDS[*]}" == "$_vyatta_op_last_comp" ] || + elif [ "${COMP_WORDS[*]:0:$[$COMP_CWORD+1]}" == "$_vyatta_op_last_comp" ] || [ ${#_vyatta_op_completions[@]} -eq 0 ]; then _vyatta_op_help "$cur" \ ${_vyatta_op_noncompletions[@]} \ @@ -401,7 +419,7 @@ _vyatta_op_expand () COMPREPLY=( "" " " ) _vyatta_op_last_comp=${_vyatta_op_last_comp_init} else - _vyatta_op_last_comp="${COMP_WORDS[*]}" + _vyatta_op_last_comp="${COMP_WORDS[*]:0:$[$COMP_CWORD+1]}" fi eval "$restore_shopts" |