@@ -58,30 +58,41 @@ auto_venv_update_check() {
58
58
# Call the update check function when the plugin loads
59
59
auto_venv_update_check
60
60
61
- # autoload is used to load the add-zsh-hook function.
62
- autoload -U add-zsh-hook
63
-
64
- # Function to search for .venv directories from the current directory upwards.
65
61
find_venv () {
66
62
local dir=$1
67
- while [[ " $dir " != " " && ! -e " $dir /.venv" ]]; do
68
- dir=${dir%/* }
63
+ local found_venvs=() # Array to hold found virtual environments
64
+
65
+ # Search for 'bin/activate' in current and all parent directories
66
+ while [[ " $dir " != " " ]]; do
67
+ # Look for 'bin/activate' in all subdirectories of the current directory
68
+ found_venvs=($( find " $dir " -maxdepth 3 -type f -name " activate" -path " */bin/activate" ) )
69
+ echo $found_venvs
70
+ if [[ " ${# found_venvs[@]} " -gt 0 ]]; then
71
+ echo " ${found_venvs[0]%/*/* } " # Return the first found virtual environment path
72
+ return
73
+ fi
74
+ dir=${dir%/* } # Move up to the parent directory
69
75
done
70
- echo $dir
71
76
}
72
77
73
78
# Function to activate or deactivate virtual environments.
74
79
activate_venv () {
75
80
local venv_path=$( find_venv $PWD )
76
81
if [[ -n " $venv_path " ]]; then
82
+ if [[ " $VIRTUAL_ENV " && " $VIRTUAL_ENV " != " $venv_path " ]]; then
83
+ deactivate
84
+ fi
77
85
# Activates the virtual environment if found.
78
- source " $venv_path /.venv/bin/activate "
86
+ source " $venv_path "
79
87
elif [[ -n " $VIRTUAL_ENV " ]]; then
80
88
# Deactivates the virtual environment if exited from project directory.
81
89
deactivate
82
90
fi
83
91
}
84
92
93
+ # autoload is used to load the add-zsh-hook function.
94
+ autoload -U add-zsh-hook
95
+
85
96
# Sets up the function to be called whenever the current directory changes.
86
97
add-zsh-hook chpwd activate_venv
87
98
0 commit comments