aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/pass.bash
blob: 760e774e41671cec13322f246eff1e1999d875d1 (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
48
49
50
51
52
53
54
55
# 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() {

    # Iterate through completions produced by subshell
    local ci comp
    while IFS= read -d '' -r comp ; do
        COMPREPLY[ci++]=$comp
    done < <(

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

        # Gather the entries
        for entry in "$pass_dir"/"$2"*.gpg ; do
            entries[ei++]=$entry
        done

        # Try to iterate into subdirs, use depth search with ** if available
        if shopt -s globstar 2>/dev/null ; then
            for entry in "$pass_dir"/"$2"*/**/*.gpg ; do
                entries[ei++]=$entry
            done
        else
            for entry in "$pass_dir"/"$2"*/*.gpg ; do
                entries[ei++]=$entry
            done
        fi

        # 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