Diff
1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
2index 343bc2fb1e6c89a71fb34036c30013f5823bf90b..8e2257fe41a364421d00ad1a679ac1eb6be437a7 100644
3--- a/autoswitch_virtualenv.plugin.zsh
4+++ b/autoswitch_virtualenv.plugin.zsh
5@@ -31,17 +31,37 @@ function _autoswitch_message() {
6 fi
7 }
8
9+function _get_venv_type() {
10+ local venv_dir="$1"
11+ local venv_type="${2:-virtualenv}"
12+ if [[ -f "$venv_dir/Pipfile" ]]; then
13+ venv_type="pipenv"
14+ elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" ]]; then
15+ venv_type="virtualenv"
16+ fi
17+ printf "%s" "$venv_type"
18+}
19
20-function _maybeworkon() {
21+
22+function _get_venv_name() {
23 local venv_dir="$1"
24 local venv_type="$2"
25- local venv_name="$(basename $venv_dir)"
26+ local venv_name="$(basename "$venv_dir")"
27
28 # clear pipenv from the extra identifiers at the end
29 if [[ "$venv_type" == "pipenv" ]]; then
30 venv_name="${venv_name%-*}"
31 fi
32
33+ printf "%s" "$venv_name"
34+}
35+
36+
37+function _maybeworkon() {
38+ local venv_dir="$1"
39+ local venv_type="$2"
40+ local venv_name="$(_get_venv_name $venv_dir $venv_type)"
41+
42 local DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[🐍%py_version]${NORMAL}"
43 if [[ "$LANG" != *".UTF-8" ]]; then
44 # Remove multibyte characters if the terminal does not support utf-8
45@@ -95,6 +115,7 @@ function check_venv()
46
47 # Get the .venv file, scanning parent directories
48 local venv_path=$(_check_venv_path "$PWD")
49+
50 if [[ -n "$venv_path" ]]; then
51
52 stat --version &> /dev/null
53@@ -122,18 +143,20 @@ function check_venv()
54 fi
55
56 # check if Pipfile exists rather than invoking pipenv as it is slow
57- if [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
58+ if [[ -f "Pipfile" ]] && type "pipenv" > /dev/null; then
59 if venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv 2>/dev/null)"; then
60 _maybeworkon "$venv_path" "pipenv"
61 return
62 fi
63 fi
64
65+ local venv_type="$(_get_venv_type "$PWD" "unknown")"
66+
67 # If we still haven't got anywhere, fallback to defaults
68- if [[ -f "$PWD/Pipfile" ]]; then
69+ if [[ "$venv_type" == "pipenv" ]]; then
70 printf "Python project detected. "
71 printf "Run ${PURPLE}pipenv install${NORMAL} to setup autoswitching\n"
72- elif [[ -f "$PWD/requirements.txt" || -f "$PWD/setup.py" ]]; then
73+ elif [[ "$venv_type" == "virtualenv" ]]; then
74 printf "Python project detected. "
75 printf "Run ${PURPLE}mkvenv${NORMAL} to setup autoswitching\n"
76 fi
77@@ -143,10 +166,12 @@ function check_venv()
78 # Switch to the default virtual environment
79 function _default_venv()
80 {
81+ local venv_type="$(_get_venv_type "$OLDPWD")"
82 if [[ -n "$AUTOSWITCH_DEFAULTENV" ]]; then
83- _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "virtualenv"
84+ _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "$venv_type"
85 elif [[ -n "$VIRTUAL_ENV" ]]; then
86- _autoswitch_message "Deactivating: ${BOLD}${PURPLE}%s${NORMAL}\n" "$(basename "$VIRTUAL_ENV")"
87+ local venv_name="$(_get_venv_name "$VIRTUAL_ENV")"
88+ _autoswitch_message "Deactivating: ${BOLD}${PURPLE}%s${NORMAL}\n" "$venv_name"
89 deactivate
90 fi
91 }