wip: instant prompt (part 2)

pull/275/head
romkatv 5 years ago
parent 164fcad558
commit 41084c089e

@ -3444,36 +3444,58 @@ function _p9k_set_prompt() {
IFS=$ifs IFS=$ifs
} }
_p9k_set_instant_prompt() {
local saved_prompt=$PROMPT
local saved_rprompt=$RPROMPT
_p9k_set_prompt instant_
typeset -g _p9k_instant_prompt=$PROMPT$'\x1f'$_p9k_prompt$'\x1f'$RPROMPT
PROMPT=$saved_prompt
RPROMPT=$saved_rprompt
}
_p9k_dump_instant_prompt() { _p9k_dump_instant_prompt() {
is-at-least 5.4 || return # `typeset -g` doesn't roundtrip in zsh prior to 5.4. is-at-least 5.4 || return # `typeset -g` doesn't roundtrip in zsh prior to 5.4.
local dir=${__p9k_dump_file:h} local user=${(%):-%n}
local file=$dir/p10k-instant-prompt-${(%):-%n}.zsh local root_dir=${__p9k_dump_file:h}
[[ -d $dir ]] || mkdir $dir || return local prompt_dir=${root_dir}/powerlevel10k-$user
[[ -w $dir ]] || return local root_file=$root_dir/p10k-instant-prompt-$user.zsh
local tmp=$file.$$-$EPOCHREALTIME-$RANDOM local prompt_file=$prompt_dir/prompt-${#_p9k_pwd}
[[ -d $prompt_dir ]] || mkdir -p $prompt_dir || return
[[ -w $root_dir && -w $prompt_dir ]] || return
if [[ ! -e $root_file ]]; then
local tmp=$root_file.tmp.$$
local -i fd local -i fd
sysopen -a -m 600 -o creat,trunc -u fd $tmp || return sysopen -a -o creat,trunc -u fd $tmp || return
{ {
>&$fd print -rn -- "() { >&$fd print -r -- "() {
emulate zsh emulate -L zsh
[[ \$ZSH_VERSION == ${(q)ZSH_VERSION} && \$ZSH_PATCHLEVEL == ${(q)ZSH_PATCHLEVEL} ]] || return 0 (( ! \$+__p9k_instant_prompt_disabled )) || return
typeset -gi __p9k_instant_prompt_disabled=1 __p9k_instant_prompt_sourced=1
[[ -t 0 && -t 1 && -t 2 && \$ZSH_VERSION == ${(q)ZSH_VERSION} && \$ZSH_PATCHLEVEL == ${(q)ZSH_PATCHLEVEL} ]] || return
local -i ZLE_RPROMPT_INDENT=${ZLE_RPROMPT_INDENT:-1} local -i ZLE_RPROMPT_INDENT=${ZLE_RPROMPT_INDENT:-1}
local PROMPT_EOL_MARK=${(q)PROMPT_EOL_MARK-%B%S%#%s%b} local PROMPT_EOL_MARK=${(q)PROMPT_EOL_MARK-%B%S%#%s%b}
[[ -n \$SSH_CLIENT || -n \$SSH_TTY || -n \$SSH_CONNECTION ]] && local ssh=1 || local ssh=0 [[ -n \$SSH_CLIENT || -n \$SSH_TTY || -n \$SSH_CONNECTION ]] && local ssh=1 || local ssh=0
local key=\${(%):-%/}\$'\0'\$ssh\$'\0'\${(%):-%#} local cr=\$'\r' lf=\$'\n' esc=\$'\e[' rs=$'\x1e' us=$'\x1f'
local cr=\$'\r' lf=\$'\n' esc=\$'\e['
local -i height=$_POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES local -i height=$_POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES
local prompt_dir=${(q)prompt_dir}
zmodload zsh/langinfo zmodload zsh/langinfo
if [[ \${langinfo[CODESET]:-} != (utf|UTF)(-|)8 ]]; then if [[ \${langinfo[CODESET]:-} != (utf|UTF)(-|)8 ]]; then
local LC_ALL=\${\${(@M)\$(locale -a 2>/dev/null):#*.(utf|UTF)(-|)8}[1]:-en_US.UTF-8} local lc=${(q)${${${_p9k_locale:-${(M)LC_CTYPE:#*.(utf|UTF)(-|)8}}:-${(M)LC_ALL:#*.(utf|UTF)(-|)8}}}:-${(M)LANG:#*.(utf|UTF)(-|)8}}
fi local LC_ALL=\${lc:-\${\${(@M)\$(locale -a 2>/dev/null):#*.(utf|UTF)(-|)8}[1]:-en_US.UTF-8}}
" fi"
>&$fd typeset -p __p9k_instant_prompt _p9k_t
>&$fd print -r -- ' >&$fd print -r -- '
local prompts=("${(@e)${(@0)__p9k_instant_prompt[$key]}}")
(( $#prompts == 3 )) || { unset _p9k_t; return }
zmodload zsh/terminfo zmodload zsh/terminfo
(( $+terminfo[cuu] && $+terminfo[cuf] && $+terminfo[sgr0] && $+terminfo[ed] && $+terminfo[sc] && $+terminfo[rc] )) || { unset _p9k_t; return } (( $+terminfo[cuu] && $+terminfo[cuf] && $+terminfo[sgr0] && $+terminfo[ed] && $+terminfo[sc] && $+terminfo[rc] )) || return
local pwd=${(%):-%/}
local prompt_file=$prompt_dir/prompt-${#pwd}
local key=$pwd:$ssh:${(%):-%#}
local content
{ content="$(<$prompt_file)" } 2>/dev/null || return
local tail=${content##*$rs$key$us}
[[ ${#tail} != ${#content} ]] || return
local -a _p9k_t=("${(@ps:$us:)${tail%%$rs*}}")
typeset -ga __p9k_used_instant_prompt=("${(@e)_p9k_t[-3,-1]}")
local _p9k_ret= local _p9k_ret=
function _p9k_prompt_length() { function _p9k_prompt_length() {
local COLUMNS=1024 local COLUMNS=1024
@ -3491,44 +3513,52 @@ _p9k_dump_instant_prompt() {
fi fi
_p9k_ret=$x _p9k_ret=$x
} }
unsetopt localoptions prompt_cr prompt_sp
_p9k_prompt_length "$PROMPT_EOL_MARK" _p9k_prompt_length "$PROMPT_EOL_MARK"
local -i fill=$((COLUMNS-_p9k_ret)) local -i fill=$((COLUMNS-_p9k_ret))
: ${prompts[1]//$lf/$((++height))} : ${__p9k_used_instant_prompt[1]//$lf/$((++height))}
local out="${(%):-$eol_mark${(pl.$fill.. .)}$cr%b%k%f%E}${(pl.$height..$lf.)}$esc${height}A$terminfo[sc]" local out="${(%):-$PROMPT_EOL_MARK${(pl.$fill.. .)}$cr%b%k%f%E}${(pl.$height..$lf.)}$esc${height}A$terminfo[sc]"
out+=${(%):-"$prompts[1]$prompts[2]"} out+=${(%):-"$__p9k_used_instant_prompt[1]$__p9k_used_instant_prompt[2]"}
if [[ -n $prompts[3] ]]; then if [[ -n $__p9k_used_instant_prompt[3] ]]; then
_p9k_prompt_length "$prompts[2]" _p9k_prompt_length "$__p9k_used_instant_prompt[2]"
local -i left_len=_p9k_ret local -i left_len=_p9k_ret
_p9k_prompt_length "$prompts[3]" _p9k_prompt_length "$__p9k_used_instant_prompt[3]"
local -i gap=$((COLUMNS - left_len - _p9k_ret - ZLE_RPROMPT_INDENT)) local -i gap=$((COLUMNS - left_len - _p9k_ret - ZLE_RPROMPT_INDENT))
if (( gap >= 40 )); then if (( gap >= 40 )); then
out+="${(pl.$gap.. .)}${(%)${prompts[3]}}$terminfo[sgr0]$cr$esc${left_len}C" out+="${(pl.$gap.. .)}${(%)${__p9k_used_instant_prompt[3]}}$terminfo[sgr0]$cr$esc${left_len}C"
fi fi
fi fi
print -rn -- "$out" { echo -n >${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$ } || return
unset _p9k_t print -rn -- "$out" || return
unset -f _p9k_prompt_length exec {__p9k_fd_1}>&1 {__p9k_fd_2}>&2 1>${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$
p10k-unsafe-first-prompt-hook() { exec 2>&1
unsetopt localoptions typeset -gi __p9k_instant_prompt_active=1
setopt prompt_cr prompt_sp function _p9k_instant_prompt_precmd_first() {
print -rn -- "${terminfo[rc]}${terminfo[sgr0]}${terminfo[ed]}" emulate -L zsh
function _p9k_instant_prompt_sched_last() {
emulate -L zsh
} }
}' zmodload zsh/sched
sched +0 _p9k_instant_prompt_sched_last
precmd_functions=(${(@)precmd_functions:#_p9k_instant_prompt_precmd_first})
}
precmd_functions=(_p9k_instant_prompt_precmd_first $precmd_functions)
} && unsetopt prompt_cr prompt_sp'
} always { } always {
exec {fd}>&- exec {fd}>&-
} }
zf_mv -f $tmp $file || return zf_mv -f $tmp $root_file || return
zcompile $file || zf_rm -f $file.zwc zcompile $root_file || return
} fi
_p9k_set_instant_prompt() { local tmp=$prompt_file.tmp.$$
local saved_prompt=$PROMPT zf_mv -f $prompt_file $tmp 2>/dev/null
local saved_rprompt=$RPROMPT if [[ "$(<$prompt_file)" == *$'\x1e'$_p9k_instant_prompt_sig$'\x1f'* ]] 2>/dev/null; then
_p9k_set_prompt instant_ echo -n >$tmp || return
__p9k_instant_prompt[$_p9k_instant_prompt_sig]=$PROMPT$'\0'$_p9k_prompt$'\0'$RPROMPT fi
PROMPT=$saved_prompt
RPROMPT=$saved_rprompt { print -rn -- entry=$'\x1e'$_p9k_instant_prompt_sig$'\x1f'${(pj:\x1f:)_p9k_t}$'\x1f'$_p9k_instant_prompt >>$tmp } 2>/dev/null || return
zf_mv -f $tmp $prompt_file 2>/dev/null || return
} }
function _p9k_update_prompt() { function _p9k_update_prompt() {
@ -3620,7 +3650,7 @@ function _p9k_dump_state() {
exec {fd}>&- exec {fd}>&-
} }
zf_mv -f $tmp $__p9k_dump_file || return zf_mv -f $tmp $__p9k_dump_file || return
zcompile $__p9k_dump_file || zf_rm -f $__p9k_dump_file.zwc zcompile $__p9k_dump_file
} }
function _p9k_restore_state() { function _p9k_restore_state() {
@ -3631,13 +3661,28 @@ function _p9k_restore_state() {
_p9k_state_restored=1 _p9k_state_restored=1
} always { } always {
unset __p9k_cached_param_sig unset __p9k_cached_param_sig
if (( !_p9k_state_restored && $+functions[_p9k_preinit] )); then if (( !_p9k_state_restored )); then
if (( $+functions[_p9k_preinit] )); then
unfunction _p9k_preinit unfunction _p9k_preinit
(( $+functions[gitstatus_stop] )) && gitstatus_stop POWERLEVEL9K (( $+functions[gitstatus_stop] )) && gitstatus_stop POWERLEVEL9K
fi fi
local user=${(%):-%n}
local root_dir=${__p9k_dump_file:h}
zf_rm -f -- $root_dir/p10k-instant-prompt-$user.zsh{,.zwc} ${root_dir}/powerlevel10k-$user/prompt-*(N) 2>/dev/null
fi
} }
} }
function _p9k_clear_instant_prompt() {
(( $+__p9k_instant_prompt_active )) || return
exec 1>&$__p9k_fd_1 2>&$__p9k_fd_2 {__p9k_fd_1}>&- {__p9k_fd_2}>&-
unset __p9k_fd_1 __p9k_fd_2 __p9k_instant_prompt_active
print -rn -- $terminfo[rc]$terminfo[sgr0]$terminfo[ed]
if [[ -s ${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$ ]]; then
cat ${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$
fi
}
_p9k_precmd_impl() { _p9k_precmd_impl() {
emulate -L zsh emulate -L zsh
setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst} setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst}
@ -3696,35 +3741,46 @@ _p9k_precmd_impl() {
_p9k_keymap=main _p9k_keymap=main
_p9k_zle_state=insert _p9k_zle_state=insert
if ! zle; then
(( ++_p9k_prompt_idx )) (( ++_p9k_prompt_idx ))
fi fi
fi
_p9k_refresh_reason=precmd _p9k_refresh_reason=precmd
_p9k_set_prompt _p9k_set_prompt
_p9k_refresh_reason='' _p9k_refresh_reason=''
_p9k_instant_prompt_sig=$_p9k_pwd$'\0'$P9K_SSH$'\0'${(%):-%#} if (( $+__p9k_instant_prompt_active )); then
functions -M _p9k_clear_instant_prompt
PROMPT='${$((_p9k_clear_instant_prompt()))+}'$PROMPT
fi
_p9k_instant_prompt_sig=$_p9k_pwd:$P9K_SSH:${(%):-%#}
if ! zle && { (( ! _p9k_dump_pid )) || ! kill -0 $_p9k_dump_pid 2>/dev/null }; then if (( ! _p9k_dump_pid )) || ! kill -0 $_p9k_dump_pid 2>/dev/null; then
_p9k_dump_pid=0 _p9k_dump_pid=0
if (( _p9k_prompt_idx == 1 && !_p9k_state_restored )); then if (( _p9k_prompt_idx == 1 )) then
_p9k_set_instant_prompt _p9k_set_instant_prompt
if (( !_p9k_state_restored )); then
_p9k_dump_instant_prompt _p9k_dump_instant_prompt
_p9k_dump_state _p9k_dump_state
_p9k_state_dump_scheduled=0 _p9k_state_dump_scheduled=0
else _p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
local -i instant=0 elif [[ "${(j::)__p9k_used_instant_prompt}" != $_p9k_instant_prompt ]]; then
if [[ -z $__p9k_instant_prompt[$_p9k_instant_prompt_sig] ]]; then _p9k_dump_instant_prompt
_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
fi
elif (( _p9k_state_dump_scheduled || ! $+_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig] )); then
(
if (( ! $+_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig] )); then
_p9k_set_instant_prompt _p9k_set_instant_prompt
instant=1 _p9k_dump_instant_prompt
fi
if (( _p9k_state_dump_scheduled )); then
_p9k_dump_state
fi fi
if (( _p9k_state_dump_scheduled || instant )); then ) &!
( (( instant )) && _p9k_dump_instant_prompt; _p9k_dump_state ) &!
_p9k_dump_pid=$! _p9k_dump_pid=$!
_p9k_state_dump_scheduled=0 _p9k_state_dump_scheduled=0
fi _p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
fi fi
fi fi
} }
@ -3736,6 +3792,7 @@ _p9k_precmd() {
[[ -o sh_glob ]] && __p9k_sh_glob=1 || __p9k_sh_glob=0 [[ -o sh_glob ]] && __p9k_sh_glob=1 || __p9k_sh_glob=0
unsetopt localoptions unsetopt localoptions
(( $+__p9k_instant_prompt_active )) && setopt prompt_sp prompt_cr
prompt_opts=(percent subst) prompt_opts=(percent subst)
[[ ! -o prompt_sp ]] || prompt_opts+=sp [[ ! -o prompt_sp ]] || prompt_opts+=sp
[[ ! -o prompt_cr ]] || prompt_opts+=cr [[ ! -o prompt_cr ]] || prompt_opts+=cr
@ -3936,7 +3993,9 @@ function _p9k_prompt_overflow_bug() {
} }
_p9k_init_vars() { _p9k_init_vars() {
typeset -gA _p9k_dumped_instant_prompt_sigs
typeset -g _p9k_instant_prompt_sig typeset -g _p9k_instant_prompt_sig
typeset -g _p9k_instant_prompt
typeset -gi _p9k_state_dump_scheduled typeset -gi _p9k_state_dump_scheduled
typeset -gi _p9k_dump_pid typeset -gi _p9k_dump_pid
typeset -gi _p9k_prompt_idx typeset -gi _p9k_prompt_idx
@ -4035,8 +4094,6 @@ _p9k_init_vars() {
typeset -g P9K_VISUAL_IDENTIFIER typeset -g P9K_VISUAL_IDENTIFIER
typeset -g P9K_CONTENT typeset -g P9K_CONTENT
typeset -g P9K_GAP typeset -g P9K_GAP
typeset -gA __p9k_instant_prompt
} }
_p9k_init_params() { _p9k_init_params() {
@ -4638,7 +4695,7 @@ _p9k_must_init() {
'${GITSTATUS_ENABLE_LOGGING}' '${GITSTATUS_DAEMON}' '${GITSTATUS_NUM_THREADS}' '${GITSTATUS_ENABLE_LOGGING}' '${GITSTATUS_DAEMON}' '${GITSTATUS_NUM_THREADS}'
'${DEFAULT_USER}' '${ZLE_RPROMPT_INDENT}' '${P9K_SSH}' '${__p9k_ksh_arrays}' '${DEFAULT_USER}' '${ZLE_RPROMPT_INDENT}' '${P9K_SSH}' '${__p9k_ksh_arrays}'
'${__p9k_sh_glob}' '${parameters[transient_rprompt]}' '${ITERM_SHELL_INTEGRATION_INSTALLED}' '${__p9k_sh_glob}' '${parameters[transient_rprompt]}' '${ITERM_SHELL_INTEGRATION_INSTALLED}'
'v8') 'v9')
IFS=$'\2' param_sig="${(e)param_sig}" IFS=$'\2' param_sig="${(e)param_sig}"
[[ $param_sig == $_p9k_param_sig ]] && return 1 [[ $param_sig == $_p9k_param_sig ]] && return 1
[[ -n $_p9k_param_sig ]] && _p9k_deinit [[ -n $_p9k_param_sig ]] && _p9k_deinit
@ -4935,6 +4992,7 @@ _p9k_deinit() {
typeset -gi __p9k_enabled=0 typeset -gi __p9k_enabled=0
typeset -gi __p9k_configured=0 typeset -gi __p9k_configured=0
typeset -gri __p9k_instant_prompt_disabled=1
prompt_powerlevel9k_setup() { prompt_powerlevel9k_setup() {
emulate -L zsh emulate -L zsh

Loading…
Cancel
Save