1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-24 04:21:31 +01:00

zsh: automatically enable/disable python virtualenvs on chdir

This commit is contained in:
bretello 2023-09-14 22:57:38 +02:00
parent 1dcdb77f97
commit 99baa7f1e0
Signed by: brethil
GPG Key ID: 876AAC6290170FE7
2 changed files with 34 additions and 0 deletions

View File

@ -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'

View File

@ -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