improved bash completion script

* fixed a bug with listprojects command name
* only long option names are expanded
* now various options depends on the given command, and are not
  expanded by default (start with -* and tab to list them)
* project names are only expanded when a suitable command is given
  before, and they are not expanded more than one time
This commit is contained in:
Davide Riccardo Caliendo 2013-11-25 21:24:35 +01:00 committed by Nico Schottelius
parent 464d90948a
commit 4de600713a

View file

@ -1,20 +1,77 @@
function inArray() {
declare -a arr1=("${!1}")
declare -a arr2=("${!2}")
for i in ${arr1[@]}; do
for j in ${arr2[@]}; do
if [[ $i == $j ]] ; then
echo $i
break
fi
done
done
echo ""
}
_ctt() _ctt()
{ {
local cur prev opts local cur prev opts
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
comms="listproject track report"
opts="-h --help -d --debug -v --verbose" # ctt available commands
cmdopts="--sd --start --ed --end -a --all -e --regexp -i --ignorecase -f --format" cmds="listprojects track report"
case "${prev}" in
track|report) # current command, if the user supplied any
local projects=$(for p in ~/.ctt/*; do basename "$p"; done ) curr_cmd=$(inArray COMP_WORDS[@] cmds[@])
COMPREPLY=( $(compgen -W "${projects} ${opts} ${cmdopts}" -- ${cur}) )
# various options,
# generics and command specific
declare -A cmdopts
opts="--help --debug --verbose "
cmdopts[_shared]="--start --end "
cmdopts[report]="--all --regexp --ignorecase --format "
cmdopts[report]+=${cmdopts[_shared]}
cmdopts[track]="--no-comment "
cmdopts[track]+=${cmdopts[_shared]}
cmdopts[listprojects]=""
# expand options based on given command
# or only generic ones
if [[ "$cur" == -* ]]; then
if [ ! -z $curr_cmd ]; then
COMPREPLY=( $(compgen -W "${opts} ${cmdopts[${curr_cmd}]}" -- ${cur}) )
else
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
fi
return 0 return 0
fi
# expand project names if a
# suitable command is specified
# also do not expand more than one project if one already expanded
if [[ ! -z $curr_cmd ]]; then
projects=$(for p in ~/.ctt/*; do basename "$p"; done )
curr_proj=$(inArray COMP_WORDS[@] projects[@])
if [ ! -z $curr_proj ]; then
return 0
fi
case "${curr_cmd}" in
track)
COMPREPLY=( $(compgen -W "${projects}" -- ${cur}) )
;;
report)
COMPREPLY=( $(compgen -W "${projects}" -- ${cur}) )
;;
listprojects)
COMPREPLY=( $(compgen -W "" -- ${cur}) )
;; ;;
esac esac
COMPREPLY=( $(compgen -W "${comms} ${opts}" -- ${cur}) ) else
COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
fi
return 0 return 0
} }
complete -F _ctt ctt complete -F _ctt ctt