-
Notifications
You must be signed in to change notification settings - Fork 2
/
zsh-smartcache.plugin.zsh
39 lines (34 loc) · 1.01 KB
/
zsh-smartcache.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
ZSH_SMARTCACHE_DIR=${ZSH_SMARTCACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/zsh-smartcache}
_smartcache-eval() {
local cache=$ZSH_SMARTCACHE_DIR/eval-$1; shift
if [[ ! -f $cache ]] {
local output=$("$@")
eval $output
printf '%s' $output >| $cache &!
} else {
source $cache
{
local output=$("$@")
[[ $output == "$(<$cache)" ]] && return
printf '%s' $output >| $cache
print "Cache updated: '$@' (applied next time)"
} &!
}
}
_smartcache-comp() {
local cache=$ZSH_SMARTCACHE_DIR/_$1; shift
if [[ ! -f $cache ]] {
"$@" >| $cache
} else {
"$@" >| $cache &!
}
fpath+=($ZSH_SMARTCACHE_DIR)
}
smartcache() {
emulate -LR zsh -o extended_glob -o err_return
(( $+commands[base64] )) || base64 --help # trigger error
[[ -d $ZSH_SMARTCACHE_DIR ]] || mkdir -p $ZSH_SMARTCACHE_DIR
local subcmd=$1; shift
local id=${$(base64 <<< "$@")%%=#}
_smartcache-$subcmd $id "$@"
}