-
Notifications
You must be signed in to change notification settings - Fork 4
/
venv.fish
37 lines (30 loc) · 1.2 KB
/
venv.fish
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
# Based on https://gist.github.com/tommyip/cf9099fa6053e30247e5d0318de2fb9e
# Based on https://gist.github.com/bastibe/c0950e463ffdfdfada7adf149ae77c6f
# Changes:
# * Instead of overriding cd, we detect directory change. This allows the script to work
# for other means of cd, such as z.
# * Update syntax to work with new versions of fish.
function __auto_source_venv --on-variable PWD --description "Activate/Deactivate virtualenv on directory change"
status --is-command-substitution; and return
# Check if we are inside a git repository
if git rev-parse --show-toplevel &>/dev/null
set dir (realpath (git rev-parse --show-toplevel))
else
set dir (pwd)
end
# Find a virtual environment in the directory
set VENV_DIR_NAMES env .env venv .venv
for venv_dir in $dir/$VENV_DIR_NAMES
if test -e "$venv_dir/bin/activate.fish"
break
end
end
# Activate venv if it was found and not activated before
if test "$VIRTUAL_ENV" != "$venv_dir" -a -e "$venv_dir/bin/activate.fish"
source $venv_dir/bin/activate.fish
# Deactivate venv if it is activated but the directory doesn't exist
else if not test -z "$VIRTUAL_ENV" -o -e "$venv_dir"
deactivate
end
end
__auto_source_venv