diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2018-12-02 17:59:29 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2018-12-02 17:59:29 +1300 |
commit | 7d6fe8b1886f902f8ffbec2a9985fae9f91121cb (patch) | |
tree | 282fac0bb1809e726a373916c90260832ab23ced /bash/bash_completion.d/pass.bash | |
parent | Reduce ud() completion to just dirnames (diff) | |
download | dotfiles-7d6fe8b1886f902f8ffbec2a9985fae9f91121cb.tar.gz dotfiles-7d6fe8b1886f902f8ffbec2a9985fae9f91121cb.zip |
Overhaul Bash completion scripts
Some general changes:
* Apply case sensitivity switching in more contexts, using a dynamically
loaded helper function
* Use array counters for appending to COMPREPLY where possible
* Lots more short-circuiting to limit structural depth
These changes are expansive and there will definitely be bugs.
Diffstat (limited to 'bash/bash_completion.d/pass.bash')
-rw-r--r-- | bash/bash_completion.d/pass.bash | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/bash/bash_completion.d/pass.bash b/bash/bash_completion.d/pass.bash index af7926d9..5a6e0b6c 100644 --- a/bash/bash_completion.d/pass.bash +++ b/bash/bash_completion.d/pass.bash @@ -1,11 +1,11 @@ +# Load _completion_ignore_case helper function +if ! declare -F _completion_ignore_case >/dev/null ; then + source "$HOME"/.bash_completion.d/_completion_ignore_case.bash +fi + # Custom completion for pass(1), because I don't like the one included with the # distribution -_pass() -{ - # If we can't read the password directory, just bail - local passdir - passdir=${PASSWORD_STORE_DIR:-"$HOME"/.password-store} - [[ -r $passdir ]] || return +_pass() { # Iterate through completions produced by subshell local ci comp @@ -13,35 +13,43 @@ _pass() COMPREPLY[ci++]=$comp done < <( - # Set shell options to expand globs the way we expect + # Make globs expand appropriately shopt -u dotglob shopt -s nullglob + if _completion_ignore_case ; then + shopt -s nocaseglob + fi + + # Set password store path + pass_dir=${PASSWORD_STORE_DIR:-"$HOME"/.password-store} - # Check Readline settings for case-insensitive matching - while read -r _ setting ; do - if [[ $setting == 'completion-ignore-case on' ]] ; then - shopt -s nocaseglob - break - fi - done < <(bind -v) + # Gather the entries + for entry in "$pass_dir"/"$2"*.gpg ; do + entries[ei++]=$entry + done - # Gather the entries, use ** for depth search if we can - entries=("$passdir"/"$2"*.gpg) + # Try to iterate into subdirs, use depth search with ** if available if shopt -s globstar 2>/dev/null ; then - entries=("${entries[@]}" "$passdir"/"$2"**/*.gpg) + for entry in "$pass_dir"/"$2"**/*.gpg ; do + entries[ei++]=$entry + done else - entries=("${entries[@]}" "$passdir"/"$2"*/*.gpg) + for entry in "$pass_dir"/"$2"*/*.gpg ; do + entries[ei++]=$entry + done 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 entries, quoted and null-delimited - printf '%q\0' "${entries[@]}" + # Iterate through entries + for entry in "${entries[@]}" ; do + # Skip directories + ! [[ -d $entry ]] || continue + # Strip leading path + entry=${entry#"$pass_dir"/} + # Strip .gpg suffix + entry=${entry%.gpg} + # Print shell-quoted entry, null terminated + printf '%q\0' "$entry" + done ) } complete -F _pass pass |