mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-08-07 06:08:39 +02:00
feat(pipenv): add completion for >= 2026.5.0 (#13686)
Co-authored-by: Carlo Sala <carlosalag@protonmail.com>
This commit is contained in:
parent
a4a224b3fe
commit
ab84cca50a
2 changed files with 55 additions and 8 deletions
|
|
@ -10,7 +10,7 @@ plugins=(... pipenv ...)
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Adds completion for pipenv
|
- Adds completion for pipenv ([install the `argcomplete` package to get it working with pipenv >= 2026.5.0](https://pipenv.pypa.io/en/latest/shell.html#shell-completion))
|
||||||
- Auto activates and deactivates pipenv shell
|
- Auto activates and deactivates pipenv shell
|
||||||
- Adds short aliases for common pipenv commands
|
- Adds short aliases for common pipenv commands
|
||||||
- `pch` is aliased to `pipenv check`
|
- `pch` is aliased to `pipenv check`
|
||||||
|
|
@ -29,6 +29,18 @@ plugins=(... pipenv ...)
|
||||||
- `pvenv` is aliased to `pipenv --venv`
|
- `pvenv` is aliased to `pipenv --venv`
|
||||||
- `ppy` is aliased to `pipenv --py`
|
- `ppy` is aliased to `pipenv --py`
|
||||||
|
|
||||||
|
## Cache
|
||||||
|
|
||||||
|
This plugin caches the Pipenv version to avoid running `pipenv --version` on every shell startup. The cache is automatically refreshed asynchronously when the plugin is loaded, which is usually when you start a new terminal session.
|
||||||
|
|
||||||
|
For legacy Pipenv versions, the generated completion script is also cached.
|
||||||
|
|
||||||
|
The cache is stored at:
|
||||||
|
|
||||||
|
- `$ZSH_CACHE_DIR/pipenv_version` version of Pipenv, used to choose between argcomplete-based completion and legacy Click-based completion.
|
||||||
|
|
||||||
|
- `$ZSH_CACHE_DIR/completions/_pipenv` legacy Click-based completion script.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
### Shell activation
|
### Shell activation
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,49 @@ if (( ! $+commands[pipenv] )); then
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If the completion file doesn't exist yet, we need to autoload it and
|
# Compatibility note:
|
||||||
# bind it to `pipenv`. Otherwise, compinit will have already done that.
|
# pipenv < 2026.5.0 used Click-based shell completion driven by the
|
||||||
if [[ ! -f "$ZSH_CACHE_DIR/completions/_pipenv" ]]; then
|
# _PIPENV_COMPLETE environment variable.
|
||||||
typeset -g -A _comps
|
#
|
||||||
autoload -Uz _pipenv
|
# pipenv >= 2026.5.0 removed this mechanism and switched to argcomplete-based
|
||||||
_comps[pipenv]=_pipenv
|
# completion using register-python-argcomplete instead.
|
||||||
|
|
||||||
|
autoload -Uz is-at-least
|
||||||
|
|
||||||
|
_pipenv_version_cache="$ZSH_CACHE_DIR/pipenv_version"
|
||||||
|
|
||||||
|
if [[ -f "$_pipenv_version_cache" ]]; then
|
||||||
|
_pipenv_version="$(< "$_pipenv_version_cache" 2>/dev/null)"
|
||||||
|
else
|
||||||
|
_pipenv_version="${$(pipenv --version 2>/dev/null)#pipenv, version }"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_PIPENV_COMPLETE=zsh_source pipenv >| "$ZSH_CACHE_DIR/completions/_pipenv" &|
|
{
|
||||||
|
_pipenv_fresh_version="${$(pipenv --version 2>/dev/null)#pipenv, version }"
|
||||||
|
[[ -n "$_pipenv_fresh_version" ]] && print -r -- "$_pipenv_fresh_version" >| "$_pipenv_version_cache"
|
||||||
|
} &|
|
||||||
|
|
||||||
|
if is-at-least 2026.5.0 "$_pipenv_version"; then
|
||||||
|
# pipenv >= 2026.5.0: argcomplete-based completion (no legacy fallback)
|
||||||
|
if (( $+commands[register-python-argcomplete] )); then
|
||||||
|
autoload -Uz bashcompinit
|
||||||
|
bashcompinit
|
||||||
|
|
||||||
|
eval "$(register-python-argcomplete pipenv)"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# legacy Click-based completion via _PIPENV_COMPLETE
|
||||||
|
|
||||||
|
# If the completion file doesn't exist yet, we need to autoload it and
|
||||||
|
# bind it to `pipenv`. Otherwise, compinit will have already done that.
|
||||||
|
if [[ ! -f "$ZSH_CACHE_DIR/completions/_pipenv" ]]; then
|
||||||
|
typeset -g -A _comps
|
||||||
|
autoload -Uz _pipenv
|
||||||
|
_comps[pipenv]=_pipenv
|
||||||
|
fi
|
||||||
|
|
||||||
|
_PIPENV_COMPLETE=zsh_source pipenv >| "$ZSH_CACHE_DIR/completions/_pipenv" &|
|
||||||
|
fi
|
||||||
|
|
||||||
if zstyle -T ':omz:plugins:pipenv' auto-shell; then
|
if zstyle -T ':omz:plugins:pipenv' auto-shell; then
|
||||||
# Automatic pipenv shell activation/deactivation
|
# Automatic pipenv shell activation/deactivation
|
||||||
|
|
@ -34,6 +68,7 @@ if zstyle -T ':omz:plugins:pipenv' auto-shell; then
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
autoload -U add-zsh-hook
|
autoload -U add-zsh-hook
|
||||||
add-zsh-hook chpwd _togglePipenvShell
|
add-zsh-hook chpwd _togglePipenvShell
|
||||||
_togglePipenvShell
|
_togglePipenvShell
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue