Skip to content

Commit 85634f3

Browse files
committed
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.
1 parent 5fb4442 commit 85634f3

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

docs/source/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ dev
1313
- Fix documentation for ``add2virtualenv`` to show the correct name
1414
for the file containing the new path entry. (contributed by
1515
:bbuser:`rvoicilas`)
16+
- Fix problem with ``virtualenvwrapper_show_workon_options`` under
17+
zsh with ``chpwd`` functions that produce output. (:bbissue:`153`)
1618

1719
.. _stevedore: http://pypi.python.org/pypi/stevedore
1820

tests/test_workon.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ test_virtualenvwrapper_show_workon_options_grep_options () {
8383
rm -f "$WORKON_HOME/link_env"
8484
}
8585

86+
test_virtualenvwrapper_show_workon_options_chpwd () {
87+
# https://bitbucket.org/dhellmann/virtualenvwrapper/issue/153
88+
function chpwd {
89+
local SEARCH=' '
90+
local REPLACE='%20'
91+
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
92+
printf '\e]7;%s\a' "$PWD_URL"
93+
echo -n "\033]0;${HOST//.*}:$USER\007"
94+
}
95+
mkdir "$WORKON_HOME/not_env"
96+
envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
97+
assertSame "env1 env2 " "$envs"
98+
rmdir "$WORKON_HOME/not_env"
99+
rm -f "$WORKON_HOME/link_env"
100+
}
101+
86102
test_virtualenvwrapper_show_workon_options_no_envs () {
87103
old_home="$WORKON_HOME"
88104
export WORKON_HOME=${TMPDIR:-/tmp}/$$

virtualenvwrapper.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,23 @@ function rmvirtualenv {
502502
# List the available environments.
503503
function virtualenvwrapper_show_workon_options {
504504
virtualenvwrapper_verify_workon_home || return 1
505-
# NOTE: DO NOT use ls here because colorized versions spew control characters
506-
# into the output list.
505+
# NOTE: DO NOT use ls or cd here because colorized versions spew control
506+
# characters into the output list.
507507
# echo seems a little faster than find, even with -depth 3.
508-
(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 '^\*$')
508+
#
509+
# 1. Look for environments by finding the activate scripts.
510+
# Use a subshell so we can suppress the message printed
511+
# by zsh if the glob pattern fails to match any files.
512+
# 2. Strip the WORKON_HOME prefix from each name.
513+
# 3. Strip the bindir/activate script suffix.
514+
# 4. Format the output to show one name on a line.
515+
# 5. Eliminate any lines with * on them because that means there
516+
# were no envs.
517+
(echo $WORKON_HOME/*/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate) 2>/dev/null \
518+
| command \sed "s|$WORKON_HOME/||g" \
519+
| command \sed "s|/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate||g" \
520+
| command \fmt -w 1 \
521+
| (unset GREP_OPTIONS; command \egrep -v '^\*$') 2>/dev/null
509522
}
510523

511524
function _lsvirtualenv_usage {

0 commit comments

Comments
 (0)