Skip to content

Commit

Permalink
completions: improve performance on zsh
Browse files Browse the repository at this point in the history
By avoiding unneeded `brew` calls we can make zsh completions
much faster.

As a rough measurement, completion for `brew list` used to take ~4s on
my machine, but now it's instant.
  • Loading branch information
ZhongRuoyu committed Sep 2, 2024
1 parent 33f53dd commit a6e169d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
17 changes: 13 additions & 4 deletions Library/Homebrew/completions/zsh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ __brew_completion_caching_policy() {
(( $#tmp )) || return 0

# otherwise, invalidate if latest tap index file is missing or newer than cache file
tmp=( $(brew --repository)/Library/Taps/*/*/.git/index(om[1]N) )
tmp=( "${HOMEBREW_REPOSITORY:-$(brew --repository)}"/Library/Taps/*/*/.git/index(om[1]N) )
[[ -z $tmp || $tmp -nt $1 ]]
}

Expand All @@ -66,7 +66,7 @@ __brew_installed_formulae() {
[[ -prefix '-' ]] && return 0

local -a formulae
formulae=($(brew list --formula))
formulae=($(command ls "${HOMEBREW_CELLAR:-$(brew --cellar)}" 2>/dev/null))
_describe -t formulae 'installed formulae' formulae
}

Expand Down Expand Up @@ -98,7 +98,7 @@ __brew_installed_casks() {

local -a list
local expl
list=( $(brew list --cask 2>/dev/null) )
list=($(command ls "$(brew --caskroom)" 2>/dev/null))
_wanted list expl 'installed casks' compadd -a list
}

Expand All @@ -114,7 +114,16 @@ __brew_installed_taps() {
[[ -prefix '-' ]] && return 0

local -a taps
taps=($(brew tap))
local dir taplib
taplib=${HOMEBREW_REPOSITORY:-$(brew --repository)}/Library/Taps

for dir in "${taplib}"/*/*
do
[[ -d ${dir} ]] || continue
dir=${dir#"${taplib}"/}
dir=${dir/homebrew-/}
taps+=("${dir}")
done
_describe -t installed-taps 'installed taps' taps
}

Expand Down
17 changes: 13 additions & 4 deletions completions/zsh/_brew
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ __brew_completion_caching_policy() {
(( $#tmp )) || return 0

# otherwise, invalidate if latest tap index file is missing or newer than cache file
tmp=( $(brew --repository)/Library/Taps/*/*/.git/index(om[1]N) )
tmp=( "${HOMEBREW_REPOSITORY:-$(brew --repository)}"/Library/Taps/*/*/.git/index(om[1]N) )
[[ -z $tmp || $tmp -nt $1 ]]
}

Expand All @@ -70,7 +70,7 @@ __brew_installed_formulae() {
[[ -prefix '-' ]] && return 0

local -a formulae
formulae=($(brew list --formula))
formulae=($(command ls "${HOMEBREW_CELLAR:-$(brew --cellar)}" 2>/dev/null))
_describe -t formulae 'installed formulae' formulae
}

Expand Down Expand Up @@ -102,7 +102,7 @@ __brew_installed_casks() {

local -a list
local expl
list=( $(brew list --cask 2>/dev/null) )
list=($(command ls "$(brew --caskroom)" 2>/dev/null))
_wanted list expl 'installed casks' compadd -a list
}

Expand All @@ -118,7 +118,16 @@ __brew_installed_taps() {
[[ -prefix '-' ]] && return 0

local -a taps
taps=($(brew tap))
local dir taplib
taplib=${HOMEBREW_REPOSITORY:-$(brew --repository)}/Library/Taps

for dir in "${taplib}"/*/*
do
[[ -d ${dir} ]] || continue
dir=${dir#"${taplib}"/}
dir=${dir/homebrew-/}
taps+=("${dir}")
done
_describe -t installed-taps 'installed taps' taps
}

Expand Down

0 comments on commit a6e169d

Please sign in to comment.