1
0
mirror of https://github.com/artizirk/dotfiles.git synced 2024-11-01 00:30:58 +02:00

Use zsh extended glob magic to find python venvs

This commit is contained in:
Arti Zirk 2023-06-21 20:59:20 +03:00
parent 5d3b5af7ed
commit c9dff2f155

20
.zshrc
View File

@ -101,21 +101,11 @@ setopt cdable_vars
# Enable or disable python virtual env # Enable or disable python virtual env
function chpwd_auto_python_venv() { function chpwd_auto_python_venv() {
local venv_dir local venv_activation_script=((../)#(.|)(v|)env/bin/activate(#qN.omY1:a))
local cur_dir="${PWD}" if [[ -n "${venv_activation_script}" && ( -z "${VIRTUAL_ENV}" || "${VIRTUAL_ENV}/bin/activate" != "${venv_activation_script}" ) ]]; then
while [[ "${cur_dir}" != / ]]; do # we found venv dir that is not yet activated or is different from currently active one
if [[ -f "${cur_dir}/venv/bin/activate" ]]; then source "${venv_activation_script}"
venv_dir="${cur_dir}/venv" elif [[ -z "${venv_activation_script}" && -n "${VIRTUAL_ENV}" ]]; then
break
fi
# :P does `realpath(3)`
# :h removes 1 trailing pathname component
cur_dir="${cur_dir:P:h}"
done
if [[ -z "${VIRTUAL_ENV}" ]] && [[ -n "${venv_dir}" ]]; then
# we found venv dir that is not yet activated
source "${venv_dir}"/bin/activate
elif [[ -z "${venv_dir}" ]] && [[ -n "${VIRTUAL_ENV}" ]]; then
# we have activated virtual env but we cant find venv folder anymore # we have activated virtual env but we cant find venv folder anymore
deactivate deactivate
fi fi