From 480f254157ea75d282044d37f242f97e605711ef Mon Sep 17 00:00:00 2001 From: Heinrich Kruger Date: Sat, 8 Mar 2025 23:39:21 +0000 Subject: [PATCH 1/2] feat(virtualenv): Use $VIRTUAL_ENV_PROMPT if set Modify the `virtualenv_prompt_info` function to use the prompt string specified when creating the virtual environment, instead of just the basename of the environment. If `VIRTUAL_ENV_PROMPT` is not set, fall back to just using the basename of `$VIRTUAL_ENV`. --- plugins/virtualenv/README.md | 7 +++++-- plugins/virtualenv/virtualenv.plugin.zsh | 12 +++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/virtualenv/README.md b/plugins/virtualenv/README.md index e0b8c2c82..5e6116497 100644 --- a/plugins/virtualenv/README.md +++ b/plugins/virtualenv/README.md @@ -7,8 +7,11 @@ To use it, add `virtualenv` to the plugins array of your zshrc file: plugins=(... virtualenv) ``` -The plugin creates a `virtualenv_prompt_info` function that you can use in your theme, which displays -the basename of the current `$VIRTUAL_ENV`. It uses two variables to control how that is shown: +The plugin creates a `virtualenv_prompt_info` function that you can use in your theme. It +displays the prompt string set when creating the virtual environment (via the `--prompt` option), +falling back to the basename of the current `$VIRTUAL_ENV`. + +It uses two variables to control how that is shown: - `ZSH_THEME_VIRTUALENV_PREFIX`: sets the prefix of the VIRTUAL_ENV. Defaults to `[`. diff --git a/plugins/virtualenv/virtualenv.plugin.zsh b/plugins/virtualenv/virtualenv.plugin.zsh index 56707bb98..b645bb2c0 100644 --- a/plugins/virtualenv/virtualenv.plugin.zsh +++ b/plugins/virtualenv/virtualenv.plugin.zsh @@ -1,6 +1,16 @@ function virtualenv_prompt_info(){ [[ -n ${VIRTUAL_ENV} ]] || return - echo "${ZSH_THEME_VIRTUALENV_PREFIX=[}${VIRTUAL_ENV:t:gs/%/%%}${ZSH_THEME_VIRTUALENV_SUFFIX=]}" + # Some versions of virtualenv (e.g the version bundled with 'uv') wrap the + # prompt in parentheses with a trailing space. + local venv_prompt="${(*)VIRTUAL_ENV_PROMPT:/#%(#b)\((*)\) /${match[1]}}" + + if [ -z ${venv_prompt} ]; then + # Older versions of virtualenv do not set VIRTUAL_ENV_PROMPT, so fall back + # to the basename of the virtualenv path. + venv_prompt="${VIRTUAL_ENV:t}" + fi + + echo "${ZSH_THEME_VIRTUALENV_PREFIX=[}${venv_prompt:gs/%/%%}${ZSH_THEME_VIRTUALENV_SUFFIX=]}" } # disables prompt mangling in virtual_env/bin/activate From 689787e117572a2a6c5d6bef4f4d27d999a6e6a6 Mon Sep 17 00:00:00 2001 From: Heinrich Kruger Date: Wed, 12 Mar 2025 19:22:59 +0000 Subject: [PATCH 2/2] docs(virtualenv): Document how the virtual env prompt is computed --- plugins/virtualenv/README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/virtualenv/README.md b/plugins/virtualenv/README.md index 5e6116497..3a934a9f4 100644 --- a/plugins/virtualenv/README.md +++ b/plugins/virtualenv/README.md @@ -7,9 +7,14 @@ To use it, add `virtualenv` to the plugins array of your zshrc file: plugins=(... virtualenv) ``` -The plugin creates a `virtualenv_prompt_info` function that you can use in your theme. It -displays the prompt string set when creating the virtual environment (via the `--prompt` option), -falling back to the basename of the current `$VIRTUAL_ENV`. +The plugin creates a `virtualenv_prompt_info` function that you can use in your +theme. It displays the prompt string set in the `$VIRTUAL_ENV_PROMPT` +environment variable. The `activate` scripts for most Python virtual environment +implementations set this variable to the value provided via the `--prompt` +option when creating the virtual environment, or the basename of the environment +directory if the option was not provided. For implementations that do not set +`$VIRTUAL_ENV_PROMPT`, the plugin will display the basename of the virtual +environment directory instead. It uses two variables to control how that is shown: