diff --git a/.config/shell/profile b/.config/shell/profile index 3a2cc64b9..222ee03bf 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 000000000..9f724e245 --- /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