aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorromkatv <roman.perepelitsa@gmail.com>2019-05-22 12:55:42 +0300
committerromkatv <roman.perepelitsa@gmail.com>2019-05-22 12:55:42 +0300
commit4a3b62cde90bdfdfe7ce5eeb7ccce54d1facf922 (patch)
tree26fc83d0bf6c6e72c17090a2c58fd58d9904f3ec
parent743b7c658406c92b6d2bf4767478be5e93dd71ca (diff)
speed up pyenv prompt
-rwxr-xr-xpowerlevel9k.zsh-theme46
1 files changed, 34 insertions, 12 deletions
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme
index 6f9d4e86..fec9e05e 100755
--- a/powerlevel9k.zsh-theme
+++ b/powerlevel9k.zsh-theme
@@ -1982,24 +1982,46 @@ prompt_virtualenv() {
fi
}
+function _p9k_read_pyenv_version_file() {
+ [[ -r $1 ]] || return
+ local content
+ read -rd $'\0' content <$1 2>/dev/null
+ _P9K_RETVAL=${${(j.:.)${(@)${=content}#python-}:-system}}
+}
+
+function _p9k_pyenv_global_version() {
+ _p9k_read_pyenv_version_file ${PYENV_ROOT:-$HOME/.pyenv}/version || _P9K_RETVAL=system
+}
+
################################################################
# Segment to display pyenv information
# https://github.com/pyenv/pyenv#choosing-the-python-version
set_default POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW false
prompt_pyenv() {
- if [[ -n "$PYENV_VERSION" ]]; then
- "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" 'PYTHON_ICON' 0 '' "${PYENV_VERSION//\%/%%}"
- elif [ $commands[pyenv] ]; then
- local pyenv_version_name="$(pyenv version-name)"
- local pyenv_global="system"
- local pyenv_root="$(pyenv root)"
- if [[ -f "${pyenv_root}/version" ]]; then
- pyenv_global="$(pyenv version-file-read ${pyenv_root}/version)"
- fi
- if [[ "${pyenv_version_name}" != "${pyenv_global}" || "${POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW}" == "true" ]]; then
- "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" 'PYTHON_ICON' 0 '' "${pyenv_version_name//\%/%%}"
- fi
+ local v=${(j.:.)${(@)${(s.:.)PYENV_VERSION}#python-}}
+ if [[ -z $v ]]; then
+ [[ $PYENV_DIR == /* ]] && local dir=$PYENV_DIR || local dir="$PWD/$PYENV_DIR"
+ while true; do
+ if _p9k_read_pyenv_version_file $dir/.python-version; then
+ v=$_P9K_RETVAL
+ break
+ fi
+ if [[ $dir == / ]]; then
+ [[ $POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW == true ]] || return
+ _p9k_pyenv_global_version
+ v=$_P9K_RETVAL
+ break
+ fi
+ dir=${dir:h}
+ done
+ fi
+
+ if [[ $POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW == false ]]; then
+ _p9k_pyenv_global_version
+ [[ $v == $_P9K_RETVAL ]] && return
fi
+
+ "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" 'PYTHON_ICON' 0 '' "${v//\%/%%}"
}
################################################################