diff options
Diffstat (limited to 'home/.oh-my-zsh/plugins/poetry-env')
| -rw-r--r-- | home/.oh-my-zsh/plugins/poetry-env/README.md | 10 | ||||
| -rw-r--r-- | home/.oh-my-zsh/plugins/poetry-env/poetry-env.plugin.zsh | 27 |
2 files changed, 37 insertions, 0 deletions
diff --git a/home/.oh-my-zsh/plugins/poetry-env/README.md b/home/.oh-my-zsh/plugins/poetry-env/README.md new file mode 100644 index 0000000..bd99d2a --- /dev/null +++ b/home/.oh-my-zsh/plugins/poetry-env/README.md @@ -0,0 +1,10 @@ +# Poetry Environment Plugin + +This plugin automatically changes poetry environment when you cd into or out of the project directory. +Note: Script looks for pyproject.toml file to determine poetry if its a poetry environment + +To use it, add `poetry-env` to the plugins array in your zshrc file: + +```zsh +plugins=(... poetry-env) +``` diff --git a/home/.oh-my-zsh/plugins/poetry-env/poetry-env.plugin.zsh b/home/.oh-my-zsh/plugins/poetry-env/poetry-env.plugin.zsh new file mode 100644 index 0000000..dca388d --- /dev/null +++ b/home/.oh-my-zsh/plugins/poetry-env/poetry-env.plugin.zsh @@ -0,0 +1,27 @@ +_togglePoetryShell() { + # 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 + if [[ $poetry_active -eq 1 ]] && { [[ $in_poetry_dir -eq 0 ]] || [[ "$PWD" != "$poetry_dir"* ]]; }; then + export poetry_active=0 + unset poetry_dir + (( $+functions[deactivate] )) && deactivate + fi + + # 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" + fi + fi +} +autoload -U add-zsh-hook +add-zsh-hook chpwd _togglePoetryShell +_togglePoetryShell # Initial call to check the current directory at shell startup |
