From 1965d1b3fcac5952b114a81f688daa2c7523b7d0 Mon Sep 17 00:00:00 2001 From: Feasuro Date: Sat, 4 May 2024 15:56:02 +0200 Subject: [PATCH] feat(python): don't auto-deactivate in a subdir --- plugins/python/README.md | 4 ++-- plugins/python/python.plugin.zsh | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/python/README.md b/plugins/python/README.md index d042ad7be..b990a26b9 100644 --- a/plugins/python/README.md +++ b/plugins/python/README.md @@ -32,8 +32,8 @@ virtual environments: `venv`) in the current directory. - `auto_vrun`: Automatically activate the venv virtual environment when entering a directory containing - `/bin/activate`, and automatically deactivate it when navigating out of it (including - subdirectories!). + `/bin/activate`, and automatically deactivate it when navigating out of it (keeps venv activated + in subdirectories). - To enable the feature, set `export PYTHON_AUTO_VRUN=true` before sourcing oh-my-zsh. - Plugin activates first virtual environment in lexicographic order whose name begins with ``. The default virtual environment name is `venv`. To use a different name, set diff --git a/plugins/python/python.plugin.zsh b/plugins/python/python.plugin.zsh index 32223c2c3..2dec4e195 100644 --- a/plugins/python/python.plugin.zsh +++ b/plugins/python/python.plugin.zsh @@ -86,12 +86,19 @@ function mkv() { if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then # Automatically activate venv when changing dir - auto_vrun() { - for activator in "${PYTHON_VENV_NAME}"*/bin/activate(N); do - if [[ -f "${activator}" ]]; then - source "${activator}" > /dev/null 2>&1 - return - fi + function auto_vrun() { + local dirname="$PWD" + + while + for activator in "${dirname}/${PYTHON_VENV_NAME}"*/bin/activate(N); do + if [[ -f "${activator}" ]]; then + source "${activator}" > /dev/null 2>&1 + return + fi + done + [[ -n "$dirname" ]] + do + dirname=${dirname%/*} done (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1