migrate over from the old worker api; use worker in battery on macOS

pull/445/head
romkatv 5 years ago
parent 148ecf7fc3
commit 565492b10e

@ -1030,32 +1030,17 @@ _p9k_prompt_disk_usage_init() {
typeset -g _p9k__disk_usage_normal=
typeset -g _p9k__disk_usage_warning=
typeset -g _p9k__disk_usage_critical=
_p9k_worker_send_params \
_POWERLEVEL9K_DISK_USAGE_CRITICAL_LEVEL \
_POWERLEVEL9K_DISK_USAGE_WARNING_LEVEL \
_POWERLEVEL9K_DISK_USAGE_ONLY_WARNING \
_p9k__disk_usage_pct \
_p9k__disk_usage_normal \
_p9k__disk_usage_warning \
_p9k__disk_usage_critical
_p9k__async_segments_compute+='_p9k_prompt_disk_usage_compute ${(q)_p9k__pwd_a}'
}
_p9k_prompt_disk_usage_compute() {
_p9k_worker_invoke \
disk_usage \
_p9k_prompt_disk_usage_cond \
"_p9k_prompt_disk_usage_async ${(q)_p9k__pwd_a}" \
_p9k_prompt_disk_usage_sync
}
_p9k_prompt_disk_usage_cond() {
(( $+commands[df] ))
(( $+commands[df] )) || return
_p9k_worker_async "_p9k_prompt_disk_usage_async ${(q)1}" _p9k_prompt_disk_usage_sync
}
_p9k_prompt_disk_usage_async() {
local pct=${${=${(f)"$(df -P $1 2>/dev/null)"}[2]}[5]%%%}
[[ $pct == <0-100> && pct != $_p9k__disk_usage_pct ]] || return
[[ $pct == <0-100> && $pct != $_p9k__disk_usage_pct ]] || return
_p9k__disk_usage_pct=$pct
_p9k__disk_usage_normal=
_p9k__disk_usage_warning=
@ -1072,6 +1057,12 @@ _p9k_prompt_disk_usage_async() {
_p9k__disk_usage_normal \
_p9k__disk_usage_warning \
_p9k__disk_usage_critical
echo -E - 'reset=1'
}
_p9k_prompt_disk_usage_sync() {
eval $REPLY
_p9k_worker_reply $REPLY
}
function _p9k_read_file() {
@ -1098,6 +1089,35 @@ prompt_fvm() {
################################################################
# Segment that displays the battery status in levels and colors
prompt_battery() {
[[ $_p9k_os == (Linux|Android) ]] && _p9k_prompt_battery_set_args
(( $#_p9k__battery_args )) && _p9k_prompt_segment "${_p9k__battery_args[@]}"
}
_p9k_prompt_battery_init() {
typeset -ga _p9k__battery_args=()
[[ $_p9k_os == OSX ]] && _p9k__async_segments_compute+=_p9k_prompt_battery_compute
}
_p9k_prompt_battery_compute() {
_p9k_worker_async _p9k_prompt_battery_async _p9k_prompt_battery_sync
}
_p9k_prompt_battery_async() {
local prev="${(pj:\0:)_p9k__battery_args}"
_p9k_prompt_battery_set_args
[[ "${(pj:\0:)_p9k__battery_args}" == $prev ]] && return 1
typeset -p _p9k__battery_args
echo -E - 'reset=2'
}
_p9k_prompt_battery_sync() {
eval $REPLY
_p9k_worker_reply $REPLY
}
_p9k_prompt_battery_set_args() {
_p9k__battery_args=()
local state remain
local -i bat_percent
@ -1212,7 +1232,7 @@ prompt_battery() {
fg=$_POWERLEVEL9K_BATTERY_LEVEL_FOREGROUND[idx]
fi
_p9k_prompt_segment $0_$state "$bg" "$fg" $icon 0 '' $msg
_p9k__battery_args=(prompt_battery_$state "$bg" "$fg" $icon 0 '' $msg)
}
################################################################
@ -1230,17 +1250,12 @@ prompt_public_ip() {
_p9k_prompt_public_ip_init() {
typeset -g _p9k__public_ip=
typeset -gF _p9k__public_ip_next_time=0
_p9k_worker_send_params \
_POWERLEVEL9K_PUBLIC_IP_METHODS \
_POWERLEVEL9K_PUBLIC_IP_TIMEOUT \
_POWERLEVEL9K_PUBLIC_IP_HOST \
_p9k__public_ip \
_p9k__public_ip_next_time
_p9k__async_segments_compute+=_p9k_prompt_public_ip_compute
}
_p9k_prompt_public_ip_cond() {
(( EPOCHREALTIME >= _p9k__public_ip_next_time ))
_p9k_prompt_public_ip_compute() {
(( EPOCHREALTIME >= _p9k__public_ip_next_time )) || return
_p9k_worker_async _p9k_prompt_public_ip_async _p9k_prompt_public_ip_sync
}
_p9k_prompt_public_ip_async() {
@ -1276,17 +1291,17 @@ _p9k_prompt_public_ip_async() {
break
fi
done
_p9k__public_ip=$ip
_p9k__public_ip_next_time=$next
typeset -p _p9k__public_ip _p9k__public_ip_next_time
typeset -p _p9k__public_ip_next_time
[[ $_p9k__public_ip == $ip ]] && return
_p9k__public_ip=$ip
typeset -p _p9k__public_ip
echo -E - 'reset=1'
}
_p9k_prompt_public_ip_sync() {
[[ $prev == $cur ]] && return
local prev_ip=$_p9k__public_ip
eval $cur
[[ $_p9k__public_ip == $prev_ip ]] && return
_p9k_worker_reply $cur
eval $REPLY
_p9k_worker_reply $REPLY
}
################################################################
@ -1908,17 +1923,12 @@ _p9k_prompt_load_init() {
typeset -g _p9k__load_normal=
typeset -g _p9k__load_warning=
typeset -g _p9k__load_critical=
_p9k_worker_send_params \
$_POWERLEVEL9K_LOAD_WHICH \
_p9k_num_cpus \
_p9k__load_value \
_p9k__load_normal \
_p9k__load_warning \
_p9k__load_critical
_p9k__async_segments_compute+=_p9k_prompt_load_compute
}
_p9k_prompt_load_cond() {
(( $+commands[sysctl] ))
_p9k_prompt_load_compute() {
(( $+commands[sysctl] )) || return
_p9k_worker_async _p9k_prompt_load_async _p9k_prompt_load_sync
}
_p9k_prompt_load_async() {
@ -1942,6 +1952,12 @@ _p9k_prompt_load_async() {
_p9k__load_normal \
_p9k__load_warning \
_p9k__load_critical
echo -E - 'reset=1'
}
_p9k_prompt_load_sync() {
eval $REPLY
_p9k_worker_reply $REPLY
}
function _p9k_cached_cmd_stdout() {
@ -2228,8 +2244,11 @@ prompt_ram() {
function _p9k_prompt_ram_init() {
typeset -g _p9k__ram_free=
_p9k_worker_send_params _p9k_os _p9k__ram_free __p9k_byte_suffix
_p9k_worker_send_functions _p9k_human_readable_bytes
_p9k__async_segments_compute+=_p9k_prompt_ram_compute
}
_p9k_prompt_ram_compute() {
_p9k_worker_async _p9k_prompt_ram_async _p9k_prompt_ram_sync
}
_p9k_prompt_ram_async() {
@ -2261,6 +2280,12 @@ _p9k_prompt_ram_async() {
[[ $_p9k_ret != $_p9k__ram_free ]] || return
_p9k__ram_free=$_p9k_ret
typeset -p _p9k__ram_free
echo -E - 'reset=1'
}
_p9k_prompt_ram_sync() {
eval $REPLY
_p9k_worker_reply $REPLY
}
function _p9k_read_rbenv_version_file() {
@ -2758,16 +2783,19 @@ instant_prompt_time() {
_p9k_prompt_time_init() {
(( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )) || return
_p9k_worker_send_functions _p9k_prompt_time_sync
_p9k__async_segments_compute+=_p9k_prompt_time_compute
}
_p9k_prompt_time_compute() {
_p9k_worker_async _p9k_prompt_time_async _p9k_prompt_time_sync
}
_p9k_prompt_time_async() {
sleep 1 || true
}
_p9k_prompt_time_sync() {
_p9k_worker_reply _p9k_prompt_time_compute
_p9k_worker_reply '_p9k_worker_invoke _p9k_prompt_time_compute _p9k_prompt_time_compute; reset=1'
}
################################################################
@ -3963,11 +3991,7 @@ function _p9k_prompt_net_iface_init() {
typeset -g _p9k__ip_ip=
typeset -g _p9k__vpn_ip_ip=
[[ -z $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]] && _p9k__public_ip_not_vpn=1
_p9k_worker_send_params \
_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE \
_POWERLEVEL9K_IP_INTERFACE \
_POWERLEVEL9K_VPN_IP_INTERFACE
_p9k_worker_send_functions _p9k_prompt_net_iface_match
_p9k__async_segments_compute+=_p9k_prompt_net_iface_compute
}
# reads `iface2ip` and sets `ip`
@ -3979,6 +4003,10 @@ function _p9k_prompt_net_iface_match() {
return 1
}
function _p9k_prompt_net_iface_compute() {
_p9k_worker_async _p9k_prompt_net_iface_async _p9k_prompt_net_iface_sync
}
function _p9k_prompt_net_iface_async() {
local iface ip line var
typeset -A iface2ip
@ -4005,23 +4033,37 @@ function _p9k_prompt_net_iface_async() {
fi
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE; then
_p9k__public_ip_vpn=1
_p9k__public_ip_not_vpn=
local public_ip_vpn=1
local public_ip_not_vpn=
else
_p9k__public_ip_vpn=
_p9k__public_ip_not_vpn=1
local public_ip_vpn=
local public_ip_not_vpn=1
fi
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_IP_INTERFACE; then
_p9k__ip_ip=$ip
local ip_ip=$ip
else
_p9k__ip_ip=
local ip_ip=
fi
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_VPN_IP_INTERFACE; then
_p9k__vpn_ip_ip=$ip
local vpn_ip_ip=$ip
else
_p9k__vpn_ip_ip=
fi
local vpn_ip_ip=
fi
[[ $_p9k__public_ip_vpn == $public_ip_vpn &&
$_p9k__public_ip_not_vpn == $public_ip_not_vpn &&
$_p9k__ip_ip == $ip_ip &&
$_p9k__vpn_ip_ip == $vpn_ip_ip ]] && return 1
_p9k__public_ip_vpn=$public_ip_vpn
_p9k__public_ip_not_vpn=$public_ip_not_vpn
_p9k__ip_ip=$ip_ip
_p9k__vpn_ip_ip=$vpn_ip_ip
typeset -p _p9k__public_ip_vpn _p9k__public_ip_not_vpn _p9k__ip_ip _p9k__vpn_ip_ip
echo -E - 'reset=1'
}
_p9k_prompt_net_iface_sync() {
eval $REPLY
_p9k_worker_reply $REPLY
}
function _p9k_build_segment() {
@ -4058,7 +4100,7 @@ function _p9k_set_prompt() {
if [[ $_p9k_refresh_reason == precmd ]]; then
local f_compute
for f_compute in "${_p9k__async_segments_compute[@]}"; do
$f_compute
_p9k_worker_invoke ${f_compute%% *} ${(e)f_compute}
done
fi
@ -6588,19 +6630,7 @@ _p9k_init() {
local f_init
for f_init in $_p9k_async_segments_init; do
$f_init || continue
local f=${f_init%init}
if (( ! $+functions[${f}sync] )); then
functions[${f}sync]='[[ $cur == $prev ]] && return; eval $cur; _p9k_worker_reply $cur'
fi
if (( ! $+functions[${f}compute] )); then
local body="_p9k_worker_invoke $f "
(( $+functions[${f}cond] )) && body+="${f}cond " || body+="'' "
(( $+functions[${f}async] )) && body+="${f}async " || body+="'' "
functions[${f}compute]="$body ${f}sync"
fi
_p9k_worker_send_functions ${f}{cond,sync,async}
_p9k__async_segments_compute+=${f}compute
$f_init continue
done
(( $#_p9k__async_segments_compute )) && _p9k_worker_start

@ -7,20 +7,30 @@ function _p9k_worker_main() {
exec 0<$_p9k__worker_file_prefix.fifo || return
rm $_p9k__worker_file_prefix.fifo || return
function _p9k_worker_reply_begin() { print -nr -- e }
function _p9k_worker_reply_end() { print -nr -- $'\x1e' }
function _p9k_worker_reply() { print -nr -- e${(pj:\n:)@}$'\x1e' }
typeset -g IFS=$' \t\n\0'
local -i reset
local req fd
local -a ready
local -A inflight # fd => id$'\x1f'sync
local -A last_async
local -ri _p9k_worker_runs_me=1
local _p9k_worker_request_id
local -A _p9k_worker_fds # fd => id$'\x1f'callback
local -A _p9k_worker_inflight # id => inflight count
function _p9k_worker_reply_begin() { print -nr -- e }
function _p9k_worker_reply_end() { print -nr -- $'\x1e' }
function _p9k_worker_reply() { print -nr -- e${(pj:\n:)@}$'\x1e' }
# usage: _p9k_worker_async <work> <callback>
function _p9k_worker_async() {
local fd async=$1
sysopen -r -o cloexec -u fd <(
() { eval $async; } && print -n '\x1e') || return
(( ++_p9k_worker_inflight[$_p9k_worker_request_id] ))
_p9k_worker_fds[$fd]=$_p9k_worker_request_id$'\x1f'$2
}
{
while zselect -a ready 0 ${(k)inflight}; do
while zselect -a ready 0 ${(k)_p9k_worker_fds}; do
[[ $ready[1] == -r ]] || return
for fd in ${ready:1}; do
if [[ $fd == 0 ]]; then
@ -32,44 +42,30 @@ function _p9k_worker_main() {
sysread 'buf[$#buf+1]' || return
done
for req in ${(ps:\x1e:)buf}; do
local parts=("${(@ps:\x1f:)req}") # id cond async sync
if () { eval $parts[2] }; then
if [[ -n $parts[3] ]]; then
sysopen -r -o cloexec -u fd <(
() { eval $parts[3]; } && print -n '\x1e') || return
inflight[$fd]=$parts[1]$'\x1f'$parts[4]
continue
fi
() { eval $parts[4] }
fi
if [[ -n $parts[1] ]]; then
print -rn -- d$parts[1]$'\x1e' || return
fi
_p9k_worker_request_id=${req%%$'\x1f'*}
() { eval $req[$#_p9k_worker_request_id+2,-1] }
(( $+_p9k_worker_inflight[$_p9k_worker_request_id] )) && continue
print -rn -- d$_p9k_worker_request_id$'\x1e' || return
done
else
local cur=
local REPLY=
while true; do
sysread -i $fd 'cur[$#cur+1]' && continue
(( $? == 5 )) || return
sysread -i $fd 'REPLY[$#REPLY+1]' && continue
(( $? == 5 )) || return
break
done
local parts=("${(@ps:\x1f:)inflight[$fd]}") # id sync
if [[ $cur == *$'\x1e' ]]; then
cur[-1]=""
() {
local prev
if [[ -n $parts[1] ]]; then
prev=$last_async[$parts[1]]
last_async[$parts[1]]=$cur
fi
eval $parts[2]
}
local cb=$_p9k_worker_fds[$fd]
_p9k_worker_request_id=${cb%%$'\x1f'*}
unset "_p9k_worker_fds[$fd]"
exec {fd}>&-
if [[ $REPLY == *$'\x1e' ]]; then
REPLY[-1]=""
() { eval $cb[$#_p9k_worker_request_id+2,-1] }
fi
if [[ -n $parts[1] ]]; then
print -rn -- d$parts[1]$'\x1e' || return
if (( --_p9k_worker_inflight[$_p9k_worker_request_id] == 0 )); then
unset "_p9k_worker_inflight[$_p9k_worker_request_id]"
print -rn -- d$_p9k_worker_request_id$'\x1e' || return
fi
unset "inflight[$fd]"
exec {fd}>&-
fi
done
done
@ -84,33 +80,16 @@ typeset -g _p9k__worker_resp_fd
typeset -g _p9k__worker_shell_pid
typeset -g _p9k__worker_file_prefix
typeset -gA _p9k__worker_request_map
typeset -ga _p9k__worker_request_queue
# todo: remove these
function _p9k_worker_send_params() { }
function _p9k_worker_send_functions() { }
# invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync>
# invoked in master: _p9k_worker_invoke <request-id> <list>
function _p9k_worker_invoke() {
if [[ -n $_p9k__worker_resp_fd ]]; then
local req=$1$'\x1f'$2$'\x1f'$3$'\x1f'$4$'\x1e'
if [[ -n $_p9k__worker_req_fd && $+_p9k__worker_request_map[$1] == 0 ]]; then
[[ -n $1 ]] && _p9k__worker_request_map[$1]=
print -rnu $_p9k__worker_req_fd -- $req
return
fi
if [[ -n $1 ]]; then
(( $+_p9k__worker_request_map[$1] )) || _p9k__worker_request_queue+=$1
_p9k__worker_request_map[$1]=$req
else
_p9k__worker_request_queue+=$req
fi
[[ -n $_p9k__worker_resp_fd ]] || return
local req=$1$'\x1f'$2$'\x1e'
if [[ -n $_p9k__worker_req_fd && $+_p9k__worker_request_map[$1] == 0 ]]; then
_p9k__worker_request_map[$1]=
print -rnu $_p9k__worker_req_fd -- $req
else
if () { eval $2 }; then
local REPLY=
() { eval $3 }
() { eval $4 }
fi
_p9k__worker_request_map[$1]=$req
fi
}
@ -134,7 +113,6 @@ function _p9k_worker_stop() {
_p9k__worker_resp_fd=
_p9k__worker_shell_pid=
_p9k__worker_request_map=()
_p9k__worker_request_queue=()
return 0
}
@ -145,14 +123,15 @@ function _p9k_worker_receive() {
{
(( $# <= 1 )) || return
local buf resp reset
local buf resp
while true; do
sysread -t 0 -i $_p9k__worker_resp_fd 'buf[$#buf+1]' && continue
(( $? == 4 )) || return
[[ $buf[-1] == (|$'\x1e') ]] && break
[[ $buf == (|*$'\x1e')$'\x05'# ]] && break
sysread -i $_p9k__worker_resp_fd 'buf[$#buf+1]' || return
done
local -i reset
for resp in ${(ps:\x1e:)${buf//$'\x05'}}; do
local arg=$resp[2,-1]
case $resp[1] in
@ -172,7 +151,6 @@ function _p9k_worker_receive() {
echo "latency: $latency ms" >>/tmp/log
start_time=0
fi
reset=1
() { eval $arg }
;;
s)
@ -181,15 +159,10 @@ function _p9k_worker_receive() {
_p9k__worker_pid=$arg
sysopen -w -o cloexec -u _p9k__worker_req_fd $_p9k__worker_file_prefix.fifo || return
local req=
for req in $_p9k__worker_request_queue; do
if [[ $req != *$'\x1e' ]]; then
local id=$req
req=$_p9k__worker_request_map[$id]
_p9k__worker_request_map[$id]=
fi
for req in $_p9k__worker_request_map; do
print -rnu $_p9k__worker_req_fd -- $req || return
done
_p9k__worker_request_queue=()
_p9k__worker_request_map=({${(k)^_p9k__worker_request_map},''})
;;
*)
return 1
@ -197,6 +170,11 @@ function _p9k_worker_receive() {
esac
done
if (( reset == 2 )); then
_p9k_refresh_reason=worker
_p9k_set_prompt
_p9k_refresh_reason=''
fi
(( reset )) && _p9k_reset_prompt
return 0
} always {
@ -246,62 +224,29 @@ function _p9k_reset_prompt() {
zle && zle reset-prompt && zle -R
}
emulate -L zsh -o prompt_subst # -o xtrace
emulate -L zsh -o prompt_subst -o interactive_comments # -o xtrace
POWERLEVEL9K_WORKER_LOG_LEVEL=DEBUG
# POWERLEVEL9K_WORKER_LOG_LEVEL=DEBUG
zmodload zsh/datetime
zmodload zsh/system
autoload -Uz add-zsh-hook
typeset -F start_time=EPOCHREALTIME
_p9k_worker_start
echo -E - $((1000*(EPOCHREALTIME-start_time)))
function foo_cond() {
typeset -gi foo_counter
typeset -g foo="[$bar] cond $1 $((foo_counter++))"
function foo_compute() {
_p9k_worker_reply 'echo sync latency: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log'
_p9k_worker_async foo_async "foo_sync $1"
}
function foo_async() {
sleep 1
REPLY="$foo / async $1"
}
function foo_sync() {
REPLY+=" / sync $1"
_p9k_worker_reply "typeset -g foo=${(q)REPLY}"
}
() {
typeset -g RPROMPT='$foo %*'
typeset -g bar='lol'
_p9k_worker_send_params bar
local f
for f in foo_{cond,async,sync}; do
_p9k_worker_invoke "" "function $f() { $functions[$f] }" "" ""
done
() {
local -i i
for i in {1..10}; do
_p9k_worker_invoke foo$i "foo_cond c$i\$\{" "foo_async a$i\$\{" "foo_sync s$i\$\{"
done
}
}
function in_worker() {
_p9k_worker_reply 'echo roundtrip: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log'
_p9k_worker_reply 'echo async latency: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log'
}
_p9k_worker_invoke "" "function in_worker() { $functions[in_worker] }" "" ""
_p9k_worker_invoke w "in_worker $EPOCHREALTIME" "" ""
# for i in {1..100}; do _p9k_worker_invoke w$i "in_worker $EPOCHREALTIME"; done
typeset -F start_time=EPOCHREALTIME
_p9k_worker_start
_p9k_worker_invoke first '_p9k_worker_reply ""'
echo -E - $((1000*(EPOCHREALTIME-start_time)))
# TODO:
#
# - Segment API: _p9k_prompt_foo_worker_{params,cond,async,sync}.
# - _p9k_worker_request -- cacheable variable that contains full request to worker.
# - _p9k_set_prompt sends stuff to worker or evals it.
# - _p9k_on_expand has _REALTIME check at the top and sends keep-alive to worker.
bm() { _p9k_worker_invoke foo$1 "foo_compute $EPOCHREALTIME" }

Loading…
Cancel
Save