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 --- Makefile | 9 +++++--- shell/bash-completion | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 shell/bash-completion diff --git a/Makefile b/Makefile index 2512cc7..2a2798c 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ BINPROGS = \ MANPAGES = \ man/asp.1 +BASH_COMPLETION = \ + shell/bash-completion + INCLUDES = \ package.inc.sh \ remote.inc.sh \ @@ -36,9 +39,9 @@ clean: $(RM) $(BINPROGS) $(MANPAGES) install: all - install -dm755 $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/share/man/man1 - install -m755 $(BINPROGS) $(DESTDIR)$(PREFIX)/bin - install -m644 $(MANPAGES) $(DESTDIR)$(PREFIX)/share/man/man1 + install -Dm755 $(BINPROGS) $(DESTDIR)$(PREFIX)/bin + install -Dm644 $(MANPAGES) $(DESTDIR)$(PREFIX)/share/man/man1 + install -Dm644 $(BASH_COMPLETION) $(DESTDIR)$(PREFIX)/share/bash-completion/completions/asp dist: git archive --format=tar --prefix=$(PACKAGE_NAME)-$(VER)/ v$(VER) | gzip -9 > $(PACKAGE_NAME)-$(VER).tar.gz 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