mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2025-12-06 23:40:39 +01:00
Work better with subdirectories
This commit is contained in:
@@ -75,32 +75,52 @@ function mkv() {
|
|||||||
|
|
||||||
python3 -m venv "${name}" || return
|
python3 -m venv "${name}" || return
|
||||||
echo >&2 "Created venv in '${venvpath}'"
|
echo >&2 "Created venv in '${venvpath}'"
|
||||||
vrun "${name}"
|
|
||||||
|
# Activate directly without relying on vrun
|
||||||
|
if [[ -f "${venvpath}/bin/activate" ]]; then
|
||||||
|
. "${venvpath}/bin/activate" || return $?
|
||||||
|
echo "Activated virtual environment ${name}"
|
||||||
|
else
|
||||||
|
echo >&2 "Error: Failed to activate the virtual environment in '${venvpath}'"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
|
if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
|
||||||
# Automatically activate the first valid venv and deactivate if leaving its root directory
|
|
||||||
function auto_vrun() {
|
function auto_vrun() {
|
||||||
# If we're inside a virtual environment, check if we're leaving its root
|
local current_dir="$PWD"
|
||||||
if [[ -n "${VIRTUAL_ENV}" ]]; then
|
local active_venv="${VIRTUAL_ENV}"
|
||||||
local venv_root="${VIRTUAL_ENV%/bin*}" # Extract the root directory of the virtual environment
|
local venv_root=""
|
||||||
# Deactivate if we are outside the root directory of the virtual environment
|
|
||||||
if [[ "$PWD" != "$venv_root"* ]]; then
|
# Determine the root of the currently active virtual environment
|
||||||
echo "Deactivating virtual environment"
|
if [[ -n "$active_venv" ]]; then
|
||||||
deactivate > /dev/null 2>&1
|
venv_root="${VIRTUAL_ENV%/bin*}"
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Attempt to activate a venv from the list in the current directory
|
# Deactivate if leaving the root directory of the virtual environment
|
||||||
|
if [[ -n "$active_venv" ]] && [[ "$current_dir" != "$venv_root"* ]]; then
|
||||||
|
deactivate > /dev/null 2>&1
|
||||||
|
active_venv=""
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Activate the virtual environment if not active and found in the current directory or its ancestors
|
||||||
|
if [[ -z "$active_venv" ]]; then
|
||||||
|
local dir="$PWD"
|
||||||
|
while [[ "$dir" != "/" ]]; do
|
||||||
for _venv_name in "${PYTHON_VENV_NAME[@]}"; do
|
for _venv_name in "${PYTHON_VENV_NAME[@]}"; do
|
||||||
local activate_script="${PWD}/${_venv_name}/bin/activate"
|
local activate_script="${dir}/${_venv_name}/bin/activate"
|
||||||
if [[ -f "${activate_script}" ]]; then
|
if [[ -f "$activate_script" ]]; then
|
||||||
source "${activate_script}" > /dev/null 2>&1
|
source "$activate_script" > /dev/null 2>&1
|
||||||
echo "Automatically activated virtual environment ${_venv_name}"
|
return
|
||||||
return 0
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
dir="$(dirname "$dir")"
|
||||||
add-zsh-hook chpwd auto_vrun
|
done
|
||||||
auto_vrun
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Hook to execute the function on directory changes
|
||||||
|
add-zsh-hook chpwd auto_vrun
|
||||||
|
auto_vrun # Run once for the current directory
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user