diff --git a/brethil_dotfile.sh b/brethil_dotfile.sh index fb7e257..7e94be2 100755 --- a/brethil_dotfile.sh +++ b/brethil_dotfile.sh @@ -107,6 +107,9 @@ if [[ $SSH_CLIENT ]]; then export SSH_AUTH_SOCK=$HOME/.ssh/ssh_auth_sock fi +# Automatically enable/disable virtualenv on chdir/step +export DOTFILES_AUTOSWITCH_VIRTUALENV=1 + alias esource='$EDITOR $HOME/.zshrc' alias resource='source $HOME/.zshrc' alias dotedit='$EDITOR $DOTFILES/brethil_dotfile.sh' diff --git a/functions/python.zsh b/functions/python.zsh index 585d229..3eb94ba 100644 --- a/functions/python.zsh +++ b/functions/python.zsh @@ -22,3 +22,34 @@ mkvenv(){ source .venv/bin/activate && echo -e " → $(color $BOLD Enabled!) 🐍 $(color $BOLD$PURPLE $(python --version | cut -d " " -f2 )) ($(color $BOLD$GREEN $(pip --version | cut -d " " -f -2)))" fi } + +function enable_venv() { + local root + local activate + + root=$(git rev-parse --show-toplevel 2>/dev/null) + if [[ -z $root ]]; then root=$PWD; fi + activate="$root/.venv/bin/activate" + if [[ -f "$activate" ]]; then + source "$activate" + fi +} + +function disable_venv() { + if [[ -z ${VIRTUAL_ENV} ]]; then + return + fi + + if [[ ! "$PWD" =~ $(dirname $VIRTUAL_ENV)}* ]]; then + echo "Deactivating venv..." + deactivate + fi +} + + +if [[ -n $DOTFILES_AUTOSWITCH_VIRTUALENV ]]; then + add-zsh-hook chpwd enable_venv + add-zsh-hook chpwd disable_venv + + enable_venv +fi