diff options
Diffstat (limited to 'completions/bash/brew')
| -rw-r--r-- | completions/bash/brew | 232 |
1 files changed, 230 insertions, 2 deletions
diff --git a/completions/bash/brew b/completions/bash/brew index 60c272f73..f885b808d 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -1,5 +1,10 @@ # Bash completion script for brew(1) +# Indicates there are no completions +__brewcomp_null() { + COMPREPLY="" +} + __brewcomp_words_include() { local i=1 while [[ "$i" -lt "$COMP_CWORD" ]] @@ -46,7 +51,7 @@ __brewcomp() { __brew_complete_formulae() { local cur="${COMP_WORDS[COMP_CWORD]}" local formulas="$(brew search)" - local shortnames="$(echo "$formulas" | grep / | cut -d / -f 3)" + local shortnames="$(echo "$formulas" | \grep / | \cut -d / -f 3)" COMPREPLY=($(compgen -W "$formulas $shortnames" -- "$cur")) } @@ -399,6 +404,7 @@ _brew_search() { return ;; esac + __brewcomp_null } _brew_style() { @@ -539,6 +545,227 @@ _brew_uses() { __brew_complete_formulae } +__brew_caskcomp_words_include () +{ + local i=1 + while [[ $i -lt $COMP_CWORD ]]; do + if [[ "${COMP_WORDS[i]}" = "$1" ]]; then + return 0 + fi + i=$((++i)) + done + return 1 +} + +# Find the previous non-switch word +__brew_caskcomp_prev () +{ + local idx=$((COMP_CWORD - 1)) + local prv="${COMP_WORDS[idx]}" + while [[ $prv == -* ]]; do + idx=$((--idx)) + prv="${COMP_WORDS[idx]}" + done + echo "$prv" +} + +__brew_caskcomp () +{ + # break $1 on space, tab, and newline characters, + # and turn it into a newline separated list of words + local list s sep=$'\n' IFS=$' '$'\t'$'\n' + local cur="${COMP_WORDS[COMP_CWORD]}" + + for s in $1; do + __brew_caskcomp_words_include "$s" && continue + list="$list$s$sep" + done + + IFS=$sep + COMPREPLY=($(compgen -W "$list" -- "$cur")) +} + +# Don't use __brew_caskcomp() in any of the __brew_cask_complete_foo functions, as +# it is too slow and is not worth it just for duplicate elimination. +__brew_cask_complete_formulae () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + local lib=$(brew --repository)/Library + local taps=${lib}/Taps + local casks=${lib}/Taps/caskroom/homebrew-cask/Casks + local ff=$(\ls ${casks} 2>/dev/null | \sed 's/\.rb//g') + + COMPREPLY=($(compgen -W "$ff" -- "$cur")) +} + +__brew_cask_complete_installed () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + local inst=$(brew cask list -1) + COMPREPLY=($(compgen -W "$inst" -- "$cur")) +} + +__brew_cask_complete_caskroom () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + local caskroom_dir=/opt/homebrew-cask/Caskroom/ + local files=$(\ls ${caskroom_dir} 2>/dev/null) + COMPREPLY=($(compgen -W "$files" -- "$cur")) +} + +_brew_cask_cleanup () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + -*) + __brew_caskcomp "--force" + return + ;; + esac + __brew_cask_complete_installed +} + +_brew_cask_fetch () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + local prv=$(__brew_caskcomp_prev) + case "$cur" in + -*) + __brew_caskcomp "--force" + return + ;; + esac + __brew_cask_complete_formulae +} + +_brew_cask_install () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + local prv=$(__brew_caskcomp_prev) + case "$cur" in + -*) + __brew_caskcomp "--force --skip-cask-deps --require-sha --language" + return + ;; + esac + __brew_cask_complete_formulae +} + +_brew_cask_list () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + -*) + __brew_caskcomp "-1 --versions" + return + ;; + esac + + __brew_cask_complete_installed +} + +_brew_cask_outdated () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + -*) + __brew_caskcomp "--greedy --verbose --quiet" + return + ;; + esac + __brew_cask_complete_installed +} + +_brew_cask_style () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + -*) + __brew_caskcomp "--fix" + return + ;; + esac + __brew_cask_complete_installed +} + +_brew_cask_uninstall () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + -*) + __brew_caskcomp "--force" + return + ;; + esac + __brew_cask_complete_installed +} + +_brew_cask_upgrade () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + -*) + __brew_caskcomp "--force --greedy" + return + ;; + esac + __brew_cask_complete_installed +} + +_brew_cask () +{ + local i=1 cmd + + # find the subcommand + while [[ $i -lt $COMP_CWORD ]]; do + local s="${COMP_WORDS[i]}" + case "$s" in + --*) + cmd="$s" + break + ;; + -*) + ;; + cask) + ;; + *) + cmd="$s" + break + ;; + esac + i=$((++i)) + done + + if [[ $i -eq $COMP_CWORD ]]; then + __brew_caskcomp "abv audit cat cleanup create doctor edit fetch home info install list ls outdated reinstall remove rm search style uninstall upgrade zap -S --force --caskroom --verbose --appdir --colorpickerdir --prefpanedir --qlplugindir --fontdir --servicedir --input_methoddir --internet_plugindir --screen_saverdir --no-binaries --debug --version" + return + fi + + # subcommands have their own completion functions + case "$cmd" in + --version) __brewcomp_null ;; + audit) __brew_cask_complete_formulae ;; + cat) __brew_cask_complete_formulae ;; + cleanup) _brew_cask_cleanup ;; + create) ;; + doctor) __brewcomp_null ;; + edit) __brew_cask_complete_formulae ;; + fetch) _brew_cask_fetch ;; + home) __brew_cask_complete_formulae ;; + info|abv) __brew_cask_complete_formulae ;; + install|instal) _brew_cask_install ;; + list|ls) _brew_cask_list ;; + outdated) _brew_cask_outdated ;; + reinstall) __brew_cask_complete_installed ;; + search) __brewcomp_null ;; + style) _brew_cask_style ;; + uninstall|remove|rm) _brew_cask_uninstall ;; + upgrade) _brew_cask_upgrade ;; + zap) __brew_cask_complete_caskroom ;; + *) ;; + esac +} + _brew() { local i=1 cmd @@ -565,7 +792,7 @@ _brew() { then # Do not auto-complete "*instal" or "*uninstal" aliases for "*install" commands. # Prefix newline to prevent not checking the first command. - local cmds=$'\n'"$(brew commands --quiet --include-aliases | grep -v instal$)" + local cmds=$'\n'"$(brew commands --quiet --include-aliases | \grep -v instal$)" __brewcomp "${cmds}" return fi @@ -578,6 +805,7 @@ _brew() { analytics) _brew_analytics ;; audit) __brew_complete_formulae ;; bottle) _brew_bottle ;; + cask) _brew_cask ;; cat) __brew_complete_formulae ;; cleanup) _brew_cleanup ;; create) _brew_create ;; |
