]> arthur.barton.de Git - brew-completion.git/blob - brew-completion.sh
Enhance support for external brew commands
[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         extpath="`brew --prefix`/Library/Contributions/examples"
35         if [ -d "$extpath" ]; then
36                 EXTCMD=`cd "$extpath"; ls -1 brew-* 2>/dev/null \
37                         | cut -d'.' -f1 | cut -d'-' -f2- | sed -e 's/\*//g'`
38         fi
39
40         declare -i i=$COMP_CWORD-1
41         while [ $i -gt 0 ]; do
42                 if [[ ${COMP_WORDS[i]} != -* \
43                    || ${COMP_WORDS[i]} == "--cache" \
44                    || ${COMP_WORDS[i]} == "--cellar" \
45                    || ${COMP_WORDS[i]} == "--config" \
46                    || ${COMP_WORDS[i]} == "--prefix" ]]; then
47                         cmd=${COMP_WORDS[i]}
48                         break
49                 fi
50                 i=$i-1
51         done
52
53         case "$cmd" in
54                 "cat"|"cleanup"|"deps"|"edit"|"home"|"log"|"prune"|"uses" \
55                 |"--cache"|"--cellar"|"--prefix")
56                         _brew_formula "$cur"
57                         return 0
58                         ;;
59                 "link"|"remove"|"rm"|"uninstall"|"unlink")
60                         _brew_formula_installed "$cur"
61                         return 0
62                         ;;
63                 "create")
64                         if [[ "$cur" == -* ]]; then
65                                 COMPREPLY=( $(compgen -W '--macports --fink \
66                                         --cache' -- "$cur") )
67                         fi
68                         return 0
69                         ;;
70                 "info")
71                         if [[ "$cur" == -* ]]; then
72                                 COMPREPLY=( $(compgen -W '--all --github' \
73                                         -- "$cur") )
74                         else
75                                 _brew_formula "$cur"
76                         fi
77                         return 0
78                         ;;
79                 "install")
80                         if [[ "$cur" == -* ]]; then
81                                 COMPREPLY=( $(compgen -W '--git --HEAD -H \
82                                         --debug -d --force -f \
83                                         --interactive -i --verbose -v \
84                                         --ignore-dependencies --use-llvm' \
85                                         -- "$cur") )
86                         else
87                                 _brew_formula "$cur"
88                         fi
89                         return 0
90                         ;;
91                 "list")
92                         if [[ "$cur" == -* ]]; then
93                                 COMPREPLY=( $(compgen -W '
94                                         --unbrewed' -- "$cur") )
95                         else
96                                 _brew_formula "$cur"
97                         fi
98                         return 0
99                         ;;
100                 *)
101                         for c in $EXTCMD; do
102                                 if [ "$cmd" = "$c" ]; then
103                                         _brew_formula "$cur"
104                                         return 0
105                                 fi
106                         done
107         esac
108
109         if [[ "$cur" == -* ]]; then
110                 COMPREPLY=( $(compgen -W '--cache --cellar --config \
111                         --env --help --prefix --version -v -H' \
112                         -- "$cur") )
113         elif [ "$cmd" = "" ]; then
114                 COMPREPLY=( $(compgen -W 'cat cleanup create deps doctor \
115                         edit home info install link list log outdated prune \
116                         remove rm search uninstall unlink up update uses \
117                         $EXTCMD' \
118                         -- "$cur") )
119         fi
120 }
121 complete -F _brew brew