From 2286662b807d0edc4436df6277c863ddddb66ce0 Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Tue, 31 Jul 2018 20:29:01 -0500 Subject: [PATCH] command-not-found: Restore idiomatic homebrewed handler loading on MacOS As is the convention in prezto, we cache the command-not-found handler to avoid incurring the performance penalty of loading ruby interpreter on every call. This restores the 'Homebrew way' of loading command-not-found handler. Further, the formally recommended command lookup mechanism in Homebrew (viz., `brew command command-not-found-init`) is ruby based and is super slow. To avoid performance penalty, we `find` it ourselves from `TAP_DIRECTORY` defined internally in Homebrew. This also reinstates support for custom taps or non-standard Homebrew location. --- modules/command-not-found/init.zsh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/modules/command-not-found/init.zsh b/modules/command-not-found/init.zsh index 2c59a4b..dc3c2b4 100644 --- a/modules/command-not-found/init.zsh +++ b/modules/command-not-found/init.zsh @@ -12,9 +12,28 @@ if [[ -s '/etc/zsh_command_not_found' ]]; then # Load command-not-found on Arch Linux-based distributions. elif [[ -s '/usr/share/doc/pkgfile/command-not-found.zsh' ]]; then source '/usr/share/doc/pkgfile/command-not-found.zsh' -# Load command-not-found on macOS when homebrew tap is configured. -elif [[ -s '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh' ]]; then - source '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh' +# Load command-not-found on macOS when Homebrew tap is configured. +# To avoid performance penalty, we do not use Homebrew's ruby based command +# lookup mechanism (viz., `brew command command-not-found-init`) and instead +# `find` it ourselves from `TAP_DIRECTORY` defined internally in Homebrew. +elif (( $+commands[brew] )); then + cnf_command="$(brew --repository 2> /dev/null)"/Library/Taps/homebrew/homebrew-command-not-found/cmd/brew-command-not-found-init.rb + if [[ -s "$cnf_command" ]]; then + cache_file="${TMPDIR:-/tmp}/prezto-brew-command-not-found-cache.$UID.zsh" + + if [[ "$cnf_command" -nt "$cache_file" \ + || "${ZDOTDIR:-$HOME}/.zpreztorc" -nt "$cache_file" \ + || ! -s "$cache_file" ]]; then + # brew command-not-found-init is slow; cache its output. + brew command-not-found-init >! "$cache_file" 2> /dev/null + fi + + source "$cache_file" + + unset cache_file + fi + + unset cnf_command # Return if requirements are not found. else return 1