Doug Hellmann avatar Doug Hellmann committed 3c1088e

Fix virtualenvwrapper_show_workon_options under zsh with chpwd

Fixes issue #153

Re-implement virtualenvwrapper_show_workon_options to work
for zsh users with chpwd hook functions that emit output.

Comments (0)

Files changed (3)

docs/source/history.rst

   - Fix documentation for ``add2virtualenv`` to show the correct name
     for the file containing the new path entry. (contributed by
     :bbuser:`rvoicilas`)
+  - Fix problem with ``virtualenvwrapper_show_workon_options`` under
+    zsh with ``chpwd`` functions that produce output. (:bbissue:`153`)
 
 .. _stevedore: http://pypi.python.org/pypi/stevedore
 

tests/test_workon.sh

     rm -f "$WORKON_HOME/link_env"
 }
 
+test_virtualenvwrapper_show_workon_options_chpwd () {
+    # https://bitbucket.org/dhellmann/virtualenvwrapper/issue/153
+    function chpwd {
+        local SEARCH=' '
+        local REPLACE='%20'
+        local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
+        printf '\e]7;%s\a' "$PWD_URL"
+        echo -n "\033]0;${HOST//.*}:$USER\007"
+    }
+    mkdir "$WORKON_HOME/not_env"
+    envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
+    assertSame "env1 env2 " "$envs"
+    rmdir "$WORKON_HOME/not_env"
+    rm -f "$WORKON_HOME/link_env"
+}
+
 test_virtualenvwrapper_show_workon_options_no_envs () {
     old_home="$WORKON_HOME"
     export WORKON_HOME=${TMPDIR:-/tmp}/$$

virtualenvwrapper.sh

 # List the available environments.
 function virtualenvwrapper_show_workon_options {
     virtualenvwrapper_verify_workon_home || return 1
-    # NOTE: DO NOT use ls here because colorized versions spew control characters
-    #       into the output list.
+    # NOTE: DO NOT use ls or cd here because colorized versions spew control 
+    #       characters into the output list.
     # echo seems a little faster than find, even with -depth 3.
-    (virtualenvwrapper_cd "$WORKON_HOME"; for f in */$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate; do echo $f; done) 2>/dev/null | command \sed 's|^\./||' | command \sed "s|/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate||" | command \sort | (unset GREP_OPTIONS; command \egrep -v '^\*$')
+    #
+    # 1. Look for environments by finding the activate scripts.
+    #    Use a subshell so we can suppress the message printed
+    #    by zsh if the glob pattern fails to match any files.
+    # 2. Strip the WORKON_HOME prefix from each name.
+    # 3. Strip the bindir/activate script suffix.
+    # 4. Format the output to show one name on a line.
+    # 5. Eliminate any lines with * on them because that means there 
+    #    were no envs.
+    (echo $WORKON_HOME/*/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate) 2>/dev/null \
+        | command \sed "s|$WORKON_HOME/||g" \
+        | command \sed "s|/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate||g" \
+        | command \fmt -w 1 \
+        | (unset GREP_OPTIONS; command \egrep -v '^\*$') 2>/dev/null
 }
 
 function _lsvirtualenv_usage {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.