From ac1c39d2e1c730f76b0430915e05d349763ba2e1 Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Wed, 30 Mar 2022 12:22:54 -0500 Subject: [PATCH] completion: Detect Homebrew prefix internally, not idiomatically For performance reasons, we prefer detecting Homebrew prefix internally instead of the more idiomatic form `brew --prefix`. We attempt looking up $HOMEBREW_PREFIX or $HOMEBREW_REPOSITORY first (in case `brew shellenv` has been sourced-in earlier). Else, we look it up by resolving absolute path of $HOMEBREW_REPOSITORY. $HOMEBREW_PREFIX is same as $HOMEBREW_REPOSITORY except when Homebrew is installed in '/usr/local' ($HOMEBREW_REPOSITORY == '/usr/local/Homebrew'). This is usually the case for Intel Macs. This should work for most standard (and officially documented) Homebrew installations. For implementation details in Homebrew, see: https://github.com/Homebrew/brew/blob/2a850e02d8f2dedcad7164c2f4b95d340a7200bb/bin/brew#L62-L70 Co-authored-by: mattmc3 --- modules/completion/init.zsh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/completion/init.zsh b/modules/completion/init.zsh index fc5cfd4..9ba9563 100644 --- a/modules/completion/init.zsh +++ b/modules/completion/init.zsh @@ -14,12 +14,16 @@ fi # Add zsh-completions to $fpath. fpath=(${0:h}/external/src $fpath) -# Add completion for keg-only brewed curl when available. -if (( $+commands[brew] )) \ - && [[ -d "${curl_prefix::="$(brew --prefix 2> /dev/null)"/opt/curl}" ]]; then - fpath=($curl_prefix/share/zsh/site-functions $fpath) +# Add completion for keg-only brewed curl on macOS when available. +if (( $+commands[brew] )); then + brew_prefix=${HOMEBREW_PREFIX:-${HOMEBREW_REPOSITORY:-$commands[brew]:A:h:h}} + # $HOMEBREW_PREFIX defaults to $HOMEBREW_REPOSITORY but is explicitly set to + # /usr/local when $HOMEBREW_REPOSITORY is /usr/local/Homebrew. + # https://github.com/Homebrew/brew/blob/2a850e02d8f2dedcad7164c2f4b95d340a7200bb/bin/brew#L66-L69 + [[ $brew_prefix == '/usr/local/Homebrew' ]] && brew_prefix=$brew_prefix:h + fpath=($brew_prefix/opt/curl/share/zsh/site-functions(/N) $fpath) + unset brew_prefix fi -unset curl_prefix # # Options