This commit is contained in:
kang 2026-01-19 10:59:59 +01:00 committed by GitHub
commit 4496f0cba3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View file

@ -34,6 +34,7 @@ virtual environments:
`<venv-name>/bin/activate`, and automatically deactivate it when navigating out of it (keeps venv activated
in subdirectories).
- To enable the feature, set `PYTHON_AUTO_VRUN=true` before sourcing oh-my-zsh.
- To also search parent directories (useful for monorepos), set `PYTHON_AUTO_VRUN_RECURSIVE=true` as well.
- The plugin activates the first existing virtual environment, in order, appearing in `$PYTHON_VENV_NAMES`.
The default virtual environment name is `venv`. To use a different name, set
`PYTHON_VENV_NAME=<venv-name>`. For example: `PYTHON_VENV_NAME=".venv"`

View file

@ -113,8 +113,21 @@ if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
# make sure we're not in a venv already
(( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
source $file > /dev/null 2>&1
break
return
done
# Search parent directories if recursive mode is enabled
if [[ "$PYTHON_AUTO_VRUN_RECURSIVE" == "true" ]]; then
local search_dir="$PWD:h"
while [[ "$search_dir" != "/" && "$search_dir" != "." ]]; do
for file in "${search_dir}/"${^PYTHON_VENV_NAMES[@]}"/bin/activate"(N.); do
(( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
source $file > /dev/null 2>&1
return
done
search_dir="${search_dir:h}"
done
fi
fi
}
add-zsh-hook chpwd auto_vrun