Fix style inconsistencies in Bash completion (#660)

This commit is contained in:
Martin Afanasjew 2016-08-08 22:53:33 +02:00 committed by GitHub
parent 8c0b72ac0d
commit cd23c263ba

View File

@ -1,133 +1,101 @@
# Bash completion script for brew(1) # Bash completion script for brew(1)
__brewcomp_words_include () __brewcomp_words_include() {
{
local i=1 local i=1
while [[ $i -lt $COMP_CWORD ]]; do while [[ "$i" -lt "$COMP_CWORD" ]]
if [[ "${COMP_WORDS[i]}" = "$1" ]]; then do
if [[ "${COMP_WORDS[i]}" = "$1" ]]
then
return 0 return 0
fi fi
i=$((++i)) i="$((++i))"
done done
return 1 return 1
} }
# Find the previous non-switch word # Find the previous non-switch word
__brewcomp_prev () __brewcomp_prev() {
{ local idx="$((COMP_CWORD - 1))"
local idx=$((COMP_CWORD - 1))
local prv="${COMP_WORDS[idx]}" local prv="${COMP_WORDS[idx]}"
while [[ $prv == -* ]]; do while [[ "$prv" = -* ]]
idx=$((--idx)) do
idx="$((--idx))"
prv="${COMP_WORDS[idx]}" prv="${COMP_WORDS[idx]}"
done done
echo "$prv" echo "$prv"
} }
__brewcomp () __brewcomp() {
{
# break $1 on space, tab, and newline characters, # break $1 on space, tab, and newline characters,
# and turn it into a newline separated list of words # and turn it into a newline separated list of words
local list s sep=$'\n' IFS=$' '$'\t'$'\n' local list s sep=$'\n' IFS=$' '$'\t'$'\n'
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
for s in $1; do for s in $1
do
__brewcomp_words_include "$s" && continue __brewcomp_words_include "$s" && continue
list="$list$s$sep" list="$list$s$sep"
done done
IFS=$sep IFS="$sep"
COMPREPLY=($(compgen -W "$list" -- "$cur")) COMPREPLY=($(compgen -W "$list" -- "$cur"))
} }
# Don't use __brewcomp() in any of the __brew_complete_foo functions, as # Don't use __brewcomp() in any of the __brew_complete_foo functions, as
# it is too slow and is not worth it just for duplicate elimination. # it is too slow and is not worth it just for duplicate elimination.
__brew_complete_formulae () __brew_complete_formulae() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
local formulas="$(brew search)" 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")) COMPREPLY=($(compgen -W "$formulas $shortnames" -- "$cur"))
} }
__brew_complete_installed () __brew_complete_installed() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
local inst=$(\ls $(brew --cellar)) local inst="$(ls "$(brew --cellar)")"
COMPREPLY=($(compgen -W "$inst" -- "$cur")) COMPREPLY=($(compgen -W "$inst" -- "$cur"))
} }
__brew_complete_outdated () __brew_complete_outdated() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
local od=$(brew outdated --quiet) local od="$(brew outdated --quiet)"
COMPREPLY=($(compgen -W "$od" -- "$cur")) COMPREPLY=($(compgen -W "$od" -- "$cur"))
} }
__brew_complete_versions () __brew_complete_versions() {
{
local formula="$1" local formula="$1"
local versions=$(brew list --versions "$formula") local versions="$(brew list --versions "$formula")"
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=($(compgen -W "$versions" -X "$formula" -- "$cur")) COMPREPLY=($(compgen -W "$versions" -X "$formula" -- "$cur"))
} }
__brew_complete_logs () __brew_complete_logs() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
local logs=$(ls ${HOMEBREW_LOGS:-~/Library/Logs/Homebrew/}) local logs="$(ls "${HOMEBREW_LOGS:-~/Library/Logs/Homebrew/}")"
COMPREPLY=($(compgen -W "$logs" -- "$cur")) COMPREPLY=($(compgen -W "$logs" -- "$cur"))
} }
_brew_switch () __brew_complete_tapped() {
{ local taplib="$(brew --repository)/Library/Taps"
case "$COMP_CWORD" in
2) __brew_complete_installed ;;
3) __brew_complete_versions "${COMP_WORDS[COMP_CWORD-1]}" ;;
*) ;;
esac
}
__brew_complete_tapped ()
{
local taplib=$(brew --repository)/Library/Taps
local dir taps local dir taps
for dir in ${taplib}/*/*; do for dir in "$taplib"/*/*
[ -d "$dir" ] || continue do
dir=${dir#${taplib}/} [[ -d "$dir" ]] || continue
dir=${dir/homebrew-/} dir="${dir#${taplib}/}"
dir="${dir/homebrew-/}"
taps="$taps $dir" taps="$taps $dir"
done done
__brewcomp "$taps" __brewcomp "$taps"
} }
_brew_tap_unpin () _brew_analytics() {
{
__brewcomp "$(brew tap --list-pinned)"
}
_brew_complete_tap ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
--*)
__brewcomp "--repair --list-official --list-pinned"
return
;;
esac
__brewcomp "$(brew tap --list-official)"
}
_brew_analytics ()
{
case "$COMP_CWORD" in case "$COMP_CWORD" in
2) __brewcomp "off on regenerate-uuid state" ;; 2) __brewcomp "off on regenerate-uuid state" ;;
esac esac
} }
_brew_bottle () _brew_bottle() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -138,13 +106,11 @@ _brew_bottle ()
__brew_complete_installed __brew_complete_installed
} }
_brew_cleanup () _brew_cleanup() {
{
__brew_complete_installed __brew_complete_installed
} }
_brew_create () _brew_create() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -154,8 +120,7 @@ _brew_create ()
esac esac
} }
_brew_deps () _brew_deps() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -166,8 +131,7 @@ _brew_deps ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_desc () _brew_desc() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -178,13 +142,7 @@ _brew_desc ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_doctor () { _brew_diy() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__brewcomp "$(brew doctor --list-checks)"
}
_brew_diy ()
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -194,10 +152,14 @@ _brew_diy ()
esac esac
} }
_brew_fetch () _brew_doctor() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
local prv=$(__brewcomp_prev) __brewcomp "$(brew doctor --list-checks)"
}
_brew_fetch() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prv="$(__brewcomp_prev)"
case "$cur" in case "$cur" in
--*) --*)
__brewcomp " __brewcomp "
@ -213,8 +175,7 @@ _brew_fetch ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_gist_logs () _brew_gist_logs() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -225,8 +186,7 @@ _brew_gist_logs ()
__brew_complete_logs __brew_complete_logs
} }
_brew_info () _brew_info() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -237,14 +197,14 @@ _brew_info ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_install () _brew_install() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
local prv=$(__brewcomp_prev) local prv="$(__brewcomp_prev)"
case "$cur" in case "$cur" in
--*) --*)
if __brewcomp_words_include "--interactive"; then if __brewcomp_words_include "--interactive"
then
__brewcomp "--devel --git --HEAD" __brewcomp "--devel --git --HEAD"
else else
__brewcomp " __brewcomp "
@ -265,8 +225,7 @@ _brew_install ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_irb () _brew_irb() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -276,8 +235,7 @@ _brew_irb ()
esac esac
} }
_brew_link () _brew_link() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -288,8 +246,7 @@ _brew_link ()
__brew_complete_installed __brew_complete_installed
} }
_brew_linkapps () _brew_linkapps() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -300,25 +257,29 @@ _brew_linkapps ()
__brew_complete_installed __brew_complete_installed
} }
_brew_list () _brew_list() {
{
local allopts="--unbrewed --verbose --pinned --versions --multiple" local allopts="--unbrewed --verbose --pinned --versions --multiple"
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
# most options to brew-list are mutually exclusive # most options to brew-list are mutually exclusive
if __brewcomp_words_include "--unbrewed"; then if __brewcomp_words_include "--unbrewed"
then
return return
elif __brewcomp_words_include "--verbose"; then elif __brewcomp_words_include "--verbose"
then
return return
elif __brewcomp_words_include "--pinned"; then elif __brewcomp_words_include "--pinned"
then
return return
# --multiple only applies with --versions # --multiple only applies with --versions
elif __brewcomp_words_include "--multiple"; then elif __brewcomp_words_include "--multiple"
then
__brewcomp "--versions" __brewcomp "--versions"
return return
elif __brewcomp_words_include "--versions"; then elif __brewcomp_words_include "--versions"
then
__brewcomp "--multiple" __brewcomp "--multiple"
return return
else else
@ -329,15 +290,15 @@ _brew_list ()
esac esac
# --multiple excludes formulae and *implies* --versions... # --multiple excludes formulae and *implies* --versions...
if __brewcomp_words_include "--multiple"; then if __brewcomp_words_include "--multiple"
then
__brewcomp "--versions" __brewcomp "--versions"
else else
__brew_complete_installed __brew_complete_installed
fi fi
} }
_brew_log () _brew_log() {
{
# if git-completion is loaded, then we complete git-log options # if git-completion is loaded, then we complete git-log options
declare -F _git_log >/dev/null || return declare -F _git_log >/dev/null || return
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
@ -357,8 +318,7 @@ _brew_log ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_man () _brew_man() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -368,8 +328,7 @@ _brew_man ()
esac esac
} }
_brew_options () _brew_options() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -380,8 +339,7 @@ _brew_options ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_outdated () _brew_outdated() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -391,8 +349,7 @@ _brew_outdated ()
esac esac
} }
_brew_postinstall () _brew_postinstall() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -403,8 +360,7 @@ _brew_postinstall ()
__brew_complete_installed __brew_complete_installed
} }
_brew_prune () _brew_prune() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -414,8 +370,7 @@ _brew_prune ()
esac esac
} }
_brew_pull () _brew_pull() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -425,8 +380,7 @@ _brew_pull ()
esac esac
} }
_brew_readall () _brew_readall() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -437,8 +391,7 @@ _brew_readall ()
__brew_complete_tapped __brew_complete_tapped
} }
_brew_search () _brew_search() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -448,8 +401,7 @@ _brew_search ()
esac esac
} }
_brew_style () _brew_style() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -460,8 +412,26 @@ _brew_style ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew_tap_info () _brew_switch() {
{ case "$COMP_CWORD" in
2) __brew_complete_installed ;;
3) __brew_complete_versions "${COMP_WORDS[COMP_CWORD-1]}" ;;
*) ;;
esac
}
_brew_tap() {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
--*)
__brewcomp "--repair --list-official --list-pinned"
return
;;
esac
__brewcomp "$(brew tap --list-official)"
}
_brew_tap_info() {
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -472,8 +442,7 @@ _brew_tap_info ()
__brew_complete_tapped __brew_complete_tapped
} }
_brew_tap_readme () _brew_tap_readme() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -483,8 +452,11 @@ _brew_tap_readme ()
esac esac
} }
_brew_tests () _brew_tap_unpin() {
{ __brewcomp "$(brew tap --list-pinned)"
}
_brew_tests() {
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -494,8 +466,7 @@ _brew_tests ()
esac esac
} }
_brew_uninstall () _brew_uninstall() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -506,8 +477,7 @@ _brew_uninstall ()
__brew_complete_installed __brew_complete_installed
} }
_brew_unlinkapps () _brew_unlinkapps() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -518,8 +488,7 @@ _brew_unlinkapps ()
__brew_complete_installed __brew_complete_installed
} }
_brew_unpack () _brew_unpack() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -529,8 +498,7 @@ _brew_unpack ()
esac esac
__brew_complete_formulae __brew_complete_formulae
} }
_brew_update () _brew_update() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -540,10 +508,9 @@ _brew_update ()
esac esac
} }
_brew_upgrade () _brew_upgrade() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
local prv=$(__brewcomp_prev) local prv="$(__brewcomp_prev)"
case "$cur" in case "$cur" in
--*) --*)
@ -560,8 +527,7 @@ _brew_upgrade ()
__brew_complete_outdated __brew_complete_outdated
} }
_brew_uses () _brew_uses() {
{
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
@ -572,12 +538,12 @@ _brew_uses ()
__brew_complete_formulae __brew_complete_formulae
} }
_brew () _brew() {
{
local i=1 cmd local i=1 cmd
# find the subcommand # find the subcommand
while [[ $i -lt $COMP_CWORD ]]; do while [[ "$i" -lt "$COMP_CWORD" ]]
do
local s="${COMP_WORDS[i]}" local s="${COMP_WORDS[i]}"
case "$s" in case "$s" in
--*) --*)
@ -591,10 +557,11 @@ _brew ()
break break
;; ;;
esac esac
i=$((++i)) i="$((++i))"
done done
if [[ $i -eq $COMP_CWORD ]]; then if [[ "$i" -eq "$COMP_CWORD" ]]
then
# Do not auto-complete "instal" abbreviation for "install" command. # Do not auto-complete "instal" abbreviation for "install" command.
# Prefix newline to prevent not checking the first command. # Prefix newline to prevent not checking the first command.
local cmds=$'\n'"$(brew commands --quiet --include-aliases)" local cmds=$'\n'"$(brew commands --quiet --include-aliases)"
@ -604,21 +571,25 @@ _brew ()
# subcommands have their own completion functions # subcommands have their own completion functions
case "$cmd" in case "$cmd" in
--cache|--cellar|--prefix) __brew_complete_formulae ;; --cache) __brew_complete_formulae ;;
--cellar) __brew_complete_formulae ;;
--prefix) __brew_complete_formulae ;;
analytics) _brew_analytics ;; analytics) _brew_analytics ;;
audit|cat|edit|home) __brew_complete_formulae ;; audit) __brew_complete_formulae ;;
test|unlink) __brew_complete_installed ;;
bottle) _brew_bottle ;; bottle) _brew_bottle ;;
cat) __brew_complete_formulae ;;
cleanup) _brew_cleanup ;; cleanup) _brew_cleanup ;;
create) _brew_create ;; create) _brew_create ;;
deps) _brew_deps ;; deps) _brew_deps ;;
desc) _brew_desc ;; desc) _brew_desc ;;
doctor|dr) _brew_doctor ;;
diy|configure) _brew_diy ;; diy|configure) _brew_diy ;;
doctor|dr) _brew_doctor ;;
edit) __brew_complete_formulae ;;
fetch) _brew_fetch ;; fetch) _brew_fetch ;;
gist-logs) _brew_gist_logs ;; gist-logs) _brew_gist_logs ;;
home|homepage) __brew_complete_formulae ;;
info|abv) _brew_info ;; info|abv) _brew_info ;;
install|instal|reinstall) _brew_install ;; install|instal) _brew_install ;;
irb) _brew_irb ;; irb) _brew_irb ;;
link|ln) _brew_link ;; link|ln) _brew_link ;;
linkapps) _brew_linkapps ;; linkapps) _brew_linkapps ;;
@ -633,20 +604,24 @@ _brew ()
prune) _brew_prune ;; prune) _brew_prune ;;
pull) _brew_pull ;; pull) _brew_pull ;;
readall) _brew_readall ;; readall) _brew_readall ;;
reinstall) _brew_install ;;
search|-S) _brew_search ;; search|-S) _brew_search ;;
style) _brew_style ;; style) _brew_style ;;
switch) _brew_switch ;; switch) _brew_switch ;;
tap) _brew_complete_tap ;; tap) _brew_tap ;;
tap-info) _brew_tap_info ;; tap-info) _brew_tap_info ;;
tap-pin) __brew_complete_tapped ;;
tap-readme) _brew_tap_readme ;; tap-readme) _brew_tap_readme ;;
tap-unpin) _brew_tap_unpin ;; tap-unpin) _brew_tap_unpin ;;
test) __brew_complete_installed ;;
tests) _brew_tests ;; tests) _brew_tests ;;
uninstall|remove|rm) _brew_uninstall ;; uninstall|remove|rm) _brew_uninstall ;;
unlink) __brew_complete_installed ;;
unlinkapps) _brew_unlinkapps ;; unlinkapps) _brew_unlinkapps ;;
unpack) _brew_unpack ;; unpack) _brew_unpack ;;
unpin) __brew_complete_formulae ;; unpin) __brew_complete_formulae ;;
untap|tap-pin) __brew_complete_tapped ;; untap) __brew_complete_tapped ;;
update) _brew_update ;; update|up) _brew_update ;;
upgrade) _brew_upgrade ;; upgrade) _brew_upgrade ;;
uses) _brew_uses ;; uses) _brew_uses ;;
*) ;; *) ;;
@ -654,8 +629,7 @@ _brew ()
} }
# keep around for compatibility # keep around for compatibility
_brew_to_completion () _brew_to_completion() {
{
_brew _brew
} }