From cfe71661326631ce3710d213126eac03bfbca14e Mon Sep 17 00:00:00 2001 From: Hongyi Zhao Date: Sat, 14 Mar 2020 23:37:21 +0800 Subject: [PATCH] Based on the _pip_completion function defined by pip completion to determine whether we should reevaluate the pip completion or not. --- completion/available/pip.completion.bash | 27 +++++++---------------- completion/available/pip3.completion.bash | 27 +++++++---------------- 2 files changed, 16 insertions(+), 38 deletions(-) diff --git a/completion/available/pip.completion.bash b/completion/available/pip.completion.bash index 728c97c8..8ee38f32 100644 --- a/completion/available/pip.completion.bash +++ b/completion/available/pip.completion.bash @@ -6,8 +6,6 @@ # If the pip package is installed within virtual environments, # you should first install pip for the corresponding environment. -# Fix pip completion for running it within/outside of pyenv/virtualenv/venv/conda environment. - #https://github.com/Bash-it/bash-it/blob/b35d41464c0bd390e573f7423eaaa63666521c70/themes/base.theme.bash#L511 function safe_append_prompt_command { local prompt_re @@ -39,26 +37,17 @@ function safe_append_prompt_command { _pip_completion_bash() { - local _pip - # For pip resides within the pyenv/virtualenv/venv/conda environments: - if [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/pip" ]; then - _pip="$VIRTUAL_ENV/bin/pip" - elif [ -n "$CONDA_PREFIX" ] && [ -x "$CONDA_PREFIX/bin/pip" ]; then - _pip="$CONDA_PREFIX/bin/pip" - # For pip resides outside of a virtual environment: - elif [ -x /usr/bin/pip ]; then - _pip=/usr/bin/pip - fi - # FIXME: do the trick without exporting variable into environment: - if [ -n "$_pip" ]; then - if [ -z "$_pip_command_path" ] || [ "$_pip_command_path" != "$_pip" ]; then - eval "$($_pip completion --bash)" - export _pip_command_path=$_pip - fi + # Based on the _pip_completion function defined by pip completion to determine + # whether we should reevaluate the pip completion or not: + if ! command -v _pip_completion >/dev/null && + ( [ -n "$VIRTUAL_ENV" -a -x "$VIRTUAL_ENV/bin/pip" ] || + [ -n "$CONDA_PREFIX" -a -x "$CONDA_PREFIX/bin/pip" ] || + [ -x /usr/bin/pip ] ); then + eval "$(pip completion --bash)" fi - unset _pip } safe_append_prompt_command _pip_completion_bash + diff --git a/completion/available/pip3.completion.bash b/completion/available/pip3.completion.bash index b32e03e8..c5f012a6 100644 --- a/completion/available/pip3.completion.bash +++ b/completion/available/pip3.completion.bash @@ -6,8 +6,6 @@ # If the pip package is installed within virtual environments, # you should first install pip for the corresponding environment. -# Fix pip completion for running it within/outside of pyenv/virtualenv/venv/conda environment. - #https://github.com/Bash-it/bash-it/blob/b35d41464c0bd390e573f7423eaaa63666521c70/themes/base.theme.bash#L511 function safe_append_prompt_command { local prompt_re @@ -39,26 +37,17 @@ function safe_append_prompt_command { _pip_completion_bash() { - local _pip - # For pip resides within the pyenv/virtualenv/venv/conda environments: - if [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/pip3" ]; then - _pip="$VIRTUAL_ENV/bin/pip3" - elif [ -n "$CONDA_PREFIX" ] && [ -x "$CONDA_PREFIX/bin/pip3" ]; then - _pip="$CONDA_PREFIX/bin/pip3" - # For pip resides outside of a virtual environment: - elif [ -x /usr/bin/pip3 ]; then - _pip=/usr/bin/pip3 - fi - # FIXME: do the trick without exporting variable into environment: - if [ -n "$_pip" ]; then - if [ -z "$_pip_command_path" ] || [ "$_pip_command_path" != "$_pip" ]; then - eval "$($_pip completion --bash)" - export _pip_command_path=$_pip - fi + # Based on the _pip_completion function defined by pip completion to determine + # whether we should reevaluate the pip completion or not: + if ! command -v _pip_completion >/dev/null && + ( [ -n "$VIRTUAL_ENV" -a -x "$VIRTUAL_ENV/bin/pip3" ] || + [ -n "$CONDA_PREFIX" -a -x "$CONDA_PREFIX/bin/pip3" ] || + [ -x /usr/bin/pip3 ] ); then + eval "$(pip3 completion --bash)" fi - unset _pip } safe_append_prompt_command _pip_completion_bash +