]> arthur.barton.de Git - brew-completion.git/blob - brew-completion.sh
Add completion for "brew --env"
[brew-completion.git] / brew-completion.sh
1 # brew-completion: bash completion function for Homebrew (brew command)
2 # Copyright (c)2010 Alexander Barton <alex@barton.de>
3 # This is free software and published under the terms of the GNU
4 # General Public License, see the file COPYING for details.
5
6 _brew_formula()
7 {
8         local formulae prefix
9         
10         prefix=`brew --prefix`
11         formulae=`brew search`
12         COMPREPLY=( $(compgen -W '$formulae' -- "$1") )
13 }
14
15 _brew_formula_installed()
16 {
17         local formulae prefix
18         
19         prefix=`brew --prefix`
20         formulae=`brew list`
21         COMPREPLY=( $(compgen -W '$formulae' -- "$1") )
22 }
23
24 _brew()
25 {
26         local cur prev cmd i
27
28         COMPREPLY=()
29         cur=`_get_cword`
30         prev=${COMP_WORDS[COMP_CWORD-1]}
31         cmd=""
32
33         # Get list of all available external commands
34         EXTCMD=`cd /usr/local/bin; ls -1 brew-* | cut -d'.' -f1 \
35                 | cut -d'-' -f2- | sed -e 's/\*//g'`
36
37         declare -i i=$COMP_CWORD-1
38         while [ $i -gt 0 ]; do
39                 if [[ ${COMP_WORDS[i]} != -* \
40                    || ${COMP_WORDS[i]} == "--cache" \
41                    || ${COMP_WORDS[i]} == "--cellar" \
42                    || ${COMP_WORDS[i]} == "--config" \
43                    || ${COMP_WORDS[i]} == "--prefix" ]]; then
44                         cmd=${COMP_WORDS[i]}
45                         break
46                 fi
47                 i=$i-1
48         done
49
50         case "$cmd" in
51                 "cat"|"cleanup"|"deps"|"edit"|"home"|"log"|"prune"|"uses" \
52                 |"--cache"|"--cellar"|"--prefix")
53                         _brew_formula "$cur"
54                         return 0
55                         ;;
56                 "link"|"remove"|"rm"|"uninstall"|"unlink")
57                         _brew_formula_installed "$cur"
58                         return 0
59                         ;;
60                 "create")
61                         if [[ "$cur" == -* ]]; then
62                                 COMPREPLY=( $(compgen -W '--macports --fink \
63                                         --cache' -- "$cur") )
64                         fi
65                         return 0
66                         ;;
67                 "info")
68                         if [[ "$cur" == -* ]]; then
69                                 COMPREPLY=( $(compgen -W '--all --github' \
70                                         -- "$cur") )
71                         else
72                                 _brew_formula "$cur"
73                         fi
74                         return 0
75                         ;;
76                 "install")
77                         if [[ "$cur" == -* ]]; then
78                                 COMPREPLY=( $(compgen -W '--git --HEAD -H \
79                                         --debug -d --force -f \
80                                         --interactive -i --verbose -v \
81                                         --ignore-dependencies --use-llvm' \
82                                         -- "$cur") )
83                         else
84                                 _brew_formula "$cur"
85                         fi
86                         return 0
87                         ;;
88                 "list")
89                         if [[ "$cur" == -* ]]; then
90                                 COMPREPLY=( $(compgen -W '
91                                         --unbrewed' -- "$cur") )
92                         else
93                                 _brew_formula "$cur"
94                         fi
95                         return 0
96                         ;;
97                 *)
98                         for c in $EXTCMD; do
99                                 if [ "$cmd" = "$c" ]; then
100                                         _brew_formula "$cur"
101                                         return 0
102                                 fi
103                         done
104         esac
105
106         if [[ "$cur" == -* ]]; then
107                 COMPREPLY=( $(compgen -W '--cache --cellar --config \
108                         --env --help --prefix --version -v -H' \
109                         -- "$cur") )
110         elif [ "$cmd" = "" ]; then
111                 COMPREPLY=( $(compgen -W 'cat cleanup create deps doctor \
112                         edit home info install link list log outdated prune \
113                         remove rm search uninstall unlink up update uses \
114                         $EXTCMD' \
115                         -- "$cur") )
116         fi
117 }
118 complete -F _brew brew