From: Maximilian Friedersdorff Date: Wed, 3 Aug 2022 11:03:00 +0000 (+0100) Subject: Automagically activate virtualenvs X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=8028d34696a67c53a9dd3f4bc2441d36c5ae2460;p=max%2Fsaltfiles.git Automagically activate virtualenvs --- diff --git a/states/zsh/files/zshrc b/states/zsh/files/zshrc index e1659e5..57c58ec 100644 --- a/states/zsh/files/zshrc +++ b/states/zsh/files/zshrc @@ -71,3 +71,31 @@ fi export WORKON_HOME=~/.virtualenvs source /usr/bin/virtualenvwrapper_lazy.sh + +chpwd() { + src_dir="$(readlink -f ~/src/)/" + if [[ $PWD == "$src_dir"* ]]; then + if git status &>/dev/null; then + venv_name=$(basename $(git rev-parse --show-toplevel)) + if [[ -n "$VIRTUAL_ENV" ]]; then + current_venv_name=$(basename "$VIRTUAL_ENV") + if [[ "$current_venv_name" != "$venv_name" ]]; then + deactivate + workon "$venv_name" + fi + else + if workon | grep -q "$venv_name"; then + workon "$venv_name" + fi + fi + else + if [[ -n "$VIRTUAL_ENV" ]]; then + deactivate + fi + fi + else + if [[ -n "$VIRTUAL_ENV" ]]; then + deactivate + fi + fi +}