mirror of
https://github.com/romkatv/powerlevel10k.git
synced 2024-11-22 12:20:07 +00:00
migrate over from the old worker api; use worker in battery on macOS
This commit is contained in:
parent
148ecf7fc3
commit
565492b10e
2 changed files with 170 additions and 195 deletions
|
@ -1030,32 +1030,17 @@ _p9k_prompt_disk_usage_init() {
|
||||||
typeset -g _p9k__disk_usage_normal=
|
typeset -g _p9k__disk_usage_normal=
|
||||||
typeset -g _p9k__disk_usage_warning=
|
typeset -g _p9k__disk_usage_warning=
|
||||||
typeset -g _p9k__disk_usage_critical=
|
typeset -g _p9k__disk_usage_critical=
|
||||||
|
_p9k__async_segments_compute+='_p9k_prompt_disk_usage_compute ${(q)_p9k__pwd_a}'
|
||||||
_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_prompt_disk_usage_compute() {
|
_p9k_prompt_disk_usage_compute() {
|
||||||
_p9k_worker_invoke \
|
(( $+commands[df] )) || return
|
||||||
disk_usage \
|
_p9k_worker_async "_p9k_prompt_disk_usage_async ${(q)1}" _p9k_prompt_disk_usage_sync
|
||||||
_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] ))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_p9k_prompt_disk_usage_async() {
|
_p9k_prompt_disk_usage_async() {
|
||||||
local pct=${${=${(f)"$(df -P $1 2>/dev/null)"}[2]}[5]%%%}
|
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_pct=$pct
|
||||||
_p9k__disk_usage_normal=
|
_p9k__disk_usage_normal=
|
||||||
_p9k__disk_usage_warning=
|
_p9k__disk_usage_warning=
|
||||||
|
@ -1072,6 +1057,12 @@ _p9k_prompt_disk_usage_async() {
|
||||||
_p9k__disk_usage_normal \
|
_p9k__disk_usage_normal \
|
||||||
_p9k__disk_usage_warning \
|
_p9k__disk_usage_warning \
|
||||||
_p9k__disk_usage_critical
|
_p9k__disk_usage_critical
|
||||||
|
echo -E - 'reset=1'
|
||||||
|
}
|
||||||
|
|
||||||
|
_p9k_prompt_disk_usage_sync() {
|
||||||
|
eval $REPLY
|
||||||
|
_p9k_worker_reply $REPLY
|
||||||
}
|
}
|
||||||
|
|
||||||
function _p9k_read_file() {
|
function _p9k_read_file() {
|
||||||
|
@ -1098,6 +1089,35 @@ prompt_fvm() {
|
||||||
################################################################
|
################################################################
|
||||||
# Segment that displays the battery status in levels and colors
|
# Segment that displays the battery status in levels and colors
|
||||||
prompt_battery() {
|
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 state remain
|
||||||
local -i bat_percent
|
local -i bat_percent
|
||||||
|
|
||||||
|
@ -1212,7 +1232,7 @@ prompt_battery() {
|
||||||
fg=$_POWERLEVEL9K_BATTERY_LEVEL_FOREGROUND[idx]
|
fg=$_POWERLEVEL9K_BATTERY_LEVEL_FOREGROUND[idx]
|
||||||
fi
|
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() {
|
_p9k_prompt_public_ip_init() {
|
||||||
typeset -g _p9k__public_ip=
|
typeset -g _p9k__public_ip=
|
||||||
typeset -gF _p9k__public_ip_next_time=0
|
typeset -gF _p9k__public_ip_next_time=0
|
||||||
|
_p9k__async_segments_compute+=_p9k_prompt_public_ip_compute
|
||||||
_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_prompt_public_ip_cond() {
|
_p9k_prompt_public_ip_compute() {
|
||||||
(( EPOCHREALTIME >= _p9k__public_ip_next_time ))
|
(( 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() {
|
_p9k_prompt_public_ip_async() {
|
||||||
|
@ -1276,17 +1291,17 @@ _p9k_prompt_public_ip_async() {
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
_p9k__public_ip=$ip
|
|
||||||
_p9k__public_ip_next_time=$next
|
_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() {
|
_p9k_prompt_public_ip_sync() {
|
||||||
[[ $prev == $cur ]] && return
|
eval $REPLY
|
||||||
local prev_ip=$_p9k__public_ip
|
_p9k_worker_reply $REPLY
|
||||||
eval $cur
|
|
||||||
[[ $_p9k__public_ip == $prev_ip ]] && return
|
|
||||||
_p9k_worker_reply $cur
|
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################
|
################################################################
|
||||||
|
@ -1908,17 +1923,12 @@ _p9k_prompt_load_init() {
|
||||||
typeset -g _p9k__load_normal=
|
typeset -g _p9k__load_normal=
|
||||||
typeset -g _p9k__load_warning=
|
typeset -g _p9k__load_warning=
|
||||||
typeset -g _p9k__load_critical=
|
typeset -g _p9k__load_critical=
|
||||||
_p9k_worker_send_params \
|
_p9k__async_segments_compute+=_p9k_prompt_load_compute
|
||||||
$_POWERLEVEL9K_LOAD_WHICH \
|
|
||||||
_p9k_num_cpus \
|
|
||||||
_p9k__load_value \
|
|
||||||
_p9k__load_normal \
|
|
||||||
_p9k__load_warning \
|
|
||||||
_p9k__load_critical
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_p9k_prompt_load_cond() {
|
_p9k_prompt_load_compute() {
|
||||||
(( $+commands[sysctl] ))
|
(( $+commands[sysctl] )) || return
|
||||||
|
_p9k_worker_async _p9k_prompt_load_async _p9k_prompt_load_sync
|
||||||
}
|
}
|
||||||
|
|
||||||
_p9k_prompt_load_async() {
|
_p9k_prompt_load_async() {
|
||||||
|
@ -1942,6 +1952,12 @@ _p9k_prompt_load_async() {
|
||||||
_p9k__load_normal \
|
_p9k__load_normal \
|
||||||
_p9k__load_warning \
|
_p9k__load_warning \
|
||||||
_p9k__load_critical
|
_p9k__load_critical
|
||||||
|
echo -E - 'reset=1'
|
||||||
|
}
|
||||||
|
|
||||||
|
_p9k_prompt_load_sync() {
|
||||||
|
eval $REPLY
|
||||||
|
_p9k_worker_reply $REPLY
|
||||||
}
|
}
|
||||||
|
|
||||||
function _p9k_cached_cmd_stdout() {
|
function _p9k_cached_cmd_stdout() {
|
||||||
|
@ -2228,8 +2244,11 @@ prompt_ram() {
|
||||||
|
|
||||||
function _p9k_prompt_ram_init() {
|
function _p9k_prompt_ram_init() {
|
||||||
typeset -g _p9k__ram_free=
|
typeset -g _p9k__ram_free=
|
||||||
_p9k_worker_send_params _p9k_os _p9k__ram_free __p9k_byte_suffix
|
_p9k__async_segments_compute+=_p9k_prompt_ram_compute
|
||||||
_p9k_worker_send_functions _p9k_human_readable_bytes
|
}
|
||||||
|
|
||||||
|
_p9k_prompt_ram_compute() {
|
||||||
|
_p9k_worker_async _p9k_prompt_ram_async _p9k_prompt_ram_sync
|
||||||
}
|
}
|
||||||
|
|
||||||
_p9k_prompt_ram_async() {
|
_p9k_prompt_ram_async() {
|
||||||
|
@ -2261,6 +2280,12 @@ _p9k_prompt_ram_async() {
|
||||||
[[ $_p9k_ret != $_p9k__ram_free ]] || return
|
[[ $_p9k_ret != $_p9k__ram_free ]] || return
|
||||||
_p9k__ram_free=$_p9k_ret
|
_p9k__ram_free=$_p9k_ret
|
||||||
typeset -p _p9k__ram_free
|
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() {
|
function _p9k_read_rbenv_version_file() {
|
||||||
|
@ -2758,16 +2783,19 @@ 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_sync
|
|
||||||
_p9k__async_segments_compute+=_p9k_prompt_time_compute
|
_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() {
|
_p9k_prompt_time_async() {
|
||||||
sleep 1 || true
|
sleep 1 || true
|
||||||
}
|
}
|
||||||
|
|
||||||
_p9k_prompt_time_sync() {
|
_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__ip_ip=
|
||||||
typeset -g _p9k__vpn_ip_ip=
|
typeset -g _p9k__vpn_ip_ip=
|
||||||
[[ -z $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]] && _p9k__public_ip_not_vpn=1
|
[[ -z $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]] && _p9k__public_ip_not_vpn=1
|
||||||
_p9k_worker_send_params \
|
_p9k__async_segments_compute+=_p9k_prompt_net_iface_compute
|
||||||
_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE \
|
|
||||||
_POWERLEVEL9K_IP_INTERFACE \
|
|
||||||
_POWERLEVEL9K_VPN_IP_INTERFACE
|
|
||||||
_p9k_worker_send_functions _p9k_prompt_net_iface_match
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# reads `iface2ip` and sets `ip`
|
# reads `iface2ip` and sets `ip`
|
||||||
|
@ -3979,6 +4003,10 @@ function _p9k_prompt_net_iface_match() {
|
||||||
return 1
|
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() {
|
function _p9k_prompt_net_iface_async() {
|
||||||
local iface ip line var
|
local iface ip line var
|
||||||
typeset -A iface2ip
|
typeset -A iface2ip
|
||||||
|
@ -4005,23 +4033,37 @@ function _p9k_prompt_net_iface_async() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE; then
|
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE; then
|
||||||
_p9k__public_ip_vpn=1
|
local public_ip_vpn=1
|
||||||
_p9k__public_ip_not_vpn=
|
local public_ip_not_vpn=
|
||||||
else
|
else
|
||||||
_p9k__public_ip_vpn=
|
local public_ip_vpn=
|
||||||
_p9k__public_ip_not_vpn=1
|
local public_ip_not_vpn=1
|
||||||
fi
|
fi
|
||||||
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_IP_INTERFACE; then
|
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_IP_INTERFACE; then
|
||||||
_p9k__ip_ip=$ip
|
local ip_ip=$ip
|
||||||
else
|
else
|
||||||
_p9k__ip_ip=
|
local ip_ip=
|
||||||
fi
|
fi
|
||||||
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_VPN_IP_INTERFACE; then
|
if _p9k_prompt_net_iface_match $_POWERLEVEL9K_VPN_IP_INTERFACE; then
|
||||||
_p9k__vpn_ip_ip=$ip
|
local vpn_ip_ip=$ip
|
||||||
else
|
else
|
||||||
_p9k__vpn_ip_ip=
|
local vpn_ip_ip=
|
||||||
fi
|
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
|
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() {
|
function _p9k_build_segment() {
|
||||||
|
@ -4058,7 +4100,7 @@ function _p9k_set_prompt() {
|
||||||
if [[ $_p9k_refresh_reason == precmd ]]; then
|
if [[ $_p9k_refresh_reason == precmd ]]; then
|
||||||
local f_compute
|
local f_compute
|
||||||
for f_compute in "${_p9k__async_segments_compute[@]}"; do
|
for f_compute in "${_p9k__async_segments_compute[@]}"; do
|
||||||
$f_compute
|
_p9k_worker_invoke ${f_compute%% *} ${(e)f_compute}
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -6588,19 +6630,7 @@ _p9k_init() {
|
||||||
|
|
||||||
local f_init
|
local f_init
|
||||||
for f_init in $_p9k_async_segments_init; do
|
for f_init in $_p9k_async_segments_init; do
|
||||||
$f_init || continue
|
$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
|
|
||||||
done
|
done
|
||||||
|
|
||||||
(( $#_p9k__async_segments_compute )) && _p9k_worker_start
|
(( $#_p9k__async_segments_compute )) && _p9k_worker_start
|
||||||
|
|
|
@ -7,20 +7,30 @@ function _p9k_worker_main() {
|
||||||
exec 0<$_p9k__worker_file_prefix.fifo || return
|
exec 0<$_p9k__worker_file_prefix.fifo || return
|
||||||
rm $_p9k__worker_file_prefix.fifo || return
|
rm $_p9k__worker_file_prefix.fifo || return
|
||||||
|
|
||||||
|
typeset -g IFS=$' \t\n\0'
|
||||||
|
|
||||||
|
local -i reset
|
||||||
|
local req fd
|
||||||
|
local -a ready
|
||||||
|
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_begin() { print -nr -- e }
|
||||||
function _p9k_worker_reply_end() { print -nr -- $'\x1e' }
|
function _p9k_worker_reply_end() { print -nr -- $'\x1e' }
|
||||||
function _p9k_worker_reply() { print -nr -- e${(pj:\n:)@}$'\x1e' }
|
function _p9k_worker_reply() { print -nr -- e${(pj:\n:)@}$'\x1e' }
|
||||||
|
|
||||||
typeset -g IFS=$' \t\n\0'
|
# usage: _p9k_worker_async <work> <callback>
|
||||||
|
function _p9k_worker_async() {
|
||||||
local req fd
|
local fd async=$1
|
||||||
local -a ready
|
sysopen -r -o cloexec -u fd <(
|
||||||
local -A inflight # fd => id$'\x1f'sync
|
() { eval $async; } && print -n '\x1e') || return
|
||||||
local -A last_async
|
(( ++_p9k_worker_inflight[$_p9k_worker_request_id] ))
|
||||||
local -ri _p9k_worker_runs_me=1
|
_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
|
[[ $ready[1] == -r ]] || return
|
||||||
for fd in ${ready:1}; do
|
for fd in ${ready:1}; do
|
||||||
if [[ $fd == 0 ]]; then
|
if [[ $fd == 0 ]]; then
|
||||||
|
@ -32,44 +42,30 @@ function _p9k_worker_main() {
|
||||||
sysread 'buf[$#buf+1]' || return
|
sysread 'buf[$#buf+1]' || return
|
||||||
done
|
done
|
||||||
for req in ${(ps:\x1e:)buf}; do
|
for req in ${(ps:\x1e:)buf}; do
|
||||||
local parts=("${(@ps:\x1f:)req}") # id cond async sync
|
_p9k_worker_request_id=${req%%$'\x1f'*}
|
||||||
if () { eval $parts[2] }; then
|
() { eval $req[$#_p9k_worker_request_id+2,-1] }
|
||||||
if [[ -n $parts[3] ]]; then
|
(( $+_p9k_worker_inflight[$_p9k_worker_request_id] )) && continue
|
||||||
sysopen -r -o cloexec -u fd <(
|
print -rn -- d$_p9k_worker_request_id$'\x1e' || return
|
||||||
() { 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
|
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
local cur=
|
local REPLY=
|
||||||
while true; do
|
while true; do
|
||||||
sysread -i $fd 'cur[$#cur+1]' && continue
|
sysread -i $fd 'REPLY[$#REPLY+1]' && continue
|
||||||
(( $? == 5 )) || return
|
(( $? == 5 )) || return
|
||||||
break
|
break
|
||||||
done
|
done
|
||||||
local parts=("${(@ps:\x1f:)inflight[$fd]}") # id sync
|
local cb=$_p9k_worker_fds[$fd]
|
||||||
if [[ $cur == *$'\x1e' ]]; then
|
_p9k_worker_request_id=${cb%%$'\x1f'*}
|
||||||
cur[-1]=""
|
unset "_p9k_worker_fds[$fd]"
|
||||||
() {
|
|
||||||
local prev
|
|
||||||
if [[ -n $parts[1] ]]; then
|
|
||||||
prev=$last_async[$parts[1]]
|
|
||||||
last_async[$parts[1]]=$cur
|
|
||||||
fi
|
|
||||||
eval $parts[2]
|
|
||||||
}
|
|
||||||
fi
|
|
||||||
if [[ -n $parts[1] ]]; then
|
|
||||||
print -rn -- d$parts[1]$'\x1e' || return
|
|
||||||
fi
|
|
||||||
unset "inflight[$fd]"
|
|
||||||
exec {fd}>&-
|
exec {fd}>&-
|
||||||
|
if [[ $REPLY == *$'\x1e' ]]; then
|
||||||
|
REPLY[-1]=""
|
||||||
|
() { eval $cb[$#_p9k_worker_request_id+2,-1] }
|
||||||
|
fi
|
||||||
|
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
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
@ -84,33 +80,16 @@ 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_request_map
|
typeset -gA _p9k__worker_request_map
|
||||||
typeset -ga _p9k__worker_request_queue
|
|
||||||
|
|
||||||
# todo: remove these
|
# invoked in master: _p9k_worker_invoke <request-id> <list>
|
||||||
function _p9k_worker_send_params() { }
|
|
||||||
function _p9k_worker_send_functions() { }
|
|
||||||
|
|
||||||
# invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync>
|
|
||||||
function _p9k_worker_invoke() {
|
function _p9k_worker_invoke() {
|
||||||
if [[ -n $_p9k__worker_resp_fd ]]; then
|
[[ -n $_p9k__worker_resp_fd ]] || return
|
||||||
local req=$1$'\x1f'$2$'\x1f'$3$'\x1f'$4$'\x1e'
|
local req=$1$'\x1f'$2$'\x1e'
|
||||||
if [[ -n $_p9k__worker_req_fd && $+_p9k__worker_request_map[$1] == 0 ]]; then
|
if [[ -n $_p9k__worker_req_fd && $+_p9k__worker_request_map[$1] == 0 ]]; then
|
||||||
[[ -n $1 ]] && _p9k__worker_request_map[$1]=
|
_p9k__worker_request_map[$1]=
|
||||||
print -rnu $_p9k__worker_req_fd -- $req
|
print -rnu $_p9k__worker_req_fd -- $req
|
||||||
return
|
else
|
||||||
fi
|
|
||||||
if [[ -n $1 ]]; then
|
|
||||||
(( $+_p9k__worker_request_map[$1] )) || _p9k__worker_request_queue+=$1
|
|
||||||
_p9k__worker_request_map[$1]=$req
|
_p9k__worker_request_map[$1]=$req
|
||||||
else
|
|
||||||
_p9k__worker_request_queue+=$req
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if () { eval $2 }; then
|
|
||||||
local REPLY=
|
|
||||||
() { eval $3 }
|
|
||||||
() { eval $4 }
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +113,6 @@ function _p9k_worker_stop() {
|
||||||
_p9k__worker_resp_fd=
|
_p9k__worker_resp_fd=
|
||||||
_p9k__worker_shell_pid=
|
_p9k__worker_shell_pid=
|
||||||
_p9k__worker_request_map=()
|
_p9k__worker_request_map=()
|
||||||
_p9k__worker_request_queue=()
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,14 +123,15 @@ function _p9k_worker_receive() {
|
||||||
{
|
{
|
||||||
(( $# <= 1 )) || return
|
(( $# <= 1 )) || return
|
||||||
|
|
||||||
local buf resp reset
|
local buf resp
|
||||||
while true; do
|
while true; do
|
||||||
sysread -t 0 -i $_p9k__worker_resp_fd 'buf[$#buf+1]' && continue
|
sysread -t 0 -i $_p9k__worker_resp_fd 'buf[$#buf+1]' && continue
|
||||||
(( $? == 4 )) || return
|
(( $? == 4 )) || return
|
||||||
[[ $buf[-1] == (|$'\x1e') ]] && break
|
[[ $buf == (|*$'\x1e')$'\x05'# ]] && break
|
||||||
sysread -i $_p9k__worker_resp_fd 'buf[$#buf+1]' || return
|
sysread -i $_p9k__worker_resp_fd 'buf[$#buf+1]' || return
|
||||||
done
|
done
|
||||||
|
|
||||||
|
local -i reset
|
||||||
for resp in ${(ps:\x1e:)${buf//$'\x05'}}; do
|
for resp in ${(ps:\x1e:)${buf//$'\x05'}}; do
|
||||||
local arg=$resp[2,-1]
|
local arg=$resp[2,-1]
|
||||||
case $resp[1] in
|
case $resp[1] in
|
||||||
|
@ -172,7 +151,6 @@ function _p9k_worker_receive() {
|
||||||
echo "latency: $latency ms" >>/tmp/log
|
echo "latency: $latency ms" >>/tmp/log
|
||||||
start_time=0
|
start_time=0
|
||||||
fi
|
fi
|
||||||
reset=1
|
|
||||||
() { eval $arg }
|
() { eval $arg }
|
||||||
;;
|
;;
|
||||||
s)
|
s)
|
||||||
|
@ -181,15 +159,10 @@ function _p9k_worker_receive() {
|
||||||
_p9k__worker_pid=$arg
|
_p9k__worker_pid=$arg
|
||||||
sysopen -w -o cloexec -u _p9k__worker_req_fd $_p9k__worker_file_prefix.fifo || return
|
sysopen -w -o cloexec -u _p9k__worker_req_fd $_p9k__worker_file_prefix.fifo || return
|
||||||
local req=
|
local req=
|
||||||
for req in $_p9k__worker_request_queue; do
|
for req in $_p9k__worker_request_map; do
|
||||||
if [[ $req != *$'\x1e' ]]; then
|
|
||||||
local id=$req
|
|
||||||
req=$_p9k__worker_request_map[$id]
|
|
||||||
_p9k__worker_request_map[$id]=
|
|
||||||
fi
|
|
||||||
print -rnu $_p9k__worker_req_fd -- $req || return
|
print -rnu $_p9k__worker_req_fd -- $req || return
|
||||||
done
|
done
|
||||||
_p9k__worker_request_queue=()
|
_p9k__worker_request_map=({${(k)^_p9k__worker_request_map},''})
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
return 1
|
return 1
|
||||||
|
@ -197,6 +170,11 @@ function _p9k_worker_receive() {
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if (( reset == 2 )); then
|
||||||
|
_p9k_refresh_reason=worker
|
||||||
|
_p9k_set_prompt
|
||||||
|
_p9k_refresh_reason=''
|
||||||
|
fi
|
||||||
(( reset )) && _p9k_reset_prompt
|
(( reset )) && _p9k_reset_prompt
|
||||||
return 0
|
return 0
|
||||||
} always {
|
} always {
|
||||||
|
@ -246,62 +224,29 @@ function _p9k_reset_prompt() {
|
||||||
zle && zle reset-prompt && zle -R
|
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/datetime
|
||||||
zmodload zsh/system
|
zmodload zsh/system
|
||||||
autoload -Uz add-zsh-hook
|
autoload -Uz add-zsh-hook
|
||||||
|
|
||||||
typeset -F start_time=EPOCHREALTIME
|
function foo_compute() {
|
||||||
_p9k_worker_start
|
_p9k_worker_reply 'echo sync latency: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log'
|
||||||
echo -E - $((1000*(EPOCHREALTIME-start_time)))
|
_p9k_worker_async foo_async "foo_sync $1"
|
||||||
|
|
||||||
function foo_cond() {
|
|
||||||
typeset -gi foo_counter
|
|
||||||
typeset -g foo="[$bar] cond $1 $((foo_counter++))"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function foo_async() {
|
function foo_async() {
|
||||||
sleep 1
|
|
||||||
REPLY="$foo / async $1"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function foo_sync() {
|
function foo_sync() {
|
||||||
REPLY+=" / sync $1"
|
_p9k_worker_reply 'echo async latency: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log'
|
||||||
_p9k_worker_reply "typeset -g foo=${(q)REPLY}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
() {
|
typeset -F start_time=EPOCHREALTIME
|
||||||
typeset -g RPROMPT='$foo %*'
|
_p9k_worker_start
|
||||||
typeset -g bar='lol'
|
_p9k_worker_invoke first '_p9k_worker_reply ""'
|
||||||
_p9k_worker_send_params bar
|
echo -E - $((1000*(EPOCHREALTIME-start_time)))
|
||||||
|
|
||||||
local f
|
bm() { _p9k_worker_invoke foo$1 "foo_compute $EPOCHREALTIME" }
|
||||||
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_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
|
|
||||||
|
|
||||||
# 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.
|
|
||||||
|
|
Loading…
Reference in a new issue