2023-09-20 16:51:27 +02:00
|
|
|
_togglePoetryShell() {
|
2024-04-12 09:24:54 +02:00
|
|
|
# Determine if currently in a Poetry-managed directory
|
|
|
|
local in_poetry_dir=0
|
|
|
|
if [[ -f "$PWD/pyproject.toml" ]] && grep -q 'tool.poetry' "$PWD/pyproject.toml"; then
|
|
|
|
in_poetry_dir=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Deactivate the current environment if moving out of a Poetry directory or into a different Poetry directory
|
2024-11-14 09:00:48 +01:00
|
|
|
if [[ $poetry_active -eq 1 ]] && { [[ $in_poetry_dir -eq 0 ]] || [[ "$PWD" != "$poetry_dir"* ]]; }; then
|
2024-04-12 09:24:54 +02:00
|
|
|
export poetry_active=0
|
|
|
|
unset poetry_dir
|
2024-10-08 21:44:18 +02:00
|
|
|
(( $+functions[deactivate] )) && deactivate
|
2023-09-20 16:51:27 +02:00
|
|
|
fi
|
|
|
|
|
2024-04-12 09:24:54 +02:00
|
|
|
# Activate the environment if in a Poetry directory and no environment is currently active
|
|
|
|
if [[ $in_poetry_dir -eq 1 ]] && [[ $poetry_active -ne 1 ]]; then
|
|
|
|
venv_dir=$(poetry env info --path 2>/dev/null)
|
|
|
|
if [[ -n "$venv_dir" ]]; then
|
|
|
|
export poetry_active=1
|
|
|
|
export poetry_dir="$PWD"
|
|
|
|
source "${venv_dir}/bin/activate"
|
2023-09-20 16:51:27 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
autoload -U add-zsh-hook
|
|
|
|
add-zsh-hook chpwd _togglePoetryShell
|
2024-04-12 09:24:54 +02:00
|
|
|
_togglePoetryShell # Initial call to check the current directory at shell startup
|