From 1019b53bc77ef1d7f38d5788a20ffe68937ede11 Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Tue, 25 Jul 2023 11:05:09 +0200 Subject: [PATCH 1/4] Update agnoster.theme.sh Add Pyenv detection to agnoster: - if pyenv is not initialized, fall back to the virtualenv detection - when pyenv is set to a version other than "system", show the version number - when pyenv is in virtulenv mode, also display the virtualenv's name --- themes/agnoster/agnoster.theme.sh | 37 ++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/themes/agnoster/agnoster.theme.sh b/themes/agnoster/agnoster.theme.sh index cf5eaa87e..fac90f08c 100644 --- a/themes/agnoster/agnoster.theme.sh +++ b/themes/agnoster/agnoster.theme.sh @@ -283,7 +283,8 @@ function prompt_end { ### virtualenv prompt function prompt_virtualenv { - if [[ -d $VIRTUAL_ENV ]]; then + # Exclude pyenv + if [[ -d $VIRTUAL_ENV ]] && [[ $PYENV_VIRTUALENV_INIT != 1 ]]; then # Python could output the version information in both stdout and # stderr (e.g. if using pyenv, the output goes to stderr). local VERSION_OUTPUT=$("$VIRTUAL_ENV"/bin/python --version 2>&1) @@ -296,6 +297,39 @@ function prompt_virtualenv { fi } +### pyenv prompt +function prompt_pyenv { + if [[ $PYENV_VIRTUALENV_INIT == 1 ]]; then + # Priority is shell > local > global + local PYENV_SHELL=$(pyenv shell 2>&1) + local PYENV_LOCAL=$(pyenv local 2>&1) + local PYENV_GLOBAL=$(pyenv global 2>&1) + # pyenv shell is not set + if [[ "$PYENV_SHELL" == *"pyenv: no shell-specific version configured"* ]]; then + # Check if pyenv local is set + if [[ "$PYENV_LOCAL" == *"pyenv: no local version configured"* ]]; then + # Neither are set, we're using the global version + local PYENV_VERSION=$PYENV_GLOBAL + else + local PYENV_VERSION=$PYENV_LOCAL + fi + else + local PYENV_VERSION=$PYENV_SHELL + fi + # If it is not the system's python, then display additional info + if [[ "$PYENV_VERSION" != "system" ]]; then + # It's a pyenv virtualenv, get the version number + if [[ -d $PYENV_VIRTUAL_ENV ]]; then + local VERSION_OUTPUT=$("$PYENV_VIRTUAL_ENV"/bin/python --version 2>&1) + local PYENV_VENV_VERSION=$(awk '{print $NF}' <<< "$VERSION_OUTPUT") + prompt_segment cyan white "[$PYENV_VERSION] $(basename "$PYENV_VENV_VERSION")" + else + prompt_segment cyan white "$PYENV_VERSION" + fi + fi + fi +} + ### conda env prompt function prompt_condaenv { if [[ -d $CONDA_PREFIX ]]; then @@ -534,6 +568,7 @@ function build_prompt { [[ -z ${AG_NO_CONTEXT+x} ]] && prompt_context if [[ ${OMB_PROMPT_SHOW_PYTHON_VENV-} ]]; then prompt_virtualenv + prompt_pyenv prompt_condaenv fi prompt_dir From 95f84f301a9b76dc0144bd3a7b2464a19f9156c1 Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Tue, 25 Jul 2023 14:41:32 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Koichi Murase --- themes/agnoster/agnoster.theme.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/agnoster/agnoster.theme.sh b/themes/agnoster/agnoster.theme.sh index fac90f08c..1c3341a5f 100644 --- a/themes/agnoster/agnoster.theme.sh +++ b/themes/agnoster/agnoster.theme.sh @@ -284,7 +284,7 @@ function prompt_end { ### virtualenv prompt function prompt_virtualenv { # Exclude pyenv - if [[ -d $VIRTUAL_ENV ]] && [[ $PYENV_VIRTUALENV_INIT != 1 ]]; then + if [[ -d $VIRTUAL_ENV && $PYENV_VIRTUALENV_INIT != 1 ]]; then # Python could output the version information in both stdout and # stderr (e.g. if using pyenv, the output goes to stderr). local VERSION_OUTPUT=$("$VIRTUAL_ENV"/bin/python --version 2>&1) @@ -299,7 +299,7 @@ function prompt_virtualenv { ### pyenv prompt function prompt_pyenv { - if [[ $PYENV_VIRTUALENV_INIT == 1 ]]; then + if [[ $PYENV_VIRTUALENV_INIT == 1 ]] && _omb_util_binary_exists pyenv; then # Priority is shell > local > global local PYENV_SHELL=$(pyenv shell 2>&1) local PYENV_LOCAL=$(pyenv local 2>&1) From cf7098e0d76253b1e21515dd6b6ab65f824fc78a Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Tue, 25 Jul 2023 15:33:33 +0200 Subject: [PATCH 3/4] Update agnoster.theme.sh: rewrite pyenv test --- themes/agnoster/agnoster.theme.sh | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/themes/agnoster/agnoster.theme.sh b/themes/agnoster/agnoster.theme.sh index 1c3341a5f..46860845e 100644 --- a/themes/agnoster/agnoster.theme.sh +++ b/themes/agnoster/agnoster.theme.sh @@ -301,20 +301,12 @@ function prompt_virtualenv { function prompt_pyenv { if [[ $PYENV_VIRTUALENV_INIT == 1 ]] && _omb_util_binary_exists pyenv; then # Priority is shell > local > global - local PYENV_SHELL=$(pyenv shell 2>&1) - local PYENV_LOCAL=$(pyenv local 2>&1) - local PYENV_GLOBAL=$(pyenv global 2>&1) - # pyenv shell is not set - if [[ "$PYENV_SHELL" == *"pyenv: no shell-specific version configured"* ]]; then - # Check if pyenv local is set - if [[ "$PYENV_LOCAL" == *"pyenv: no local version configured"* ]]; then - # Neither are set, we're using the global version - local PYENV_VERSION=$PYENV_GLOBAL - else - local PYENV_VERSION=$PYENV_LOCAL - fi + # When pyenv shell is set, the environment variable $PYENV_VERSION is set with the value we want + if [[ -n $PYENV_VERSION ]]; then + local PYENV_VERSION="$PYENV_VERSION" + # Otherwise, fall back to pyenv local/global to get the version else - local PYENV_VERSION=$PYENV_SHELL + local PYENV_VERSION=$(pyenv local 2>/dev/null || pyenv global 2>/dev/null) fi # If it is not the system's python, then display additional info if [[ "$PYENV_VERSION" != "system" ]]; then From f51f24b10030da5b15ec2946c869ac5216fcbc1b Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Wed, 26 Jul 2023 10:28:25 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Koichi Murase --- themes/agnoster/agnoster.theme.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/themes/agnoster/agnoster.theme.sh b/themes/agnoster/agnoster.theme.sh index 46860845e..569039526 100644 --- a/themes/agnoster/agnoster.theme.sh +++ b/themes/agnoster/agnoster.theme.sh @@ -284,7 +284,9 @@ function prompt_end { ### virtualenv prompt function prompt_virtualenv { # Exclude pyenv - if [[ -d $VIRTUAL_ENV && $PYENV_VIRTUALENV_INIT != 1 ]]; then + [[ $PYENV_VIRTUALENV_INIT == 1 ]] && _omb_util_binary_exists pyenv && return 0 + + if [[ -d $VIRTUAL_ENV ]]; then # Python could output the version information in both stdout and # stderr (e.g. if using pyenv, the output goes to stderr). local VERSION_OUTPUT=$("$VIRTUAL_ENV"/bin/python --version 2>&1) @@ -302,10 +304,8 @@ function prompt_pyenv { if [[ $PYENV_VIRTUALENV_INIT == 1 ]] && _omb_util_binary_exists pyenv; then # Priority is shell > local > global # When pyenv shell is set, the environment variable $PYENV_VERSION is set with the value we want - if [[ -n $PYENV_VERSION ]]; then - local PYENV_VERSION="$PYENV_VERSION" - # Otherwise, fall back to pyenv local/global to get the version - else + if [[ ! ${PYENV_VERSION-} ]]; then + # If not set, fall back to pyenv local/global to get the version local PYENV_VERSION=$(pyenv local 2>/dev/null || pyenv global 2>/dev/null) fi # If it is not the system's python, then display additional info