diff options
Diffstat (limited to 'powerlevel9k.zsh-theme')
-rwxr-xr-x | powerlevel9k.zsh-theme | 118 |
1 files changed, 69 insertions, 49 deletions
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c4b74c9c..2a875f34 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 @@ -471,29 +472,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 @@ -935,7 +928,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 } @@ -1045,13 +1054,23 @@ powerlevel9k_init() { # Display a warning if the terminal does not support 256 colors local term_colors term_colors=$(echotc Co) - if (( term_colors < 256 )); then + if (( $term_colors < 256 )); then print -P "%F{red}WARNING!%f Your terminal appears to support less than 256 colors!" print -P "If your terminal supports 256 colors, please export the appropriate environment variable" print -P "_before_ loading this theme in your \~\/.zshrc. In most terminal emulators, putting" print -P "%F{blue}export TERM=\"xterm-256color\"%f at the top of your \~\/.zshrc is sufficient." fi + # If the terminal `LANG` is set to `C`, this theme will not work at all. + local term_lang + term_lang=$(echo $LANG) + if [[ $term_lang == 'C' ]]; then + print -P "\t%F{red}WARNING!%f Your terminal's 'LANG' is set to 'C', which breaks this theme!" + print -P "\t%F{red}WARNING!%f Please set your 'LANG' to a UTF-8 language, like 'en_US.UTF-8'" + print -P "\t%F{red}WARNING!%f _before_ loading this theme in your \~\.zshrc. Putting" + print -P "\t%F{red}WARNING!%f %F{blue}export LANG=\"en_US.UTF-8\"%f at the top of your \~\/.zshrc is sufficient." + fi + # Display a warning if deprecated segments are in use. typeset -AH deprecated_segments # old => new @@ -1081,3 +1100,4 @@ powerlevel9k_init() { } powerlevel9k_init "$@" + |