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

wip: instant prompt (part 4)

This commit is contained in:
romkatv 2019-10-19 14:55:38 +02:00
parent 68d6ffd844
commit 41c0a87918
2 changed files with 122 additions and 98 deletions

View file

@ -2239,7 +2239,7 @@ prompt_prompt_char() {
}
instant_prompt_prompt_char() {
_p9k_prompt_segment prompt_char_OK_VIINS "$_p9k_color1" 76 '' 0 '' ''
_p9k_prompt_segment prompt_prompt_char_OK_VIINS "$_p9k_color1" 76 '' 0 '' ''
}
################################################################
@ -3474,8 +3474,9 @@ _p9k_set_instant_prompt() {
RPROMPT=$saved_rprompt
}
typeset -gri __p9k_instant_prompt_version=1
_p9k_dump_instant_prompt() {
is-at-least 5.4 || return # `typeset -g` doesn't roundtrip in zsh prior to 5.4.
local user=${(%):-%n}
local root_dir=${__p9k_dump_file:h}
local prompt_dir=${root_dir}/p10k-$user
@ -3484,97 +3485,108 @@ _p9k_dump_instant_prompt() {
[[ -d $prompt_dir ]] || mkdir -p $prompt_dir || return
[[ -w $root_dir && -w $prompt_dir ]] || return
if [[ ! -e $root_file ]]; then
if [[ ! -e $root_file ||
($+__p9k_instant_prompt_sourced == 1 && $__p9k_instant_prompt_sourced != $__p9k_instant_prompt_version) ]]; then
local tmp=$root_file.tmp.$$
local -i fd
sysopen -a -o creat,trunc -u fd $tmp || return
{
>&$fd print -r -- "() {
emulate -L zsh
(( ! \$+__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 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
local cr=\$'\r' lf=\$'\n' esc=\$'\e[' rs=$'\x1e' us=$'\x1f'
local -i height=$_POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES
local prompt_dir=${(q)prompt_dir}
zmodload zsh/langinfo
if [[ \${langinfo[CODESET]:-} != (utf|UTF)(-|)8 ]]; then
local lc=${(q)${${${_p9k_locale:-${(M)LC_CTYPE:#*.(utf|UTF)(-|)8}}:-${(M)LC_ALL:#*.(utf|UTF)(-|)8}}}:-${(M)LANG:#*.(utf|UTF)(-|)8}}
local LC_ALL=\${lc:-\${\${(@M)\$(locale -a 2>/dev/null):#*.(utf|UTF)(-|)8}[1]:-en_US.UTF-8}}
fi"
emulate -L zsh
(( ! \$+__p9k_instant_prompt_disabled )) || return
typeset -gi __p9k_instant_prompt_disabled=1 __p9k_instant_prompt_sourced=$__p9k_instant_prompt_version
[[ -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 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
local cr=\$'\r' lf=\$'\n' esc=\$'\e[' rs=$'\x1e' us=$'\x1f'
local -i height=$_POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES
local prompt_dir=${(q)prompt_dir}
zmodload zsh/langinfo
if [[ \${langinfo[CODESET]:-} != (utf|UTF)(-|)8 ]]; then
local lc=${(q)${${${_p9k_locale:-${(M)LC_CTYPE:#*.(utf|UTF)(-|)8}}:-${(M)LC_ALL:#*.(utf|UTF)(-|)8}}}:-${(M)LANG:#*.(utf|UTF)(-|)8}}
local LC_ALL=\${lc:-\${\${(@M)\$(locale -a 2>/dev/null):#*.(utf|UTF)(-|)8}[1]:-en_US.UTF-8}}
fi"
>&$fd print -r -- '
zmodload zsh/terminfo
(( $+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=
function _p9k_prompt_length() {
local COLUMNS=1024
local -i x y=$#1 m
if (( y )); then
while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
x=y
(( y *= 2 ));
done
local xy
while (( y > x + 1 )); do
m=$(( x + (y - x) / 2 ))
typeset ${${(%):-$1%$m(l.x.y)}[-1]}=$m
done
fi
_p9k_ret=$x
}
_p9k_prompt_length "$PROMPT_EOL_MARK"
local -i fill=$((COLUMNS-_p9k_ret))
: ${__p9k_used_instant_prompt[1]//$lf/$((++height))}
local out="${(%):-$PROMPT_EOL_MARK${(pl.$fill.. .)}$cr%b%k%f%E}${(pl.$height..$lf.)}$esc${height}A$terminfo[sc]"
out+=${(%):-"$__p9k_used_instant_prompt[1]$__p9k_used_instant_prompt[2]"}
if [[ -n $__p9k_used_instant_prompt[3] ]]; then
_p9k_prompt_length "$__p9k_used_instant_prompt[2]"
local -i left_len=_p9k_ret
_p9k_prompt_length "$__p9k_used_instant_prompt[3]"
local -i gap=$((COLUMNS - left_len - _p9k_ret - ZLE_RPROMPT_INDENT))
if (( gap >= 40 )); then
out+="${(pl.$gap.. .)}${(%)${__p9k_used_instant_prompt[3]}}$terminfo[sgr0]$cr$esc${left_len}C"
fi
zmodload zsh/terminfo
(( $+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*}}")'
(( __p9k_ksh_arrays )) && >&$fd print -r -- ' setopt ksh_arrays'
(( __p9k_sh_glob )) && >&$fd print -r -- ' setopt sh_glob'
>&$fd print -r -- ' typeset -ga __p9k_used_instant_prompt=("${(@e)_p9k_t[-3,-1]}")'
(( __p9k_ksh_arrays )) && >&$fd print -r -- ' unsetopt ksh_arrays'
(( __p9k_sh_glob )) && >&$fd print -r -- ' unsetopt sh_glob'
>&$fd print -r -- '
local _p9k_ret=
function _p9k_prompt_length() {
local COLUMNS=1024
local -i x y=$#1 m
if (( y )); then
while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
x=y
(( y *= 2 ));
done
local xy
while (( y > x + 1 )); do
m=$(( x + (y - x) / 2 ))
typeset ${${(%):-$1%$m(l.x.y)}[-1]}=$m
done
fi
{ echo -n >${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$ } || return
print -rn -- "$out" || return
exec {__p9k_fd_1}>&1 {__p9k_fd_2}>&2 1>${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$
exec 2>&1
typeset -gi __p9k_instant_prompt_active=1
function _p9k_instant_prompt_precmd_first() {
_p9k_ret=$x
}
_p9k_prompt_length "$PROMPT_EOL_MARK"
local -i fill=$((COLUMNS-_p9k_ret))
: ${__p9k_used_instant_prompt[1]//$lf/$((++height))}
local out="${(%):-$PROMPT_EOL_MARK${(pl.$fill.. .)}$cr%b%k%f%E}${(pl.$height..$lf.)}$esc${height}A$terminfo[sc]"
out+=${(%):-"$__p9k_used_instant_prompt[1]$__p9k_used_instant_prompt[2]"}
if [[ -n $__p9k_used_instant_prompt[3] ]]; then
_p9k_prompt_length "$__p9k_used_instant_prompt[2]"
local -i left_len=_p9k_ret
_p9k_prompt_length "$__p9k_used_instant_prompt[3]"
local -i gap=$((COLUMNS - left_len - _p9k_ret - ZLE_RPROMPT_INDENT))
if (( gap >= 40 )); then
out+="${(pl.$gap.. .)}${(%)${__p9k_used_instant_prompt[3]}}$terminfo[sgr0]$cr$esc${left_len}C"
fi
fi
{ echo -n >${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$ } || return
print -rn -- "$out" || return
exec {__p9k_fd_1}>&1 {__p9k_fd_2}>&2 1>${TMPDIR:-/tmp}/p10k-instant-prompt-output-$$
exec 2>&1
typeset -gi __p9k_instant_prompt_active=1
function _p9k_instant_prompt_precmd_first() {
emulate -L zsh
function _p9k_instant_prompt_sched_last() {
emulate -L zsh
function _p9k_instant_prompt_sched_last() {
emulate -L zsh
if (( $+__p9k_instant_prompt_active )); then
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
typeset -gi __p9k_instant_prompt_erased=1
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
zmodload -F zsh/files b:zf_rm
zf_rm -f -- ${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh{,.zwc} 2>/dev/null
if (( $+__p9k_instant_prompt_active )); then
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
typeset -gi __p9k_instant_prompt_erased=1
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
}
zmodload zsh/sched
sched +0 _p9k_instant_prompt_sched_last
precmd_functions=(${(@)precmd_functions:#_p9k_instant_prompt_precmd_first})
zmodload -F zsh/files b:zf_rm
zf_rm -f -- ${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh{,.zwc} 2>/dev/null
setopt prompt_cr prompt_sp
fi
}
precmd_functions=(_p9k_instant_prompt_precmd_first $precmd_functions)
} && unsetopt prompt_cr prompt_sp'
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)
typeset -g __p9k_instant_prompt_dump_file=${XDG_CACHE_HOME:-~/.cache}/p10k-dump-${(%):-%n}.zsh
if source $__p9k_instant_prompt_dump_file 2>/dev/null && (( $+functions[_p9k_preinit] )); then
_p9k_preinit
fi
} || true'
} always {
exec {fd}>&-
}
@ -3660,7 +3672,6 @@ _p9k_save_status() {
}
function _p9k_dump_state() {
is-at-least 5.4 || return # `typeset -g` doesn't roundtrip in zsh prior to 5.4.
local dir=${__p9k_dump_file:h}
[[ -d $dir ]] || mkdir -p $dir || return
[[ -w $dir ]] || return
@ -3669,7 +3680,7 @@ function _p9k_dump_state() {
sysopen -a -m 600 -o creat,trunc -u fd $tmp || return
{
local include='_POWERLEVEL9K_*|_p9k_*|icons|OS|DEFAULT_COLOR|DEFAULT_COLOR_INVERTED'
local exclude='_p9k_gitstatus_*|_p9k_cache_stat_meta|_p9k_cache_stat_fprint|_p9k_cache_fprint_key|_p9k_param_sig|_p9k_public_ip|_p9k_prompt|_p9k_prompt_idx|_p9k_dump_pid|_p9k_state_dump_scheduled|_p9k_line_finished|_p9k_preexec_cmd|_p9k_status|_p9k_pipestatus|_p9k_timer_start|_p9k_region_active|_p9k_keymap|_p9k_zle_state|_p9k_async_pump_*|_p9k_cache_ephemeral'
local exclude='_p9k_instant_prompt_sig|_p9k_gitstatus_*|_p9k_cache_stat_meta|_p9k_cache_stat_fprint|_p9k_cache_fprint_key|_p9k_param_sig|_p9k_public_ip|_p9k_prompt|_p9k_prompt_idx|_p9k_dump_pid|_p9k_state_dump_scheduled|_p9k_line_finished|_p9k_preexec_cmd|_p9k_status|_p9k_pipestatus|_p9k_timer_start|_p9k_region_active|_p9k_keymap|_p9k_zle_state|_p9k_async_pump_*|_p9k_cache_ephemeral'
typeset -g __p9k_cached_param_sig=$_p9k_param_sig
typeset -p __p9k_cached_param_sig >&$fd || return
unset __p9k_cached_param_sig
@ -3784,20 +3795,24 @@ _p9k_precmd_impl() {
PROMPT='${$((_p9k_clear_instant_prompt()))+}'$PROMPT
fi
_p9k_instant_prompt_sig=$_p9k_pwd:$P9K_SSH:${(%):-%#}
(( __p9k_dumps_enabled )) || return 0
# TODO: use _POWERLEVEL9K_DISABLE_INSTANT_PROMPT.
_p9k_instant_prompt_sig=$_p9k_pwd:$P9K_SSH:${(%):-%#}
if (( ! _p9k_dump_pid )) || ! kill -0 $_p9k_dump_pid 2>/dev/null; then
_p9k_dump_pid=0
if (( _p9k_prompt_idx == 1 )) then
_p9k_set_instant_prompt
(( _POWERLEVEL9K_DISABLE_INSTANT_PROMPT )) || _p9k_set_instant_prompt
if (( !_p9k_state_restored )); then
_p9k_dump_instant_prompt
if (( !_POWERLEVEL9K_DISABLE_INSTANT_PROMPT )); then
_p9k_dump_instant_prompt
_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
fi
_p9k_dump_state
_p9k_state_dump_scheduled=0
_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
elif [[ "${(pj:\x1f:)__p9k_used_instant_prompt}" != "${(e)_p9k_instant_prompt}" ]]; then
elif [[ $_POWERLEVEL9K_DISABLE_INSTANT_PROMPT == 0 &&
( $+__p9k_instant_prompt_sourced == 1 && $__p9k_instant_prompt_sourced != $__p9k_instant_prompt_version ||
"${(pj:\x1f:)__p9k_used_instant_prompt}" != "${(e)_p9k_instant_prompt}" ) ]]; then
_p9k_dump_instant_prompt
if (( ! $+_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig] )); then
_p9k_dump_state
@ -3805,9 +3820,9 @@ _p9k_precmd_impl() {
_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
fi
fi
elif (( _p9k_state_dump_scheduled || ! $+_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig] )); then
elif (( _p9k_state_dump_scheduled || ! (_POWERLEVEL9K_DISABLE_INSTANT_PROMPT || $+_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]) )); then
(
if (( ! $+_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig] )); then
if ! (( _POWERLEVEL9K_DISABLE_INSTANT_PROMPT || $+_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig] )); then
_p9k_set_instant_prompt
_p9k_dump_instant_prompt
_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
@ -3816,7 +3831,7 @@ _p9k_precmd_impl() {
) &!
_p9k_dump_pid=$!
_p9k_state_dump_scheduled=0
_p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
(( _POWERLEVEL9K_DISABLE_INSTANT_PROMPT )) || _p9k_dumped_instant_prompt_sigs[$_p9k_instant_prompt_sig]=1
fi
fi
}
@ -4691,9 +4706,9 @@ _p9k_init_prompt() {
_p9k_reset_on_line_finish=$((!$?))
_p9k_t+=$_p9k_gap_pre
_p9k_gap_pre='${(e)_p9k_t['$#_p9k_t']}'
_p9k_gap_pre='${(e)_p9k_t['$(($#_p9k_t - __p9k_ksh_arrays))']}'
_p9k_t+=$_p9k_prompt_prefix_left
_p9k_prompt_prefix_left='${(e)_p9k_t['$#_p9k_t']}'
_p9k_prompt_prefix_left='${(e)_p9k_t['$(($#_p9k_t - __p9k_ksh_arrays))']}'
}
_p9k_init_ssh() {
@ -4734,7 +4749,7 @@ _p9k_must_init() {
'${GITSTATUS_ENABLE_LOGGING}' '${GITSTATUS_DAEMON}' '${GITSTATUS_NUM_THREADS}'
'${DEFAULT_USER}' '${ZLE_RPROMPT_INDENT}' '${P9K_SSH}' '${__p9k_ksh_arrays}'
'${__p9k_sh_glob}' '${parameters[transient_rprompt]}' '${ITERM_SHELL_INTEGRATION_INSTALLED}'
'v9')
'${PROMPT_EOL_MARK}' 'LANG' 'LC_ALL' 'LC_CTYPE' 'v9')
IFS=$'\2' param_sig="${(e)param_sig}"
[[ $param_sig == $_p9k_param_sig ]] && return 1
[[ -n $_p9k_param_sig ]] && _p9k_deinit
@ -5060,6 +5075,13 @@ typeset -gi __p9k_enabled=0
typeset -gi __p9k_configured=0
typeset -gri __p9k_instant_prompt_disabled=1
# `typeset -g` doesn't roundtrip in zsh prior to 5.4.
if is-at-least 5.4; then
typeset -gri __p9k_dumps_enabled=1
else
typeset -gri __p9k_dumps_enabled=0
fi
prompt_powerlevel9k_setup() {
emulate -L zsh
setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst}

View file

@ -27,7 +27,7 @@
return
fi
typeset -gr __p9k_dump_file=${XDG_CACHE_HOME:-~/.cache}/p10k-dump-${(%):-%n}.zsh
if [[ -z $__p9k_dump_file(.zwc|)(#qNW) ]] && source $__p9k_dump_file 2>/dev/null && (( $+functions[_p9k_preinit] )); then
if [[ $__p9k_dump_file != $__p9k_instant_prompt_dump_file ]] && source $__p9k_dump_file 2>/dev/null && (( $+functions[_p9k_preinit] )); then
_p9k_preinit
fi
typeset -gr __p9k_sourced=1
@ -40,5 +40,7 @@
source $__p9k_root_dir/internal/p10k.zsh || true
}
(( ! $+__p9k_instant_prompt_active )) || unsetopt local_options prompt_cr prompt_sp
(( ${#__p9k_src_opts} )) && setopt ${__p9k_src_opts[@]}
'builtin' 'unset' '__p9k_src_opts'