mirror of
https://git.decapod.one/brethil/dotfiles
synced 2024-12-22 15:21:30 +01:00
zsh: automatically enable/disable python virtualenvs on chdir
This commit is contained in:
parent
1dcdb77f97
commit
99baa7f1e0
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user