aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/pass.bash
blob: a03b088721cb675ce31c6c7b72c6f13ce599bc26 (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
# Bail if no pass(1)
if ! hash pass 2>/dev/null ; then
    return
fi

# Completion for pass(1), adapted from source package; still needs some tweaking
_pass()
{
    # Get current word, prefix, and suffix
    local word=${COMP_WORDS[COMP_CWORD]}
    local prefix=${PASSWORD_STORE_DIR:-$HOME/.password-store}
    local suffix=.gpg

    # Iterate through possible completions
    local IFS=$'\n'
    local items=( $(compgen -f "$prefix"/"$word") )
    local item
    for item in "${items[@]}" ; do
        if [[ $item == "$prefix"/.* ]] ; then
            continue
        fi

        # If there is a unique match, and it is a directory with one entry, then
        # recursively autocomplete the subentry as well
        if ((${#items[@]} == 1)) ; then
            local subitems
            while [[ -d $item ]] ; do
                subitems=( $(compgen -f "$item"/) )
                if ((${#subitems[@]} == 1)) ; then
                    item=${subitems[0]}
                else
                    break
                fi
            done
        fi

        # Append slash to directories
        if [[ -d $item ]] ; then
            item=$item/
        fi

        # Add item to possible completions
        item=${item%$suffix}
        item=${item#$prefix/}
        COMPREPLY=("${COMPREPLY[@]}" "$item")
    done
}

# Completion only has -o nospace in Bash >=3.0
if ((BASH_VERSINFO[0] >= 3)) ; then
    complete -o filenames -o nospace -F _pass pass
else
    complete -o filenames -F _pass pass
fi