Skip to content

Commit

Permalink
improved bash completion script
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
drc0 authored and Nico Schottelius committed Nov 27, 2013
1 parent 464d909 commit 4de6007
Showing 1 changed file with 67 additions and 10 deletions.
77 changes: 67 additions & 10 deletions extras/completion/ctt
@@ -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()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
comms="listproject track report"
opts="-h --help -d --debug -v --verbose"
cmdopts="--sd --start --ed --end -a --all -e --regexp -i --ignorecase -f --format"
case "${prev}" in
track|report)
local projects=$(for p in ~/.ctt/*; do basename "$p"; done )
COMPREPLY=( $(compgen -W "${projects} ${opts} ${cmdopts}" -- ${cur}) )

# ctt available commands
cmds="listprojects track report"

# current command, if the user supplied any
curr_cmd=$(inArray COMP_WORDS[@] cmds[@])

# 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
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
;;
esac
COMPREPLY=( $(compgen -W "${comms} ${opts}" -- ${cur}) )
fi
case "${curr_cmd}" in
track)
COMPREPLY=( $(compgen -W "${projects}" -- ${cur}) )
;;
report)
COMPREPLY=( $(compgen -W "${projects}" -- ${cur}) )
;;
listprojects)
COMPREPLY=( $(compgen -W "" -- ${cur}) )
;;
esac
else
COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
fi

return 0
}
complete -F _ctt ctt

0 comments on commit 4de6007

Please sign in to comment.