diff --git a/misc/bartibCompletion.sh b/misc/bartibCompletion.sh old mode 100644 new mode 100755 index ed3f2f8..472751e --- a/misc/bartibCompletion.sh +++ b/misc/bartibCompletion.sh @@ -1,18 +1,59 @@ #!/bin/bash -_bartib_completions() +function _bartib_completions() { - local CWORD=${COMP_WORDS[COMP_CWORD]} - ALL_PROJECTS=`bartib projects` + word="${COMP_WORDS[$COMP_CWORD]}" + prev="${COMP_WORDS[$COMP_CWORD - 1]}" - local IFS=$'\n' - CANDIATE_PROJECTS=($(compgen -W "${ALL_PROJECTS[*]}" -- "$CWORD")) + case "${prev}" in + bartib) + # COMPLETE COMMAND + words="start stop current continue edit report list help cancel check last projects sanity change" + COMPREPLY=($(compgen -W "$words" -- "$word")) + ;; + -p|--project) + # COMPLETE PROJECT NAME + local IFS=$'\n' + ALL_PROJECTS=$(bartib projects) + CANDIATE_PROJECTS=($(compgen -W "${ALL_PROJECTS[*]}" -- "$word")) - if [ ${#CANDIATE_PROJECTS[*]} -eq 0 ]; then - COMPREPLY=() - else - COMPREPLY=($(printf "\"%s\"\n" "${CANDIATE_PROJECTS[@]}")) - fi + if [ ${#CANDIATE_PROJECTS[*]} -eq 0 ]; then + COMPREPLY=() + else + COMPREPLY=($(printf "\"%s\"\n" "${CANDIATE_PROJECTS[@]}")) + fi + ;; + -d) + if [ -n "$BARTIB_FILE" ] + then + # COMPLETE TASK + local IFS=$'\n' + # Get selected project and remove surrounding quotes + local project="${COMP_WORDS[$COMP_CWORD - 2]}" + project=$(echo "$project" | tr -d '"') + # Get tasks for that project + tasks=$(grep "| $project |" /opt/shared/bartib-hist | cut -d'|' -f 3 | sort | uniq | sed 's/ //' | sed -e 's/\(.*\)/"\1"/') + + candidate_tasks=($(compgen -W "${tasks[*]}" -- "$word")) + + if [ ${#candidate_tasks[*]} -eq 0 ]; then + COMPREPLY=() + else + COMPREPLY=($(printf "\"%s\"\n" "${candidate_tasks[@]}")) + fi + fi + ;; + list) + words="--today --help --last_week --current_week --yesterday --project --no_grouping --date --from --number --round --to" + COMPREPLY=($(compgen -W "$words" -- "$word")) + ;; + report) + words="--today --help --last_week --current_week --yesterday --project --date --from --round --to" + COMPREPLY=($(compgen -W "$words" -- "$word")) + ;; + *) + ;; + esac } complete -F _bartib_completions bartib