1
0
mirror of https://github.com/robbyrussell/oh-my-zsh.git synced 2025-12-06 15:30:40 +01:00

Work better with subdirectories

This commit is contained in:
Levan Vachnadze
2024-12-02 00:41:12 +04:00
parent 33663127b0
commit 5e722f1bff

View File

@@ -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
for _venv_name in "${PYTHON_VENV_NAME[@]}"; do if [[ -n "$active_venv" ]] && [[ "$current_dir" != "$venv_root"* ]]; then
local activate_script="${PWD}/${_venv_name}/bin/activate" deactivate > /dev/null 2>&1
if [[ -f "${activate_script}" ]]; then active_venv=""
source "${activate_script}" > /dev/null 2>&1 fi
echo "Automatically activated virtual environment ${_venv_name}"
return 0 # Activate the virtual environment if not active and found in the current directory or its ancestors
fi if [[ -z "$active_venv" ]]; then
done local dir="$PWD"
while [[ "$dir" != "/" ]]; do
for _venv_name in "${PYTHON_VENV_NAME[@]}"; do
local activate_script="${dir}/${_venv_name}/bin/activate"
if [[ -f "$activate_script" ]]; then
source "$activate_script" > /dev/null 2>&1
return
fi
done
dir="$(dirname "$dir")"
done
fi
} }
# Hook to execute the function on directory changes
add-zsh-hook chpwd auto_vrun add-zsh-hook chpwd auto_vrun
auto_vrun auto_vrun # Run once for the current directory
fi fi