1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,63 @@ |
1 |
+VIRTUAL_ENV_DISABLE_PROMPT='1' |
|
2 |
+ |
|
3 |
+__prompt_venv_rel() |
|
4 |
+{ |
|
5 |
+ __prompt_venv_rel_dir="$PWD" |
|
6 |
+ __prompt_venv_rel_up='' |
|
7 |
+ while [ "${1#$__prompt_venv_rel_dir/}" = "$1" ] |
|
8 |
+ do |
|
9 |
+ __prompt_venv_rel_dir="${__prompt_venv_rel_dir%/*}" |
|
10 |
+ __prompt_venv_rel_up="../$__prompt_venv_rel_up" |
|
11 |
+ done |
|
12 |
+ printf '%s' "$__prompt_venv_rel_up${1#$__prompt_venv_rel_dir/}" |
|
13 |
+} |
|
14 |
+ |
|
15 |
+__prompt_venv() |
|
16 |
+{ |
|
17 |
+ if [ "${VIRTUAL_ENV:-}" ] |
|
18 |
+ then |
|
19 |
+ printf "$1" "$(__prompt_venv_rel "$VIRTUAL_ENV")" |
|
20 |
+ fi |
|
21 |
+} |
|
22 |
+ |
|
23 |
+# Ask to activate the first virtual environment found, searching from the |
|
24 |
+# current directory up to `$HOME` or `/`, whichever comes first. |
|
25 |
+# https://github.com/python/cpython/blob/master/Lib/venv/scripts/common/activate |
|
26 |
+__prompt_venv_activate() |
|
27 |
+{ |
|
28 |
+ if [ "${VIRTUAL_ENV:-}" ] |
|
29 |
+ then |
|
30 |
+ deactivate |
|
31 |
+ fi |
|
32 |
+ __prompt_venv_dir="$PWD" |
|
33 |
+ while |
|
34 |
+ [ "$__prompt_venv_dir" != "$HOME" ] && |
|
35 |
+ [ "$__prompt_venv_dir" != '' ] |
|
36 |
+ do |
|
37 |
+ for __prompt_venv_activate in \ |
|
38 |
+ "$__prompt_venv_dir/"*'/bin/activate' \ |
|
39 |
+ "$__prompt_venv_dir/."*'/bin/activate' |
|
40 |
+ do |
|
41 |
+ [ -r "$__prompt_venv_activate" ] || continue |
|
42 |
+ grep -q 'VIRTUAL_ENV' "$__prompt_venv_activate" || continue |
|
43 |
+ __prompt_venv="${__prompt_venv_activate%/*/*}" |
|
44 |
+ if [ "$__prompt_venv" != "$__prompt_venv_prev" ] |
|
45 |
+ then |
|
46 |
+ while true |
|
47 |
+ do |
|
48 |
+ printf 'Activate %s? [y/N] ' "$(__prompt_venv_rel "$__prompt_venv")" |
|
49 |
+ read -r __prompt_venv_reply |
|
50 |
+ case "$__prompt_venv_reply" in |
|
51 |
+ 'y'|'n'|'') break; ;; |
|
52 |
+ esac |
|
53 |
+ done |
|
54 |
+ fi |
|
55 |
+ case "$__prompt_venv_reply" in |
|
56 |
+ 'y') . "$__prompt_venv_activate"; ;; |
|
57 |
+ esac |
|
58 |
+ __prompt_venv_prev="$__prompt_venv" |
|
59 |
+ return 0 |
|
60 |
+ done |
|
61 |
+ __prompt_venv_dir="${__prompt_venv_dir%/*}" |
|
62 |
+ done |
|
63 |
+} |