-
Notifications
You must be signed in to change notification settings - Fork 3
/
zsh-auto-venv.zsh
48 lines (40 loc) · 1.05 KB
/
zsh-auto-venv.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
42
43
44
45
46
47
48
function _activate() {
local venv_dir="$1"
# Don't reactivate an already activated virtual environment
if [[ -z "$VIRTUAL_ENV" || "$venv_dir" != "$VIRTUAL_ENV" ]]; then
source "$venv_dir/bin/activate"
fi
}
function _deactivate() {
if [[ -n "$VIRTUAL_ENV" ]]; then
deactivate
fi
}
# Gives the path to the nearest target file
function _check_path()
{
local check_dir="$1"
local check_file="$2"
if [[ -d "${check_dir}/$check_file" ]]; then
printf "${check_dir}/$check_file"
return
else
# Abort search at file system root or HOME directory (latter is a performance optimisation).
if [[ "$check_dir" = "/" || "$check_dir" = "$HOME" ]]; then
return
fi
_check_path "$(dirname "$check_dir")" "$check_file"
fi
}
function _check_venv()
{
local venv_path="$(_check_path "$PWD" "venv")"
if [[ -n "$venv_path" ]]; then
_activate "$venv_path"
else
_deactivate
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook chpwd _check_venv
_check_venv