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

massive worker simplification (figure out during massive hangover)

This commit is contained in:
romkatv 2020-01-24 19:47:01 +01:00
parent f1314f9072
commit 2aa1d07b4a

View file

@ -1,11 +1,11 @@
# invoked in worker: _p9k_worker_main <timeout> # invoked in worker: _p9k_worker_main <timeout>
function _p9k_worker_main() { function _p9k_worker_main() {
emulate -L zsh zmodload zsh/zselect || return
setopt no_hist_expand extended_glob no_prompt_bang prompt_percent prompt_subst no_aliases no_bgnice ! { zselect -t0 || (( $? != 1 )) } || return
mkfifo $_p9k__worker_file_prefix.fifo || return
zmodload zsh/system || return echo -nE - s${1}$'\x1e' || return
zmodload zsh/zselect || return exec 0<$_p9k__worker_file_prefix.fifo || return
! { zselect -t0 || (( $? != 1 )) } || return rm $_p9k__worker_file_prefix.fifo || return
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' }
@ -83,44 +83,14 @@ 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 -gaU _p9k__worker_params
typeset -gaU _p9k__worker_functions
typeset -gA _p9k__worker_request_map typeset -gA _p9k__worker_request_map
typeset -ga _p9k__worker_request_queue typeset -ga _p9k__worker_request_queue
function _p9k_worker_print_params() {
local names=(${@:/(#m)*/${${${+parameters[$MATCH]}:#0}:+$MATCH}})
(( ! $#names )) && return
print -n -- '\x1f' && typeset -p -- $names && print -n -- '\x1f\x1f\x1e'
}
function _p9k_worker_print_functions() {
local names=(${@:/(#m)*/${${${+functions[$MATCH]}:#0}:+$MATCH}})
(( ! $#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() { }
if [[ -n $_p9k__worker_req_fd ]]; then
_p9k_worker_print_params ${(u)@} >&$_p9k__worker_req_fd && return
_p9k_worker_stop
return 1
else
_p9k__worker_params+=($@)
fi
}
# 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() { }
if [[ -n $_p9k__worker_req_fd ]]; then
_p9k_worker_print_functions ${(u)@} >&$_p9k__worker_req_fd && return
_p9k_worker_stop
return 1
else
_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>
function _p9k_worker_invoke() { function _p9k_worker_invoke() {
@ -165,8 +135,6 @@ function _p9k_worker_stop() {
_p9k__worker_req_fd= _p9k__worker_req_fd=
_p9k__worker_resp_fd= _p9k__worker_resp_fd=
_p9k__worker_shell_pid= _p9k__worker_shell_pid=
_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
@ -214,36 +182,16 @@ function _p9k_worker_receive() {
[[ $arg == <1-> ]] || return [[ $arg == <1-> ]] || return
_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 init=" for req in $_p9k__worker_request_queue; do
zmodload zsh/datetime if [[ $req != *$'\x1e' ]]; then
zmodload zsh/mathfunc local id=$req
zmodload zsh/parameter req=$_p9k__worker_request_map[$id]
zmodload zsh/system _p9k__worker_request_map[$id]=
zmodload zsh/termcap fi
zmodload zsh/terminfo print -rnu $_p9k__worker_req_fd -- $req || return
zmodload zsh/zleparameter done
zmodload -F zsh/stat b:zstat _p9k__worker_request_queue=()
zmodload -F zsh/net/socket b:zsocket
zmodload -F zsh/files b:zf_mv b:zf_rm
autoload -Uz is-at-least
() { $functions[_p9k_worker_main] }"
print -r -- ${init//$'\n'/$'\x1e'} || return
_p9k_worker_print_params $_p9k__worker_params || return
_p9k_worker_print_functions $_p9k__worker_functions || return
_p9k__worker_params=()
_p9k__worker_functions()
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
print -rnu $_p9k__worker_req_fd -- $req || return
done
_p9k__worker_request_queue=()
} >&$_p9k__worker_req_fd
;; ;;
*) *)
return 1 return 1
@ -259,42 +207,24 @@ function _p9k_worker_receive() {
} }
function _p9k_worker_start() { function _p9k_worker_start() {
setopt no_bgnice setopt no_bgnice monitor
{ {
[[ -n $_p9k__worker_resp_fd ]] && return [[ -n $_p9k__worker_resp_fd ]] && return
_p9k__worker_file_prefix=${TMPDIR:-/tmp}/p10k.worker.$EUID.$$.$EPOCHSECONDS _p9k__worker_file_prefix=${TMPDIR:-/tmp}/p10k.worker.$EUID.$$.$EPOCHSECONDS
if [[ -n $_POWERLEVEL9K_WORKER_LOG_LEVEL ]]; then
local trace=x
local log_file=$file_prefix.log
else
local trace=
local log_file=/dev/null
fi
log_file=/tmp/log # todo: remove
trace=x
local fifo=$_p9k__worker_file_prefix.fifo
local zsh=${${:-/proc/self/exe}:A}
[[ -x $zsh ]] || zsh=zsh
local bootstrap='
"emulate" "-L" "zsh" "-o" "no_aliases" "-o" "no_bgnice"
{
local fifo='${(q)fifo}'
{
zmodload zsh/system &&
mkfifo $fifo &&
exec >&4 &&
echo -n "s$sysparams[pid]\x1e" &&
exec 0<$fifo || exit
} always { rm -f -- $fifo }
IFS= read -r && eval ${REPLY//$'"'\x1e'"'/$'"'\n'"'}
} &!
exec true'
sysopen -r -o cloexec -u _p9k__worker_resp_fd <( sysopen -r -o cloexec -u _p9k__worker_resp_fd <(
_p9k_worker_bootstrap=${bootstrap// ##} </dev/null 4>&1 &>>$log_file \ if [[ -n $_POWERLEVEL9K_WORKER_LOG_LEVEL ]]; then
exec $zsh -${trace}dfmc '"eval" "$_p9k_worker_bootstrap"') || return exec 2>$_p9k__worker_file_prefix.log
setopt xtrace
else
exec 2>/dev/null
fi
# todo: remove
exec 2>>/tmp/log
setopt xtrace
local pid=$sysparams[pid]
_p9k_worker_main $pid &
exec =true) || return
zle -F $_p9k__worker_resp_fd _p9k_worker_receive zle -F $_p9k__worker_resp_fd _p9k_worker_receive
_p9k__worker_shell_pid=$sysparams[pid] _p9k__worker_shell_pid=$sysparams[pid]
add-zsh-hook zshexit _p9k_worker_cleanup add-zsh-hook zshexit _p9k_worker_cleanup