1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-11 00:00:06 +00:00

add two-level fs cache and use it everywhere instead of the one-level cache

This commit is contained in:
romkatv 2019-10-09 19:57:18 +02:00
parent 7f3637ee0d
commit ab160b49f5

View file

@ -269,6 +269,63 @@ _p9k_cache_get() {
[[ -n $v ]] && _p9k_cache_val=("${(@0)${v[1,-2]}}") [[ -n $v ]] && _p9k_cache_val=("${(@0)${v[1,-2]}}")
} }
_p9k_cache_stat_get() {
local -H stat
local label=$1 f
shift
_p9k_cache_stat_meta=
_p9k_cache_stat_fprint=
for f; do
if zstat -H stat -- $f 2>/dev/null; then
_p9k_cache_stat_meta+="${(q)f} $stat[inode] $stat[mtime] $stat[size] $stat[mode]; "
fi
done
if _p9k_cache_get $0 $label meta "$@" && [[ $_p9k_cache_val[1] == $_p9k_cache_stat_meta ]]; then
_p9k_cache_stat_fprint=$_p9k_cache_val[2]
local -a key=($0 $label fprint "$@" "$_p9k_cache_stat_fprint")
_p9k_cache_fprint_key="${(pj:\0:)key}"
shift 2 _p9k_cache_val
return
fi
if (( $+commands[md5] )); then
local -a md5=(md5 -q)
elif (( $+commands[md5sum] )); then
local -a md5=(md5sum -b)
else
return 1
fi
local fprint
for f; do
if fprint="$($md5 $f 2>/dev/null)"; then
_p9k_cache_stat_fprint+="${(q)fprint} "
fi
done
local meta_key=$_p9k_cache_key
if _p9k_cache_get $0 $label fprint "$@" "$_p9k_cache_stat_fprint"; then
_p9k_cache_fprint_key=$_p9k_cache_key
_p9k_cache_key=$meta_key
_p9k_cache_set "$_p9k_cache_stat_meta" "$_p9k_cache_stat_fprint" "$_p9k_cache_val[@]"
shift 2 _p9k_cache_val
return
fi
_p9k_cache_fprint_key=$_p9k_cache_key
_p9k_cache_key=$meta_key
return 1
}
_p9k_cache_stat_set() {
_p9k_cache_set "$_p9k_cache_stat_meta" "$_p9k_cache_stat_fprint" "$@"
_p9k_cache_key=$_p9k_cache_fprint_key
_p9k_cache_set "$@"
}
# _p9k_param prompt_foo_BAR BACKGROUND red # _p9k_param prompt_foo_BAR BACKGROUND red
_p9k_param() { _p9k_param() {
local key="_p9k_param ${(pj:\0:)*}" local key="_p9k_param ${(pj:\0:)*}"
@ -895,17 +952,14 @@ prompt_aws_eb_env() {
dir=${dir:h} dir=${dir:h}
done done
local -H stat if ! _p9k_cache_stat_get $0 $dir/.elasticbeanstalk/config.yml; then
zstat -H stat -- $dir/.elasticbeanstalk/config.yml 2>/dev/null || return
local sig="$stat[inode].$stat[mtime].$stat[size].$stat[mode]"
if ! _p9k_cache_get $0 $dir || [[ $_p9k_cache_val[1] != $sig ]]; then
local env local env
env="$(command eb list 2>/dev/null)" || env= env="$(command eb list 2>/dev/null)" || env=
env="${${(@M)${(@f)env}:#\* *}#\* }" env="${${(@M)${(@f)env}:#\* *}#\* }"
_p9k_cache_set "$sig" "$env" _p9k_cache_stat_set "$env"
fi fi
[[ -n $_p9k_cache_val[2] ]] || return [[ -n $_p9k_cache_val[1] ]] || return
_p9k_prompt_segment "$0" black green 'AWS_EB_ICON' 0 '' "${_p9k_cache_val[2]//\%/%%}" _p9k_prompt_segment "$0" black green 'AWS_EB_ICON' 0 '' "${_p9k_cache_val[1]//\%/%%}"
} }
################################################################ ################################################################
@ -1294,12 +1348,10 @@ prompt_dir() {
for pat in $_POWERLEVEL9K_DIR_PACKAGE_FILES; do for pat in $_POWERLEVEL9K_DIR_PACKAGE_FILES; do
for pkg_file in $markers; do for pkg_file in $markers; do
[[ $pkg_file == $dir/${~pat} ]] || continue [[ $pkg_file == $dir/${~pat} ]] || continue
local -H stat=() if ! _p9k_cache_stat_get $0_pkg $pkg_file; then
zstat -H stat -- $pkg_file 2>/dev/null || continue
if ! _p9k_cache_get $0_pkg $stat[inode] $stat[mtime] $stat[size]; then
local pkg_name='' local pkg_name=''
pkg_name="$(jq -j '.name | select(. != null)' <$pkg_file 2>/dev/null)" || pkg_name='' pkg_name="$(jq -j '.name | select(. != null)' <$pkg_file 2>/dev/null)" || pkg_name=''
_p9k_cache_set "$pkg_name" _p9k_cache_stat_set "$pkg_name"
fi fi
[[ -n $_p9k_cache_val[1] ]] || continue [[ -n $_p9k_cache_val[1] ]] || continue
parts[1,i]=($_p9k_cache_val[1]) parts[1,i]=($_p9k_cache_val[1])
@ -1667,12 +1719,10 @@ function _p9k_cached_cmd_stdout() {
local cmd=${commands[$1]:A} local cmd=${commands[$1]:A}
[[ -n $cmd ]] || return [[ -n $cmd ]] || return
shift shift
local -H stat if ! _p9k_cache_stat_get $0" ${(q)*}" $cmd; then
zstat -H stat -- $cmd 2>/dev/null || return
if ! _p9k_cache_get $0 $stat[inode] $stat[mtime] $stat[size] $stat[mode] $cmd "$@"; then
local out local out
out="$($cmd "$@" 2>/dev/null)" out="$($cmd "$@" 2>/dev/null)"
_p9k_cache_set $(( ! $? )) "$out" _p9k_cache_stat_set $(( ! $? )) "$out"
fi fi
(( $_p9k_cache_val[1] )) || return (( $_p9k_cache_val[1] )) || return
_p9k_ret=$_p9k_cache_val[2] _p9k_ret=$_p9k_cache_val[2]
@ -1682,12 +1732,10 @@ function _p9k_cached_cmd_stdout_stderr() {
local cmd=${commands[$1]:A} local cmd=${commands[$1]:A}
[[ -n $cmd ]] || return [[ -n $cmd ]] || return
shift shift
local -H stat if ! _p9k_cache_stat_get $0" ${(q)*}" $cmd; then
zstat -H stat -- $cmd 2>/dev/null || return
if ! _p9k_cache_get $0 $stat[inode] $stat[mtime] $stat[size] $stat[mode] $cmd "$@"; then
local out local out
out="$($cmd "$@" 2>&1)" # this line is the only diff with _p9k_cached_cmd_stdout out="$($cmd "$@" 2>&1)" # this line is the only diff with _p9k_cached_cmd_stdout
_p9k_cache_set $(( ! $? )) "$out" _p9k_cache_stat_set $(( ! $? )) "$out"
fi fi
(( $_p9k_cache_val[1] )) || return (( $_p9k_cache_val[1] )) || return
_p9k_ret=$_p9k_cache_val[2] _p9k_ret=$_p9k_cache_val[2]
@ -2232,14 +2280,12 @@ prompt_date() {
prompt_todo() { prompt_todo() {
local todo=$commands[todo.sh] local todo=$commands[todo.sh]
[[ -n $todo && -r $_p9k_todo_file ]] || return [[ -n $todo && -r $_p9k_todo_file ]] || return
local -H stat if ! _p9k_cache_stat_get $0 $_p9k_todo_file; then
zstat -H stat -- $_p9k_todo_file 2>/dev/null || return
if ! _p9k_cache_get $0 $stat[inode] $stat[mtime] $stat[size]; then
local count="$($todo -p ls | tail -1)" local count="$($todo -p ls | tail -1)"
if [[ $count == (#b)'TODO: '[[:digit:]]##' of '([[:digit:]]##)' '* ]]; then if [[ $count == (#b)'TODO: '[[:digit:]]##' of '([[:digit:]]##)' '* ]]; then
_p9k_cache_set 1 $match[1] _p9k_cache_stat_set 1 $match[1]
else else
_p9k_cache_set 0 0 _p9k_cache_stat_set 0 0
fi fi
fi fi
(( $_p9k_cache_val[1] )) || return (( $_p9k_cache_val[1] )) || return
@ -2944,15 +2990,7 @@ prompt_dir_writable() {
prompt_kubecontext() { prompt_kubecontext() {
(( $+commands[kubectl] )) || return (( $+commands[kubectl] )) || return
local cfg if ! _p9k_cache_stat_get $0 ${(s.:.)${KUBECONFIG:-$HOME/.kube/config}}; then
local -a key
for cfg in ${(s.:.)${KUBECONFIG:-$HOME/.kube/config}}; do
local -H stat
zstat -H stat -- $cfg 2>/dev/null || continue
key+=($cfg $stat[inode] $stat[mtime] $stat[size] $stat[mode])
done
if ! _p9k_cache_get $0 "${key[@]}"; then
local name namespace cluster cloud_name cloud_account cloud_zone cloud_cluster text state local name namespace cluster cloud_name cloud_account cloud_zone cloud_cluster text state
() { () {
local cfg && cfg=(${(f)"$(kubectl config view -o=yaml 2>/dev/null)"}) || return local cfg && cfg=(${(f)"$(kubectl config view -o=yaml 2>/dev/null)"}) || return
@ -3012,7 +3050,7 @@ prompt_kubecontext() {
fi fi
done done
fi fi
_p9k_cache_set "$name" "$namespace" "$cluster" "$cloud_name" "$cloud_account" "$cloud_zone" "$cloud_cluster" "$text" "$state" _p9k_cache_stat_set "$name" "$namespace" "$cluster" "$cloud_name" "$cloud_account" "$cloud_zone" "$cloud_cluster" "$text" "$state"
fi fi
typeset -g P9K_KUBECONTEXT_NAME=$_p9k_cache_val[1] typeset -g P9K_KUBECONTEXT_NAME=$_p9k_cache_val[1]
@ -3056,16 +3094,13 @@ prompt_java_version() {
prompt_azure() { prompt_azure() {
(( $+commands[az] )) || return (( $+commands[az] )) || return
local -H stat if ! _p9k_cache_stat_get $0 ${AZURE_CONFIG_DIR:-$HOME/.azure}/azureProfile.json; then
zstat -H stat -- ${AZURE_CONFIG_DIR:-$HOME/.azure}/azureProfile.json 2>/dev/null || return
local sig="$stat[inode].$stat[mtime].$stat[size].$stat[mode]"
if ! _p9k_cache_get $0 || [[ $_p9k_cache_val[1] != $sig ]]; then
local name local name
name="$(az account show --query name --output tsv 2>/dev/null)" || name= name="$(az account show --query name --output tsv 2>/dev/null)" || name=
_p9k_cache_set "$sig" "$name" _p9k_cache_stat_set "$name"
fi fi
[[ -n $_p9k_cache_val[2] ]] || return [[ -n $_p9k_cache_val[1] ]] || return
_p9k_prompt_segment "$0" "blue" "white" "AZURE_ICON" 0 '' "${_p9k_cache_val[2]//\%/%%}" _p9k_prompt_segment "$0" "blue" "white" "AZURE_ICON" 0 '' "${_p9k_cache_val[1]//\%/%%}"
} }
typeset -gra __p9k_nordvpn_tag=( typeset -gra __p9k_nordvpn_tag=(
@ -3400,7 +3435,7 @@ function _p9k_dump_state() {
sysopen -a -m 600 -o creat,trunc -u fd $tmp || return sysopen -a -m 600 -o creat,trunc -u fd $tmp || return
{ {
local include='_POWERLEVEL9K_*|_p9k_*|icons|OS|DEFAULT_COLOR|DEFAULT_COLOR_INVERTED' local include='_POWERLEVEL9K_*|_p9k_*|icons|OS|DEFAULT_COLOR|DEFAULT_COLOR_INVERTED'
local exclude='_p9k_gitstatus_*|_p9k_param_sig|_p9k_public_ip|_p9k_prompt|_p9k_prompt_idx|_p9k_dump_pid|_p9k_dump_scheduled|_p9k_async_pump_*' local exclude='_p9k_gitstatus_*|_p9k_cache_stat_meta|_p9k_cache_stat_fprint|_p9k_cache_fprint_key|_p9k_param_sig|_p9k_public_ip|_p9k_prompt|_p9k_prompt_idx|_p9k_dump_pid|_p9k_dump_scheduled|_p9k_async_pump_*'
typeset -g __p9k_cached_param_sig=$_p9k_param_sig typeset -g __p9k_cached_param_sig=$_p9k_param_sig
typeset -p __p9k_cached_param_sig >&$fd || return typeset -p __p9k_cached_param_sig >&$fd || return
unset __p9k_cached_param_sig unset __p9k_cached_param_sig
@ -3731,6 +3766,9 @@ _p9k_init_vars() {
typeset -g _p9k_ret typeset -g _p9k_ret
typeset -g _p9k_cache_key typeset -g _p9k_cache_key
typeset -ga _p9k_cache_val typeset -ga _p9k_cache_val
typeset -g _p9k_cache_stat_meta
typeset -g _p9k_cache_stat_fprint
typeset -g _p9k_cache_fprint_key
typeset -gA _p9k_cache typeset -gA _p9k_cache
typeset -ga _p9k_t typeset -ga _p9k_t
typeset -g _p9k_n typeset -g _p9k_n