From 8028d34696a67c53a9dd3f4bc2441d36c5ae2460 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Wed, 3 Aug 2022 12:03:00 +0100 Subject: [PATCH] Automagically activate virtualenvs --- states/zsh/files/zshrc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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 +} -- 2.46.2