From ffc1f175c28acf6af8fc3976abbc83b746c7555b Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 11:27:57 +0800 Subject: [PATCH 01/13] Document some logic in prompt_virtualenv() --- internal/p10k.zsh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index d105665ea..7dbb01b0d 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4285,11 +4285,19 @@ prompt_virtualenv() { if (( _POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION )) && _p9k_python_version; then msg="${_p9k__ret//\%/%%} " fi - local v=${VIRTUAL_ENV:t} + # Determine virtualenv name + # VIRTUAL_ENV* are set inside virtualenvs. VIRTUAL_ENV is the virtualenv's + # on-disk path, VIRTUAL_ENV_PROMPT is `(name_of_virtualenv) `. + local v + # Use custom name if it was set (python -m venv -p "foo" .venv) if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($v) " ]]; then v=$VIRTUAL_ENV_PROMPT[2,-3] + # Use parent directory name if virtualenv name is generic (eg .venv) elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then v=${VIRTUAL_ENV:h:t} + # Otherwise use the virtualenv name as-is + else + v=${VIRTUAL_ENV:t} fi msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${v//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" case $_POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV in From e3e5605a61c70460c459e7536eb16f9c40761707 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 13:33:59 +0800 Subject: [PATCH 02/13] Add Poetry support to virtualenv Heavily based on #1994. Integrate Poetry virtualenv detection into the existing virtualenv segment. For poetry virtualenvs, use the package name as reported by `poetry version`. --- config/p10k-classic.zsh | 2 ++ config/p10k-lean-8colors.zsh | 2 ++ config/p10k-lean.zsh | 2 ++ config/p10k-pure.zsh | 2 ++ config/p10k-rainbow.zsh | 2 ++ internal/p10k.zsh | 56 ++++++++++++++++++++++++++---------- 6 files changed, 51 insertions(+), 15 deletions(-) diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh index 5ea8faec9..eb9b82329 100644 --- a/config/p10k-classic.zsh +++ b/config/p10k-classic.zsh @@ -944,6 +944,8 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false + # Check for virtual environments managed by poetry + typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/config/p10k-lean-8colors.zsh b/config/p10k-lean-8colors.zsh index 38328c4e4..55a1c2216 100644 --- a/config/p10k-lean-8colors.zsh +++ b/config/p10k-lean-8colors.zsh @@ -922,6 +922,8 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false + # Check for virtual environments managed by poetry + typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh index 7cb97e90d..9327c89c5 100644 --- a/config/p10k-lean.zsh +++ b/config/p10k-lean.zsh @@ -918,6 +918,8 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false + # Check for virtual environments managed by poetry + typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/config/p10k-pure.zsh b/config/p10k-pure.zsh index 97c1a2072..26cdcaf67 100644 --- a/config/p10k-pure.zsh +++ b/config/p10k-pure.zsh @@ -93,6 +93,8 @@ # Don't show Python version. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION=false typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= + # Check for virtual environments managed by poetry + typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Blue current directory. typeset -g POWERLEVEL9K_DIR_FOREGROUND=$blue diff --git a/config/p10k-rainbow.zsh b/config/p10k-rainbow.zsh index ab36838f6..e18e82a48 100644 --- a/config/p10k-rainbow.zsh +++ b/config/p10k-rainbow.zsh @@ -988,6 +988,8 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false + # Check for virtual environments managed by poetry + typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 7dbb01b0d..5a1cd8f85 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -2403,6 +2403,7 @@ _p9k_prompt_load_sync() { # The first argument says whether to capture stderr (1) or ignore it (0). # The second argument can be empty or a file. If it's a file, the # output of the command is presumed to potentially depend on it. +# Returns output in $_p9k__ret. function _p9k_cached_cmd() { local cmd=$commands[$3] [[ -n $cmd ]] || return @@ -4280,32 +4281,56 @@ function instant_prompt_chezmoi_shell() { # Virtualenv: current working virtualenv # More information on virtualenv (Python): # https://virtualenv.pypa.io/en/latest/ + +_virtualenv_VIRTUAL_ENV() { + local n + # VIRTUAL_ENV is the on-disk path, VIRTUAL_ENV_PROMPT is `(name_of_virtualenv) `. + # Use custom name if it was set (python -m venv -p "foo" .venv) + if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($n) " ]]; then + n=$VIRTUAL_ENV_PROMPT[2,-3] + # Use parent directory name if virtualenv name is generic (eg .venv) + elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then + n=${VIRTUAL_ENV:h:t} + # Otherwise use the virtualenv name as-is + else + n=${VIRTUAL_ENV:t} + fi + echo "$n" +} +_virtualenv_poetry() { + local idx=$1 + local dir=${_p9k__parent_dirs[idx]} + local pyproject="$dir/pyproject.toml" + _p9k_cached_cmd 0 '' poetry -C "$dir" version + # Return the first word only, eg the value of pyproject.toml's `poetry.name` + echo "${_p9k__ret%% *}" +} prompt_virtualenv() { local msg='' if (( _POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION )) && _p9k_python_version; then msg="${_p9k__ret//\%/%%} " fi - # Determine virtualenv name - # VIRTUAL_ENV* are set inside virtualenvs. VIRTUAL_ENV is the virtualenv's - # on-disk path, VIRTUAL_ENV_PROMPT is `(name_of_virtualenv) `. - local v - # Use custom name if it was set (python -m venv -p "foo" .venv) - if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($v) " ]]; then - v=$VIRTUAL_ENV_PROMPT[2,-3] - # Use parent directory name if virtualenv name is generic (eg .venv) - elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then - v=${VIRTUAL_ENV:h:t} - # Otherwise use the virtualenv name as-is + # Determine virtualenv name with a few strategies + local n='' + if [[ -n $VIRTUAL_ENV ]]; then + n=$(_virtualenv_VIRTUAL_ENV) else - v=${VIRTUAL_ENV:t} + if [[ $POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY == true ]]; then + local start end + _p9k_upglob pyproject.toml + local idx=$? + if (( idx > 0 )); then + n=$(_virtualenv_poetry $idx) + fi + fi fi - msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${v//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" + msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${n//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" case $_POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV in false) _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${(M)${#P9K_PYENV_PYTHON_VERSION}:#0}' "$msg" ;; if-different) - _p9k_escape $v + _p9k_escape $n _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${${:-'$_p9k__ret'}:#$_p9k__pyenv_version}' "$msg" ;; *) @@ -4315,7 +4340,8 @@ prompt_virtualenv() { } _p9k_prompt_virtualenv_init() { - typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$VIRTUAL_ENV' + # Init if VIRTUAL_ENV is non-null or poetry is installed + typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${VIRTUAL_ENV:-${commands[poetry]:-${${+functions[poetry]}:#0}}}' } # _p9k_read_pyenv_like_version_file [prefix] From a79a934f2748cdbe8da06a01126cbbdddf0fa986 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 13:49:13 +0800 Subject: [PATCH 03/13] Remove POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY Added for debugging purposes. --- config/p10k-classic.zsh | 2 -- config/p10k-lean-8colors.zsh | 2 -- config/p10k-lean.zsh | 2 -- config/p10k-pure.zsh | 2 -- config/p10k-rainbow.zsh | 2 -- internal/p10k.zsh | 12 +++++------- 6 files changed, 5 insertions(+), 17 deletions(-) diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh index eb9b82329..5ea8faec9 100644 --- a/config/p10k-classic.zsh +++ b/config/p10k-classic.zsh @@ -944,8 +944,6 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false - # Check for virtual environments managed by poetry - typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/config/p10k-lean-8colors.zsh b/config/p10k-lean-8colors.zsh index 55a1c2216..38328c4e4 100644 --- a/config/p10k-lean-8colors.zsh +++ b/config/p10k-lean-8colors.zsh @@ -922,8 +922,6 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false - # Check for virtual environments managed by poetry - typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh index 9327c89c5..7cb97e90d 100644 --- a/config/p10k-lean.zsh +++ b/config/p10k-lean.zsh @@ -918,8 +918,6 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false - # Check for virtual environments managed by poetry - typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/config/p10k-pure.zsh b/config/p10k-pure.zsh index 26cdcaf67..97c1a2072 100644 --- a/config/p10k-pure.zsh +++ b/config/p10k-pure.zsh @@ -93,8 +93,6 @@ # Don't show Python version. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION=false typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= - # Check for virtual environments managed by poetry - typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Blue current directory. typeset -g POWERLEVEL9K_DIR_FOREGROUND=$blue diff --git a/config/p10k-rainbow.zsh b/config/p10k-rainbow.zsh index e18e82a48..ab36838f6 100644 --- a/config/p10k-rainbow.zsh +++ b/config/p10k-rainbow.zsh @@ -988,8 +988,6 @@ # If set to "false", won't show virtualenv if pyenv is already shown. # If set to "if-different", won't show virtualenv if it's the same as pyenv. typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=false - # Check for virtual environments managed by poetry - typeset -g POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY=true # Separate environment name from Python version only with a space. typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= # Custom icon. diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 5a1cd8f85..d341f772b 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4315,13 +4315,11 @@ prompt_virtualenv() { if [[ -n $VIRTUAL_ENV ]]; then n=$(_virtualenv_VIRTUAL_ENV) else - if [[ $POWERLEVEL9K_VIRTUALENV_ENABLE_POETRY == true ]]; then - local start end - _p9k_upglob pyproject.toml - local idx=$? - if (( idx > 0 )); then - n=$(_virtualenv_poetry $idx) - fi + local start end + _p9k_upglob pyproject.toml + local idx=$? + if (( idx > 0 )); then + n=$(_virtualenv_poetry $idx) fi fi msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${n//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" From e162e2a891f1d7aba7cb533fdbb6025036c404e4 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 13:50:45 +0800 Subject: [PATCH 04/13] Fix up function return value passing Don't use echo, instead set a variable which the calling function checks. This is the standard in p10, presumably because it's faster..? --- internal/p10k.zsh | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index d341f772b..ae77cd8ce 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4287,15 +4287,14 @@ _virtualenv_VIRTUAL_ENV() { # VIRTUAL_ENV is the on-disk path, VIRTUAL_ENV_PROMPT is `(name_of_virtualenv) `. # Use custom name if it was set (python -m venv -p "foo" .venv) if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($n) " ]]; then - n=$VIRTUAL_ENV_PROMPT[2,-3] + _virtualenv_name=$VIRTUAL_ENV_PROMPT[2,-3] # Use parent directory name if virtualenv name is generic (eg .venv) elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then - n=${VIRTUAL_ENV:h:t} + _virtualenv_name=${VIRTUAL_ENV:h:t} # Otherwise use the virtualenv name as-is else - n=${VIRTUAL_ENV:t} + _virtualenv_name=${VIRTUAL_ENV:t} fi - echo "$n" } _virtualenv_poetry() { local idx=$1 @@ -4303,32 +4302,31 @@ _virtualenv_poetry() { local pyproject="$dir/pyproject.toml" _p9k_cached_cmd 0 '' poetry -C "$dir" version # Return the first word only, eg the value of pyproject.toml's `poetry.name` - echo "${_p9k__ret%% *}" + _virtualenv_name="${_p9k__ret%% *}" } prompt_virtualenv() { local msg='' if (( _POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION )) && _p9k_python_version; then msg="${_p9k__ret//\%/%%} " fi - # Determine virtualenv name with a few strategies - local n='' + _virtualenv_name='' if [[ -n $VIRTUAL_ENV ]]; then - n=$(_virtualenv_VIRTUAL_ENV) + _virtualenv_VIRTUAL_ENV else local start end _p9k_upglob pyproject.toml local idx=$? if (( idx > 0 )); then - n=$(_virtualenv_poetry $idx) + _virtualenv_poetry $idx fi fi - msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${n//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" + msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${_virtualenv_name//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" case $_POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV in false) _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${(M)${#P9K_PYENV_PYTHON_VERSION}:#0}' "$msg" ;; if-different) - _p9k_escape $n + _p9k_escape $_virtualenv_name _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${${:-'$_p9k__ret'}:#$_p9k__pyenv_version}' "$msg" ;; *) From db4b650febd4290a8cdb67850574be8d52a255a2 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 13:51:38 +0800 Subject: [PATCH 05/13] cosmetics --- internal/p10k.zsh | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index ae77cd8ce..2430fba9b 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4281,7 +4281,6 @@ function instant_prompt_chezmoi_shell() { # Virtualenv: current working virtualenv # More information on virtualenv (Python): # https://virtualenv.pypa.io/en/latest/ - _virtualenv_VIRTUAL_ENV() { local n # VIRTUAL_ENV is the on-disk path, VIRTUAL_ENV_PROMPT is `(name_of_virtualenv) `. From c10e62ad292fc0d8b63cd727d17af3b61d8cb74d Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 13:51:42 +0800 Subject: [PATCH 06/13] Try to cache poetry command --- internal/p10k.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 2430fba9b..55cdb7bb9 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4299,7 +4299,7 @@ _virtualenv_poetry() { local idx=$1 local dir=${_p9k__parent_dirs[idx]} local pyproject="$dir/pyproject.toml" - _p9k_cached_cmd 0 '' poetry -C "$dir" version + _p9k_cached_cmd 0 '$pyproject' poetry -C "$dir" version # Return the first word only, eg the value of pyproject.toml's `poetry.name` _virtualenv_name="${_p9k__ret%% *}" } From 414c3305816f98454cfdfea6d4d9c23a97e96615 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 13:53:19 +0800 Subject: [PATCH 07/13] fixup! Fix up function return value passing --- internal/p10k.zsh | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 55cdb7bb9..3e84a32ac 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4282,7 +4282,6 @@ function instant_prompt_chezmoi_shell() { # More information on virtualenv (Python): # https://virtualenv.pypa.io/en/latest/ _virtualenv_VIRTUAL_ENV() { - local n # VIRTUAL_ENV is the on-disk path, VIRTUAL_ENV_PROMPT is `(name_of_virtualenv) `. # Use custom name if it was set (python -m venv -p "foo" .venv) if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($n) " ]]; then From 86088963cebde429b6c59eeabd4fb5da8ad9ce34 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 13:54:30 +0800 Subject: [PATCH 08/13] Improve function names Make them a little less ambiguous. --- internal/p10k.zsh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 3e84a32ac..6edfd6e12 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4281,7 +4281,7 @@ function instant_prompt_chezmoi_shell() { # Virtualenv: current working virtualenv # More information on virtualenv (Python): # https://virtualenv.pypa.io/en/latest/ -_virtualenv_VIRTUAL_ENV() { +_virtualenv_load_name_VIRTUAL_ENV() { # VIRTUAL_ENV is the on-disk path, VIRTUAL_ENV_PROMPT is `(name_of_virtualenv) `. # Use custom name if it was set (python -m venv -p "foo" .venv) if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($n) " ]]; then @@ -4294,7 +4294,7 @@ _virtualenv_VIRTUAL_ENV() { _virtualenv_name=${VIRTUAL_ENV:t} fi } -_virtualenv_poetry() { +_virtualenv_load_name_poetry() { local idx=$1 local dir=${_p9k__parent_dirs[idx]} local pyproject="$dir/pyproject.toml" @@ -4309,13 +4309,13 @@ prompt_virtualenv() { fi _virtualenv_name='' if [[ -n $VIRTUAL_ENV ]]; then - _virtualenv_VIRTUAL_ENV + _virtualenv_load_name_VIRTUAL_ENV else local start end _p9k_upglob pyproject.toml local idx=$? if (( idx > 0 )); then - _virtualenv_poetry $idx + _virtualenv_load_name_poetry $idx fi fi msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${_virtualenv_name//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" From fe9b9f9c3e379b2d0b43466e036377b87bc7c734 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 14:10:08 +0800 Subject: [PATCH 09/13] Fix cleanup of generic virtualenv names --- internal/p10k.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 6edfd6e12..952270183 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4287,7 +4287,7 @@ _virtualenv_load_name_VIRTUAL_ENV() { if [[ $VIRTUAL_ENV_PROMPT == '('?*') ' && $VIRTUAL_ENV_PROMPT != "($n) " ]]; then _virtualenv_name=$VIRTUAL_ENV_PROMPT[2,-3] # Use parent directory name if virtualenv name is generic (eg .venv) - elif [[ $v == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then + elif [[ ${VIRTUAL_ENV:t} == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then _virtualenv_name=${VIRTUAL_ENV:h:t} # Otherwise use the virtualenv name as-is else From 893e130bd112159a817818af6210094cc12095b1 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 14:10:35 +0800 Subject: [PATCH 10/13] Fix POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV=if-different (I think) We were comparing virtualenv's name to pyenv's version, which would never be the same. Now this setting works as intended, although I'm not sure how useful it is. It's probably more useful to reorder this logic so virtualenv has precedence. --- internal/p10k.zsh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 952270183..a54ce85ca 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4304,8 +4304,10 @@ _virtualenv_load_name_poetry() { } prompt_virtualenv() { local msg='' + local version='' if (( _POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION )) && _p9k_python_version; then - msg="${_p9k__ret//\%/%%} " + version="${_p9k__ret//\%/%%}" + msg="$version " fi _virtualenv_name='' if [[ -n $VIRTUAL_ENV ]]; then @@ -4324,7 +4326,7 @@ prompt_virtualenv() { _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${(M)${#P9K_PYENV_PYTHON_VERSION}:#0}' "$msg" ;; if-different) - _p9k_escape $_virtualenv_name + _p9k_escape $version _p9k_prompt_segment "$0" "blue" "$_p9k_color1" 'PYTHON_ICON' 0 '${${:-'$_p9k__ret'}:#$_p9k__pyenv_version}' "$msg" ;; *) From 7e835be2d80f6d8143f9355fc9f057db6e03ff88 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 14:37:53 +0800 Subject: [PATCH 11/13] Correct caching for pyproject.toml --- internal/p10k.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index a54ce85ca..956f13403 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4298,7 +4298,7 @@ _virtualenv_load_name_poetry() { local idx=$1 local dir=${_p9k__parent_dirs[idx]} local pyproject="$dir/pyproject.toml" - _p9k_cached_cmd 0 '$pyproject' poetry -C "$dir" version + _p9k_cached_cmd 0 "$pyproject" poetry -C "$dir" version # Return the first word only, eg the value of pyproject.toml's `poetry.name` _virtualenv_name="${_p9k__ret%% *}" } From f2772bd56fdafaf0842a0cb6dddd51a3a0c8bb7d Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 14:38:08 +0800 Subject: [PATCH 12/13] virtualenv: Handle not being in a virtualenv Pre-Poetry support, we only entered this codepath when we were inside a virtualenv. Now we enter it for every directory, but we don't want to display an empty segment for non-virtualenv locations. --- internal/p10k.zsh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 956f13403..a76305fee 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4320,6 +4320,10 @@ prompt_virtualenv() { _virtualenv_load_name_poetry $idx fi fi + # Display nothing if there's no active virtualenv + if [[ -z $_virtualenv_name ]]; then + return + fi msg+="$_POWERLEVEL9K_VIRTUALENV_LEFT_DELIMITER${_virtualenv_name//\%/%%}$_POWERLEVEL9K_VIRTUALENV_RIGHT_DELIMITER" case $_POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV in false) From 81c474ce53641854097dc6ec7efbd7d05881f5ef Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Thu, 12 Oct 2023 14:42:43 +0800 Subject: [PATCH 13/13] Add error handling for poetry cmd --- internal/p10k.zsh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index a76305fee..269a4e464 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4289,7 +4289,7 @@ _virtualenv_load_name_VIRTUAL_ENV() { # Use parent directory name if virtualenv name is generic (eg .venv) elif [[ ${VIRTUAL_ENV:t} == $~_POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES ]]; then _virtualenv_name=${VIRTUAL_ENV:h:t} - # Otherwise use the virtualenv name as-is + # Otherwise use the name as-is else _virtualenv_name=${VIRTUAL_ENV:t} fi @@ -4298,7 +4298,10 @@ _virtualenv_load_name_poetry() { local idx=$1 local dir=${_p9k__parent_dirs[idx]} local pyproject="$dir/pyproject.toml" - _p9k_cached_cmd 0 "$pyproject" poetry -C "$dir" version + if ! _p9k_cached_cmd 0 "$pyproject" poetry -C "$dir" version; then + # poetry execution failed. Display nothing. + _virtualenv_name="" + fi # Return the first word only, eg the value of pyproject.toml's `poetry.name` _virtualenv_name="${_p9k__ret%% *}" }