From 5e640c2534a287210f83dfb492dbe9a37b895a39 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sat, 16 Aug 2014 21:18:25 -0400 Subject: add bash-completion --- shell/bash-completion | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 shell/bash-completion (limited to 'shell/bash-completion') diff --git a/shell/bash-completion b/shell/bash-completion new file mode 100644 index 0000000..b59be7e --- /dev/null +++ b/shell/bash-completion @@ -0,0 +1,61 @@ +#!/bin/bash + +in_array() { + for _ in "${@:2}"; do + [[ $_ = "$1" ]] && return 0 + done + return 1 +} + +_asp() { + local a= cur= prev= comps= + + _get_comp_words_by_ref cur prev + + # top level commands + local -A verbs=( + [ALL_PACKAGES]='checkout difflog export list-arches list-repos log shortlog' + [LOCAL_PACKAGES]='untrack update' + [NONE]='disk-usage gc help list-all' + ) + + # flags + local -A opts=( + [UNKNOWN]='-a' + [NONE]='-f -h -V' + ) + + if in_array "$prev" ${opts[UNKNOWN]}; then + return 0 + fi + + if [[ $cur = -* ]]; then + COMPREPLY=( $(compgen -W '${opts[*]}' -- "$cur") ) + return 0 + fi + + for word in "${COMP_WORDS[@]}"; do + if in_array "$word" ${verbs[ALL_PACKAGES]}; then + a=$word + comps=$(\asp list-all | sed 's,.*/,,') + break + elif in_array "$word" ${verbs[LOCAL_PACKAGES]}; then + a=$word + comps=$(\asp list-local | sed 's,.*/,,') + break + elif in_array "$word" ${verbs[NONE]}; then + a=$word + break + fi + done + + if [[ -z $a ]]; then + comps=${verbs[*]} + fi + + if [[ $comps ]]; then + COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) + fi +} + +complete -F _asp asp -- cgit v1.2.3-70-g09d2