Merge pull request #5394 from bosr/zsh/completion-cache

Zsh/completion cache for 'brew search' / formulae name
This commit is contained in:
Mike McQuaid 2018-12-13 09:19:34 +00:00 committed by GitHub
commit 99c7e9d4ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,9 +39,26 @@ __brew_formulae_or_ruby_files() {
'files:files:{_files -g *.rb}'
}
# completions remain in cache until any tap has new commits
__brew_completion_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 modified homebrew tap index file
local latest_modified_git_index=(${HOMEBREW_REPOSITORY:-/usr/local/Homebrew}/Library/Taps/*/*/.git/index(om[1]))
local -i commit_mtime=$(date -r "$latest_modified_git_index" +%s)
(( $cache_mtime < $commit_mtime ))
}
__brew_formulae() {
zstyle ":completion:${curcontext}:" cache-policy __brew_completion_caching_policy
local -a formulae
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
}
@ -129,22 +146,10 @@ __brew_common_commands() {
_describe -t common-commands 'common commands' commands
}
# completions are cached for 24 hour
__brew_commands_caching_policy() {
local -a oldp
oldp=( "$1"(Nmh+24) )
(( $#oldp ))
}
__brew_all_commands() {
local cache_policy
zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
if [[ -z "$cache_policy" ]]; then
zstyle ":completion:${curcontext}:" cache-policy __brew_commands_caching_policy
fi
zstyle ":completion:${curcontext}:" cache-policy __brew_completion_caching_policy
local -a commands
local comp_cachename=brew_all_commands
if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then
commands=($(_call_program brew brew commands --quiet --include-aliases))
_store_cache $comp_cachename commands