From 26b3467806b7cc6c49878da8acc24379ec9f154e Mon Sep 17 00:00:00 2001 From: Feasuro Date: Sat, 4 May 2024 15:35:05 +0200 Subject: [PATCH] feat(python): pattern matching in `auto_vrun` --- plugins/python/README.md | 3 ++- plugins/python/python.plugin.zsh | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/python/README.md b/plugins/python/README.md index c99697b22..d042ad7be 100644 --- a/plugins/python/README.md +++ b/plugins/python/README.md @@ -35,5 +35,6 @@ virtual environments: `/bin/activate`, and automatically deactivate it when navigating out of it (including subdirectories!). - To enable the feature, set `export PYTHON_AUTO_VRUN=true` before sourcing oh-my-zsh. - - The default virtual environment name is `venv`. To use a different name, set + - 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 `export PYTHON_VENV_NAME=`. For example: `export PYTHON_VENV_NAME=".venv"` diff --git a/plugins/python/python.plugin.zsh b/plugins/python/python.plugin.zsh index f6ea85027..32223c2c3 100644 --- a/plugins/python/python.plugin.zsh +++ b/plugins/python/python.plugin.zsh @@ -87,11 +87,14 @@ function mkv() { if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then # Automatically activate venv when changing dir auto_vrun() { - if [[ -f "${PYTHON_VENV_NAME}/bin/activate" ]]; then - source "${PYTHON_VENV_NAME}/bin/activate" > /dev/null 2>&1 - else - (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1 - fi + for activator in "${PYTHON_VENV_NAME}"*/bin/activate(N); do + if [[ -f "${activator}" ]]; then + source "${activator}" > /dev/null 2>&1 + return + fi + done + + (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1 } add-zsh-hook chpwd auto_vrun auto_vrun