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