aboutsummaryrefslogtreecommitdiffstats
path: root/cli/bash-completion.sh
blob: e1ae871afa8d4ab0fe3085e2bc712d0539f5d8e3 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Bash completion support for icevault(1)
# Copyright © 2015 Guilhem Moulin <guilhem@fripost.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

_icevault() {
    local cur prev words cword
    _init_completion -n := || return

    COMPREPLY=()
    declare -a files

    local cmd firstopt=2  opts nargs
    if [ $cword -eq 1 -a -z "$cur" ] || [[ "${words[1]}" =~ :// ]] || [[ "${words[1]}" =~ ^- ]]; then
        firstopt=1
        cmd=fill # default command
    elif [ $cword -gt 1 ]; then
        cmd="${words[1]}"
    fi

    local commands='fill clip cp dump edit git insert ls mv reencrypt rm'
    case "$cmd" in
        fill)      opts='-f --force -p --show-passwords -s --socket='; nargs=1;;
        clip)      opts=; nargs=1 ;;
        cp)        opts='-f --force'; nargs=2;;
        dump)      opts='-p --show-passwords'; nargs=1;;
        edit)      opts=; nargs=1 ;;
        git)       opts= ;;
        insert)    opts='-f --force -s --socket='; nargs=0;;
        ls)        opts='-0 --zero -r --recursive'; nargs=-1;;
        mv)        opts='-f --force'; nargs=2;;
        reencrypt) opts=; nargs=-1 ;;
        rm)        opts='-f --force -r --recursive'; nargs=-1;;
    esac
    opts="${opts:+$opts }--debug -h --help --version"

    if [ "$cmd" = git ]; then
        # use bash completion for git by ignoring the first word ('icevault')
        [ -f /usr/share/bash-completion/completions/git ] && . /usr/share/bash-completion/completions/git || return
        COMP_CWORD=$(( $COMP_CWORD - 1 ))
        COMP_WORDS=( "${COMP_WORDS[@]:1}" )
        local GIT_DIR GIT_WORK_TREE # fetch store and git-dir from the config-file
        GIT_WORK_TREE=$(sed -n '/^store\s*=\s*/{s///p;q}' "${XDG_CONFIG_HOME:-$HOME/.config}/icevault")
        GIT_WORK_TREE=$( echo "${GIT_WORK_TREE:-icevault}" | sed -re "s@^~/@$HOME@; t; s@^/@/@; t; s@^@${XDG_DATA_HOME:-$HOME/.local/share}/@" )
        GIT_DIR=$(sed -n '/^git-dir\s*=\s*/{s///p;q}' "${XDG_CONFIG_HOME:-$HOME/.config}/icevault")
        GIT_DIR=$( echo "${GIT_DIR:-.git}" | sed -re "s@^~/@$HOME@; t; s@^/@/@; t; s@^@$GIT_WORK_TREE/@" )
        export GIT_DIR GIT_WORK_TREE
        _git
        return
    fi

    local OPTIND socket
    # find out if our $cur is an optional argument or not
    for (( OPTIND = firstopt; OPTIND < cword; OPTIND++ )); do
        case "${words[OPTIND]}" in
            -s) (( OPTIND++ )); [ $OPTIND -lt $cword ] && socket="${words[OPTIND]}";;
            --) break;;
            --socket=*) [ $OPTIND -lt $cword ] && socket="${words[OPTIND]#--socket=}";;
            -*) ;;
            *) break;;
        esac
    done

    local p
    # list sockets (and dirs) only
    if [[ $OPTIND -eq $(( $cword + 1)) && "$prev" = -s ]] || [[ $OPTIND -eq $cword && "$cur" =~ ^--socket= ]]; then
        [[ $OPTIND -eq $cword && "$cur" =~ ^--socket= ]] && cur="${cur#--socket=}"
        _filedir
        files=( "${COMPREPLY[@]}" )
        COMPREPLY=()
        for p in "${files[@]}"; do
            [ -d "$p" -o -S "$p" ] && COMPREPLY+=( "$p" )
        done
        return
    elif [[ $OPTIND -eq $cword && "$cur" =~ ^- ]]; then
        COMPREPLY+=( $(compgen -W "$opts" -- "$cur") )
        [ "${#COMPREPLY[@]}" -eq 1 -a "${COMPREPLY[0]}" = '--socket=' ] && compopt -o nospace
        return
    fi

    local trim=
    if [ "$cmd" = fill -a \( $OPTIND -eq $cword -o $(( $OPTIND + 1)) -eq $cword \) -a -z "$cur" ]; then
        cur="$(icevault _geturi ${socket:+--socket="$socket"})"/ # get URI from webpage
    elif [ -z "$cmd" -a "$cword" -eq 1 -a "$cur" ]; then
        # autocomplete command
        COMPREPLY+=( $(compgen -W "$commands" -- "$cur") )
        return
    else
        cur=$(dequote "$cur")
        # trim words with : or = in $COMP_WORDBREAKS; see __ltrim_colon_completions
        if [[ "$cur" == *[:=]* && "$COMP_WORDBREAKS" == *:* && "$COMP_WORDBREAKS" == *=* ]]; then
            trim=${cur%"${cur##*[:=]}"}
        elif [[ "$cur" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
            trim=${cur%"${cur##*:}"}
        elif [[ "$cur" == *=* && "$COMP_WORDBREAKS" == *=* ]]; then
            trim=${cur%"${cur##*=}"}
        fi
    fi

    local uri
    # autocomplete with identities as long as we don't exeed $nargs
    if [ $nargs -lt 0 -o $(($cword - $OPTIND + 1)) -le $nargs ]; then
        compopt -o filenames -o noquote
        while read -r -d $'\0' uri; do
            # quote manually (so we don't quote the : and =)
            uri=$( echo "${uri#$trim}" | sed "s/[][\\{}*?~<>;'\"|&()\!$\` \t]/\\\\&/g" )
            COMPREPLY+=( "$uri" )
        done < <(icevault _complete -0 "$cur" 2>/dev/null)
        [ "${#COMPREPLY[@]}" -eq 1 -a "${COMPREPLY[0]: -1:1}" = / ] && compopt -o nospace
        return 0
    fi
}
complete -F _icevault icevault