aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-12-01 21:47:45 +1300
committerTom Ryder <tom@sanctum.geek.nz>2018-12-02 12:42:25 +1300
commit1351b3f831a93f78665ee39c5a5b66d14a169d3d (patch)
tree3cdb9d9703081b9f554ae5c3e2dd451d253a8dc7
parentAdjust syntax of two more completion loads (diff)
downloaddotfiles-1351b3f831a93f78665ee39c5a5b66d14a169d3d.tar.gz
dotfiles-1351b3f831a93f78665ee39c5a5b66d14a169d3d.zip
Overhaul pass(1) completion
Remove Bash 4.0 requirement by using globstar dynamically if found.
-rw-r--r--bash/bash_completion.d/pass.bash46
1 files changed, 23 insertions, 23 deletions
diff --git a/bash/bash_completion.d/pass.bash b/bash/bash_completion.d/pass.bash
index c1246757..af7926d9 100644
--- a/bash/bash_completion.d/pass.bash
+++ b/bash/bash_completion.d/pass.bash
@@ -1,6 +1,3 @@
-# Requires Bash >= 4.0 for globstar
-((BASH_VERSINFO[0] >= 4)) || return
-
# Custom completion for pass(1), because I don't like the one included with the
# distribution
_pass()
@@ -10,37 +7,40 @@ _pass()
passdir=${PASSWORD_STORE_DIR:-"$HOME"/.password-store}
[[ -r $passdir ]] || return
- # Iterate through list of .gpg paths, extension stripped, null-delimited,
- # and filter them down to the ones matching the completing word (compgen
- # doesn't seem to do this properly with a null delimiter)
- local entry
- while IFS= read -rd '' entry ; do
- [[ -n $entry ]] || continue
- COMPREPLY+=("$entry")
+ # Iterate through completions produced by subshell
+ local ci comp
+ while IFS= read -d '' -r comp ; do
+ COMPREPLY[ci++]=$comp
done < <(
# Set shell options to expand globs the way we expect
shopt -u dotglob
- shopt -s globstar nullglob
+ shopt -s nullglob
- # Make globbing case-insensitive if appropriate
+ # Check Readline settings for case-insensitive matching
while read -r _ setting ; do
- case $setting in
- ('completion-ignore-case on')
- shopt -s nocaseglob
- break
- ;;
- esac
+ if [[ $setting == 'completion-ignore-case on' ]] ; then
+ shopt -s nocaseglob
+ break
+ fi
done < <(bind -v)
- # Gather the entries and remove their .gpg suffix
- declare -a entries
- entries=("$passdir"/"$2"*/**/*.gpg \
- "$passdir"/"$2"*.gpg)
+ # Gather the entries, use ** for depth search if we can
+ entries=("$passdir"/"$2"*.gpg)
+ if shopt -s globstar 2>/dev/null ; then
+ entries=("${entries[@]}" "$passdir"/"$2"**/*.gpg)
+ else
+ entries=("${entries[@]}" "$passdir"/"$2"*/*.gpg)
+ fi
+
+ # Bail out if there are no entries
+ ((${#entries[@]})) || exit
+
+ # Strip leading path and .gpg suffix from entry names
entries=("${entries[@]#"$passdir"/}")
entries=("${entries[@]%.gpg}")
- # Print quoted entries, null-delimited
+ # Print entries, quoted and null-delimited
printf '%q\0' "${entries[@]}"
)
}