1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-12-18 05:40:07 +00:00

Changed code to comply with www.shellcheck.net.

This commit is contained in:
Dominik Ritter 2015-09-12 21:59:56 +02:00
parent fd191a5894
commit d133680704

View file

@ -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