From e01b5d9529de31899ad5ee4d90a02704ca7dcef4 Mon Sep 17 00:00:00 2001 From: romkatv Date: Mon, 20 Jan 2020 20:12:05 +0100 Subject: [PATCH] refactor worker code --- internal/p10k.zsh | 134 ++++++++++++++++++++++++++------------------ internal/worker.zsh | 96 +++++++++++++++---------------- notes.txt | 7 --- 3 files changed, 125 insertions(+), 112 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 8f099001..a3b4bb1f 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -1041,6 +1041,8 @@ _p9k_prompt_disk_usage_init() { _p9k__disk_usage_normal \ _p9k__disk_usage_warning \ _p9k__disk_usage_critical + + _p9k__async_segments_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() { - (( $+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() { - [[ $REPLY == <0-100> && $REPLY != $_p9k__disk_usage_pct ]] || return - _p9k__disk_usage_pct=$REPLY + [[ $disk_usage_pct != $_p9k__disk_usage_pct ]] || return + _p9k__disk_usage_pct=$disk_usage_pct _p9k__disk_usage_normal= _p9k__disk_usage_warning= _p9k__disk_usage_critical= @@ -1065,11 +1071,13 @@ _p9k_prompt_disk_usage_sync() { elif (( ! _POWERLEVEL9K_DISK_USAGE_ONLY_WARNING )); then _p9k__disk_usage_normal=1 fi - _p9k_worker_send_params \ + _p9k_worker_reply_begin + typeset -p \ _p9k__disk_usage_pct \ _p9k__disk_usage_normal \ _p9k__disk_usage_warning \ _p9k__disk_usage_critical + _p9k_worker_reply_end } function _p9k_read_file() { @@ -1240,6 +1248,8 @@ _p9k_prompt_public_ip_init() { _p9k_prompt_public_ip_cond \ _p9k_prompt_public_ip_async \ _p9k_prompt_public_ip_sync + + _p9k__async_segments_compute+=_p9k_prompt_public_ip_compute } _p9k_prompt_public_ip_compute() { @@ -1284,16 +1294,16 @@ _p9k_prompt_public_ip_async() { break fi done - REPLY="$ip $next" + typeset -p ip next } _p9k_prompt_public_ip_sync() { - local ip_next=($=REPLY) - (( $#ip_next == 2 )) || return - _p9k__public_ip_next_time=$ip_next[2] - if [[ $ip_next[1] != _p9k__public_ip ]]; then - _p9k__public_ip=$ip_next[1] - _p9k_worker_send_params _p9k__public_ip + _p9k__public_ip_next_time=$next + if [[ $ip != _p9k__public_ip ]]; then + _p9k__public_ip=$ip + _p9k_worker_reply_begin + typeset -p _p9k__public_ip + _p9k_worker_reply_end fi } @@ -1894,28 +1904,30 @@ _p9k_prompt_load_init() { _p9k_worker_send_params \ $_POWERLEVEL9K_LOAD_WHICH \ - _p9k_os \ _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_compute() { - [[ $_p9k_os == (OSX|BSD) ]] || return _p9k_worker_invoke load '(( $+commands[sysctl] ))' _p9k_prompt_load_async _p9k_prompt_load_sync } _p9k_prompt_load_async() { local load 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() { - [[ $REPLY == <->(|.<->) && $REPLY != $_p9k__load_value ]] || return - _p9k__load_value=$REPLY + [[ $load != $_p9k__load_value ]] || return + _p9k__load_value=$load _p9k__load_normal= _p9k__load_warning= _p9k__load_critical= @@ -1927,11 +1939,14 @@ _p9k_prompt_load_sync() { else _p9k__load_normal=1 fi - _p9k_worker_send_params \ - _p9k__load_value \ - _p9k__load_normal \ - _p9k__load_warning \ + + _p9k_worker_reply_begin + typeset -p \ + _p9k__load_value \ + _p9k__load_normal \ + _p9k__load_warning \ _p9k__load_critical + _p9k_worker_reply_end } function _p9k_cached_cmd_stdout() { @@ -2223,6 +2238,7 @@ function _p9k_prompt_ram_init() { _p9k_human_readable_bytes \ _p9k_prompt_ram_async \ _p9k_prompt_ram_sync + _p9k__async_segments_compute+=_p9k_prompt_ram_compute } function _p9k_prompt_ram_compute() { @@ -2255,13 +2271,16 @@ _p9k_prompt_ram_async() { esac _p9k_human_readable_bytes $free_bytes - REPLY=$_p9k_ret + local ram_free=$_p9k_ret + typeset -p ram_free } _p9k_prompt_ram_sync() { - [[ $_p9k__ram_free == $REPLY ]] && return - _p9k__ram_free=$REPLY - _p9k_worker_send_params _p9k__ram_free + [[ $ram_free == $_p9k__ram_free ]] && return + _p9k__ram_free=$ram_free + _p9k_worker_reply_begin + typeset -p _p9k__ram_free + _p9k_worker_reply_end } function _p9k_read_rbenv_version_file() { @@ -2759,13 +2778,18 @@ instant_prompt_time() { _p9k_prompt_time_init() { (( _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() { - (( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )) || return - _p9k_worker_invoke \ - 'time' '(( _p9k_worker_runs_me ))' 'sleep 1' '_p9k_worker_reply _p9k_prompt_time_compute' + _p9k_worker_invoke 'time' '' 'sleep 1 || true' _p9k_prompt_time_sync +} + +_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_async \ _p9k_prompt_net_iface_sync + _p9k__async_segments_compute+=_p9k_prompt_net_iface_compute } function _p9k_prompt_net_iface_compute() { @@ -4003,29 +4028,33 @@ function _p9k_prompt_net_iface_async() { done fi + local params if _p9k_prompt_net_iface_match $_POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE; then - REPLY+="typeset -g _p9k__public_ip_vpn=1"$'\n' - REPLY+="typeset -g _p9k__public_ip_not_vpn="$'\n' + params+="typeset -g _p9k__public_ip_vpn=1"$'\n' + params+="typeset -g _p9k__public_ip_not_vpn="$'\n' else - REPLY+="typeset -g _p9k__public_ip_vpn="$'\n' - REPLY+="typeset -g _p9k__public_ip_not_vpn=1"$'\n' + params+="typeset -g _p9k__public_ip_vpn="$'\n' + params+="typeset -g _p9k__public_ip_not_vpn=1"$'\n' fi 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 - REPLY+="typeset -g _p9k__ip_ip="$'\n' + params+="typeset -g _p9k__ip_ip="$'\n' fi 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 - REPLY+="typeset -g _p9k__vpn_ip_ip="$'\n' + params+="typeset -g _p9k__vpn_ip_ip="$'\n' fi + typeset -p params } function _p9k_prompt_net_iface_sync() { - [[ -n $REPLY && $REPLY != $_p9k__net_iface_last ]] || return - _p9k__net_iface_last=$REPLY - _p9k_worker_reply $REPLY + [[ $params == $_p9k__net_iface_params ]] && return + _p9k__net_iface_params=$params + _p9k_worker_reply_begin + print -rn -- $params + _p9k_worker_reply_end } function _p9k_build_segment() { @@ -4060,8 +4089,8 @@ function _p9k_set_prompt() { IFS=$' \t\n\0' if [[ $_p9k_refresh_reason == precmd ]]; then - local f_init f_compute - for f_init f_compute in "${_p9k_async_segments[@]}"; do + local f_compute + for f_compute in "${_p9k__async_segments_compute[@]}"; do $f_compute done fi @@ -5053,7 +5082,8 @@ _p9k_init_vars() { typeset -g _p9k__last_prompt_pwd typeset -gA _p9k_display_k 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__dir_stat_cache @@ -6337,13 +6367,8 @@ function _p9k_init_cacheable() { segments+=net_iface fi for elem in $segments; do - [[ $elem == time && $_POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME == 0 ]] && continue local f_init=_p9k_prompt_${elem}_init - local f_compute=_p9k_prompt_${elem}_compute - 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 + (( $+functions[$f_init] )) && _p9k_async_segments_init+=$f_init done if [[ $_POWERLEVEL9K_TRANSIENT_PROMPT != off ]]; then @@ -6587,13 +6612,12 @@ _p9k_init() { _p9k_init_vars _p9k_restore_state || _p9k_init_cacheable - if (( $#_p9k_async_segments )); then - _p9k_worker_start - local f_init f_compute - for f_init f_compute in "${_p9k_async_segments[@]}"; do - $f_init - done - fi + local f_init + for f_init in $_p9k_async_segments_init; do + $f_init + done + + (( $#_p9k__async_segments_compute )) && _p9k_worker_start local k v for k v in ${(kv)_p9k_display_k}; do diff --git a/internal/worker.zsh b/internal/worker.zsh index ead2b6b1..ae0b8dfb 100644 --- a/internal/worker.zsh +++ b/internal/worker.zsh @@ -7,9 +7,12 @@ function _p9k_worker_main() { zmodload zsh/zselect || 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' - local req fd + local req fd buf local -a ready local -A inflight # fd => id$'\x1f'sync local -ri _p9k_worker_runs_me=1 @@ -19,7 +22,7 @@ function _p9k_worker_main() { [[ $ready[1] == -r ]] || return for fd in ${ready:1}; do if [[ $fd == 0 ]]; then - local buf= + buf= while true; do sysread -t 0 'buf[$#buf+1]' && continue (( $? == 4 )) || return @@ -31,11 +34,10 @@ function _p9k_worker_main() { if () { eval $parts[2] }; then if [[ -n $parts[3] ]]; then 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] continue fi - local REPLY= () { eval $parts[4] } fi if [[ -n $parts[1] ]]; then @@ -43,14 +45,16 @@ function _p9k_worker_main() { fi done else - local REPLY= + buf= while true; do - sysread -i $fd 'REPLY[$#REPLY+1]' && continue - (( $? == 5 )) || return + sysread -i $fd 'buf[$#buf+1]' && continue + (( $? == 5 )) || return break done 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 print -rn -- d$parts[1]$'\x1e' || return fi @@ -64,51 +68,48 @@ function _p9k_worker_main() { } } -typeset -g _p9k__worker_pid -typeset -g _p9k__worker_req_fd -typeset -g _p9k__worker_resp_fd -typeset -g _p9k__worker_shell_pid -typeset -g _p9k__worker_file_prefix -typeset -ga _p9k__worker_params -typeset -gA _p9k__worker_request_map -typeset -ga _p9k__worker_request_queue +typeset -g _p9k__worker_pid +typeset -g _p9k__worker_req_fd +typeset -g _p9k__worker_resp_fd +typeset -g _p9k__worker_shell_pid +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_queue -# invoked in master: _p9k_worker_reply ... -# -# worker also has _p9k_worker_reply but its implemented as _p9k_worker_reply_remote -function _p9k_worker_reply() { eval $1 } +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' +} -# invoked in worker where it's called _p9k_worker_reply -# usage: _p9k_worker_reply -function _p9k_worker_reply_remote() { print -rn -- e$1$'\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]... function _p9k_worker_send_params() { - [[ -z $_p9k__worker_resp_fd || $# == 0 ]] && return if [[ -n $_p9k__worker_req_fd ]]; then - { - print -rn -- $'\x1f' && typeset -pm -- ${(j.|.)${(b)@}} && print -rn -- $'\x1f\x1f\x1e' && return - } >&$_p9k__worker_req_fd + _p9k_worker_print_params ${(u)@} >&$_p9k__worker_req_fd && return _p9k_worker_stop return 1 else - _p9k__worker_params+=($*) + _p9k__worker_params+=($@) 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]... function _p9k_worker_send_functions() { - [[ -z $_p9k__worker_resp_fd || $# == 0 ]] && return - local func req - for func; do - req+="function $func() { $functions[$func] }"$'\n' - done - _p9k_worker_invoke "" "$req" "" "" + 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 @@ -155,6 +156,7 @@ function _p9k_worker_stop() { _p9k__worker_resp_fd= _p9k__worker_shell_pid= _p9k__worker_params=() + _p9k__worker_functions=() _p9k__worker_request_map=() _p9k__worker_request_queue=() return 0 @@ -215,17 +217,12 @@ function _p9k_worker_receive() { zmodload -F zsh/net/socket b:zsocket zmodload -F zsh/files b:zf_mv b:zf_rm autoload -Uz is-at-least - function _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" + () { $functions[_p9k_worker_main] }" print -r -- ${init//$'\n'/$'\x1e'} || return - if (( $#_p9k__worker_params )); then - print -rn -- $'\x1f' || return - typeset -pm -- ${(j.|.)${(b)_p9k__worker_params}} || return - print -rn -- $'\x1f\x1f\x1e' || return - _p9k__worker_params=() - fi + _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 @@ -237,7 +234,6 @@ function _p9k_worker_receive() { done _p9k__worker_request_queue=() } >&$_p9k__worker_req_fd - echo shit sent >>/tmp/log ;; *) return 1 diff --git a/notes.txt b/notes.txt index ea62842c..e40affd7 100644 --- a/notes.txt +++ b/notes.txt @@ -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 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