From d75c3354d5cff80ebbd624a8a4af68bc5ede0d71 Mon Sep 17 00:00:00 2001 From: Alan Bradley Date: Sun, 18 Mar 2018 18:58:28 +0000 Subject: [PATCH] Plugin for pipenv Add completion for pipenv Auto activate and deactivate for pipenv shell --- plugins/pipenv/pipenv.plugin.zsh | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 plugins/pipenv/pipenv.plugin.zsh diff --git a/plugins/pipenv/pipenv.plugin.zsh b/plugins/pipenv/pipenv.plugin.zsh new file mode 100644 index 00000000..86edd3a4 --- /dev/null +++ b/plugins/pipenv/pipenv.plugin.zsh @@ -0,0 +1,36 @@ +#compdef pipenv +_pipenv() { + eval $(env COMMANDLINE="${words[1,$CURRENT]}" _PIPENV_COMPLETE=complete-zsh pipenv) +} + +_togglePipenvShell() { + # deactivate shell if Pipfile doesn't exit and not in a sub dir + if [[ ! -a "$PWD/Pipfile" ]]; then + if [[ "$PIPENV_ACTIVE" == 1 ]]; then + if [[ "$PWD" != "$pipfile_dir"* ]]; then + exit + fi + fi + fi + + # activate the shell if Pipfile exists + if [[ "$PIPENV_ACTIVE" != 1 ]]; then + if [[ -a "$PWD/Pipfile" ]]; then + export pipfile_dir="$PWD" + pipenv shell + fi + fi +} + +if [[ "$(basename ${(%):-%x})" != "_pipenv" ]]; then + autoload -U compinit && compinit + compdef _pipenv pipenv +fi + +export PROMPT_COMMAND=_togglePipenvShell +if [ -n "$ZSH_VERSION" ]; then + function chpwd() { + _togglePipenvShell + } +fi +