aboutsummaryrefslogtreecommitdiff
path: root/bash-completion/cave
blob: c71f9eee179a51a17e3d73f7fdf3a164f17d43d6 (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
# Bash completion function for cave
# Written by Mike Kelly
# vim: set et sw=4 sts=4 ts=4 ft=sh :

# Based in part upon 'git' bash completion from git-1.6.4.2

__cave_find_cmd() {
    local w c cmd

    for (( c=1; c < COMP_CWORD; c++ )) ; do
        w="${COMP_WORDS[c]}"
        case "${w}" in
            --*) ;;
            --help) cmd="help"; break ;;
            config|@(display|execute)-resolution|help|import|perform|print-@(categories|commands|environment-metadata|id-@(contents|executables|metadata)|ids|owners|packages|repositor@(ies|y-formats)|sets|sync-protocols)|resolve|show|sync|update-world) cmd="${w}"; break ;;
        esac
    done

    echo "${cmd}"
}

__cave_enum_value() {
    local w c v enum="${1}"
    for (( c=COMP_CWORD; c > 1; c-- )) ; do
        w="${COMP_WORDS[c]}"
        if [[ "${w}" =~ ^${enum}$ ]] ; then
            v="${COMP_WORDS[c+1]}"
            break;
        fi
    done

    echo "${v}"
}

__cave_global() {
    case "${prev}" in
        --log-level)
            COMPREPLY=( $(compgen -W "debug qa warning silent" -- "${cur}") )
            ;;
        --environment|-E)
            COMPREPLY=( $(compgen -W "paludis: portage:" -- "${cur}") )
            ;;
        *)
            COMPREPLY=( $(compgen -W "$(cave print-commands -a) -E --environment --log-level" -- "${cur}") )
            ;;
    esac
}

__cave_help() {
    # only complete once
    if [[ "${prev}" == "help" ]] ; then
        COMPREPLY=( $(compgen -W "$(cave print-commands -a) -a --all" -- "${cur}") )
    fi
}

__cave_sync() {
    COMPREPLY=( $(compgen -W "$(cave print-repositories) --sequential" -- "${cur}") )
}

__cave_show() {
    local type="$(__cave_enum_value '--type|-t')"
    case "${prev}" in
        --type|-t)
            COMPREPLY=( $(compgen -W "auto repository set wildcard package" -- "${cur}") )
            ;;
        *)
            case "${cur}" in
                -*)
                    COMPREPLY=( $(compgen -W "--type -t --complex-keys -c --internal-keys -i --flat -f --raw-names -r" -- "${cur}") )
                    ;;
                *)
                    case "${type}" in
                        repository)
                            COMPREPLY=( $(compgen -W "$(cave print-repositories)" -- "${cur}") )
                            ;;
                        set)
                            COMPREPLY=( $(compgen -W "$(cave print-sets)" -- "${cur}") )
                            ;;
                        package)
                            COMPREPLY=( $(compgen -W "$(cave print-packages)" -- "${cur}") )
                            ;;
                        auto|wildcard|*)
                            # TODO: provide reasonable completions for 'spec' here
                            COMPREPLY=()
                            ;;
                    esac
                    ;;
            esac
            ;;
    esac
}

_cave() {
    local cmd=`__cave_find_cmd` prev=`_get_pword` cur=`_get_cword`

    COMPREPLY=()

    if [[ -z "${cmd}" ]] ; then
        __cave_global
    else
        if [[ "$(type -t "__cave_${cmd}")" == "function" ]] ; then
            "__cave_${cmd}"
        fi
    fi
}
complete -F _cave cave