]> arthur.barton.de Git - brew-completion.git/blob - brew-completion.sh
c0c0e4e8d20917b2fa43bf212c4d2e6f60694db6
[brew-completion.git] / brew-completion.sh
1 # brew-completion: bash completion function for Homebrew (brew command)
2 # Copyright (c)1020 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         pushd "$prefix"/Library/Formula >/dev/null
12         formulae=`echo * | sed 's/\.rb//g'`
13         popd >/dev/null
14         COMPREPLY=( $(compgen -W '$formulae' -- "$1") )
15 }
16
17 _brew_formula_installed()
18 {
19         local formulae prefix
20         
21         prefix=`brew --prefix`
22         pushd "$prefix"/Cellar >/dev/null
23         formulae=`echo *`
24         popd >/dev/null
25         COMPREPLY=( $(compgen -W '$formulae' -- "$1") )
26 }
27
28 _brew()
29 {
30         local cur prev cmd i
31
32         COMPREPLY=()
33         cur=`_get_cword`
34         prev=${COMP_WORDS[COMP_CWORD-1]}
35         cmd=""
36
37         declare -i i=$COMP_CWORD-1
38         while [ $i -gt 0 ]; do
39                 if [[ ${COMP_WORDS[i]} != -* ]]; then
40                         cmd=${COMP_WORDS[i]}
41                         break
42                 fi
43                 i=$i-1
44         done
45
46         case "$cmd" in
47                 "edit"|"home"|"log"|"prune")
48                         _brew_formula "$cur"
49                         return 0
50                         ;;
51                 "link"|"remove"|"uninstall"|"unlink")
52                         _brew_formula_installed "$cur"
53                         return 0
54                         ;;
55                 "create")
56                         if [[ "$cur" == -* ]]; then
57                                 COMPREPLY=( $(compgen -W '--macports' \
58                                         -- "$cur") )
59                         fi
60                         return 0
61                         ;;
62                 "info")
63                         if [[ "$cur" == -* ]]; then
64                                 COMPREPLY=( $(compgen -W '--github' \
65                                         -- "$cur") )
66                         else
67                                 _brew_formula "$cur"
68                         fi
69                         return 0
70                         ;;
71                 "install")
72                         if [[ "$cur" == -* ]]; then
73                                 COMPREPLY=( $(compgen -W '--HEAD -H \
74                                         --debug -d --interactive -i
75                                         --verbose -v --ignore-dependencies' \
76                                         -- "$cur") )
77                         else
78                                 _brew_formula "$cur"
79                         fi
80                         return 0
81                         ;;
82                 "list")
83                         if [[ "$cur" == -* ]]; then
84                                 COMPREPLY=( $(compgen -W '--brewed \
85                                         --unbrewed' -- "$cur") )
86                         else
87                                 _brew_formula "$cur"
88                         fi
89                         return 0
90                         ;;
91                 "search"|"update")
92                         return 0
93                         ;;
94         esac
95
96         if [[ "$cur" == -* ]]; then
97                 COMPREPLY=( $(compgen -W '--cache --help --prefix --version \
98                         -v -H' -- "$cur") )
99         elif [ "$cmd" = "" ]; then
100                 COMPREPLY=( $(compgen -W 'create edit home info install \
101                         link list log prune remove search uninstall unlink \
102                         update' -- "$cur") )
103         fi
104 }
105 complete -F _brew brew