summaryrefslogtreecommitdiff
path: root/plugins/poetry-env/poetry-env.plugin.zsh
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/poetry-env/poetry-env.plugin.zsh')
-rw-r--r--plugins/poetry-env/poetry-env.plugin.zsh27
1 files changed, 27 insertions, 0 deletions
diff --git a/plugins/poetry-env/poetry-env.plugin.zsh b/plugins/poetry-env/poetry-env.plugin.zsh
new file mode 100644
index 000000000..86e5fad4e
--- /dev/null
+++ b/plugins/poetry-env/poetry-env.plugin.zsh
@@ -0,0 +1,27 @@
+# Automatic poetry environment activation/deactivation
+_togglePoetryShell() {
+ # deactivate environment if pyproject.toml doesn't exist and not in a subdir
+ if [[ ! -f "$PWD/pyproject.toml" ]] ; then
+ if [[ "$poetry_active" == 1 ]]; then
+ if [[ "$PWD" != "$poetry_dir"* ]]; then
+ export poetry_active=0
+ deactivate
+ return
+ fi
+ fi
+ fi
+
+ # activate the environment if pyproject.toml exists
+ if [[ "$poetry_active" != 1 ]]; then
+ if [[ -f "$PWD/pyproject.toml" ]]; then
+ if grep -q 'tool.poetry' "$PWD/pyproject.toml"; then
+ export poetry_active=1
+ export poetry_dir="$PWD"
+ source "$(poetry env info --path)/bin/activate"
+ fi
+ fi
+ fi
+}
+autoload -U add-zsh-hook
+add-zsh-hook chpwd _togglePoetryShell
+_togglePoetryShell