-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_activate.fish
84 lines (73 loc) · 2.37 KB
/
_activate.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# We use a subshell for process isolation; no need to unset any variables
# Don't run this file directly or it will pollute your env
#
# This file must be run like so:
# fish -i -C". <this file>"
# Put these variables in the outter scope
set -g venv_dir .venv
set -g cwd (pwd)
function _findvenv
if test (count $argv) -gt 0
set -g venv_dir $argv[1]
if test (count $argv) -gt 1
set -g cwd $argv[2]
end
end
if test -d $cwd/$venv_dir
return 0
else if [ $cwd = '/' ]
set -e cwd
return 1
else
_findvenv $venv_dir (dirname $cwd)
return $status
end
end
function _setvenv
_findvenv $argv[1] $argv[2]
if [ $status != 0 ]
echo "Couldn't find a virtualenv. Looked for $venv_dir/ here and in all parents" 1>&2
echo "Usage: ./activate.fish [venv dir=.venv] [initial search path=./]" 1>&2
return 1
else
# Give some feedback if we found the virtual env directory somewhere other than the current directory
if [ $cwd != (pwd) ]
echo "Found virtualenv $venv_dir/ in: $cwd"
end
set -gx VIRTUAL_ENV $cwd/.venv
set -gx VIRTUAL_ENV_PROJECT (basename (dirname $VIRTUAL_ENV))
set -gx PATH "$VIRTUAL_ENV/bin" $PATH
# unset PYTHONHOME if set
if set -q PYTHONHOME
set -e PYTHONHOME
end
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
# fish uses a function instead of an env var to generate the prompt.
# save the current fish_prompt function as the function _old_fish_prompt
functions -c fish_prompt _old_fish_prompt
# with the original prompt function renamed, we can override with our own.
function fish_prompt
# Save the return status of the last command
set -l old_status $status
# Prepend env
printf "(%s) " $VIRTUAL_ENV_PROJECT
# Restore the return status of the previous command.
echo "exit $old_status" | .
_old_fish_prompt
end
end
return 0
end
end
# Check for args and execute
if test (count $argv) -gt 0
set -g venv_dir $argv[1]
if test (count $argv) -gt 1
set -g cwd $argv[2]
else
set -g cwd (pwd)
end
else
set -g venv_dir .venv
end
_setvenv $venv_dir $cwd