From 4de600713ad3c3949463fbe63d7cd882b1345180 Mon Sep 17 00:00:00 2001 From: Davide Riccardo Caliendo Date: Mon, 25 Nov 2013 21:24:35 +0100 Subject: [PATCH] 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 --- extras/completion/ctt | 77 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/extras/completion/ctt b/extras/completion/ctt index e8745af..105d3fd 100644 --- a/extras/completion/ctt +++ b/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