Skip to content

Commit

Permalink
themes/agnoster : Detect and show pyenv information (#458)
Browse files Browse the repository at this point in the history
 - 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

Co-authored-by: Koichi Murase <myoga.murase@gmail.com>
  • Loading branch information
Epithumia and akinomyoga authored Jul 26, 2023
1 parent a75f284 commit 9c9c92b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions themes/agnoster/agnoster.theme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ function prompt_end {

### virtualenv prompt
function prompt_virtualenv {
# Exclude pyenv
[[ $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).
Expand All @@ -296,6 +299,29 @@ function prompt_virtualenv {
fi
}

### pyenv prompt
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 [[ ! ${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
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
Expand Down Expand Up @@ -534,6 +560,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
Expand Down

0 comments on commit 9c9c92b

Please sign in to comment.