]> Sergey Matveev's repositories - zsh-autoenv.git/blobdiff - README.md
README: improve recipe for virtualenv auto-activation
[zsh-autoenv.git] / README.md
index 9dfb58095d949bf12dea73b0a9b0a45d5852cd5a..e6cbaf7594ab577b8d3ff62ecb3022d884da538b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -85,6 +85,11 @@ Add the following to your `.zshrc` where you are loading your plugins:
 
     zgen load Tarrasch/zsh-autoenv
 
+### Using [zplug](https://github.com/zplug/zplug)
+
+Add the following to your `.zshrc` where you are loading your plugins:
+
+    zplug "Tarrasch/zsh-autoenv"
 
 ## Configuration
 
@@ -121,8 +126,64 @@ Default: 0
 ### AUTOENV\_DEBUG
 Enable debugging. Multiple levels are supported (max 2).
 
+- 0: no debug messages
+- 1: generic debug logging
+- 2: more verbose messages
+  - messages about adding/removing files on the internal stack
+- 3: everything
+  - sets xtrace option (`set -x`) while sourcing env files
+
 Default: `0`
 
+## Recipes
+
+### Automatically activate Python virtualenvs
+
+Given `AUTOENV_FILE_ENTER=.autoenv.zsh`, `AUTOENV_FILE_LEAVE=.autoenv.zsh` and
+`AUTOENV_HANDLE_LEAVE=1` the following script will activate Python virtualenvs
+automatically in all subdirectories (`.venv` directories get used by
+[pipenv](https://github.com/kennethreitz/pipenv) with
+`PIPENV_VENV_IN_PROJECT=1`):
+
+```zsh
+# Environment file for all projects.
+#  - (de)activates Python virtualenvs (.venv) from pipenv
+
+if [[ $autoenv_event == 'enter' ]]; then
+  autoenv_source_parent
+
+  _my_autoenv_venv_chpwd() {
+    if [[ -z "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
+      return
+    fi
+
+    setopt localoptions extendedglob
+    local -a venv
+    venv=(./(../)#.venv(NY1:A))
+
+    if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
+      if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
+        unset _ZSH_ACTIVATED_VIRTUALENV
+        echo "De-activating virtualenv: ${(D)VIRTUAL_ENV}" >&2
+        deactivate
+      fi
+    fi
+
+    if [[ -z "$VIRTUAL_ENV" ]]; then
+      if (( $#venv )); then
+        echo "Activating virtualenv: ${(D)venv}" >&2
+        source $venv[1]/bin/activate
+        _ZSH_ACTIVATED_VIRTUALENV="$venv[1]"
+      fi
+    fi
+  }
+  autoload -U add-zsh-hook
+  add-zsh-hook chpwd _my_autoenv_venv_chpwd
+  _my_autoenv_venv_chpwd
+else
+  add-zsh-hook -d chpwd _my_autoenv_venv_chpwd
+fi
+```
 
 ## Related projects
 - https://github.com/direnv/direnv