From d2c0d06d8fc5a56a7d3218751f60c6aa6cfef94b Mon Sep 17 00:00:00 2001 From: Romain Bossart Date: Mon, 10 Dec 2018 22:35:14 +0100 Subject: [PATCH] _brew | allow 'brew search' to be cached by zsh --- completions/zsh/_brew | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/completions/zsh/_brew b/completions/zsh/_brew index af360c30b3..af5735acbe 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -39,9 +39,28 @@ __brew_formulae_or_ruby_files() { 'files:files:{_files -g *.rb}' } +# formulae completions are cached as long as homebrew does have new commits +__brew_formulae_caching_policy() { + # rebuild cache if no cache file exists (anyway we cannot proceed further down) + ! [[ -f "$1" ]] && return 0 + # cache file modification date (seconds since epoch) + local -i cache_mtime=$(date -r "$1" +%s) + # latest homebrew commit on HEAD (branch 'stable' by default) + local brew_repo=${HOMEBREW_PREFIX:-/usr/local}/Homebrew/.git + local latest_commit=$(git --git-dir=${brew_repo} rev-parse HEAD) # --branches=refs/stable + # latest homebrew commit date (seconds since epoch) + local -i commit_mtime=$(git --git-dir=${brew_repo} rev-list -1 --format=format:'%at' $latest_commit | tail +2) + (( $cache_mtime < $commit_mtime )) +} + __brew_formulae() { + zstyle ":completion:${curcontext}:" cache-policy __brew_formulae_caching_policy local -a formulae - formulae=($(brew search)) + local comp_cachename=brew_formulae + if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then + formulae=($(brew search)) + _store_cache $comp_cachename formulae + fi _describe -t formulae 'all formulae' formulae }