aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2021-02-10 15:32:50 -0500
committerYour Name <you@example.com>2021-02-10 15:32:50 -0500
commit3339dc7f29540758f59e2c20462d99643a8d225e (patch)
treeb3929c75df8be33dafbc7bc26a88443318a8a657
parent3af6b0cb80be974079a4f723b8c1dfa2128f9d41 (diff)
downloadlibbible-3339dc7f29540758f59e2c20462d99643a8d225e.tar.gz
libbible-3339dc7f29540758f59e2c20462d99643a8d225e.tar.bz2
libbible-3339dc7f29540758f59e2c20462d99643a8d225e.zip
Added bash completions
-rw-r--r--Makefile1
-rw-r--r--bible.bash76
2 files changed, 77 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index f497ff4..fdbf13c 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ install: $(LIBRARY) $(EXECUTABLE)
install -m 644 libbible.h $(DESTDIR)$(PREFIX)/include/
install -d $(DESTDIR)$(PREFIX)/bin/
install -m 755 bible $(DESTDIR)$(PREFIX)/bin/
+ install -m 644 bible.bash `pkg-config --variable=completionsdir bash-completion`
test: $(OBJECTS) testLibbible.o
$(CC) $(OBJECTS) testLibbible.o -o $@ $(LDFLAGS) `pkg-config $(LIBS) --libs` -lcppunit
diff --git a/bible.bash b/bible.bash
new file mode 100644
index 0000000..e614cbc
--- /dev/null
+++ b/bible.bash
@@ -0,0 +1,76 @@
+# bible(1) completion -*- shell-script -*-
+
+# These are bash completions for the bible executable
+
+_bible()
+{
+ local cur prev words cword split
+ # -s splits option=value into option in $prev and value in $cur
+ # -n : keeps the colon from signifying the end of a token
+ _init_completion -s -n : || return
+ case $prev in
+ --module | -m | --set-default-module | --remove-module)
+ COMPREPLY=($(compgen -W "`bible --list-modules | tail -n+2 | awk '{print $1}'`" -- "$cur"))
+ return
+ ;;
+ --list-chapters)
+ # If we already have -m, use that. Else if a default is set, use that. Else, just return.
+ # Scan COMP_WORDS for -m or --module
+ mod="`grep -Po '(?<=\-m |\-\-module ).*?(?= )' <<< "${COMP_WORDS[@]}"`"
+ if [ -z "$mod" ]; then
+ # If we get here, then we need to set mod using the default module
+ mod="`bible --list-modules | grep '(default)' | awk '{print $1}'`"
+ if [ -z "$mod" ]; then
+ # We tried.
+ return
+ fi
+ fi
+ # Tricky because of spaces in names
+ books="`bible -m $mod --list-books | tail -n+2 | sed 's/^ //' | sed 's/ /\\\\ /g'`"
+ books="${books//\\ /___}"
+ COMPREPLY=()
+ for iter in $books; do
+ if [[ $iter =~ "___" ]]; then
+ iter="\"${iter//___/ }\""
+ fi
+ if [[ $iter =~ ^$cur ]] || [[ $iter =~ ^\"$cur ]]; then
+ COMPREPLY+=("$iter")
+ fi
+ done
+ return
+ ;;
+ --install-network)
+ # Use cur to first grab language, then module
+ if [[ $cur == *:* ]]; then
+ # We're working on the mod, not language
+ lang=`cut -d: -f1 <<< $cur`
+ COMPREPLY=($(compgen -W "`bible --list-installable | grep -Pzo '(?s)For language '$lang':.*?(?=For language)' | tail -n+2 | sed 's/ /'$lang':/' | tr -d '\000'`" -- "$cur"))
+ else
+ # We're working on the language, not mod
+ compopt -o nospace
+ COMPREPLY=($(compgen -W "`bible --list-installable | grep 'For language' | awk '{print $3}'`" -- "$cur"))
+ fi
+ __ltrim_colon_completions "$cur"
+ return
+ ;;
+ --install-zip)
+ _filedir '@(zip)'
+ return
+ ;;
+ --list-installable)
+ COMPREPLY=($(compgen -W "`bible --list-installable | grep 'For language' | sed 's/For language //' | cut -d: -f1`" -- "$cur"))
+ return
+ ;;
+ --help | --list-books | --omit-verse-numbers | --list-modules | --list-installable | -[ho])
+ return
+ ;;
+ esac
+
+ $split && return
+
+ if [[ $cur == -* ]]; then
+ COMPREPLY=($(compgen -W "`_parse_help "$1"`" -- "$cur"))
+ [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
+ fi
+} &&
+ complete -F _bible bible