mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-08-12 22:31:27 +04:00
feat(gitignore): add caching to gi completion (#13735)
Adds a cache policy for the `gi` command completion function, i.e. `_gitignoreio`. Previously, every completion call would request the list endpoint of gitignore.io over the network, resulting in a delay between pressing <Tab> and the autocomplete options showing up. Since we don't expect the gitignore.io templates to change that frequently, configured the cache policy to expire after 7 days. Signed-off-by: Rishvic Pushpakaran <rishvic@gmail.com> Co-authored-by: Carlo Sala <carlosalag@protonmail.com>
This commit is contained in:
co-authored by
Carlo Sala
parent
0912e05c05
commit
069512d14a
@@ -10,12 +10,48 @@ function gi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_gitignoreio_get_command_list() {
|
_gitignoreio_get_command_list() {
|
||||||
|
setopt local_options pipe_fail
|
||||||
_gi_curl "list" | tr "," "\n"
|
_gi_curl "list" | tr "," "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
_gitignoreio () {
|
__gitignoreio_caching_policy() {
|
||||||
compset -P '*,'
|
local -a oldp
|
||||||
compadd -S '' $(_gitignoreio_get_command_list)
|
oldp=("$1"(Nm+7))
|
||||||
|
(($#oldp))
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _gitignoreio gi
|
_gitignoreio_retrieve_stale_cache() {
|
||||||
|
zstyle -t ":completion:${curcontext}:" use-cache || return 1
|
||||||
|
|
||||||
|
local cache_dir
|
||||||
|
zstyle -s ":completion:${curcontext}:" cache-path cache_dir
|
||||||
|
: ${cache_dir:=${ZDOTDIR:-$HOME}/.zcompcache}
|
||||||
|
|
||||||
|
[[ -e "$cache_dir/gi-list" ]] || return 1
|
||||||
|
. "$cache_dir/gi-list"
|
||||||
|
}
|
||||||
|
|
||||||
|
_gitignoreio() {
|
||||||
|
compset -P '*,'
|
||||||
|
|
||||||
|
local cache_policy
|
||||||
|
zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
|
||||||
|
if [[ -z "$cache_policy" ]]; then
|
||||||
|
zstyle ":completion:${curcontext}:" cache-policy __gitignoreio_caching_policy
|
||||||
|
fi
|
||||||
|
|
||||||
|
local -a _gi_list
|
||||||
|
if _cache_invalid gi-list || ! _retrieve_cache gi-list; then
|
||||||
|
local command_list
|
||||||
|
if command_list="$(_gitignoreio_get_command_list)" && [[ -n "$command_list" ]]; then
|
||||||
|
_gi_list=(${(f)command_list})
|
||||||
|
_store_cache gi-list _gi_list
|
||||||
|
else
|
||||||
|
_gitignoreio_retrieve_stale_cache
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
compadd -S '' -a _gi_list
|
||||||
|
}
|
||||||
|
|
||||||
|
compdef _gitignoreio gi
|
||||||
|
|||||||
Reference in New Issue
Block a user