]> arthur.barton.de Git - brew-completion.git/blob - brew-completion.sh
Added support for external brew commands stored in /usr/local/bin
[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]} == "--prefix" ]]; then
43                         cmd=${COMP_WORDS[i]}
44                         break
45                 fi
46                 i=$i-1
47         done
48
49         case "$cmd" in
50                 "cleanup"|"edit"|"home"|"log"|"prune" \
51                 |"--cache"|"--cellar"|"--prefix")
52                         _brew_formula "$cur"
53                         return 0
54                         ;;
55                 "link"|"remove"|"uninstall"|"unlink")
56                         _brew_formula_installed "$cur"
57                         return 0
58                         ;;
59                 "create")
60                         if [[ "$cur" == -* ]]; then
61                                 COMPREPLY=( $(compgen -W '--macports' \
62                                         -- "$cur") )
63                         fi
64                         return 0
65                         ;;
66                 "info")
67                         if [[ "$cur" == -* ]]; then
68                                 COMPREPLY=( $(compgen -W '--github' \
69                                         -- "$cur") )
70                         else
71                                 _brew_formula "$cur"
72                         fi
73                         return 0
74                         ;;
75                 "install")
76                         if [[ "$cur" == -* ]]; then
77                                 COMPREPLY=( $(compgen -W '--HEAD -H \
78                                         --debug -d --interactive -i
79                                         --verbose -v --ignore-dependencies' \
80                                         -- "$cur") )
81                         else
82                                 _brew_formula "$cur"
83                         fi
84                         return 0
85                         ;;
86                 "list")
87                         if [[ "$cur" == -* ]]; then
88                                 COMPREPLY=( $(compgen -W '
89                                         --unbrewed' -- "$cur") )
90                         else
91                                 _brew_formula "$cur"
92                         fi
93                         return 0
94                         ;;
95                 "search"|"update")
96                         return 0
97                         ;;
98                 *)
99                         for c in $EXTCMD; do
100                                 if [ "$cmd" = "$c" ]; then
101                                         _brew_formula "$cur"
102                                         return 0
103                                 fi
104                         done
105         esac
106
107         if [[ "$cur" == -* ]]; then
108                 COMPREPLY=( $(compgen -W '--cache --cellar --help --prefix \
109                         --version -v -H' -- "$cur") )
110         elif [ "$cmd" = "" ]; then
111                 COMPREPLY=( $(compgen -W 'cleanup create edit home info \
112                         install link list log outdated prune remove search \
113                         uninstall unlink update $EXTCMD' -- "$cur") )
114         fi
115 }
116 complete -F _brew brew