Diff
1diff --git a/README.rst b/README.rst
2index b8491256c0d47dfe993a758ba6508c2b488d9215..49332138a41c8f33e6350f9c9d2157e39c71320b 100644
3--- a/README.rst
4+++ b/README.rst
5@@ -11,6 +11,7 @@ virtualenvs automatically as you move between directories.
6 * Installing_
7 * `Pipenv Integration`_
8 * Commands_
9+* `Customising Messages`_
10 * Options_
11 * `Security Warnings`_
12 * `Running Tests`_
13@@ -165,6 +166,38 @@ This will delete the virtual environment in ``.venv`` and remove the
14 $ rmvenv
15 No .venv file in the current directory!
16
17+
18+Customising Messages
19+--------------------
20+
21+By default, the following message is displayed in bold when an alias is found:
22+
23+::
24+
25+ Switching %venv_type: %venv_name [%py_version]
26+
27+Where the following variables represent:
28+
29+* ``%venv_type`` - the type of virtualenv being activated (virtualenv, pipenv)
30+* ``%venv_name`` - the name of the virtualenv being activated
31+* ``%py_version`` - the version of python used by the virtualenv being activated
32+
33+This default message can be customised by setting the ``AUTOSWITCH_MESSAGE_FORMAT`` environment variable.
34+
35+If for example, you wish to display your own custom message in red, you can add the
36+following to your ``~/.zshrc``:
37+
38+::
39+
40+ export AUTOSWITCH_MESSAGE_FORMAT="$(tput setaf 1)Switching to %venv_name 🐍 %py_version $(tput sgr0)"
41+
42+``$(tput setaf 1)`` generates the escape code terminals use for red foreground text. ``$(tput sgr0)`` sets
43+the text back to a normal color.
44+
45+You can read more about how you can use tput and terminal escape codes here:
46+http://wiki.bash-hackers.org/scripting/terminalcodes
47+
48+
49 Options
50 -------
51
52diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
53index c4a42062bb158a8d44d73aee40d5aca64b55e0b2..03297737aec56950980692c4a85a8c6e8b0cad41 100644
54--- a/autoswitch_virtualenv.plugin.zsh
55+++ b/autoswitch_virtualenv.plugin.zsh
56@@ -10,6 +10,8 @@ PURPLE="\e[35m"
57 BOLD="\e[1m"
58 NORMAL="\e[0m"
59
60+DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[%py_version]${NORMAL}"
61+
62
63 if ! type "virtualenv" > /dev/null; then
64 export DISABLE_AUTOSWITCH_VENV="1"
65@@ -22,45 +24,34 @@ if ! type "virtualenv" > /dev/null; then
66 fi
67
68
69-function _print_python_version() {
70- # For some reason python --version writes to stderr
71- if type python > /dev/null; then
72- printf "${GREEN}[%s]${NORMAL}\n" "$(python --version 2>&1)"
73- elif type python3 > /dev/null; then
74- printf "${GREEN}[%s]${NORMAL}\n" "$(python3 --version 2>&1)"
75+function _python_version() {
76+ PYTHON_BIN="$1"
77+ if [[ -f "$PYTHON_BIN" ]] then
78+ # For some reason python --version writes to stderr
79+ printf "%s" "$($PYTHON_BIN --version 2>&1)"
80 else
81- printf "Unable to find python installed on this machine"
82- fi
83+ printf "unknown"
84+ fi
85 }
86
87
88 function _maybeworkon() {
89- if [[ -z "$VIRTUAL_ENV" || "$1" != "$(basename $VIRTUAL_ENV)" ]]; then
90- if [ -z "$AUTOSWITCH_SILENT" ]; then
91- printf "Switching virtualenv: ${BOLD}${PURPLE}%s${NORMAL} " $1
92- fi
93-
94- # Much faster to source the activate file directly rather than use the `workon` command
95- source "$VIRTUAL_ENV_DIR/$1/bin/activate"
96+ venv_name="$1"
97+ venv_type="$2"
98
99+ if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
100 if [ -z "$AUTOSWITCH_SILENT" ]; then
101- _print_python_version
102- fi
103- fi
104-}
105-
106+ py_version="$(_python_version "$VIRTUAL_ENV_DIR/$venv_name/bin/python")"
107
108-function _maybepipenv() {
109- if [[ -z "$VIRTUAL_ENV" || "$1" != "$VIRTUAL_ENV" ]]; then
110- if [ -z "$AUTOSWITCH_SILENT" ]; then
111- printf "Switching pipenv: ${BOLD}${PURPLE}%s${NORMAL} " "$(basename "$1")"
112+ message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
113+ message="${message//\%venv_type/$venv_type}"
114+ message="${message//\%venv_name/$venv_name}"
115+ message="${message//\%py_version/$py_version}"
116+ printf "${message}\n"
117 fi
118
119- source "$1/bin/activate"
120-
121- if [ -z "$AUTOSWITCH_SILENT" ]; then
122- _print_python_version
123- fi
124+ # Much faster to source the activate file directly rather than use the `workon` command
125+ source "$VIRTUAL_ENV_DIR/$venv_name/bin/activate"
126 fi
127 }
128
129@@ -120,11 +111,12 @@ function check_venv()
130 fi
131
132 if [[ -n "$SWITCH_TO" ]]; then
133- _maybeworkon "$SWITCH_TO"
134+ _maybeworkon "$SWITCH_TO" "virtualenv"
135
136 # check if Pipfile exists rather than invoking pipenv as it is slow
137 elif [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
138- _maybepipenv "$(pipenv --venv)"
139+ venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv)"
140+ _maybeworkon "$(basename "$venv_path")" "pipenv"
141 else
142 _default_venv
143 fi
144@@ -135,7 +127,7 @@ function check_venv()
145 function _default_venv()
146 {
147 if [[ -n "$AUTOSWITCH_DEFAULTENV" ]]; then
148- _maybeworkon "$AUTOSWITCH_DEFAULTENV"
149+ _maybeworkon "$AUTOSWITCH_DEFAULTENV" "virtualenv"
150 elif [[ -n "$VIRTUAL_ENV" ]]; then
151 deactivate
152 fi
153diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
154index e627c3b5014a6db64733d769d5108dd1cd327264..b419c0159c532fd6dfd34d3ed5f9924ee5005f3a 100644
155--- a/tests/test_check_venv.zunit
156+++ b/tests/test_check_venv.zunit
157@@ -11,6 +11,7 @@
158 run python -m virtualenv "$HOME/.virtualenvs/foodefault" --python=python3
159 fi
160
161+ unset AUTOSWITCH_MESSAGE_FORMAT
162 PYTHON_VERSION="$(python3 --version 2>&1)"
163
164 export DISABLE_AUTOSWITCH_VENV="1"
165diff --git a/tests/test_maybepipenv.zunit b/tests/test_maybepipenv.zunit
166deleted file mode 100644
167index ed03b9712a79f98a7b5ce317bc6a223120029850..0000000000000000000000000000000000000000
168--- a/tests/test_maybepipenv.zunit
169+++ /dev/null
170@@ -1,62 +0,0 @@
171-#!/usr/bin/env zunit
172-
173-@setup {
174-
175- if ! type "pipenv" > /dev/null; then
176- skip "You need to install pipenv to run these tests"
177- fi
178-
179- echo "Creating test pipenv"
180- run pipenv --python 3
181-
182- PYTHON_VERSION="$(python3 --version 2>&1)"
183-
184- export DISABLE_AUTOSWITCH_VENV="1"
185- load "../autoswitch_virtualenv.plugin.zsh"
186- TARGET="$(mktemp -d)"
187-}
188-
189-@teardown {
190- rm -rf "$TARGET"
191- run deactivate 2>/dev/null
192- run pipenv --rm
193- rm -rf Pipfile
194-}
195-
196-
197-@test '_maybepipenv - switches virtualenv if nothing is activated' {
198- VIRTUAL_ENV=""
199-
200- run _maybepipenv "$(pipenv --venv)"
201-
202- assert $state equals 0
203- assert "$output" contains "Switching pipenv: \e[1m\e[35m$(basename "$PWD")"
204-}
205-
206-@test '_maybepipenv - switches virtualenv if current virtualenv is different' {
207- VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
208-
209- run _maybepipenv "$(pipenv --venv)"
210-
211- assert $state equals 0
212- assert "$output" contains "Switching pipenv: \e[1m\e[35m$(basename "$PWD")"
213-}
214-
215-@test '_maybepipenv - switches virtualenv if current virtualenv is different (silent)' {
216- VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
217- AUTOSWITCH_SILENT="1"
218-
219- run _maybepipenv "$(pipenv --venv)"
220-
221- assert $state equals 0
222- assert "$output" is_empty
223-}
224-
225-@test '_maybepipenv - does not switch to already activated virtualenv' {
226- VIRTUAL_ENV="$(pipenv --venv)"
227-
228- run _maybepipenv "$(pipenv --venv)"
229-
230- assert $state equals 0
231- assert "$output" is_empty
232-}
233diff --git a/tests/test_maybeworkon.zunit b/tests/test_maybeworkon.zunit
234index 7af6531af8768942f08a4eeeebb438c96e9c6065..d698ef504424cad6010b74d5ba6bedfdda12f72e 100644
235--- a/tests/test_maybeworkon.zunit
236+++ b/tests/test_maybeworkon.zunit
237@@ -8,6 +8,7 @@
238 run python -m virtualenv "$HOME/.virtualenvs/foobar" --python=python3
239 fi
240
241+ unset AUTOSWITCH_MESSAGE_FORMAT
242 PYTHON_VERSION="$(python3 --version 2>&1)"
243
244 export DISABLE_AUTOSWITCH_VENV="1"
245@@ -23,26 +24,36 @@
246 @test '_maybeworkon - switches virtualenv if nothing is activated' {
247 VIRTUAL_ENV=""
248
249- run _maybeworkon foobar
250+ run _maybeworkon foobar virtualenv
251
252 assert $state equals 0
253 assert "$output" same_as "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
254 }
255
256+@test '_maybeworkon - custom message' {
257+ VIRTUAL_ENV=""
258+ AUTOSWITCH_MESSAGE_FORMAT="Hello %venv_type - this is %venv_name (%py_version)"
259+
260+ run _maybeworkon foobar cheese
261+
262+ assert $state equals 0
263+ assert "$output" same_as "Hello cheese - this is foobar ($PYTHON_VERSION)"
264+}
265+
266 @test '_maybeworkon - switches virtualenv if current virtualenv is different' {
267 VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
268
269- run _maybeworkon foobar
270+ run _maybeworkon foobar virtualfoo
271
272 assert $state equals 0
273- assert "$output" contains "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
274+ assert "$output" contains "Switching virtualfoo: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
275 }
276
277 @test '_maybeworkon - switches virtualenv if current virtualenv is different (silent)' {
278 VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
279 AUTOSWITCH_SILENT="1"
280
281- run _maybeworkon foobar
282+ run _maybeworkon foobar virtualenv
283
284 assert $state equals 0
285 assert "$output" is_empty
286@@ -51,7 +62,7 @@
287 @test '_maybeworkon - does not switch to already activated virtualenv' {
288 VIRTUAL_ENV="$HOME/.virtualenvs/bwaaah"
289
290- run _maybeworkon bwaaah
291+ run _maybeworkon bwaaah virtualenv
292
293 assert $state equals 0
294 assert "$output" is_empty
295diff --git a/tests/test_plugin.zunit b/tests/test_plugin.zunit
296index 5a9a3bffd002389ead0c373797b250cb32f51d4a..f8580b94f0824c7f0633f958a604946d2577c127 100644
297--- a/tests/test_plugin.zunit
298+++ b/tests/test_plugin.zunit
299@@ -6,6 +6,7 @@
300 run python -m virtualenv "$HOME/.virtualenvs/foobar" --python=python3
301 fi
302 unset VIRTUAL_ENV
303+ unset AUTOSWITCH_MESSAGE_FORMAT
304 PYTHON_VERSION="$(python3 --version)"
305 }
306