aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/brew_bash_completion.sh
blob: 5b9bef45c20e40749dfb16a508edcb29ac0cd774 (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
# Bash completion script for brew(1)
#
# To use, edit your .bashrc and add:
#   source `brew --prefix`/Library/Contributions/brew_bash_completion.sh

_brew_to_completion()
{
    local cur="${COMP_WORDS[COMP_CWORD]}"

    # Subcommand list
    [[ ${COMP_CWORD} -eq 1 ]] && {
        local actions="--cache --cellar --config --env --prefix --repository audit cat cleanup
            configure create deps doctor edit fetch help home info install link list log options
            outdated prune remove search test uninstall unlink update upgrade uses versions"
        local ext=$(\ls $(brew --repository)/Library/Contributions/examples |
                    sed -e "s/\.rb//g" -e "s/brew-//g")
        COMPREPLY=( $(compgen -W "${actions} ${ext}" -- ${cur}) )
        return
    }

    # Find the previous non-switch word
    local prev_index=$((COMP_CWORD - 1))
    local prev="${COMP_WORDS[prev_index]}"
    while [[ $prev == -* ]]; do
        prev_index=$((--prev_index))
        prev="${COMP_WORDS[prev_index]}"
    done

    # Handle installation --options
    if [[ ${COMP_WORDS[1]} == "install" && "$cur" == --* ]]; then
        local opts=$(
            local opts=(
              --force --verbose --debug --use-clang --use-gcc --use-llvm --ignore-dependencies --build-from-source --HEAD
              $(brew options --compact "$prev")
            )
            for o in ${opts[*]}; do
                [[ " ${COMP_WORDS[*]} " =~ " $o " ]] || echo "$o"
            done
        )
        COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
        return
    fi

    case "$prev" in
    # Commands that take a formula
    cat|deps|edit|fetch|home|homepage|info|install|log|missing|options|uses|versions)
        local ff=$(\ls $(brew --repository)/Library/Formula | sed "s/\.rb//g")
        local af=$(\ls $(brew --repository)/Library/Aliases 2> /dev/null | sed "s/\.rb//g")
        COMPREPLY=( $(compgen -W "${ff} ${af}" -- ${cur}) )
        return
        ;;
    # Commands that take an existing brew
    abv|cleanup|link|list|ln|ls|remove|rm|test|uninstall|unlink|upgrade)
        COMPREPLY=( $(compgen -W "$(\ls $(brew --cellar))" -- ${cur}) )
        return
        ;;
    esac
}

complete -o bashdefault -o default -F _brew_to_completion brew