-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzsh-smartcache.plugin.zsh
51 lines (46 loc) · 1.44 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
40
41
42
43
44
45
46
47
48
49
50
51
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
zcompile $cache
} &!
} else {
source $cache
{
local output=$("$@")
[[ $output == "$(<$cache)" ]] && return
printf '%s' $output >| $cache
source $cache
zcompile $cache
print "Cache updated: '$@' (applied next time)"
} &!
}
}
_smartcache-comp() {
local cache=$ZSH_SMARTCACHE_DIR/_$1; shift
if [[ ! -f $cache ]] {
"$@" >| $cache
} else {
{
local output=$("$@")
[[ $output == "$(<$cache)" ]] && return
printf '%s' $output >| $cache
print "Cache updated: '$@' (applied next time)"
} &!
}
# fpath is set unique by default so it's OK to append multiple times.
fpath+=($ZSH_SMARTCACHE_DIR)
}
smartcache() {
emulate -LR zsh -o extended_glob -o err_return
# Doing these lately as users might change settings after plugin loading.
(( $+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 "$@"
}