From a6e169d41c72d3ba0e638e530ffa722e69550e7c Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Sun, 1 Sep 2024 18:35:33 +0800 Subject: [PATCH] completions: improve performance on zsh 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. --- Library/Homebrew/completions/zsh.erb | 17 +++++++++++++---- completions/zsh/_brew | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/completions/zsh.erb b/Library/Homebrew/completions/zsh.erb index 025ab24ea6e1a..37f761ceb6a5e 100644 --- a/Library/Homebrew/completions/zsh.erb +++ b/Library/Homebrew/completions/zsh.erb @@ -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 ]] } @@ -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 } @@ -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 } @@ -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 } diff --git a/completions/zsh/_brew b/completions/zsh/_brew index 926ca009f6797..692507ce8a13b 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -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 ]] } @@ -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 } @@ -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 } @@ -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 }