-
Notifications
You must be signed in to change notification settings - Fork 2
/
venv_manager.plugin.zsh
41 lines (34 loc) · 1.12 KB
/
venv_manager.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
## Plugin to create/change/activate/deactivate python virtual environment depending
## on the current path. Searches through each parent in PWD for the folder
## $VENV_DIR (by default ".venv"), and activates it appropriately.
## If you are under $VENV_MANAGER_IGNORE_DIR, it does nothing
## TODO: Allow multiple ignore dirs
_OLD_PWD="/"
: "${VENV_DIR:=.venv}"
: "${VENV_MANAGER_IGNORE_DIR:=}"
function venv_manager_switch_venv() {
if [[ -n "${VENV_MANAGER_IGNORE_DIR}" ]] && [[ "$(pwd)/" == "$VENV_MANAGER_IGNORE_DIR"* ]]; then
return
fi
if [ "$_OLD_PWD" = "$(pwd)" ]; then
if [ ! -d "$VIRTUAL_ENV" ]; then
command -v deactivate &>/dev/null && deactivate
fi
return
fi
_OLD_PWD="$(pwd)"
new_venv=""
cur_dir="$(pwd)"
while [ "$cur_dir" != "/" ]; do
if [ -d "${cur_dir}/${VENV_DIR}" ]; then
new_venv="${cur_dir}/${VENV_DIR}"
break
fi
cur_dir="$(dirname "$cur_dir")"
done
if [ -n "$new_venv" ]; then
source "$new_venv/bin/activate"
else
command -v deactivate &>/dev/null && deactivate
fi
}