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

rewrite the handling of custom prompt in virtualenv (#2540)

This commit is contained in:
Roman Perepelitsa 2024-01-20 10:30:56 +01:00
parent ecf91710c0
commit 8fefef2285

View file

@ -4255,23 +4255,33 @@ function instant_prompt_chezmoi_shell() {
} }
function _p9k_parse_virtualenv_cfg() { function _p9k_parse_virtualenv_cfg() {
local cfg=$1 typeset -ga reply=(0)
echo $cfg [[ -f $1 && -r $1 ]] || return
typeset -g reply
[[ -f $cfg && -r $cfg ]] || return
local -a lines local cfg
lines=(${(f)"$(<$cfg)"}) || return cfg=$(<$1) || return
local line prompt
local -a match mbegin mend local -a match mbegin mend
for line in $lines; do [[ $'\n'$cfg$'\n' == (#b)*$'\n'prompt[$' \t']#=[$' \t']#([^$' \t']#)[$' \t']#$'\n'* ]] || return
if [[ $line =~ 'prompt = (.*)' ]]; then local res=$match[1]
prompt=$match[1] if [[ $res == (\"*\"|\'*\') ]]; then
eval "reply=$prompt" # The string is quoted in python style, which isn't the same as quoting in zsh.
return # For example, the literal 'foo"\'bar' denotes foo"'bar in python but in zsh
# it is malformed.
#
# We cheat a bit and impelement not exactly correct unquoting. It may produce
# different visual results but won't perform unintended expansions or bleed out
# any escape sequences.
#
# Note that venv performs unusual and obviously unintended expansions on the
# value of `prompt`: single-word expansions are performed twice by `activate`,
# and then again on every prompt if `prompt_subst` is in effect. While in general
# I am OK with being bug-compatible with other software, the bugs in venv are a
# bit too extreme for my comfort. I am going to disable all expansions and
# display the configured prompt literally.
res=${(Vg:e:)${res[2,-2]}}
fi fi
done reply=(1 "$res")
} }
################################################################ ################################################################
@ -4283,20 +4293,21 @@ prompt_virtualenv() {
if (( _POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION )) && _p9k_python_version; then if (( _POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION )) && _p9k_python_version; then
msg="${_p9k__ret//\%/%%} " msg="${_p9k__ret//\%/%%} "
fi fi
local cfg=$VIRTUAL_ENV/pyvenv.cfg
if ! _p9k_cache_stat_get $0 $cfg; then
local -a reply
_p9k_parse_virtualenv_cfg $cfg
_p9k_cache_stat_set "${reply[@]}"
fi
if (( _p9k__cache_val[1] )); then
local v=$_p9k__cache_val[2]
else
local v=${VIRTUAL_ENV:t} local v=${VIRTUAL_ENV:t}
if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($v) " ]]; then if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($v) " ]]; then
v=$VIRTUAL_ENV_PROMPT[2,-3] v=$VIRTUAL_ENV_PROMPT[2,-3]
elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then
v=${VIRTUAL_ENV:h:t} v=${VIRTUAL_ENV:h:t}
fi fi
local cfg="$VIRTUAL_ENV/pyvenv.cfg"
if ! _p9k_cache_stat_get $0 $cfg; then
local -a reply
_p9k_parse_virtualenv_cfg $cfg
_p9k_cache_stat_set $reply
fi
if [[ -n $_p9k__cache_val[1] ]]; then
v=$_p9k__cache_val[1]
fi fi
msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${v//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${v//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER"
case $_POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV in case $_POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV in
@ -9446,7 +9457,7 @@ if [[ $__p9k_dump_file != $__p9k_instant_prompt_dump_file && -n $__p9k_instant_p
zf_rm -f -- $__p9k_instant_prompt_dump_file{,.zwc} 2>/dev/null zf_rm -f -- $__p9k_instant_prompt_dump_file{,.zwc} 2>/dev/null
fi fi
typeset -g P9K_VERSION=1.19.13 typeset -g P9K_VERSION=1.19.14
unset VSCODE_SHELL_INTEGRATION unset VSCODE_SHELL_INTEGRATION
_p9k_init_ssh _p9k_init_ssh