diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | functions/icons.zsh | 9 | ||||
-rwxr-xr-x | powerlevel9k.zsh-theme | 106 |
3 files changed, 65 insertions, 51 deletions
@@ -116,6 +116,7 @@ The segments that are currently available are: * **Python Segments:** * `virtualenv` - Your Python [VirtualEnv](https://virtualenv.pypa.io/en/latest/). * [`anaconda`](#anaconda) - Your active [Anaconda](https://www.continuum.io/why-anaconda) environment. + * `pyenv` - Your active python version as reported by the first word of [`pyenv version`](https://github.com/yyuu/pyenv). Note that the segment is not displayed if that word is _system_ i.e. the segment is inactive if you are using system python. * **Ruby Segments:** * [`chruby`](#chruby) - Ruby environment information using `chruby` (if one is active). * [`rbenv`](#rbenv) - Ruby environment information using `rbenv` (if one is active). diff --git a/functions/icons.zsh b/functions/icons.zsh index 6de09ee8..c2565f26 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -69,7 +69,8 @@ case $POWERLEVEL9K_MODE in VCS_REMOTE_BRANCH_ICON ' '$'\UE804 ' # VCS_GIT_ICON $'\UE20E ' # VCS_HG_ICON $'\UE1C3 ' # - RUST_ICON '' + RUST_ICON '' + PYTHON_ICON $'\U1F40D' # 🐍 ) ;; 'awesome-fontconfig') @@ -121,7 +122,8 @@ case $POWERLEVEL9K_MODE in VCS_REMOTE_BRANCH_ICON ' '$'\UF204 ' # VCS_GIT_ICON $'\UF113 ' # VCS_HG_ICON $'\UF0C3 ' # - RUST_ICON $'\UE6A8' # + RUST_ICON $'\UE6A8' # + PYTHON_ICON $'\U1F40D' # 🐍 ) ;; *) @@ -173,7 +175,8 @@ case $POWERLEVEL9K_MODE in VCS_REMOTE_BRANCH_ICON $'\u2192' # → VCS_GIT_ICON '' VCS_HG_ICON '' - RUST_ICON '' + RUST_ICON '' + PYTHON_ICON '' ) ;; esac diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index d5c2c8bb..8fa623a1 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -20,43 +20,44 @@ #zstyle ':vcs_info:*+*:*' debug true #set -o xtrace -# Check if the theme was called as a function (e.g., from prezto) -if [[ $(whence -w prompt_powerlevel9k_setup) =~ "function" ]]; then +# Try to set the installation path +if [[ -n "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then + # If an installation path was set manually, + # it should trump any other location found. + # Do nothing. This is all right, as we use the + # POWERLEVEL9K_INSTALLATION_PATH for further processing. +elif [[ $(whence -w prompt_powerlevel9k_setup) =~ "function" ]]; then + # Check if the theme was called as a function (e.g., from prezto) autoload -U is-at-least if is-at-least 5.0.8; then # Try to find the correct path of the script. - 0=$(whence -v $0 | sed "s/$0 is a shell function from //") + POWERLEVEL9K_INSTALLATION_PATH=$(whence -v $0 | sed "s/$0 is a shell function from //") elif [[ -f "${ZDOTDIR:-$HOME}/.zprezto/modules/prompt/init.zsh" ]]; then # If there is an prezto installation, we assume that powerlevel9k is linked there. - 0="${ZDOTDIR:-$HOME}/.zprezto/modules/prompt/functions/prompt_powerlevel9k_setup" - else - # Fallback: specify an installation path! - if [[ -z "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - print -P "%F{red}We could not locate the installation path of powerlevel9k.%f" - print -P "Please specify by setting %F{blue}POWERLEVEL9K_INSTALLATION_PATH%f (full path incl. file name) at the very beginning of your ~/.zshrc" - return 1 - elif [[ -L "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # Symlink - 0="$POWERLEVEL9K_INSTALLATION_PATH" - elif [[ -f "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # File - 0="$POWERLEVEL9K_INSTALLATION_PATH" - elif [[ -d "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # Directory - 0="${POWERLEVEL9K_INSTALLATION_PATH}/powerlevel9k.zsh-theme" - fi + POWERLEVEL9K_INSTALLATION_PATH="${ZDOTDIR:-$HOME}/.zprezto/modules/prompt/functions/prompt_powerlevel9k_setup" fi +else + # Last resort: Set installation path is script path + POWERLEVEL9K_INSTALLATION_PATH="$0" fi -# If this theme is sourced as a symlink, we need to locate the true URL -if [[ -L $0 ]]; then - # Script is a symlink - filename="$(realpath -P $0 2>/dev/null || readlink -f $0 2>/dev/null || perl -MCwd=abs_path -le 'print abs_path readlink(shift);' $0 2>/dev/null)" -elif [[ -f $0 ]]; then +# Resolve the instllation path +if [[ -L "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then + # If this theme is sourced as a symlink, we need to locate the real URL + filename="$(realpath -P $POWERLEVEL9K_INSTALLATION_PATH 2>/dev/null || readlink -f $POWERLEVEL9K_INSTALLATION_PATH 2>/dev/null || perl -MCwd=abs_path -le 'print abs_path readlink(shift);' $POWERLEVEL9K_INSTALLATION_PATH 2>/dev/null)" +elif [[ -d "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then + # Directory + filename="${POWERLEVEL9K_INSTALLATION_PATH}/powerlevel9k.zsh-theme" +elif [[ -f "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then # Script is a file - filename="$0" + filename="$POWERLEVEL9K_INSTALLATION_PATH" +elif [[ -z "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then + # Fallback: specify an installation path! + print -P "%F{red}We could not locate the installation path of powerlevel9k.%f" + print -P "Please specify by setting %F{blue}POWERLEVEL9K_INSTALLATION_PATH%f (full path incl. file name) at the very beginning of your ~/.zshrc" + return 1 else - print -P "%F{red}Script location could not be found!%f" + print -P "%F{red}Script location could not be found! Maybe your %F{blue}POWERLEVEL9K_INSTALLATION_PATH%F{red} is not correct?%f" return 1 fi script_location="$(dirname $filename)" @@ -359,7 +360,7 @@ prompt_battery() { local time_remaining=$(echo $raw_data | grep TimeRemaining | awk '{ print $5 }') if [[ -n $time_remaining ]]; then # this value is set to a very high number when the system is calculating - [[ $time_remaining -gt 10000 ]] && local tstring="..." || local tstring=${(f)$(date -u -r $(($time_remaining * 60)) +%k:%M)} + [[ $time_remaining -gt 10000 ]] && local tstring="..." || local tstring=${(f)$(/bin/date -u -r $(($time_remaining * 60)) +%k:%M)} fi # Get charge values @@ -469,29 +470,21 @@ prompt_dir() { local name repo_path package_path current_dir zero # Get the path of the Git repo, which should have the package.json file - if repo_path=$(git rev-parse --git-dir 2>/dev/null); then - if [[ "$repo_path" == ".git" ]]; then - # If the current path is the root of the project, then the package path is - # the current directory and we don't want to append anything to represent - # the path to a subdirectory - package_path="." - subdirectory_path="" - else - # If the current path is something else, get the path to the package.json - # file by finding the repo path and removing the '.git` from the path - package_path=${repo_path:0:-4} - zero='%([BSUbfksu]|([FB]|){*})' - current_dir=$(pwd) - # Then, find the length of the package_path string, and save the - # subdirectory path as a substring of the current directory's path from 0 - # to the length of the package path's string - subdirectory_path=$(truncatePathFromRight "/${current_dir:${#${(S%%)package_path//$~zero/}}}") - fi + if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == "true" ]]; then + package_path=$(git rev-parse --show-toplevel) + elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then + package_path=${$(pwd)%%/.git*} fi + zero='%([BSUbfksu]|([FB]|){*})' + current_dir=$(pwd) + # Then, find the length of the package_path string, and save the + # subdirectory path as a substring of the current directory's path from 0 + # to the length of the package path's string + subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}") # Parse the 'name' from the package.json; if there are any problems, just # print the file path - if name=$( cat "$package_path/package.json" 2> /dev/null | grep "\"name\""); then + if name=$( cat "$package_path/package.json" 2> /dev/null | grep -m 1 "\"name\""); then name=$(echo $name | awk -F ':' '{print $2}' | awk -F '"' '{print $2}') # Instead of printing out the full path, print out the name of the package @@ -933,7 +926,23 @@ prompt_vi_mode() { prompt_virtualenv() { local virtualenv_path="$VIRTUAL_ENV" if [[ -n "$virtualenv_path" && "$VIRTUAL_ENV_DISABLE_PROMPT" != true ]]; then - "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" "($(basename "$virtualenv_path"))" + "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" "$(basename "$virtualenv_path")" 'PYTHON_ICON' + fi +} + +# pyenv: current active python version (with restrictions) +# More information on pyenv (Python version manager like rbenv and rvm): +# https://github.com/yyuu/pyenv +# the prompt parses output of pyenv version and only displays the first word +prompt_pyenv() { + local pyenv_version="$(pyenv version 2>/dev/null)" + pyenv_version="${pyenv_version%% *}" + # XXX: The following should return the same as above. + # This reads better for devs familiar with sed/awk/grep/cut utilities + # Using shell expansion/substitution may hamper future maintainability + #local pyenv_version="$(pyenv version 2>/dev/null | head -n1 | cut -d' ' -f1)" + if [[ -n "$pyenv_version" && "$pyenv_version" != "system" ]]; then + "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" "$pyenv_version" 'PYTHON_ICON' fi } @@ -1079,3 +1088,4 @@ powerlevel9k_init() { } powerlevel9k_init "$@" + |