commit 0b507b9c341b31a0e28f6f4b3e5dc8633f804e37
parent 22673811164a583dbeaf38ecdccb65db7b23515f
Author: René Wagner <rwa@clttr.info>
Date: Wed, 19 Apr 2023 21:14:06 +0200
add initial bash completion function
Diffstat:
2 files changed, 63 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
@@ -3,6 +3,7 @@ PREFIX?=/usr/local
BINDIR?=$(PREFIX)/bin
MANDIR?=$(PREFIX)/share/man
DOCDIR?=$(PREFIX)/share/doc
+COMPLDIR?=$(PREFIX)/share/bash-completion
.DEFAULT_GOAL=all
phosta:
@@ -25,21 +26,23 @@ clean:
rm -rf phosta.1 phosta-alias.5 phosta-examples.7 phosta-faq.7
install: all
- mkdir -p $(DESTDIR)/$(BINDIR) $(DESTDIR)/$(MANDIR)/man1 $(DESTDIR)/$(MANDIR)/man5 $(DESTDIR)/$(MANDIR)/man7 $(DESTDIR)/$(DOCDIR)/phosta/
+ mkdir -p $(DESTDIR)/$(BINDIR) $(DESTDIR)/$(MANDIR)/man1 $(DESTDIR)/$(MANDIR)/man5 $(DESTDIR)/$(MANDIR)/man7 $(DESTDIR)/$(DOCDIR)/phosta/ $(DESTDIR)/$(COMPLDIR)/completions
install -m755 phosta $(DESTDIR)/$(BINDIR)/phosta
install -m644 phosta.1 $(DESTDIR)/$(MANDIR)/man1/phosta.1
install -m644 phosta-alias.5 $(DESTDIR)/$(MANDIR)/man5/phosta-alias.5
install -m644 phosta-examples.7 $(DESTDIR)/$(MANDIR)/man7/phosta-examples.7
install -m644 phosta-faq.7 $(DESTDIR)/$(MANDIR)/man7/phosta-faq.7
install -m644 LICENSE $(DESTDIR)/$(DOCDIR)/phosta/LICENSE
+ install -m644 phosta-completion.sh $(DESTDIR)/$(COMPLDIR)/completions/phosta
-uninstall:
+uninstall: all
rm -f $(DESTDIR)/$(BINDIR)/phosta
rm -f $(DESTDIR)/$(MANDIR)/man1/phosta.1
rm -f $(DESTDIR)/$(MANDIR)/man5/phosta-alias.5
rm -f $(DESTDIR)/$(MANDIR)/man7/phosta-examples.7
rm -f $(DESTDIR)/$(MANDIR)/man7/phosta-faq.7
rm -rf $(DESTDIR)/$(DOCDIR)/phosta/
+ rm -rf $(DESTDIR)/$(COMPLDIR)/completions/phosta
-check: phosta phosta.1 phosta-alias.5 phosta-examples.7 phosta-faq.7
+check: all
@find test -perm -111 -exec '{}' \;
diff --git a/phosta-completion.sh b/phosta-completion.sh
@@ -0,0 +1,57 @@
+#!/bin/bash
+
+_phosta_completions()
+{
+ local cur=${COMP_WORDS[COMP_CWORD]}
+ local prev=${COMP_WORDS[COMP_CWORD-1]}
+
+ local suggestions
+ case "${prev}" in
+ "-o")
+ suggestions=($(compgen -W "year month week hour file maker model lensmaker lens aperture exposuretime iso focallength focallength35mm" "${cur}"))
+ ;;
+ "-s")
+ suggestions=($(compgen -W "file maker model lensmaker lens aperture exposuretime iso focallength focallength35mm" "${cur}"))
+ ;;
+ "-g")
+ suggestions=($(compgen -W "year month week hour" "${cur}"))
+ ;;
+ "-T")
+ suggestions=($(compgen -W "auto on off" "${cur}"))
+ ;;
+ "-n")
+ suggestions=($(compgen -W "10 20 50 100" "${cur}"))
+ ;;
+ "-W")
+ suggestions=($(compgen -W "80 100 120" "${cur}"))
+ ;;
+ "-t")
+ if [ "${cur}" == "" ]; then
+ suggestions=($(compgen -W "$(date +%Y)0101-$(date +%Y)1231"))
+ else
+ suggestions=($(compgen -W "${cur}0101-${cur}1231" "${cur}"))
+ fi
+ ;;
+ "-p")
+ suggestions=($(compgen -d "${cur}"))
+ ;;
+ "-D")
+ suggestions=($(compgen -f "${cur}"))
+ ;;
+ esac
+
+ if [ "${#suggestions[@]}" == "1" ]; then
+ # if there's only one match, we remove the command literal
+ # to proceed with the automatic completion of the command
+ suggestion=$(echo "${suggestions[0]/%\ */}")
+ COMPREPLY=("${suggestion}")
+ else
+ # more than one suggestions resolved,
+ # respond with the suggestions intact
+ COMPREPLY=("${suggestions[@]}")
+ fi
+
+ return 0
+}
+
+complete -o nospace -F _phosta_completions phosta