aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/pass.bash
blob: af7926d96ef625bfbaf63cae8e124407c9a2880e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

    # 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 nullglob

        # 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, 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 entries, quoted and null-delimited
        printf '%q\0' "${entries[@]}"
    )
}
complete -F _pass pass