Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit b5dc8f1

Browse files
committed
Added plugin files
1 parent 75ac792 commit b5dc8f1

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

zsh-auto-venv.plugin.zsh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source ${0:A:h}/zsh-auto-venv.zsh
2+

zsh-auto-venv.zsh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function _activate() {
2+
local venv_dir="$1"
3+
4+
# Don't reactivate an already activated virtual environment
5+
if [[ -z "$VIRTUAL_ENV" || "$venv_dir" != "$$VIRTUAL_ENV)" ]]; then
6+
source "$venv_dir/bin/activate"
7+
fi
8+
}
9+
10+
function _deactivate() {
11+
if [[ -n "$VIRTUAL_ENV" ]]; then
12+
deactivate
13+
fi
14+
}
15+
16+
# Gives the path to the nearest target file
17+
function _check_path()
18+
{
19+
local check_dir="$1"
20+
local check_file="$2"
21+
22+
if [[ -d "${check_dir}/$check_file" ]]; then
23+
printf "${check_dir}/$check_file"
24+
return
25+
else
26+
# Abort search at file system root or HOME directory (latter is a performance optimisation).
27+
if [[ "$check_dir" = "/" || "$check_dir" = "$HOME" ]]; then
28+
return
29+
fi
30+
_check_path "$(dirname "$check_dir")" "$check_file"
31+
fi
32+
}
33+
34+
function _check_venv()
35+
{
36+
local venv_path="$(_check_path "$PWD" "venv")"
37+
38+
if [[ -n "$venv_path" ]]; then
39+
_activate "$venv_path"
40+
return
41+
else
42+
_deactivate
43+
fi
44+
}
45+
46+
autoload -Uz add-zsh-hook
47+
add-zsh-hook chpwd _check_venv
48+
49+
_check_venv

0 commit comments

Comments
 (0)