Skip to content

Commit d4c9655

Browse files
authored
Add prompt customization (#476)
1 parent e9b88cc commit d4c9655

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ You can set certain environment variables to control pyenv-virtualenv.
243243
* `PIP_VERSION`, if set and `venv` is preferred
244244
over `virtualenv`, install the specified version of pip.
245245
* `PYENV_VIRTUALENV_VERBOSE_ACTIVATE`, if set, shows some verbose outputs on activation and deactivation
246+
* `PYENV_VIRTUALENV_PROMPT`, if set, allows users to customize how `pyenv-virtualenv` modifies their shell prompt. The default prompt ("(venv)") is overwritten with any user-specified text. Specify the location of the virtual environment name with the string `{venv}`. For example, the default prompt string would be `({venv})`.
246247
247248
## Version History
248249

bin/pyenv-sh-activate

+6-1
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,14 @@ EOS
262262
fi
263263
;;
264264
* )
265+
if [ -z "${PYENV_VIRTUALENV_PROMPT}" ]; then
266+
PYENV_VIRTUALENV_PROMPT="(${venv})"
267+
else
268+
PYENV_VIRTUALENV_PROMPT="${PYENV_VIRTUALENV_PROMPT/\{venv\}/${venv}}"
269+
fi
265270
cat <<EOS
266271
export _OLD_VIRTUAL_PS1="\${PS1:-}";
267-
export PS1="(${venv}) \${PS1:-}";
272+
export PS1="${PYENV_VIRTUALENV_PROMPT} \${PS1:-}";
268273
EOS
269274
;;
270275
esac

test/activate.bats

+26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ setup() {
1515
unset PYENV_VIRTUALENV_DISABLE_PROMPT
1616
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
1717
unset VIRTUAL_ENV_DISABLE_PROMPT
18+
unset PYENV_VIRTUALENV_PROMPT
1819
unset _OLD_VIRTUAL_PS1
1920
stub pyenv-hooks "activate : echo"
2021
}
@@ -44,6 +45,31 @@ EOS
4445
unstub pyenv-sh-deactivate
4546
}
4647

48+
@test "activate virtualenv from current version with custom prompt" {
49+
export PYENV_VIRTUALENV_INIT=1
50+
51+
stub pyenv-version-name "echo venv"
52+
stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
53+
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
54+
stub pyenv-sh-deactivate "--force --quiet : echo deactivated"
55+
56+
PYENV_SHELL="bash" PYENV_VERSION="venv" PYENV_VIRTUALENV_PROMPT='venv:{venv}' run pyenv-sh-activate
57+
58+
assert_success
59+
assert_output <<EOS
60+
deactivated
61+
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/venv";
62+
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv";
63+
export _OLD_VIRTUAL_PS1="\${PS1:-}";
64+
export PS1="venv:venv \${PS1:-}";
65+
EOS
66+
67+
unstub pyenv-version-name
68+
unstub pyenv-virtualenv-prefix
69+
unstub pyenv-prefix
70+
unstub pyenv-sh-deactivate
71+
}
72+
4773
@test "activate virtualenv from current version (quiet)" {
4874
export PYENV_VIRTUALENV_INIT=1
4975

test/conda-activate.bats

+30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ setup() {
1616
unset PYENV_VIRTUALENV_DISABLE_PROMPT
1717
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
1818
unset VIRTUAL_ENV_DISABLE_PROMPT
19+
unset PYENV_VIRTUALENV_PROMPT
1920
unset _OLD_VIRTUAL_PS1
2021
stub pyenv-hooks "activate : echo"
2122
}
@@ -53,6 +54,35 @@ EOS
5354
teardown_conda "anaconda-2.3.0"
5455
}
5556

57+
@test "activate conda root from current version with custom prompt" {
58+
export PYENV_VIRTUALENV_INIT=1
59+
60+
setup_conda "anaconda-2.3.0"
61+
stub pyenv-version-name "echo anaconda-2.3.0"
62+
stub pyenv-virtualenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\""
63+
stub pyenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\""
64+
stub pyenv-sh-deactivate "--force --quiet : echo deactivated"
65+
66+
PYENV_SHELL="bash" PYENV_VERSION="anaconda-2.3.0" PYENV_VIRTUALENV_PROMPT='venv:{venv}' run pyenv-sh-activate
67+
68+
assert_success
69+
assert_output <<EOS
70+
deactivated
71+
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/anaconda-2.3.0";
72+
export VIRTUAL_ENV="${PYENV_ROOT}/versions/anaconda-2.3.0";
73+
export CONDA_DEFAULT_ENV="root";
74+
export _OLD_VIRTUAL_PS1="\${PS1:-}";
75+
export PS1="venv:anaconda-2.3.0 \${PS1:-}";
76+
export CONDA_PREFIX="${TMP}/pyenv/versions/anaconda-2.3.0";
77+
EOS
78+
79+
unstub pyenv-version-name
80+
unstub pyenv-virtualenv-prefix
81+
unstub pyenv-prefix
82+
unstub pyenv-sh-deactivate
83+
teardown_conda "anaconda-2.3.0"
84+
}
85+
5686
@test "activate conda root from current version (fish)" {
5787
export PYENV_VIRTUALENV_INIT=1
5888

test/hooks.bats

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ setup() {
66
export PYENV_ROOT="${TMP}/pyenv"
77
export HOOK_PATH="${TMP}/i has hooks"
88
mkdir -p "$HOOK_PATH"
9+
unset PYENV_VIRTUALENV_PROMPT
910
}
1011

1112
@test "pyenv-virtualenv hooks" {

0 commit comments

Comments
 (0)