From d1336807049fcb8ca00196c290a77a9468dfef8c Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 12 Sep 2015 21:59:56 +0200 Subject: [PATCH 1/8] Changed code to comply with www.shellcheck.net. --- powerlevel9k.zsh-theme | 133 +++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 64 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 50911e27..57c204dc 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -51,9 +51,9 @@ function print_icon() { local ICON_USER_VARIABLE=POWERLEVEL9K_${icon_name} local USER_ICON=${(P)ICON_USER_VARIABLE} if defined "$ICON_USER_VARIABLE"; then - echo -n $USER_ICON + echo -n "$USER_ICON" else - echo -n ${icons[$icon_name]} + echo -n "${icons[$icon_name]}" fi } @@ -65,20 +65,20 @@ printSizeHumanReadable() { # if the base is not Bytes if [[ -n $2 ]]; then - for idx in ${extension}; do + for idx in "${extension[@]}"; do if [[ "$2" == "$idx" ]]; then break fi - index=$(( $index + 1 )) + index=$(( index + 1 )) done fi - while (( ($size / 1024) > 0 )); do - size=$(( $size / 1024 )) - index=$(( $index + 1 )) + while (( (size / 1024) > 0 )); do + size=$(( size / 1024 )) + index=$(( index + 1 )) done - echo $size${extension[$index]} + echo "$size${extension[$index]}" } # Gets the first value out of a list of items that is not empty. @@ -97,16 +97,16 @@ function getRelevantItem() { for item in $list; do # The first non-empty item wins - try=$(eval $callback) + try=$(eval "$callback") if [[ -n "$try" ]]; then - echo $try + echo "$try" break; fi done } get_icon_names() { - for key in "${(@k)icons}"; do + for key in ${(@k)icons}; do echo "POWERLEVEL9K_$key: ${icons[$key]}" done } @@ -264,7 +264,8 @@ esac # `sed` is unfortunately not consistent across OSes when it comes to flags. SED_EXTENDED_REGEX_PARAMETER="-r" if [[ "$OS" == 'OSX' ]]; then - local IS_BSD_SED=$(sed --version &>> /dev/null || echo "BSD sed") + local IS_BSD_SED + IS_BSD_SED=$(sed --version &>> /dev/null || echo "BSD sed") if [[ -n "$IS_BSD_SED" ]]; then SED_EXTENDED_REGEX_PARAMETER="-E" fi @@ -361,12 +362,12 @@ left_prompt_segment() { # Overwrite given background-color by user defined variable for this segment. local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_BACKGROUND local BG_COLOR_MODIFIER=${(P)BACKGROUND_USER_VARIABLE} - [[ -n $BG_COLOR_MODIFIER ]] && 2=$BG_COLOR_MODIFIER + [[ -n $BG_COLOR_MODIFIER ]] && 2="$BG_COLOR_MODIFIER" # Overwrite given foreground-color by user defined variable for this segment. local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_FOREGROUND local FG_COLOR_MODIFIER=${(P)FOREGROUND_USER_VARIABLE} - [[ -n $FG_COLOR_MODIFIER ]] && 3=$FG_COLOR_MODIFIER + [[ -n $FG_COLOR_MODIFIER ]] && 3="$FG_COLOR_MODIFIER" local bg fg [[ -n $2 ]] && bg="%K{$2}" || bg="%k" @@ -405,12 +406,12 @@ right_prompt_segment() { # Overwrite given background-color by user defined variable for this segment. local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_BACKGROUND local BG_COLOR_MODIFIER=${(P)BACKGROUND_USER_VARIABLE} - [[ -n $BG_COLOR_MODIFIER ]] && 2=$BG_COLOR_MODIFIER + [[ -n $BG_COLOR_MODIFIER ]] && 2="$BG_COLOR_MODIFIER" # Overwrite given foreground-color by user defined variable for this segment. local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_FOREGROUND local FG_COLOR_MODIFIER=${(P)FOREGROUND_USER_VARIABLE} - [[ -n $FG_COLOR_MODIFIER ]] && 3=$FG_COLOR_MODIFIER + [[ -n $FG_COLOR_MODIFIER ]] && 3="$FG_COLOR_MODIFIER" local bg fg [[ -n $2 ]] && bg="%K{$2}" || bg="%k" @@ -427,9 +428,9 @@ prompt_vcs() { if [[ -n "$vcs_prompt" ]]; then if [[ "$VCS_WORKDIR_DIRTY" == true ]]; then - $1_prompt_segment "$0_MODIFIED" "yellow" "$DEFAULT_COLOR" + "$1_prompt_segment" "$0_MODIFIED" "yellow" "$DEFAULT_COLOR" else - $1_prompt_segment "$0" "green" "$DEFAULT_COLOR" + "$1_prompt_segment" "$0" "green" "$DEFAULT_COLOR" fi echo -n "$vcs_prompt " @@ -447,17 +448,17 @@ function +vi-git-aheadbehind() { local ahead behind branch_name local -a gitstatus - branch_name=${$(git symbolic-ref --short HEAD 2>/dev/null)} + branch_name=$(git symbolic-ref --short HEAD 2>/dev/null) # for git prior to 1.7 # ahead=$(git rev-list origin/${branch_name}..HEAD | wc -l) ahead=$(git rev-list ${branch_name}@{upstream}..HEAD 2>/dev/null | wc -l) - (( $ahead )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_OUTGOING_CHANGES_ICON')${ahead// /}%f" ) + (( ahead )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_OUTGOING_CHANGES_ICON')${ahead// /}%f" ) # for git prior to 1.7 # behind=$(git rev-list HEAD..origin/${branch_name} | wc -l) behind=$(git rev-list HEAD..${branch_name}@{upstream} 2>/dev/null | wc -l) - (( $behind )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_INCOMING_CHANGES_ICON')${behind// /}%f" ) + (( behind )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_INCOMING_CHANGES_ICON')${behind// /}%f" ) hook_com[misc]+=${(j::)gitstatus} } @@ -467,7 +468,7 @@ function +vi-git-remotebranch() { # Are we on a remote-tracking branch? remote=${$(git rev-parse --verify HEAD@{upstream} --symbolic-full-name 2>/dev/null)/refs\/(remotes|heads)\/} - branch_name=${$(git symbolic-ref --short HEAD 2>/dev/null)} + branch_name=$(git symbolic-ref --short HEAD 2>/dev/null) hook_com[branch]="%F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_BRANCH_ICON')${hook_com[branch]}%f" # Always show the remote @@ -529,7 +530,7 @@ prompt_aws() { local aws_profile="$AWS_DEFAULT_PROFILE" if [[ -n "$aws_profile" ]]; then - $1_prompt_segment "$0" red white "$(print_icon 'AWS_ICON') $aws_profile" + "$1_prompt_segment" "$0" red white "$(print_icon 'AWS_ICON') $aws_profile" fi } @@ -539,9 +540,9 @@ prompt_context() { if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then if [[ $(print -P "%#") == '#' ]]; then # Shell runs as root - $1_prompt_segment "$0_ROOT" "$DEFAULT_COLOR" "yellow" "$USER@%m" + "$1_prompt_segment" "$0_ROOT" "$DEFAULT_COLOR" "yellow" "$USER@%m" else - $1_prompt_segment "$0_DEFAULT" "$DEFAULT_COLOR" "011" "$USER@%m" + "$1_prompt_segment" "$0_DEFAULT" "$DEFAULT_COLOR" "011" "$USER@%m" fi fi } @@ -565,21 +566,21 @@ prompt_dir() { fi - $1_prompt_segment "$0" "blue" "$DEFAULT_COLOR" "$(print_icon 'HOME_ICON')$current_path" + "$1_prompt_segment" "$0" "blue" "$DEFAULT_COLOR" "$(print_icon 'HOME_ICON')$current_path" } # Command number (in local history) prompt_history() { - $1_prompt_segment "$0" "244" "$DEFAULT_COLOR" '%h' + "$1_prompt_segment" "$0" "244" "$DEFAULT_COLOR" '%h' } prompt_icons_test() { - for key in "${(@k)icons}"; do + for key in ${(@k)icons}; do # The lower color spectrum in ZSH makes big steps. Choosing # the next color has enough contrast to read. local random_color=$((RANDOM % 8)) local next_color=$((random_color+1)) - $1_prompt_segment "$0" "$random_color" "$next_color" "$key: ${icons[$key]}" + "$1_prompt_segment" "$0" "$random_color" "$next_color" "$key: ${icons[$key]}" done } @@ -587,30 +588,30 @@ prompt_ip() { if [[ "$OS" == "OSX" ]]; then if defined POWERLEVEL9K_IP_INTERFACE; then # Get the IP address of the specified interface. - ip=$(ipconfig getifaddr $POWERLEVEL9K_IP_INTERFACE) + ip=$(ipconfig getifaddr "$POWERLEVEL9K_IP_INTERFACE") else local interfaces callback # Get network interface names ordered by service precedence. interfaces=$(networksetup -listnetworkserviceorder | grep -o "Device:\s*[a-z0-9]*" | grep -o -E '[a-z0-9]*$') callback='ipconfig getifaddr $item' - ip=$(getRelevantItem $interfaces $callback) + ip=$(getRelevantItem "$interfaces" "$callback") fi else if defined POWERLEVEL9K_IP_INTERFACE; then # Get the IP address of the specified interface. - ip=$(ip -4 a show $POWERLEVEL9K_IP_INTERFACE | grep -o "inet\s*[0-9.]*" | grep -o "[0-9.]*") + ip=$(ip -4 a show "$POWERLEVEL9K_IP_INTERFACE" | grep -o "inet\s*[0-9.]*" | grep -o "[0-9.]*") else local interfaces callback # Get all network interface names that are up interfaces=$(ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o "[a-z0-9]*") callback='ip -4 a show $item | grep -o "inet\s*[0-9.]*" | grep -o "[0-9.]*"' - ip=$(getRelevantItem $interfaces $callback) + ip=$(getRelevantItem "$interfaces" "$callback") fi fi - $1_prompt_segment "$0" "cyan" "$DEFAULT_COLOR" "$(print_icon 'NETWORK_ICON') $ip" + "$1_prompt_segment" "$0" "cyan" "$DEFAULT_COLOR" "$(print_icon 'NETWORK_ICON') $ip" } set_default POWERLEVEL9K_LOAD_SHOW_FREE_RAM true @@ -620,7 +621,7 @@ prompt_load() { if [[ "$POWERLEVEL9K_LOAD_SHOW_FREE_RAM" == true ]]; then ramfree=$(vm_stat | grep "Pages free" | grep -o -E '[0-9]+') # Convert pages into Bytes - ramfree=$(( $ramfree * 4096 )) + ramfree=$(( ramfree * 4096 )) base='' fi else @@ -645,10 +646,10 @@ prompt_load() { FUNCTION_SUFFIX="_NORMAL" fi - $1_prompt_segment "$0$FUNCTION_SUFFIX" $BACKGROUND_COLOR "$DEFAULT_COLOR" "$(print_icon 'LOAD_ICON') $load_avg_5min" + "$1_prompt_segment" "$0$FUNCTION_SUFFIX" "$BACKGROUND_COLOR" "$DEFAULT_COLOR" "$(print_icon 'LOAD_ICON') $load_avg_5min" if [[ "$POWERLEVEL9K_LOAD_SHOW_FREE_RAM" == true ]]; then - echo -n "$(print_icon 'RAM_ICON') $(printSizeHumanReadable $ramfree $base) " + echo -n "$(print_icon 'RAM_ICON') $(printSizeHumanReadable "$ramfree" $base) " fi } @@ -663,27 +664,28 @@ prompt_longstatus() { symbols+="%F{226}%? ↵" bg="009" else - symbols+="%{%F{"046"}%}$(print_icon 'OK_ICON')" + symbols+="%{%F{046}%}$(print_icon 'OK_ICON')" bg="008" fi [[ "$UID" -eq 0 ]] && symbols+="%{%F{yellow}%} $(print_icon 'ROOT_ICON')" [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$(print_icon 'BACKGROUND_JOBS_ICON')" - [[ -n "$symbols" ]] && $1_prompt_segment "$0" "$bg" "$DEFAULT_COLOR" "$symbols" + [[ -n "$symbols" ]] && "$1_prompt_segment" "$0" "$bg" "$DEFAULT_COLOR" "$symbols" } # Node version prompt_node_version() { - local nvm_prompt=$(node -v 2>/dev/null) + local nvm_prompt + nvm_prompt=$(node -v 2>/dev/null) [[ -z "${nvm_prompt}" ]] && return - $1_prompt_segment "$0" "green" "white" "${nvm_prompt:1} $(print_icon 'NODE_ICON')" + "$1_prompt_segment" "$0" "green" "white" "${nvm_prompt:1} $(print_icon 'NODE_ICON')" } # print a little OS icon prompt_os_icon() { - $1_prompt_segment "$0" "008" "255" "$OS_ICON" + "$1_prompt_segment" "$0" "008" "255" "$OS_ICON" } # print PHP version number @@ -692,33 +694,34 @@ prompt_php_version() { php_version=$(php -v 2>&1 | grep -oe "^PHP\s*[0-9.]*") if [[ -n "$php_version" ]]; then - $1_prompt_segment "$0" "013" "255" "$php_version" + "$1_prompt_segment" "$0" "013" "255" "$php_version" fi } # rbenv information prompt_rbenv() { if [[ -n "$RBENV_VERSION" ]]; then - $1_prompt_segment "$0" "red" "$DEFAULT_COLOR" "$RBENV_VERSION" + "$1_prompt_segment" "$0" "red" "$DEFAULT_COLOR" "$RBENV_VERSION" fi } # RSpec test ratio prompt_rspec_stats() { if [[ (-d app && -d spec) ]]; then - local code_amount=$(ls -1 app/**/*.rb | wc -l) - local tests_amount=$(ls -1 spec/**/*.rb | wc -l) + local code_amount tests_amount + code_amount=$(ls -1 app/**/*.rb | wc -l) + tests_amount=$(ls -1 spec/**/*.rb | wc -l) - build_test_stats "$1" $0 "$code_amount" $tests_amount "RSpec $(print_icon 'TEST_ICON')" + build_test_stats "$1" "$0" "$code_amount" "$tests_amount" "RSpec $(print_icon 'TEST_ICON')" fi } # Ruby Version Manager information prompt_rvm() { local rvm_prompt - rvm_prompt=`rvm-prompt` + rvm_prompt=$(rvm-prompt) if [ "$rvm_prompt" != "" ]; then - $1_prompt_segment "$0" "240" "$DEFAULT_COLOR" "$rvm_prompt $(print_icon 'RUBY_ICON') " + "$1_prompt_segment" "$0" "240" "$DEFAULT_COLOR" "$rvm_prompt $(print_icon 'RUBY_ICON') " fi } @@ -731,14 +734,15 @@ prompt_status() { [[ "$UID" -eq 0 ]] && symbols+="%{%F{yellow}%} $(print_icon 'ROOT_ICON')" [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$(print_icon 'BACKGROUND_JOBS_ICON')" - [[ -n "$symbols" ]] && $1_prompt_segment "$0" "$DEFAULT_COLOR" "default" "$symbols" + [[ -n "$symbols" ]] && "$1_prompt_segment" "$0" "$DEFAULT_COLOR" "default" "$symbols" } # Symfony2-PHPUnit test ratio prompt_symfony2_tests() { if [[ (-d src && -d app && -f app/AppKernel.php) ]]; then - local code_amount=$(ls -1 src/**/*.php | grep -v Tests | wc -l) - local tests_amount=$(ls -1 src/**/*.php | grep Tests | wc -l) + local code_amount tests_amount + code_amount=$(ls -1 src/**/*.php | grep -vc Tests) + tests_amount=$(ls -1 src/**/*.php | grep -c Tests) build_test_stats "$1" "$0" "$code_amount" "$tests_amount" "SF2 $(print_icon 'TEST_ICON')" fi @@ -747,8 +751,9 @@ prompt_symfony2_tests() { # Symfony2-Version prompt_symfony2_version() { if [[ -f app/bootstrap.php.cache ]]; then - local symfony2_version=$(grep " VERSION " app/bootstrap.php.cache | sed -e 's/[^.0-9]*//g') - $1_prompt_segment "$0" "240" "$DEFAULT_COLOR" "$(print_icon 'SYMFONY_ICON') $symfony2_version" + local symfony2_version + symfony2_version=$(grep " VERSION " app/bootstrap.php.cache | sed -e 's/[^.0-9]*//g') + "$1_prompt_segment" "$0" "240" "$DEFAULT_COLOR" "$(print_icon 'SYMFONY_ICON') $symfony2_version" fi } @@ -762,9 +767,9 @@ build_test_stats() { typeset -F 2 ratio local ratio=$(( (tests_amount/code_amount) * 100 )) - [[ ratio -ge 0.75 ]] && $1_prompt_segment "${2}_GOOD" "cyan" "$DEFAULT_COLOR" "$headline: $ratio%%" - [[ ratio -ge 0.5 && ratio -lt 0.75 ]] && $1_prompt_segment "$2_AVG" "yellow" "$DEFAULT_COLOR" "$headline: $ratio%%" - [[ ratio -lt 0.5 ]] && $1_prompt_segment "$2_BAD" "red" "$DEFAULT_COLOR" "$headline: $ratio%%" + (( ratio >= 75 )) && "$1_prompt_segment" "${2}_GOOD" "cyan" "$DEFAULT_COLOR" "$headline: $ratio%%" + (( ratio >= 50 && ratio < 75 )) && "$1_prompt_segment" "$2_AVG" "yellow" "$DEFAULT_COLOR" "$headline: $ratio%%" + (( ratio < 50 )) && "$1_prompt_segment" "$2_BAD" "red" "$DEFAULT_COLOR" "$headline: $ratio%%" } # System time @@ -774,7 +779,7 @@ prompt_time() { time_format="$POWERLEVEL9K_TIME_FORMAT" fi - $1_prompt_segment "$0" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$time_format" + "$1_prompt_segment" "$0" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$time_format" } # Virtualenv: current working virtualenv @@ -783,7 +788,7 @@ prompt_time() { prompt_virtualenv() { local virtualenv_path="$VIRTUAL_ENV" if [[ -n "$virtualenv_path" && -n "$VIRTUAL_ENV_DISABLE_PROMPT" ]]; then - $1_prompt_segment "$0" "blue" "$DEFAULT_COLOR" "(`basename $virtualenv_path`)" + "$1_prompt_segment" "$0" "blue" "$DEFAULT_COLOR" "($(basename "$virtualenv_path"))" fi } @@ -797,8 +802,8 @@ build_left_prompt() { defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs) - for element in $POWERLEVEL9K_LEFT_PROMPT_ELEMENTS; do - prompt_$element "left" + for element in "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[@]}"; do + "prompt_$element" "left" done left_prompt_end @@ -810,8 +815,8 @@ build_right_prompt() { defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(longstatus history time) - for element in $POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS; do - prompt_$element "right" + for element in "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]}"; do + "prompt_$element" "right" done } @@ -819,7 +824,7 @@ powerlevel9k_init() { # Display a warning if the terminal does not support 256 colors local term_colors term_colors=$(tput colors) - if (( $term_colors < 256 )); then + if (( term_colors < 256 )); then print -P "%F{red}WARNING!%f Your terminal supports less than 256 colors!" print "You should set TERM=xterm-256colors in your ~/.zshrc" fi From c8e41ad4ee3644731f8985874ed34407d0865528 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 12 Sep 2015 23:46:46 +0200 Subject: [PATCH 2/8] Simplified the way the build-prompt-functions are called. Before it was quite obscure how and when ZSH called the right functions. It was a matter of a complex quote syntax. Now, by adding a new precmd-hook, the use of the quotes in the PROMPT and RPROMPT-variables is much simpler. --- powerlevel9k.zsh-theme | 52 ++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 57c204dc..55a6e14c 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -661,7 +661,7 @@ prompt_longstatus() { symbols=() if [[ "$RETVAL" -ne 0 ]]; then - symbols+="%F{226}%? ↵" + symbols+="%F{226}$RETVAL ↵" bg="009" else symbols+="%{%F{046}%}$(print_icon 'OK_ICON')" @@ -798,8 +798,6 @@ prompt_virtualenv() { # Main prompt build_left_prompt() { - RETVAL=$? - defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs) for element in "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[@]}"; do @@ -811,8 +809,6 @@ build_left_prompt() { # Right prompt build_right_prompt() { - RETVAL=$? - defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(longstatus history time) for element in "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]}"; do @@ -820,6 +816,30 @@ build_right_prompt() { done } +powerlevel9k_prepare_prompts() { + RETVAL=$? + + if [[ "$POWERLEVEL9K_PROMPT_ON_NEWLINE" == true ]]; then + PROMPT="$(print_icon 'MULTILINE_FIRST_PROMPT_PREFIX')%{%f%b%k%}$(build_left_prompt) +$(print_icon 'MULTILINE_SECOND_PROMPT_PREFIX')" + # The right prompt should be on the same line as the first line of the left + # prompt. To do so, there is just a quite ugly workaround: Before zsh draws + # the RPROMPT, we advise it, to go one line up. At the end of RPROMPT, we + # advise it to go one line down. See: + # http://superuser.com/questions/357107/zsh-right-justify-in-ps1 + RPROMPT_PREFIX='%{'$'\e[1A''%}' # one line up + RPROMPT_SUFFIX='%{'$'\e[1B''%}' # one line down + else + PROMPT="%{%f%b%k%}$(build_left_prompt)" + RPROMPT_PREFIX='' + RPROMPT_SUFFIX='' + fi + + if [[ "$POWERLEVEL9K_DISABLE_RPROMPT" != true ]]; then + RPROMPT="$RPROMPT_PREFIX%{%f%b%k%}$(build_right_prompt)%{$reset_color%}$RPROMPT_SUFFIX" + fi +} + powerlevel9k_init() { # Display a warning if the terminal does not support 256 colors local term_colors @@ -838,28 +858,10 @@ powerlevel9k_init() { # initialize VCS autoload -Uz add-zsh-hook - add-zsh-hook precmd vcs_info - if [[ "$POWERLEVEL9K_PROMPT_ON_NEWLINE" == true ]]; then - PROMPT="$(print_icon 'MULTILINE_FIRST_PROMPT_PREFIX')%{%f%b%k%}"'$(build_left_prompt)'" -$(print_icon 'MULTILINE_SECOND_PROMPT_PREFIX')" - # The right prompt should be on the same line as the first line of the left - # prompt. To do so, there is just a quite ugly workaround: Before zsh draws - # the RPROMPT, we advise it, to go one line up. At the end of RPROMPT, we - # advise it to go one line down. See: - # http://superuser.com/questions/357107/zsh-right-justify-in-ps1 - RPROMPT_PREFIX='%{'$'\e[1A''%}' # one line up - RPROMPT_SUFFIX='%{'$'\e[1B''%}' # one line down - else - PROMPT="%{%f%b%k%}"'$(build_left_prompt)' - RPROMPT_PREFIX='' - RPROMPT_SUFFIX='' - fi - - if [[ "$POWERLEVEL9K_DISABLE_RPROMPT" != true ]]; then - RPROMPT=$RPROMPT_PREFIX"%{%f%b%k%}"'$(build_right_prompt)'"%{$reset_color%}"$RPROMPT_SUFFIX - fi + # prepare prompts + add-zsh-hook precmd powerlevel9k_prepare_prompts } powerlevel9k_init "$@" From 22c29b1253ae25c5808af0225d56a3b7a379009d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 12 Sep 2015 23:52:20 +0200 Subject: [PATCH 3/8] Moved the icon definitions to the top, for better structure. --- powerlevel9k.zsh-theme | 182 ++++++++++++++++++++--------------------- 1 file changed, 91 insertions(+), 91 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 55a6e14c..bb8bd7de 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -20,97 +20,6 @@ #zstyle ':vcs_info:*+*:*' debug true #set -o xtrace -################################################################ -# Utility functions -################################################################ - -# Exits with 0 if a variable has been previously defined (even if empty) -# Takes the name of a variable that should be checked. -function defined() { - local varname="$1" - - typeset -p "$varname" > /dev/null 2>&1 -} - -# Given the name of a variable and a default value, sets the variable -# value to the default only if it has not been defined. -# -# Typeset cannot set the value for an array, so this will only work -# for scalar values. -function set_default() { - local varname="$1" - local default_value="$2" - - defined "$varname" || typeset -g "$varname"="$default_value" -} - -# Safety function for printing icons -# Prints the named icon, or if that icon is undefined, the string name. -function print_icon() { - local icon_name=$1 - local ICON_USER_VARIABLE=POWERLEVEL9K_${icon_name} - local USER_ICON=${(P)ICON_USER_VARIABLE} - if defined "$ICON_USER_VARIABLE"; then - echo -n "$USER_ICON" - else - echo -n "${icons[$icon_name]}" - fi -} - -printSizeHumanReadable() { - local size=$1 - local extension - extension=(B K M G T P E Z Y) - local index=1 - - # if the base is not Bytes - if [[ -n $2 ]]; then - for idx in "${extension[@]}"; do - if [[ "$2" == "$idx" ]]; then - break - fi - index=$(( index + 1 )) - done - fi - - while (( (size / 1024) > 0 )); do - size=$(( size / 1024 )) - index=$(( index + 1 )) - done - - echo "$size${extension[$index]}" -} - -# Gets the first value out of a list of items that is not empty. -# The items are examined by a callback-function. -# Takes two arguments: -# * $list - A list of items -# * $callback - A callback function to examine if the item is -# worthy. The callback function has access to -# the inner variable $item. -function getRelevantItem() { - setopt shwordsplit # We need to split the words in $interfaces - - local list callback - list=$1 - callback=$2 - - for item in $list; do - # The first non-empty item wins - try=$(eval "$callback") - if [[ -n "$try" ]]; then - echo "$try" - break; - fi - done -} - -get_icon_names() { - for key in ${(@k)icons}; do - echo "POWERLEVEL9K_$key: ${icons[$key]}" - done -} - ################################################################ # Icons ################################################################ @@ -227,6 +136,97 @@ if [[ "$POWERLEVEL9K_HIDE_BRANCH_ICON" == true ]]; then icons[VCS_BRANCH_ICON]='' fi +################################################################ +# Utility functions +################################################################ + +# Exits with 0 if a variable has been previously defined (even if empty) +# Takes the name of a variable that should be checked. +function defined() { + local varname="$1" + + typeset -p "$varname" > /dev/null 2>&1 +} + +# Given the name of a variable and a default value, sets the variable +# value to the default only if it has not been defined. +# +# Typeset cannot set the value for an array, so this will only work +# for scalar values. +function set_default() { + local varname="$1" + local default_value="$2" + + defined "$varname" || typeset -g "$varname"="$default_value" +} + +# Safety function for printing icons +# Prints the named icon, or if that icon is undefined, the string name. +function print_icon() { + local icon_name=$1 + local ICON_USER_VARIABLE=POWERLEVEL9K_${icon_name} + local USER_ICON=${(P)ICON_USER_VARIABLE} + if defined "$ICON_USER_VARIABLE"; then + echo -n "$USER_ICON" + else + echo -n "${icons[$icon_name]}" + fi +} + +printSizeHumanReadable() { + local size=$1 + local extension + extension=(B K M G T P E Z Y) + local index=1 + + # if the base is not Bytes + if [[ -n $2 ]]; then + for idx in "${extension[@]}"; do + if [[ "$2" == "$idx" ]]; then + break + fi + index=$(( index + 1 )) + done + fi + + while (( (size / 1024) > 0 )); do + size=$(( size / 1024 )) + index=$(( index + 1 )) + done + + echo "$size${extension[$index]}" +} + +# Gets the first value out of a list of items that is not empty. +# The items are examined by a callback-function. +# Takes two arguments: +# * $list - A list of items +# * $callback - A callback function to examine if the item is +# worthy. The callback function has access to +# the inner variable $item. +function getRelevantItem() { + setopt shwordsplit # We need to split the words in $interfaces + + local list callback + list=$1 + callback=$2 + + for item in $list; do + # The first non-empty item wins + try=$(eval "$callback") + if [[ -n "$try" ]]; then + echo "$try" + break; + fi + done +} + +get_icon_names() { + for key in ${(@k)icons}; do + echo "POWERLEVEL9K_$key: ${icons[$key]}" + done +} + # OS detection for the `os_icon` segment case $(uname) in Darwin) From 1c91430aa49c09a7a25bdd68c78cb3446bc593a7 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 12 Sep 2015 23:58:56 +0200 Subject: [PATCH 4/8] Explicit setopt --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index bb8bd7de..2f4d7e7d 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -851,7 +851,7 @@ powerlevel9k_init() { setopt LOCAL_OPTIONS unsetopt XTRACE KSH_ARRAYS - prompt_opts=(cr percent subst) + setopt cr percent subst # initialize colors autoload -U colors && colors From dfe4c109bdb4a6895dc7d58383c22bbe6313b074 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 13 Sep 2015 00:01:06 +0200 Subject: [PATCH 5/8] Double quote to prevent globbing and word splitting in git-aheadbehind. --- powerlevel9k.zsh-theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 2f4d7e7d..76c3c3c0 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -452,12 +452,12 @@ function +vi-git-aheadbehind() { # for git prior to 1.7 # ahead=$(git rev-list origin/${branch_name}..HEAD | wc -l) - ahead=$(git rev-list ${branch_name}@{upstream}..HEAD 2>/dev/null | wc -l) + ahead=$(git rev-list "${branch_name}"@{upstream}..HEAD 2>/dev/null | wc -l) (( ahead )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_OUTGOING_CHANGES_ICON')${ahead// /}%f" ) # for git prior to 1.7 # behind=$(git rev-list HEAD..origin/${branch_name} | wc -l) - behind=$(git rev-list HEAD..${branch_name}@{upstream} 2>/dev/null | wc -l) + behind=$(git rev-list HEAD.."${branch_name}"@{upstream} 2>/dev/null | wc -l) (( behind )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_INCOMING_CHANGES_ICON')${behind// /}%f" ) hook_com[misc]+=${(j::)gitstatus} From 08c3649c6a1a4f575fc0829654c217a4eec98b8b Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 13 Sep 2015 00:15:43 +0200 Subject: [PATCH 6/8] Obey shellcheck.net: Quote the rhs of != in [[ ]] to prevent glob matching --- powerlevel9k.zsh-theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 76c3c3c0..16df093d 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -372,7 +372,7 @@ left_prompt_segment() { local bg fg [[ -n $2 ]] && bg="%K{$2}" || bg="%k" [[ -n $3 ]] && fg="%F{$3}" || fg="%f" - if [[ $CURRENT_BG != 'NONE' && $2 != $CURRENT_BG ]]; then + if [[ $CURRENT_BG != 'NONE' ]] && [[ "$2" != "$CURRENT_BG" ]]; then # Middle segment echo -n "%{$bg%F{$CURRENT_BG}%}$(print_icon 'LEFT_SEGMENT_SEPARATOR')%{$fg%} " else @@ -474,7 +474,7 @@ function +vi-git-remotebranch() { # Always show the remote #if [[ -n ${remote} ]] ; then # Only show the remote if it differs from the local - if [[ -n ${remote} && ${remote#*/} != ${branch_name} ]] ; then + if [[ -n ${remote} ]] && [[ "${remote#*/}" != "${branch_name}" ]] ; then hook_com[branch]+="%F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_REMOTE_BRANCH_ICON')%f%F{$POWERLEVEL9K_VCS_FOREGROUND}${remote// /}%f" fi } From 689b6d8f8cb0a527eb826c1e5fa99d1119776b02 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 16 Sep 2015 00:46:46 +0200 Subject: [PATCH 7/8] Shellcheck.net: Quote variables. --- powerlevel9k.zsh-theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 6be7874a..ea2e2ca5 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -381,7 +381,7 @@ left_prompt_segment() { if [[ $CURRENT_BG != 'NONE' ]] && [[ "$2" != "$CURRENT_BG" ]]; then # Middle segment echo -n "%{$bg%F{$CURRENT_BG}%}$(print_icon 'LEFT_SEGMENT_SEPARATOR')%{$fg%} " - elif [[ "$CURRENT_BG" == $2 ]]; then + elif [[ "$CURRENT_BG" == "$2" ]]; then # Middle segment with same color as previous segment # We take the current foreground color as color for our # subsegment (or the default color). This should have @@ -433,7 +433,7 @@ right_prompt_segment() { [[ -n $2 ]] && bg="%K{$2}" || bg="%k" [[ -n $3 ]] && fg="%F{$3}" || fg="%f" - if [[ "$CURRENT_RIGHT_BG" == $2 ]]; then + if [[ "$CURRENT_RIGHT_BG" == "$2" ]]; then # Middle segment with same color as previous segment # We take the current foreground color as color for our # subsegment (or the default color). This should have From 87fab65609d2c695cb46e89034edb8af62c1d7bb Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 16 Sep 2015 00:52:16 +0200 Subject: [PATCH 8/8] Corrected ZSH option names. --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index ea2e2ca5..a60fd12d 100644 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -881,7 +881,7 @@ powerlevel9k_init() { setopt LOCAL_OPTIONS unsetopt XTRACE KSH_ARRAYS - setopt cr percent subst + setopt PROMPT_CR PROMPT_PERCENT PROMPT_SUBST MULTIBYTE # initialize colors autoload -U colors && colors