From c9dff2f155b41b23d3b4a3884207b6fa1c013718 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Wed, 21 Jun 2023 20:59:20 +0300 Subject: [PATCH] Use zsh extended glob magic to find python venvs --- .zshrc | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/.zshrc b/.zshrc index 62c652d..1643d09 100644 --- a/.zshrc +++ b/.zshrc @@ -101,21 +101,11 @@ setopt cdable_vars # Enable or disable python virtual env function chpwd_auto_python_venv() { - local venv_dir - local cur_dir="${PWD}" - while [[ "${cur_dir}" != / ]]; do - if [[ -f "${cur_dir}/venv/bin/activate" ]]; then - venv_dir="${cur_dir}/venv" - 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 + local venv_activation_script=((../)#(.|)(v|)env/bin/activate(#qN.omY1:a)) + if [[ -n "${venv_activation_script}" && ( -z "${VIRTUAL_ENV}" || "${VIRTUAL_ENV}/bin/activate" != "${venv_activation_script}" ) ]]; then + # we found venv dir that is not yet activated or is different from currently active one + source "${venv_activation_script}" + elif [[ -z "${venv_activation_script}" && -n "${VIRTUAL_ENV}" ]]; then # we have activated virtual env but we cant find venv folder anymore deactivate fi