refactor worker code

pull/445/head
romkatv 5 years ago
parent f1e9d9ade7
commit e01b5d9529

@ -1041,6 +1041,8 @@ _p9k_prompt_disk_usage_init() {
_p9k__disk_usage_normal \ _p9k__disk_usage_normal \
_p9k__disk_usage_warning \ _p9k__disk_usage_warning \
_p9k__disk_usage_critical _p9k__disk_usage_critical
_p9k__async_segments_compute+=_p9k_prompt_disk_usage_compute
} }
_p9k_prompt_disk_usage_compute() { _p9k_prompt_disk_usage_compute() {
@ -1049,12 +1051,16 @@ _p9k_prompt_disk_usage_compute() {
} }
_p9k_prompt_disk_usage_async() { _p9k_prompt_disk_usage_async() {
(( $+commands[df] )) && REPLY=${${=${(f)"$(df -P $1 2>/dev/null)"}[2]}[5]%%%} (( $+commands[df] )) || return
local disk_usage_pct
disk_usage_pct=${${=${(f)"$(df -P $1 2>/dev/null)"}[2]}[5]%%%}
[[ $disk_usage_pct == <0-100> ]] || return
typeset -p disk_usage_pct
} }
_p9k_prompt_disk_usage_sync() { _p9k_prompt_disk_usage_sync() {
[[ $REPLY == <0-100> && $REPLY != $_p9k__disk_usage_pct ]] || return [[ $disk_usage_pct != $_p9k__disk_usage_pct ]] || return
_p9k__disk_usage_pct=$REPLY _p9k__disk_usage_pct=$disk_usage_pct
_p9k__disk_usage_normal= _p9k__disk_usage_normal=
_p9k__disk_usage_warning= _p9k__disk_usage_warning=
_p9k__disk_usage_critical= _p9k__disk_usage_critical=
@ -1065,11 +1071,13 @@ _p9k_prompt_disk_usage_sync() {
elif (( ! _POWERLEVEL9K_DISK_USAGE_ONLY_WARNING )); then elif (( ! _POWERLEVEL9K_DISK_USAGE_ONLY_WARNING )); then
_p9k__disk_usage_normal=1 _p9k__disk_usage_normal=1
fi fi
_p9k_worker_send_params \ _p9k_worker_reply_begin
typeset -p \
_p9k__disk_usage_pct \ _p9k__disk_usage_pct \
_p9k__disk_usage_normal \ _p9k__disk_usage_normal \
_p9k__disk_usage_warning \ _p9k__disk_usage_warning \
_p9k__disk_usage_critical _p9k__disk_usage_critical
_p9k_worker_reply_end
} }
function _p9k_read_file() { function _p9k_read_file() {
@ -1240,6 +1248,8 @@ _p9k_prompt_public_ip_init() {
_p9k_prompt_public_ip_cond \ _p9k_prompt_public_ip_cond \
_p9k_prompt_public_ip_async \ _p9k_prompt_public_ip_async \
_p9k_prompt_public_ip_sync _p9k_prompt_public_ip_sync
_p9k__async_segments_compute+=_p9k_prompt_public_ip_compute
} }
_p9k_prompt_public_ip_compute() { _p9k_prompt_public_ip_compute() {
@ -1284,16 +1294,16 @@ _p9k_prompt_public_ip_async() {
break break
fi fi
done done
REPLY="$ip $next" typeset -p ip next
} }
_p9k_prompt_public_ip_sync() { _p9k_prompt_public_ip_sync() {
local ip_next=($=REPLY) _p9k__public_ip_next_time=$next
(( $#ip_next == 2 )) || return if [[ $ip != _p9k__public_ip ]]; then
_p9k__public_ip_next_time=$ip_next[2] _p9k__public_ip=$ip
if [[ $ip_next[1] != _p9k__public_ip ]]; then _p9k_worker_reply_begin
_p9k__public_ip=$ip_next[1] typeset -p _p9k__public_ip
_p9k_worker_send_params _p9k__public_ip _p9k_worker_reply_end
fi fi
} }
@ -1894,28 +1904,30 @@ _p9k_prompt_load_init() {
_p9k_worker_send_params \ _p9k_worker_send_params \
$_POWERLEVEL9K_LOAD_WHICH \ $_POWERLEVEL9K_LOAD_WHICH \
_p9k_os \
_p9k_num_cpus \ _p9k_num_cpus \
_p9k__load_value \ _p9k__load_value \
_p9k__load_normal \ _p9k__load_normal \
_p9k__load_warning \ _p9k__load_warning \
_p9k__load_critical _p9k__load_critical
_p9k__async_segments_compute+=_p9k_prompt_load_compute
} }
_p9k_prompt_load_compute() { _p9k_prompt_load_compute() {
[[ $_p9k_os == (OSX|BSD) ]] || return
_p9k_worker_invoke load '(( $+commands[sysctl] ))' _p9k_prompt_load_async _p9k_prompt_load_sync _p9k_worker_invoke load '(( $+commands[sysctl] ))' _p9k_prompt_load_async _p9k_prompt_load_sync
} }
_p9k_prompt_load_async() { _p9k_prompt_load_async() {
local load local load
load="$(sysctl -n vm.loadavg 2>/dev/null)" || return load="$(sysctl -n vm.loadavg 2>/dev/null)" || return
REPLY=${${(A)=load}[_POWERLEVEL9K_LOAD_WHICH+1]//,/.} load=${${(A)=load}[_POWERLEVEL9K_LOAD_WHICH+1]//,/.}
[[ $load == <->(|.<->) ]] || return
typeset -p load
} }
_p9k_prompt_load_sync() { _p9k_prompt_load_sync() {
[[ $REPLY == <->(|.<->) && $REPLY != $_p9k__load_value ]] || return [[ $load != $_p9k__load_value ]] || return
_p9k__load_value=$REPLY _p9k__load_value=$load
_p9k__load_normal= _p9k__load_normal=
_p9k__load_warning= _p9k__load_warning=
_p9k__load_critical= _p9k__load_critical=
@ -1927,11 +1939,14 @@ _p9k_prompt_load_sync() {
else else
_p9k__load_normal=1 _p9k__load_normal=1
fi fi
_p9k_worker_send_params \
_p9k__load_value \ _p9k_worker_reply_begin
_p9k__load_normal \ typeset -p \
_p9k__load_warning \ _p9k__load_value \
_p9k__load_normal \
_p9k__load_warning \
_p9k__load_critical _p9k__load_critical
_p9k_worker_reply_end
} }
function _p9k_cached_cmd_stdout() { function _p9k_cached_cmd_stdout() {
@ -2223,6 +2238,7 @@ function _p9k_prompt_ram_init() {
_p9k_human_readable_bytes \ _p9k_human_readable_bytes \
_p9k_prompt_ram_async \ _p9k_prompt_ram_async \
_p9k_prompt_ram_sync _p9k_prompt_ram_sync
_p9k__async_segments_compute+=_p9k_prompt_ram_compute
} }
function _p9k_prompt_ram_compute() { function _p9k_prompt_ram_compute() {
@ -2255,13 +2271,16 @@ _p9k_prompt_ram_async() {
esac esac
_p9k_human_readable_bytes $free_bytes _p9k_human_readable_bytes $free_bytes
REPLY=$_p9k_ret local ram_free=$_p9k_ret
typeset -p ram_free
} }
_p9k_prompt_ram_sync() { _p9k_prompt_ram_sync() {
[[ $_p9k__ram_free == $REPLY ]] && return [[ $ram_free == $_p9k__ram_free ]] && return
_p9k__ram_free=$REPLY _p9k__ram_free=$ram_free
_p9k_worker_send_params _p9k__ram_free _p9k_worker_reply_begin
typeset -p _p9k__ram_free
_p9k_worker_reply_end
} }
function _p9k_read_rbenv_version_file() { function _p9k_read_rbenv_version_file() {
@ -2759,13 +2778,18 @@ instant_prompt_time() {
_p9k_prompt_time_init() { _p9k_prompt_time_init() {
(( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )) || return (( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )) || return
_p9k_worker_send_functions _p9k_prompt_time_compute _p9k_worker_send_functions _p9k_prompt_time_sync
_p9k__async_segments_compute+=_p9k_prompt_time_compute
} }
_p9k_prompt_time_compute() { _p9k_prompt_time_compute() {
(( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )) || return _p9k_worker_invoke 'time' '' 'sleep 1 || true' _p9k_prompt_time_sync
_p9k_worker_invoke \ }
'time' '(( _p9k_worker_runs_me ))' 'sleep 1' '_p9k_worker_reply _p9k_prompt_time_compute'
_p9k_prompt_time_sync() {
_p9k_worker_reply_begin
echo _p9k_prompt_time_compute
_p9k_worker_reply_end
} }
################################################################ ################################################################
@ -3963,6 +3987,7 @@ function _p9k_prompt_net_iface_init() {
_p9k_prompt_net_iface_match \ _p9k_prompt_net_iface_match \
_p9k_prompt_net_iface_async \ _p9k_prompt_net_iface_async \
_p9k_prompt_net_iface_sync _p9k_prompt_net_iface_sync
_p9k__async_segments_compute+=_p9k_prompt_net_iface_compute
} }
function _p9k_prompt_net_iface_compute() { function _p9k_prompt_net_iface_compute() {
@ -4003,29 +4028,33 @@ function _p9k_prompt_net_iface_async() {
done done
fi fi
local params
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE; then if _p9k_prompt_net_iface_match $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE; then
REPLY+="typeset -g _p9k__public_ip_vpn=1"$'\n' params+="typeset -g _p9k__public_ip_vpn=1"$'\n'
REPLY+="typeset -g _p9k__public_ip_not_vpn="$'\n' params+="typeset -g _p9k__public_ip_not_vpn="$'\n'
else else
REPLY+="typeset -g _p9k__public_ip_vpn="$'\n' params+="typeset -g _p9k__public_ip_vpn="$'\n'
REPLY+="typeset -g _p9k__public_ip_not_vpn=1"$'\n' params+="typeset -g _p9k__public_ip_not_vpn=1"$'\n'
fi fi
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_IP_INTERFACE; then if _p9k_prompt_net_iface_match $_POWERLEVEL9K_IP_INTERFACE; then
REPLY+="typeset -g _p9k__ip_ip=$ip"$'\n' params+="typeset -g _p9k__ip_ip=$ip"$'\n'
else else
REPLY+="typeset -g _p9k__ip_ip="$'\n' params+="typeset -g _p9k__ip_ip="$'\n'
fi fi
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_VPN_IP_INTERFACE; then if _p9k_prompt_net_iface_match $_POWERLEVEL9K_VPN_IP_INTERFACE; then
REPLY+="typeset -g _p9k__vpn_ip_ip=$ip"$'\n' params+="typeset -g _p9k__vpn_ip_ip=$ip"$'\n'
else else
REPLY+="typeset -g _p9k__vpn_ip_ip="$'\n' params+="typeset -g _p9k__vpn_ip_ip="$'\n'
fi fi
typeset -p params
} }
function _p9k_prompt_net_iface_sync() { function _p9k_prompt_net_iface_sync() {
[[ -n $REPLY && $REPLY != $_p9k__net_iface_last ]] || return [[ $params == $_p9k__net_iface_params ]] && return
_p9k__net_iface_last=$REPLY _p9k__net_iface_params=$params
_p9k_worker_reply $REPLY _p9k_worker_reply_begin
print -rn -- $params
_p9k_worker_reply_end
} }
function _p9k_build_segment() { function _p9k_build_segment() {
@ -4060,8 +4089,8 @@ function _p9k_set_prompt() {
IFS=$' \t\n\0' IFS=$' \t\n\0'
if [[ $_p9k_refresh_reason == precmd ]]; then if [[ $_p9k_refresh_reason == precmd ]]; then
local f_init f_compute local f_compute
for f_init f_compute in "${_p9k_async_segments[@]}"; do for f_compute in "${_p9k__async_segments_compute[@]}"; do
$f_compute $f_compute
done done
fi fi
@ -5053,7 +5082,8 @@ _p9k_init_vars() {
typeset -g _p9k__last_prompt_pwd typeset -g _p9k__last_prompt_pwd
typeset -gA _p9k_display_k typeset -gA _p9k_display_k
typeset -ga _p9k__display_v typeset -ga _p9k__display_v
typeset -ga _p9k_async_segments typeset -ga _p9k_async_segments_init
typeset -ga _p9k__async_segments_compute
typeset -gA _p9k__dotnet_stat_cache typeset -gA _p9k__dotnet_stat_cache
typeset -gA _p9k__dir_stat_cache typeset -gA _p9k__dir_stat_cache
@ -6337,13 +6367,8 @@ function _p9k_init_cacheable() {
segments+=net_iface segments+=net_iface
fi fi
for elem in $segments; do for elem in $segments; do
[[ $elem == time && $_POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME == 0 ]] && continue
local f_init=_p9k_prompt_${elem}_init local f_init=_p9k_prompt_${elem}_init
local f_compute=_p9k_prompt_${elem}_compute (( $+functions[$f_init] )) && _p9k_async_segments_init+=$f_init
if (( $+functions[$f_init] || $+functions[$f_compute] )); then
(( $+functions[$f_init] )) && _p9k_async_segments+="$f_init" || _p9k_async_segments+=''
(( $+functions[$f_compute] )) && _p9k_async_segments+="$f_compute" || _p9k_async_segments+=''
fi
done done
if [[ $_POWERLEVEL9K_TRANSIENT_PROMPT != off ]]; then if [[ $_POWERLEVEL9K_TRANSIENT_PROMPT != off ]]; then
@ -6587,13 +6612,12 @@ _p9k_init() {
_p9k_init_vars _p9k_init_vars
_p9k_restore_state || _p9k_init_cacheable _p9k_restore_state || _p9k_init_cacheable
if (( $#_p9k_async_segments )); then local f_init
_p9k_worker_start for f_init in $_p9k_async_segments_init; do
local f_init f_compute $f_init
for f_init f_compute in "${_p9k_async_segments[@]}"; do done
$f_init
done (( $#_p9k__async_segments_compute )) && _p9k_worker_start
fi
local k v local k v
for k v in ${(kv)_p9k_display_k}; do for k v in ${(kv)_p9k_display_k}; do

@ -7,9 +7,12 @@ function _p9k_worker_main() {
zmodload zsh/zselect || return zmodload zsh/zselect || return
! { zselect -t0 || (( $? != 1 )) } || return ! { zselect -t0 || (( $? != 1 )) } || return
function _p9k_worker_reply_begin() { print e }
function _p9k_worker_reply_end() { print -n -- '\x1e' }
typeset -g IFS=$' \t\n\0' typeset -g IFS=$' \t\n\0'
local req fd local req fd buf
local -a ready local -a ready
local -A inflight # fd => id$'\x1f'sync local -A inflight # fd => id$'\x1f'sync
local -ri _p9k_worker_runs_me=1 local -ri _p9k_worker_runs_me=1
@ -19,7 +22,7 @@ function _p9k_worker_main() {
[[ $ready[1] == -r ]] || return [[ $ready[1] == -r ]] || return
for fd in ${ready:1}; do for fd in ${ready:1}; do
if [[ $fd == 0 ]]; then if [[ $fd == 0 ]]; then
local buf= buf=
while true; do while true; do
sysread -t 0 'buf[$#buf+1]' && continue sysread -t 0 'buf[$#buf+1]' && continue
(( $? == 4 )) || return (( $? == 4 )) || return
@ -31,11 +34,10 @@ function _p9k_worker_main() {
if () { eval $parts[2] }; then if () { eval $parts[2] }; then
if [[ -n $parts[3] ]]; then if [[ -n $parts[3] ]]; then
sysopen -r -o cloexec -u fd <( sysopen -r -o cloexec -u fd <(
local REPLY=; eval $parts[3]; print -rn -- $REPLY) || return () { eval $parts[3]; } && print -n '\x1e') || return
inflight[$fd]=$parts[1]$'\x1f'$parts[4] inflight[$fd]=$parts[1]$'\x1f'$parts[4]
continue continue
fi fi
local REPLY=
() { eval $parts[4] } () { eval $parts[4] }
fi fi
if [[ -n $parts[1] ]]; then if [[ -n $parts[1] ]]; then
@ -43,14 +45,16 @@ function _p9k_worker_main() {
fi fi
done done
else else
local REPLY= buf=
while true; do while true; do
sysread -i $fd 'REPLY[$#REPLY+1]' && continue sysread -i $fd 'buf[$#buf+1]' && continue
(( $? == 5 )) || return (( $? == 5 )) || return
break break
done done
local parts=("${(@ps:\x1f:)inflight[$fd]}") # id sync local parts=("${(@ps:\x1f:)inflight[$fd]}") # id sync
() { eval $parts[2] } if [[ $buf == *$'\x1e' ]]; then
() { eval ${buf[1,-2]}$'\n'$parts[2] }
fi
if [[ -n $parts[1] ]]; then if [[ -n $parts[1] ]]; then
print -rn -- d$parts[1]$'\x1e' || return print -rn -- d$parts[1]$'\x1e' || return
fi fi
@ -64,51 +68,48 @@ function _p9k_worker_main() {
} }
} }
typeset -g _p9k__worker_pid typeset -g _p9k__worker_pid
typeset -g _p9k__worker_req_fd typeset -g _p9k__worker_req_fd
typeset -g _p9k__worker_resp_fd typeset -g _p9k__worker_resp_fd
typeset -g _p9k__worker_shell_pid typeset -g _p9k__worker_shell_pid
typeset -g _p9k__worker_file_prefix typeset -g _p9k__worker_file_prefix
typeset -ga _p9k__worker_params typeset -gaU _p9k__worker_params
typeset -gA _p9k__worker_request_map typeset -gaU _p9k__worker_functions
typeset -ga _p9k__worker_request_queue typeset -gA _p9k__worker_request_map
typeset -ga _p9k__worker_request_queue
# invoked in master: _p9k_worker_reply <list>...
# function _p9k_worker_print_params() {
# worker also has _p9k_worker_reply but its implemented as _p9k_worker_reply_remote local names=(${@:/(#m)*/${${${+parameters[$MATCH]}:#0}:+$MATCH}})
function _p9k_worker_reply() { eval $1 } (( ! $#names )) && return
print -n -- '\x1f' && typeset -p -- $names && print -n -- '\x1f\x1f\x1e'
}
# invoked in worker where it's called _p9k_worker_reply function _p9k_worker_print_functions() {
# usage: _p9k_worker_reply <list> local names=(${@:/(#m)*/${${${+functions[$MATCH]}:#0}:+$MATCH}})
function _p9k_worker_reply_remote() { print -rn -- e$1$'\x1e' } (( ! $#names )) && return
print -n -- '\x1f' && functions -- $names && print -n -- '\x1f\x1f\x1e'
}
# invoked in master: _p9k_worker_send_params [param]... # invoked in master: _p9k_worker_send_params [param]...
function _p9k_worker_send_params() { function _p9k_worker_send_params() {
[[ -z $_p9k__worker_resp_fd || $# == 0 ]] && return
if [[ -n $_p9k__worker_req_fd ]]; then if [[ -n $_p9k__worker_req_fd ]]; then
{ _p9k_worker_print_params ${(u)@} >&$_p9k__worker_req_fd && return
print -rn -- $'\x1f' && typeset -pm -- ${(j.|.)${(b)@}} && print -rn -- $'\x1f\x1f\x1e' && return
} >&$_p9k__worker_req_fd
_p9k_worker_stop _p9k_worker_stop
return 1 return 1
else else
_p9k__worker_params+=($*) _p9k__worker_params+=($@)
fi fi
} }
function _p9k_worker_send_params_remote() {
[[ $# == 0 ]] && return
print -rn -- e && typeset -pm -- ${(j.|.)${(b)@}} && print -rn -- $'\x1e'
}
# invoked in master: _p9k_worker_send_functions [function-name]... # invoked in master: _p9k_worker_send_functions [function-name]...
function _p9k_worker_send_functions() { function _p9k_worker_send_functions() {
[[ -z $_p9k__worker_resp_fd || $# == 0 ]] && return if [[ -n $_p9k__worker_req_fd ]]; then
local func req _p9k_worker_print_functions ${(u)@} >&$_p9k__worker_req_fd && return
for func; do _p9k_worker_stop
req+="function $func() { $functions[$func] }"$'\n' return 1
done else
_p9k_worker_invoke "" "$req" "" "" _p9k__worker_functions+=($@)
fi
} }
# invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync> # invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync>
@ -155,6 +156,7 @@ function _p9k_worker_stop() {
_p9k__worker_resp_fd= _p9k__worker_resp_fd=
_p9k__worker_shell_pid= _p9k__worker_shell_pid=
_p9k__worker_params=() _p9k__worker_params=()
_p9k__worker_functions=()
_p9k__worker_request_map=() _p9k__worker_request_map=()
_p9k__worker_request_queue=() _p9k__worker_request_queue=()
return 0 return 0
@ -215,17 +217,12 @@ function _p9k_worker_receive() {
zmodload -F zsh/net/socket b:zsocket zmodload -F zsh/net/socket b:zsocket
zmodload -F zsh/files b:zf_mv b:zf_rm zmodload -F zsh/files b:zf_mv b:zf_rm
autoload -Uz is-at-least autoload -Uz is-at-least
function _p9k_worker_main() { $functions[_p9k_worker_main] } () { $functions[_p9k_worker_main] }"
function _p9k_worker_reply() { $functions[_p9k_worker_reply_remote] }
function _p9k_worker_send_params() { $functions[_p9k_worker_send_params_remote] }
_p9k_worker_main"
print -r -- ${init//$'\n'/$'\x1e'} || return print -r -- ${init//$'\n'/$'\x1e'} || return
if (( $#_p9k__worker_params )); then _p9k_worker_print_params $_p9k__worker_params || return
print -rn -- $'\x1f' || return _p9k_worker_print_functions $_p9k__worker_functions || return
typeset -pm -- ${(j.|.)${(b)_p9k__worker_params}} || return _p9k__worker_params=()
print -rn -- $'\x1f\x1f\x1e' || return _p9k__worker_functions()
_p9k__worker_params=()
fi
local req= local req=
for req in $_p9k__worker_request_queue; do for req in $_p9k__worker_request_queue; do
if [[ $req != *$'\x1e' ]]; then if [[ $req != *$'\x1e' ]]; then
@ -237,7 +234,6 @@ function _p9k_worker_receive() {
done done
_p9k__worker_request_queue=() _p9k__worker_request_queue=()
} >&$_p9k__worker_req_fd } >&$_p9k__worker_req_fd
echo shit sent >>/tmp/log
;; ;;
*) *)
return 1 return 1

@ -1,10 +1,3 @@
- assume worker works; drop support for synchronous evaluation of workerized prompts.
- call *_init methods from init_cacheable. their only purpose is to add stuff to two arrays:
_p9k_worker_functions and _p9k_worker_params. both arrays are cacheable. move all _p9__* param
initialization from *_init methods to _p9k_init_vars
- inside worker, eval stdout from async
- add two methods for the use inside *_sync: _p9k_worker_reply_begin and _p9k_worker_reply_end.
they simply print 'e' and $'\x1e'. *_sync can send params with `typeset -p` now.
- declare local _p9k_worker_reset in _p9k_worker_receive. it can be set when replies from worker - declare local _p9k_worker_reset in _p9k_worker_receive. it can be set when replies from worker
are eval'd. initial value 0. 1 means reset-prompt is needed. 2 for _p9k_set_prompt + reset-prompt. are eval'd. initial value 0. 1 means reset-prompt is needed. 2 for _p9k_set_prompt + reset-prompt.
- battery: when state or icon change, update with _p9k_set_prompt. make sure icons can be sent with - battery: when state or icon change, update with _p9k_set_prompt. make sure icons can be sent with

Loading…
Cancel
Save