ace9d97d373e4c96776a94ddb591cb37bce13ab4

Author
Michael Aquilina <michaelaquilina@gmail.com>
Committer
Michael Aquilina <michaelaquilina@gmail.com>
Date

Message

Allow customising of messages when switching virtualenvs

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 1274c08e62c9dce62cbfa7b2e7259436f79010d9..03297737aec56950980692c4a85a8c6e8b0cad41 100644
 54--- a/autoswitch_virtualenv.plugin.zsh
 55+++ b/autoswitch_virtualenv.plugin.zsh
 56@@ -43,7 +43,7 @@ function _maybeworkon() {
 57      if [ -z "$AUTOSWITCH_SILENT" ]; then
 58         py_version="$(_python_version "$VIRTUAL_ENV_DIR/$venv_name/bin/python")"
 59 
 60-        message="$DEFAULT_MESSAGE_FORMAT"
 61+        message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
 62         message="${message//\%venv_type/$venv_type}"
 63         message="${message//\%venv_name/$venv_name}"
 64         message="${message//\%py_version/$py_version}"
 65diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
 66index e627c3b5014a6db64733d769d5108dd1cd327264..b419c0159c532fd6dfd34d3ed5f9924ee5005f3a 100644
 67--- a/tests/test_check_venv.zunit
 68+++ b/tests/test_check_venv.zunit
 69@@ -11,6 +11,7 @@
 70         run python -m virtualenv "$HOME/.virtualenvs/foodefault" --python=python3
 71     fi
 72 
 73+    unset AUTOSWITCH_MESSAGE_FORMAT
 74     PYTHON_VERSION="$(python3 --version 2>&1)"
 75 
 76     export DISABLE_AUTOSWITCH_VENV="1"
 77diff --git a/tests/test_maybeworkon.zunit b/tests/test_maybeworkon.zunit
 78index 60a92d0321806802f804c7d13f4a95ceec3274bf..d698ef504424cad6010b74d5ba6bedfdda12f72e 100644
 79--- a/tests/test_maybeworkon.zunit
 80+++ b/tests/test_maybeworkon.zunit
 81@@ -8,6 +8,7 @@
 82         run python -m virtualenv "$HOME/.virtualenvs/foobar" --python=python3
 83     fi
 84 
 85+    unset AUTOSWITCH_MESSAGE_FORMAT
 86     PYTHON_VERSION="$(python3 --version 2>&1)"
 87 
 88     export DISABLE_AUTOSWITCH_VENV="1"
 89@@ -29,6 +30,16 @@
 90     assert "$output" same_as "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
 91 }
 92 
 93+@test '_maybeworkon - custom message' {
 94+    VIRTUAL_ENV=""
 95+    AUTOSWITCH_MESSAGE_FORMAT="Hello %venv_type - this is %venv_name (%py_version)"
 96+
 97+    run _maybeworkon foobar cheese
 98+
 99+    assert $state equals 0
100+    assert "$output" same_as "Hello cheese - this is foobar ($PYTHON_VERSION)"
101+}
102+
103 @test '_maybeworkon - switches virtualenv if current virtualenv is different' {
104     VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
105 
106diff --git a/tests/test_plugin.zunit b/tests/test_plugin.zunit
107index 5a9a3bffd002389ead0c373797b250cb32f51d4a..f8580b94f0824c7f0633f958a604946d2577c127 100644
108--- a/tests/test_plugin.zunit
109+++ b/tests/test_plugin.zunit
110@@ -6,6 +6,7 @@
111         run python -m virtualenv "$HOME/.virtualenvs/foobar" --python=python3
112     fi
113     unset VIRTUAL_ENV
114+    unset AUTOSWITCH_MESSAGE_FORMAT
115     PYTHON_VERSION="$(python3 --version)"
116 }
117