From ecb09bdee57f925b62a97b0b86494abb9805fd0c Mon Sep 17 00:00:00 2001 From: Luca Bilke Date: Tue, 2 Aug 2022 14:46:25 +0200 Subject: [PATCH] Add autopyenv zsh plugin --- .config/shell/profile | 1 + .../plugins/autopyenv/autopyenv.plugin.zsh | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 .local/share/zsh/plugins/autopyenv/autopyenv.plugin.zsh diff --git a/.config/shell/profile b/.config/shell/profile index 3a2cc64b..222ee03b 100644 --- a/.config/shell/profile +++ b/.config/shell/profile @@ -19,6 +19,7 @@ export BROWSER="librewolf" export XDG_CONFIG_HOME="$HOME/.config" export XDG_DATA_HOME="$HOME/.local/share" export XDG_CACHE_HOME="$HOME/.cache" +export PYENVS_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/pyenvs" export XINITRC="${XDG_CONFIG_HOME:-$HOME/.config}/x11/xinitrc" # export XAUTHORITY="$XDG_RUNTIME_DIR/Xauthority" # This line will break some DMs. export NOTMUCH_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/notmuch-config" diff --git a/.local/share/zsh/plugins/autopyenv/autopyenv.plugin.zsh b/.local/share/zsh/plugins/autopyenv/autopyenv.plugin.zsh new file mode 100644 index 00000000..9f724e24 --- /dev/null +++ b/.local/share/zsh/plugins/autopyenv/autopyenv.plugin.zsh @@ -0,0 +1,32 @@ +PYENV_DIR="${PYENVS_HOME:-${XDG_DATA_HOME:-$HOME/.local/share}/pyenvs}" +function chpwd_activate(){ + for pydir in $(ls $PYENV_DIR); do + if [[ $(pwd|sed -e s@/@_@g) =~ "^$pydir" ]]; then + if [ "x$VIRTUAL_ENV" != "x$PYENV_DIR/$pydir" ]; then + echo "Activating virtual env $pydir" + source "$PYENV_DIR/$pydir/bin/activate" + return + fi + fi + done + if [ "x$VIRTUAL_ENV" != "x" ]; then + echo "Deactivating virtual env $VIRTUAL_ENV" + deactivate + fi +} + +function venv-here(){ + envdir=$(pwd|sed -e s@/@_@g) + if [ ! -e "$PYENV_DIR/${envdir}" ]; then + echo "Creating python venv for ${envdir}.." + mkdir "$PYENV_DIR" -p + python3 -m venv "$PYENV_DIR/${envdir}" + echo "Activating virtual env ${envdir}" + source "$PYENV_DIR/${envdir}/bin/activate" + else + echo "A venv for this path already exists" + fi +} + +autoload -U add-zsh-hook +add-zsh-hook chpwd chpwd_activate