speed up the cache-miss branch of {left,right}_prompt_segment

pull/22/head
romkatv 6 years ago
parent 27a599e03e
commit c344b4ee9d

@ -94,9 +94,9 @@ fi
################################################################ ################################################################
# The `CURRENT_BG` variable is used to remember what the last BG color used was # The `CURRENT_BG` variable is used to remember what the last BG color used was
# when building the left-hand prompt. Because the RPROMPT is created from # when building a prompt segment.
# right-left but reads the opposite, this isn't necessary for the other side. CURRENT_BG=NONE
CURRENT_BG='NONE' LAST_SEGMENT_INDEX=''
# Cache the results of expensive computations to avoid avoid recomputing them on every prompt. # Cache the results of expensive computations to avoid avoid recomputing them on every prompt.
# Setting this option to 'true' can greatly speed up prompt generation. However, any changes # Setting this option to 'true' can greatly speed up prompt generation. However, any changes
@ -112,46 +112,92 @@ set_default POWERLEVEL9K_USE_CACHE false
# with data. # with data.
set_default POWERLEVEL9K_MAX_CACHE_SIZE 10000 set_default POWERLEVEL9K_MAX_CACHE_SIZE 10000
# Functions producing left and right prompts are called from subshells, so any if [[ $POWERLEVEL9K_USE_CACHE == true ]]; then
# changes to the environment variables they do get wiped out after the prompt is # Functions producing left and right prompts are called from subshells, so any
# printed. In order to cache the results of expensive computations in these functions, # changes to the environment variables they do get wiped out after the prompt is
# we use a temporary file to communicate with the parent shell and to ask it to # printed. In order to cache the results of expensive computations in these functions,
# change environment variables. # we use a temporary file to communicate with the parent shell and to ask it to
typeset -AH _p9k_cache_data=() # change environment variables.
typeset _P9K_CACHE_CHANNEL=${$(mktemp -u)%/*}/p9k_cache_channel.$$ typeset -AH _p9k_cache_data=()
typeset -H _P9K_CACHE_CHANNEL=${$(mktemp -u)%/*}/p9k_cache_channel.$$
# Store a key-value pair in the cache.
# # Note: Several performance-critical functions return results to the caller via global
# * $1: Key. # variable _P9K_RETVAL rather than stdout. This is faster.
# * $2: Value. Can be empty.
# # Store a key-value pair in the cache.
# Note that an attempt to retrieve the value right away won't succeed. All requested #
# cache update get batched and flushed together after a prompt is built. # * $1: Key.
p9k_cache_set() { # * $2: Value. Can be empty.
if [[ $POWERLEVEL9K_USE_CACHE == true ]]; then #
# Note that an attempt to retrieve the value right away won't succeed. All requested
# cache update get batched and flushed together after a prompt is built.
_p9k_cache_set() {
# Prepend dot to the value so that we can easily tell apart empty # Prepend dot to the value so that we can easily tell apart empty
# values from missing in p9k_cache_get. # values from missing in _p9k_cache_get.
echo -E "_p9k_cache_data+=(${(qq)1} .${(qq)2})" >>$_P9K_CACHE_CHANNEL echo -E "_p9k_cache_data+=(${(qq)1} .${(qq)2})" >>$_P9K_CACHE_CHANNEL
_P9K_RETVAL=$2
}
# Retrieve a value from the cache.
#
# * $1: Key.
#
# If there is value associated with the specified key, sets _P9K_RETVAL and returns 0.
# Otherwise returns 1.
_p9k_cache_get() {
local V=${_p9k_cache_data[$1]}
if [[ -n $V ]]; then
_P9K_RETVAL=${V:1}
else
return 1
fi
}
# Resolves $(print_icon $1) and stores the result in a global variable.
# Use _p9k_get_icon to retrieve the value.
_p9k_memoize_icon() {
local icon_name=$1
typeset -gH _P9K_ICON_${icon_name}="$(print_icon $icon_name)"
}
# If `_p9k_memoize_icon $1` has been called, sets _P9K_RETVAL to the memoized value.
# Otherwise sets _P9K_RETVAL to $(print_icon $icon_name).
_p9k_get_icon() {
local icon_name=$1
local var_name=_P9K_ICON_${icon_name}
if [[ -v "$var_name" ]]; then
_P9K_RETVAL=${(P)var_name}
else
_P9K_RETVAL="$(print_icon $icon_name)"
fi
}
else
# Implementations of the cache functions when caching is disabled.
_p9k_cache_set() { _P9K_RETVAL=$2 }
_p9k_cache_get() { false }
_p9k_memoize_icon() { }
_p9k_get_icon() { _P9K_RETVAL="$(print_icon $1)" }
fi
# Resolves a color to its numerical value, or an empty string. Communicates the result back
# by setting _P9K_RETVAL.
_p9k_color() {
local color=$1
local segment_name=$2
local suffix=$3
local cache_key="$0 ${(q)1} ${(q)2} ${(q)3}"
if ! _p9k_cache_get $cache_key; then
local user_var=POWERLEVEL9K_${(U)${segment_name}#prompt_}_${suffix}
_p9k_cache_set $cache_key $(getColorCode ${${(P)user_var}:-${color}})
fi fi
} }
# Retrieve a value from the cache. _p9k_background() {
# [[ -n $1 ]] && _P9K_RETVAL="%K{$1}" || _P9K_RETVAL="%k"
# * $1: Key. }
#
# If there is value associated with the specified key, sets _P9K_CACHE_VALUE and returns 0. _p9k_foreground() {
# Otherwise unsets _P9K_CACHE_VALUE and returns 1. [[ -n $1 ]] && _P9K_RETVAL="%F{$1}" || _P9K_RETVAL="%f"
#
# Note: The value is returned to the caller via a global variable rather than stdout for
# better performance.
p9k_cache_get() {
local V=${_p9k_cache_data[$1]}
if [[ -n $V ]]; then
_P9K_CACHE_VALUE=${V:1}
else
unset _P9K_CACHE_VALUE
return 1
fi
} }
# Begin a left prompt segment # Begin a left prompt segment
@ -164,112 +210,93 @@ p9k_cache_get() {
# * $5: The segment content # * $5: The segment content
# * $6: An identifying icon (must be a key of the icons array) # * $6: An identifying icon (must be a key of the icons array)
# The latter three can be omitted, # The latter three can be omitted,
set_default last_left_element_index 1
set_default POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS " " set_default POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS " "
_p9k_memoize_icon LEFT_SEGMENT_SEPARATOR
_p9k_memoize_icon LEFT_SUBSEGMENT_SEPARATOR
left_prompt_segment() { left_prompt_segment() {
local cache_key="${(q)0} ${(q)1} ${(q)2} ${(q)3} ${(q)4} ${(q)5:+1} ${(q)6} ${(q)CURRENT_BG}" local cache_key="${(q)0} ${(q)1} ${(q)2} ${(q)3} ${(q)4} ${(q)5:+1} ${(q)6} ${(q)CURRENT_BG}"
if ! p9k_cache_get $cache_key; then if ! _p9k_cache_get $cache_key; then
local output=''
local segment_name=$1 local segment_name=$1
local current_index=$2 local current_index=$2
# Check if the segment should be joined with the previous one
local joined _p9k_color $3 $segment_name BACKGROUND
segmentShouldBeJoined $current_index $last_left_element_index "$POWERLEVEL9K_LEFT_PROMPT_ELEMENTS" && joined=true || joined=false local background_color=$_P9K_RETVAL
# Colors _p9k_color $4 $segment_name FOREGROUND
local backgroundColor="${3}" local foreground_color=$_P9K_RETVAL
local foregroundColor="${4}" _p9k_foreground $foreground_color
local foreground=$_P9K_RETVAL
# Overwrite given background-color by user defined variable for this segment.
local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_BACKGROUND _p9k_background $background_color
local BG_COLOR_MODIFIER=${(P)BACKGROUND_USER_VARIABLE} local output=$_P9K_RETVAL
[[ -n $BG_COLOR_MODIFIER ]] && backgroundColor="$BG_COLOR_MODIFIER"
if [[ $CURRENT_BG == NONE ]]; then
# Overwrite given foreground-color by user defined variable for this segment. # The first segment on the line.
local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_FOREGROUND output+=$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS
local FG_COLOR_MODIFIER=${(P)FOREGROUND_USER_VARIABLE} elif ! segmentShouldBeJoined $current_index $LAST_SEGMENT_INDEX $POWERLEVEL9K_LEFT_PROMPT_ELEMENTS; then
[[ -n $FG_COLOR_MODIFIER ]] && foregroundColor="$FG_COLOR_MODIFIER" if [[ $background_color == $CURRENT_BG ]]; then
# Middle segment with same color as previous segment
# Get color codes here to save some calls later on # We take the current foreground color as color for our
backgroundColor="$(getColorCode ${backgroundColor})" # subsegment (or the default color). This should have
foregroundColor="$(getColorCode ${foregroundColor})" # enough contrast.
if [[ $foreground == "%f" ]]; then
local background foreground _p9k_foreground $DEFAULT_COLOR
[[ -n "${backgroundColor}" ]] && background="$(backgroundColor ${backgroundColor})" || background="%k" output+=$_P9K_RETVAL
[[ -n "${foregroundColor}" ]] && foreground="$(foregroundColor ${foregroundColor})" || foreground="%f" else
output+=$foreground
if [[ $CURRENT_BG != 'NONE' ]] && ! isSameColor "${backgroundColor}" "$CURRENT_BG"; then fi
output+="${background}%F{$CURRENT_BG}" _p9k_get_icon LEFT_SUBSEGMENT_SEPARATOR
if [[ $joined == false ]]; then output+="${_P9K_RETVAL}${POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS}"
# Middle segment else
output+="$(print_icon 'LEFT_SEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS" _p9k_foreground $CURRENT_BG
fi output+=$_P9K_RETVAL
elif isSameColor "$CURRENT_BG" "${backgroundColor}"; then _p9k_get_icon LEFT_SEGMENT_SEPARATOR
# Middle segment with same color as previous segment output+="${_P9K_RETVAL}${POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS}"
# We take the current foreground color as color for our
# subsegment (or the default color). This should have
# enough contrast.
local complement
[[ -n "${foregroundColor}" ]] && complement="${foreground}" || complement="$(foregroundColor $DEFAULT_COLOR)"
output+="${background}${complement}"
if [[ $joined == false ]]; then
output+="$(print_icon 'LEFT_SUBSEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi fi
else
# First segment
output+="${background}$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi fi
local visual_identifier
if [[ -n $6 ]]; then if [[ -n $6 ]]; then
visual_identifier="$(print_icon $6)" _p9k_get_icon $6
if [[ -n "$visual_identifier" ]]; then if [[ -n $_P9K_RETVAL ]]; then
local icon=$_P9K_RETVAL
_p9k_foreground $foreground_color $segment_name VISUAL_IDENTIFIER_COLOR
# Add an whitespace if we print more than just the visual identifier. # Add an whitespace if we print more than just the visual identifier.
# To avoid cutting off the visual identifier in some terminal emulators (e.g., Konsole, st), # To avoid cutting off the visual identifier in some terminal emulators (e.g., Konsole, st),
# we need to color both the visual identifier and the whitespace. # we need to color both the visual identifier and the whitespace.
[[ -n "$5" ]] && visual_identifier="$visual_identifier " output+="${_P9K_RETVAL}${icon}${5:+ }"
# Allow users to overwrite the color for the visual identifier only.
local visual_identifier_color_variable=POWERLEVEL9K_${(U)${segment_name}#prompt_}_VISUAL_IDENTIFIER_COLOR
set_default $visual_identifier_color_variable "${foregroundColor}"
visual_identifier="$(foregroundColor ${(P)visual_identifier_color_variable})${visual_identifier}"
fi fi
fi fi
# Print the visual identifier output+=$foreground
output+="${visual_identifier}" _p9k_cache_set $cache_key "${(qq)output} ${(qq)background_color}"
_P9K_CACHE_VALUE="${(qq)output} ${(qq)backgroundColor} ${(qq)foreground}"
p9k_cache_set $cache_key $_P9K_CACHE_VALUE
fi fi
# output backgroundColor foreground local tuple=("${(@Q)${(z)_P9K_RETVAL}}")
local tuple=("${(@Q)${(z)_P9K_CACHE_VALUE}}") echo -n "${tuple[1]}${5}${POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS}"
echo -n $tuple[1] LAST_SEGMENT_INDEX=$2
CURRENT_BG=$tuple[2] CURRENT_BG=$tuple[2]
[[ -n "$5" ]] && echo -n "${tuple[3]}${5}"
echo -n "${POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS}"
last_left_element_index=$2
} }
# End the left prompt, closes the final segment. # End the left prompt, closes the final segment.
_p9k_memoize_icon LEFT_SEGMENT_SEPARATOR
_p9k_memoize_icon LEFT_SEGMENT_END_SEPARATOR
left_prompt_end() { left_prompt_end() {
local cache_key="$0 ${(q)CURRENT_BG}" local cache_key="$0 ${(q)CURRENT_BG}"
if ! p9k_cache_get $cache_key; then if ! _p9k_cache_get $cache_key; then
if [[ -n $CURRENT_BG ]]; then local output="%k"
_P9K_CACHE_VALUE="%k$(foregroundColor ${CURRENT_BG})$(print_icon 'LEFT_SEGMENT_SEPARATOR')" if [[ $CURRENT_BG != NONE ]]; then
else _p9k_foreground $CURRENT_BG
_P9K_CACHE_VALUE="%k" output+=$_P9K_RETVAL
_p9k_get_icon LEFT_SEGMENT_SEPARATOR
output+="${_P9K_RETVAL}"
fi fi
_P9K_CACHE_VALUE+="%f$(print_icon 'LEFT_SEGMENT_END_SEPARATOR')" _p9k_get_icon LEFT_SEGMENT_END_SEPARATOR
p9k_cache_set $cache_key $_P9K_CACHE_VALUE output+="%f${_P9K_RETVAL}"
_p9k_cache_set $cache_key $output
fi fi
echo -n $_P9K_CACHE_VALUE echo -n $_P9K_RETVAL
CURRENT_BG=''
} }
CURRENT_RIGHT_BG='NONE'
# Begin a right prompt segment # Begin a right prompt segment
# Takes four arguments: # Takes four arguments:
# * $1: Name of the function that was originally invoked (mandatory). # * $1: Name of the function that was originally invoked (mandatory).
@ -280,99 +307,78 @@ CURRENT_RIGHT_BG='NONE'
# * $5: The segment content # * $5: The segment content
# * $6: An identifying icon (must be a key of the icons array) # * $6: An identifying icon (must be a key of the icons array)
# No ending for the right prompt segment is needed (unlike the left prompt, above). # No ending for the right prompt segment is needed (unlike the left prompt, above).
set_default last_right_element_index 1
set_default POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS " " set_default POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS " "
_p9k_memoize_icon RIGHT_SUBSEGMENT_SEPARATOR
_p9k_memoize_icon RIGHT_SEGMENT_SEPARATOR
right_prompt_segment() { right_prompt_segment() {
local cache_key="${(q)0} ${(q)1} ${(q)2} ${(q)3} ${(q)4} ${(q)5:+1} ${(q)6} ${(q)CURRENT_RIGHT_BG}" local cache_key="${(q)0} ${(q)1} ${(q)2} ${(q)3} ${(q)4} ${(q)6} ${(q)CURRENT_BG}"
if ! p9k_cache_get $cache_key; then if ! _p9k_cache_get $cache_key; then
local output='' local segment_name=$1
local segment_name="${1}"
local current_index=$2 local current_index=$2
# Check if the segment should be joined with the previous one _p9k_color $3 $segment_name BACKGROUND
local joined local background_color=$_P9K_RETVAL
segmentShouldBeJoined $current_index $last_right_element_index "$POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS" && joined=true || joined=false _p9k_background $background_color
local background=$_P9K_RETVAL
# Colors
local backgroundColor="${3}"
local foregroundColor="${4}"
# Overwrite given background-color by user defined variable for this segment.
local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_BACKGROUND
local BG_COLOR_MODIFIER=${(P)BACKGROUND_USER_VARIABLE}
[[ -n $BG_COLOR_MODIFIER ]] && backgroundColor="$BG_COLOR_MODIFIER"
# Overwrite given foreground-color by user defined variable for this segment.
local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_FOREGROUND
local FG_COLOR_MODIFIER=${(P)FOREGROUND_USER_VARIABLE}
[[ -n $FG_COLOR_MODIFIER ]] && foregroundColor="$FG_COLOR_MODIFIER"
# Get color codes here to save some calls later on _p9k_color $4 $segment_name FOREGROUND
backgroundColor="$(getColorCode ${backgroundColor})" local foreground_color=$_P9K_RETVAL
foregroundColor="$(getColorCode ${foregroundColor})" _p9k_foreground $foreground_color
local foreground=$_P9K_RETVAL
local background foreground local output=''
[[ -n "${backgroundColor}" ]] && background="$(backgroundColor ${backgroundColor})" || background="%k"
[[ -n "${foregroundColor}" ]] && foreground="$(foregroundColor ${foregroundColor})" || foreground="%f"
# If CURRENT_RIGHT_BG is "NONE", we are the first right segment.
if [[ "$CURRENT_RIGHT_BG" != "NONE" ]]; then
# This is the closing whitespace for the previous segment
output+="${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}%f"
fi
if [[ $joined == false ]] || [[ "$CURRENT_RIGHT_BG" == "NONE" ]]; then if [[ $CURRENT_BG == NONE ]] ||
if isSameColor "$CURRENT_RIGHT_BG" "${backgroundColor}"; then ! segmentShouldBeJoined $current_index $LAST_SEGMENT_INDEX $POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS; then
if [[ $background_color == $CURRENT_BG ]]; then
# Middle segment with same color as previous segment # Middle segment with same color as previous segment
# We take the current foreground color as color for our # We take the current foreground color as color for our
# subsegment (or the default color). This should have # subsegment (or the default color). This should have
# enough contrast. # enough contrast.
local complement if [[ $foreground == "%f" ]]; then
[[ -n "${foregroundColor}" ]] && complement="${foreground}" || complement="$(foregroundColor $DEFAULT_COLOR)" _p9k_foreground $DEFAULT_COLOR
output+="$complement$(print_icon 'RIGHT_SUBSEGMENT_SEPARATOR')%f" output+=$_P9K_RETVAL
else
output+=$foreground
fi
_p9k_get_icon RIGHT_SUBSEGMENT_SEPARATOR
output+=$_P9K_RETVAL
else else
# Use the new Background Color as the foreground of the segment separator _p9k_foreground $background_color
output+="$(foregroundColor ${backgroundColor})$(print_icon 'RIGHT_SEGMENT_SEPARATOR')%f" output+=$_P9K_RETVAL
_p9k_get_icon RIGHT_SEGMENT_SEPARATOR
output+=$_P9K_RETVAL
fi fi
output+="${background}${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}"
else
output+=$background
fi fi
local visual_identifier output+=$foreground
if [[ -n "$6" ]]; then
visual_identifier="$(print_icon $6)" local icon=''
if [[ -n "$visual_identifier" ]]; then if [[ -n $6 ]]; then
_p9k_get_icon $6
if [[ -n $_P9K_RETVAL ]]; then
local icon=$_P9K_RETVAL
_p9k_foreground $foreground_color $segment_name VISUAL_IDENTIFIER_COLOR
# Add an whitespace if we print more than just the visual identifier. # Add an whitespace if we print more than just the visual identifier.
# To avoid cutting off the visual identifier in some terminal emulators (e.g., Konsole, st), # To avoid cutting off the visual identifier in some terminal emulators (e.g., Konsole, st),
# we need to color both the visual identifier and the whitespace. # we need to color both the visual identifier and the whitespace.
[[ -n "$5" ]] && visual_identifier=" $visual_identifier" icon="${_P9K_RETVAL}${icon} "
# Allow users to overwrite the color for the visual identifier only.
local visual_identifier_color_variable=POWERLEVEL9K_${(U)${segment_name}#prompt_}_VISUAL_IDENTIFIER_COLOR
set_default $visual_identifier_color_variable "${foregroundColor}"
visual_identifier="$(foregroundColor ${(P)visual_identifier_color_variable})${visual_identifier}"
fi fi
fi fi
output+="${background}${foreground}" _p9k_cache_set $cache_key "${(qq)output} ${(qq)background_color} ${(qq)icon}"
# Print whitespace only if segment is not joined or first right segment
[[ $joined == false ]] || [[ "$CURRENT_RIGHT_BG" == "NONE" ]] && output+="${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}"
_P9K_CACHE_VALUE="${(qq)output} ${(qq)backgroundColor} ${(qq)visual_identifier}"
p9k_cache_set $cache_key $_P9K_CACHE_VALUE
fi fi
# output backgroundColor visual_identifier local tuple=("${(@Q)${(z)_P9K_RETVAL}}")
local tuple=("${(@Q)${(z)_P9K_CACHE_VALUE}}") local prompt="${tuple[1]}${5}${5:+ }${tuple[3]}"
echo -n $tuple[1] echo -n $prompt
CURRENT_RIGHT_BG=$tuple[2]
# Print segment content if there is any
[[ -n "$5" ]] && echo -n "${5}"
# Print the visual identifier
echo -n "${tuple[3]}"
last_right_element_index=$2 CURRENT_BG=$tuple[2]
LAST_SEGMENT_INDEX=$2
} }
################################################################ ################################################################
@ -417,6 +423,7 @@ prompt_aws_eb_env() {
# Segment to indicate background jobs with an icon. # Segment to indicate background jobs with an icon.
set_default POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE true set_default POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE true
set_default POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS false set_default POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS false
_p9k_memoize_icon BACKGROUND_JOBS_ICON
prompt_background_jobs() { prompt_background_jobs() {
local job_lines=("${(@f)$(jobs -lr)}") local job_lines=("${(@f)$(jobs -lr)}")
[[ ${(c)#job_lines} == 0 ]] && return [[ ${(c)#job_lines} == 0 ]] && return
@ -424,16 +431,16 @@ prompt_background_jobs() {
local num_jobs=$#job_lines local num_jobs=$#job_lines
local cache_key="$0 $num_jobs" local cache_key="$0 $num_jobs"
if ! p9k_cache_get $cache_key; then if ! _p9k_cache_get $cache_key; then
if [[ "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE" == "true" ]] && ([[ "$num_jobs" -gt 1 ]] || [[ "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS" == "true" ]]); then if [[ "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE" == "true" &&
_P9K_CACHE_VALUE=$num_jobs ("$num_jobs" -gt 1 || "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS" == "true") ]]; then
_p9k_cache_set $cache_key $num_jobs
else else
_P9K_CACHE_VALUE='' _p9k_cache_set $cache_key ''
fi fi
p9k_cache_set $cache_key $_P9K_CACHE_VALUE
fi fi
"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "cyan" "$_P9K_CACHE_VALUE" 'BACKGROUND_JOBS_ICON' "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "cyan" "$_P9K_RETVAL" 'BACKGROUND_JOBS_ICON'
} }
################################################################ ################################################################
@ -451,7 +458,7 @@ prompt_newline() {
"$0" \ "$0" \
"$2" \ "$2" \
"" "" "${newline}" "" "" "${newline}"
CURRENT_BG='NONE' CURRENT_BG=NONE
POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS=$lws POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS=$lws
} }
@ -816,6 +823,7 @@ prompt_custom() {
################################################################ ################################################################
# Display the duration the command needed to run. # Display the duration the command needed to run.
_p9k_memoize_icon EXECUTION_TIME_ICON
prompt_command_execution_time() { prompt_command_execution_time() {
set_default POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD 3 set_default POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD 3
set_default POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION 2 set_default POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION 2
@ -881,12 +889,17 @@ function getUniqueFolder() {
set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/"
set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~"
set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false
_p9k_memoize_icon FOLDER_ICON
_p9k_memoize_icon HOME_ICON
_p9k_memoize_icon HOME_SUB_ICON
_p9k_memoize_icon LOCK_ICON
_p9k_memoize_icon ETC_ICON
prompt_dir() { prompt_dir() {
# using $PWD instead of "$(print -P '%~')" to allow use of POWERLEVEL9K_DIR_PATH_ABSOLUTE # using $PWD instead of "$(print -P '%~')" to allow use of POWERLEVEL9K_DIR_PATH_ABSOLUTE
local current_path=$PWD # WAS: local current_path="$(print -P '%~')" local current_path=$PWD # WAS: local current_path="$(print -P '%~')"
local cache_key="$0 $2 ${(q)current_path}" local cache_key="$0 $2 ${(q)current_path}"
[[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]] && cache_key+=" W"; [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]] && cache_key+=" W";
if ! p9k_cache_get $cache_key; then if ! _p9k_cache_get $cache_key; then
# check if the user wants to use absolute paths or "~" paths # check if the user wants to use absolute paths or "~" paths
[[ ${(L)POWERLEVEL9K_DIR_PATH_ABSOLUTE} != "true" ]] && current_path=${current_path/#$HOME/"~"} [[ ${(L)POWERLEVEL9K_DIR_PATH_ABSOLUTE} != "true" ]] && current_path=${current_path/#$HOME/"~"}
# declare all local variables # declare all local variables
@ -1127,11 +1140,10 @@ prompt_dir() {
current_path=${current_path:s/~/$POWERLEVEL9K_HOME_FOLDER_ABBREVIATION} current_path=${current_path:s/~/$POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}
fi fi
_P9K_CACHE_VALUE="$0_$current_state ${(qq)2} blue ${(qq)DEFAULT_COLOR} ${(qq)current_path} ${(qq)dir_states[$current_state]}" _p9k_cache_set $cache_key "$0_$current_state ${(qq)2} blue ${(qq)DEFAULT_COLOR} ${(qq)current_path} ${(qq)dir_states[$current_state]}"
p9k_cache_set $cache_key $_P9K_CACHE_VALUE
fi fi
"$1_prompt_segment" "${(@Q)${(z)_P9K_CACHE_VALUE}}" "$1_prompt_segment" "${(@Q)${(z)_P9K_RETVAL}}"
} }
################################################################ ################################################################
@ -1424,6 +1436,7 @@ prompt_chruby() {
################################################################ ################################################################
# Segment to print an icon if user is root. # Segment to print an icon if user is root.
_p9k_memoize_icon ROOT_ICON
prompt_root_indicator() { prompt_root_indicator() {
if [[ "$UID" -eq 0 ]]; then if [[ "$UID" -eq 0 ]]; then
"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "yellow" "" 'ROOT_ICON' "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "yellow" "" 'ROOT_ICON'
@ -1501,9 +1514,12 @@ exit_code_or_status() {
fi fi
} }
_p9k_memoize_icon OK_ICON
_p9k_memoize_icon FAIL_ICON
_p9k_memoize_icon CARRIAGE_RETURN_ICON
prompt_status() { prompt_status() {
local cache_key="$0 $2 $RETVAL $RETVALS" local cache_key="$0 $2 $RETVAL $RETVALS"
if ! p9k_cache_get $cache_key; then if ! _p9k_cache_get $cache_key; then
local ec_text local ec_text
local ec_sum local ec_sum
local ec local ec
@ -1530,18 +1546,18 @@ prompt_status() {
if (( ec_sum > 0 )); then if (( ec_sum > 0 )); then
if [[ "$POWERLEVEL9K_STATUS_CROSS" == false && "$POWERLEVEL9K_STATUS_VERBOSE" == true ]]; then if [[ "$POWERLEVEL9K_STATUS_CROSS" == false && "$POWERLEVEL9K_STATUS_VERBOSE" == true ]]; then
_P9K_CACHE_VALUE="$0_ERROR ${(qq)2} red yellow1 ${(qq)ec_text} CARRIAGE_RETURN_ICON" _P9K_RETVAL="$0_ERROR ${(qq)2} red yellow1 ${(qq)ec_text} CARRIAGE_RETURN_ICON"
else else
_P9K_CACHE_VALUE="$0_ERROR ${(qq)2} ${(qq)DEFAULT_COLOR} red '' FAIL_ICON" _P9K_RETVAL="$0_ERROR ${(qq)2} ${(qq)DEFAULT_COLOR} red '' FAIL_ICON"
fi fi
elif [[ "$POWERLEVEL9K_STATUS_OK" == true ]] && [[ "$POWERLEVEL9K_STATUS_VERBOSE" == true || "$POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE" == true ]]; then elif [[ "$POWERLEVEL9K_STATUS_OK" == true ]] && [[ "$POWERLEVEL9K_STATUS_VERBOSE" == true || "$POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE" == true ]]; then
_P9K_CACHE_VALUE="$0_OK ${(qq)2} ${(qq)DEFAULT_COLOR} green '' OK_ICON" _P9K_RETVAL="$0_OK ${(qq)2} ${(qq)DEFAULT_COLOR} green '' OK_ICON"
fi fi
if (( $#RETVALS < 3 )); then if (( $#RETVALS < 3 )); then
p9k_cache_set $cache_key $_P9K_CACHE_VALUE _p9k_cache_set $cache_key $_P9K_RETVAL
fi fi
fi fi
"$1_prompt_segment" "${(@Q)${(z)_P9K_CACHE_VALUE}}" "$1_prompt_segment" "${(@Q)${(z)_P9K_RETVAL}}"
} }
################################################################ ################################################################
@ -1611,6 +1627,7 @@ build_test_stats() {
################################################################ ################################################################
# System time # System time
_p9k_memoize_icon TIME_ICON
prompt_time() { prompt_time() {
set_default POWERLEVEL9K_TIME_FORMAT "%D{%H:%M:%S}" set_default POWERLEVEL9K_TIME_FORMAT "%D{%H:%M:%S}"
@ -1743,25 +1760,22 @@ powerlevel9k_vcs_init() {
# POWERLEVEL9K_VCS_STATUS_COMMAND="$HOME/bin/gitstatus --dirty-max-index-size=4096" # POWERLEVEL9K_VCS_STATUS_COMMAND="$HOME/bin/gitstatus --dirty-max-index-size=4096"
set_default POWERLEVEL9K_VCS_STATUS_COMMAND "" set_default POWERLEVEL9K_VCS_STATUS_COMMAND ""
if [[ $POWERLEVEL9K_USE_CACHE == true ]]; then
_P9K_VCS_GIT_GITHUB_ICON=$(print_icon VCS_GIT_GITHUB_ICON)
_P9K_VCS_GIT_BITBUCKET_ICON=$(print_icon VCS_GIT_BITBUCKET_ICON)
_P9K_VCS_GIT_GITLAB_ICON=$(print_icon VCS_GIT_GITLAB_ICON)
_P9K_VCS_GIT_ICON=$(print_icon VCS_GIT_ICON)
_P9K_VCS_BRANCH_ICON=$(print_icon VCS_BRANCH_ICON)
_P9K_VCS_REMOTE_BRANCH_ICON=$(print_icon VCS_REMOTE_BRANCH_ICON)
_P9K_VCS_STAGED_ICON=$(print_icon VCS_STAGED_ICON)
_P9K_VCS_UNSTAGED_ICON=$(print_icon VCS_UNSTAGED_ICON)
_P9K_VCS_UNTRACKED_ICON=$(print_icon VCS_UNTRACKED_ICON)
_P9K_VCS_OUTGOING_CHANGES_ICON=$(print_icon VCS_OUTGOING_CHANGES_ICON)
_P9K_VCS_INCOMING_CHANGES_ICON=$(print_icon VCS_INCOMING_CHANGES_ICON)
_P9K_VCS_STASH_ICON=$(print_icon VCS_STASH_ICON)
fi
################################################################ ################################################################
# Segment to show VCS information # Segment to show VCS information
_p9k_memoize_icon VCS_GIT_GITHUB_ICON
_p9k_memoize_icon VCS_GIT_BITBUCKET_ICON
_p9k_memoize_icon VCS_GIT_GITLAB_ICON
_p9k_memoize_icon VCS_GIT_ICON
_p9k_memoize_icon VCS_BRANCH_ICON
_p9k_memoize_icon VCS_REMOTE_BRANCH_ICON
_p9k_memoize_icon VCS_STAGED_ICON
_p9k_memoize_icon VCS_UNSTAGED_ICON
_p9k_memoize_icon VCS_UNTRACKED_ICON
_p9k_memoize_icon VCS_OUTGOING_CHANGES_ICON
_p9k_memoize_icon VCS_INCOMING_CHANGES_ICON
_p9k_memoize_icon VCS_STASH_ICON
prompt_vcs() { prompt_vcs() {
if [[ -z $POWERLEVEL9K_VCS_STATUS_COMMAND || $POWERLEVEL9K_USE_CACHE != true ]]; then if [[ -z $POWERLEVEL9K_VCS_STATUS_COMMAND ]]; then
VCS_WORKDIR_DIRTY=false VCS_WORKDIR_DIRTY=false
VCS_WORKDIR_HALF_DIRTY=false VCS_WORKDIR_HALF_DIRTY=false
local current_state="" local current_state=""
@ -1788,7 +1802,7 @@ prompt_vcs() {
local props local props
props=("${(@fQ)$("${(@Q)${(z)POWERLEVEL9K_VCS_STATUS_COMMAND}}" 2>/dev/null)}") || return props=("${(@fQ)$("${(@Q)${(z)POWERLEVEL9K_VCS_STATUS_COMMAND}}" 2>/dev/null)}") || return
local cache_key="$0 ${(@q)props}" local cache_key="$0 ${(@q)props}"
if ! p9k_cache_get $cache_key; then if ! _p9k_cache_get $cache_key; then
local local_branch=$props[1] local local_branch=$props[1]
local remote_branch=$props[2] local remote_branch=$props[2]
local remote_url=$props[3] local remote_url=$props[3]
@ -1811,34 +1825,42 @@ prompt_vcs() {
local vcs_prompt local vcs_prompt
if [[ "$remote_url" =~ "github" ]] then if [[ "$remote_url" =~ "github" ]] then
vcs_prompt+=$_P9K_VCS_GIT_GITHUB_ICON _p9k_get_icon VCS_GIT_GITHUB_ICON
vcs_prompt+=$_P9K_RETVAL
elif [[ "$remote_url" =~ "bitbucket" ]] then elif [[ "$remote_url" =~ "bitbucket" ]] then
vcs_prompt+=$_P9K_VCS_GIT_BITBUCKET_ICON _p9k_get_icon VCS_GIT_BITBUCKET_ICON
vcs_prompt+=$_P9K_RETVAL
elif [[ "$remote_url" =~ "stash" ]] then elif [[ "$remote_url" =~ "stash" ]] then
vcs_prompt+=$_P9K_VCS_GIT_GITHUB_ICON _p9k_get_icon VCS_GIT_GITHUB_ICON
vcs_prompt+=$_P9K_RETVAL
elif [[ "$remote_url" =~ "gitlab" ]] then elif [[ "$remote_url" =~ "gitlab" ]] then
vcs_prompt+=$_P9K_VCS_GIT_GITLAB_ICON _p9k_get_icon VCS_GIT_GITLAB_ICON
vcs_prompt+=$_P9K_RETVAL
else else
vcs_prompt+=$_P9K_VCS_GIT_ICON _p9k_get_icon VCS_GIT_ICON
vcs_prompt+=$_P9K_RETVAL
fi fi
vcs_prompt+="$_P9K_VCS_BRANCH_ICON$local_branch" _p9k_get_icon VCS_BRANCH_ICON
vcs_prompt+="$_P9K_RETVAL$local_branch"
if [[ -n $action ]]; then if [[ -n $action ]]; then
vcs_prompt+=" %F{${POWERLEVEL9K_VCS_ACTIONFORMAT_FOREGROUND}}| $action%f" vcs_prompt+=" %F{${POWERLEVEL9K_VCS_ACTIONFORMAT_FOREGROUND}}| $action%f"
else else
[[ -n $remote_branch && $local_branch != $remote_branch ]] && vcs_prompt+=" $_P9K_VCS_REMOTE_BRANCH_ICON$remote_branch" if [[ -n $remote_branch && $local_branch != $remote_branch ]]; then
[[ $has_staged == 1 ]] && vcs_prompt+=" $_P9K_VCS_STAGED_ICON" _p9k_get_icon VCS_REMOTE_BRANCH_ICON
[[ $has_unstaged == 1 ]] && vcs_prompt+=" $_P9K_VCS_UNSTAGED_ICON" vcs_prompt+=" $_P9K_RETVAL$remote_branch"
[[ $has_untracked == 1 ]] && vcs_prompt+=" $_P9K_VCS_UNTRACKED_ICON" fi
[[ $ahead -gt 0 ]] && vcs_prompt+=" $_P9K_VCS_OUTGOING_CHANGES_ICON$ahead" [[ $has_staged == 1 ]] && _p9k_get_icon VCS_STAGED_ICON && vcs_prompt+=" $_P9K_RETVAL"
[[ $behind -gt 0 ]] && vcs_prompt+=" $_P9K_VCS_INCOMING_CHANGES_ICON$behind" [[ $has_unstaged == 1 ]] && _p9k_get_icon VCS_UNSTAGED_ICON && vcs_prompt+=" $_P9K_RETVAL"
[[ $stashes -gt 0 ]] && vcs_prompt+=" $_P9K_VCS_STASH_ICON$stashes" [[ $has_untracked == 1 ]] && _p9k_get_icon VCS_UNTRACKED_ICON && vcs_prompt+=" $_P9K_RETVAL"
[[ $ahead -gt 0 ]] && _p9k_get_icon VCS_OUTGOING_CHANGES_ICON && vcs_prompt+=" $_P9K_RETVAL$ahead"
[[ $behind -gt 0 ]] && _p9k_get_icon VCS_INCOMING_CHANGES_ICON && vcs_prompt+=" $_P9K_RETVAL$behind"
[[ $stashes -gt 0 ]] && _p9k_get_icon VCS_STASH_ICON && vcs_prompt+=" $_P9K_RETVAL$stashes"
fi fi
_P9K_CACHE_VALUE="${0}_${(U)current_state} $2 ${(qq)vcs_states[$current_state]} ${(qq)DEFAULT_COLOR} ${(qq)vcs_prompt}" _p9k_cache_set $cache_key "${0}_${(U)current_state} $2 ${(qq)vcs_states[$current_state]} ${(qq)DEFAULT_COLOR} ${(qq)vcs_prompt}"
p9k_cache_set $cache_key $_P9K_CACHE_VALUE
fi fi
"$1_prompt_segment" "${(@Q)${(z)_P9K_CACHE_VALUE}}" "$1_prompt_segment" "${(@Q)${(z)_P9K_RETVAL}}"
fi fi
} }
@ -1915,6 +1937,7 @@ prompt_swift_version() {
################################################################ ################################################################
# dir_writable: Display information about the user's permission to write in the current directory # dir_writable: Display information about the user's permission to write in the current directory
_p9k_memoize_icon LOCK_ICON
prompt_dir_writable() { prompt_dir_writable() {
if [[ ! -w "$PWD" ]]; then if [[ ! -w "$PWD" ]]; then
"$1_prompt_segment" "$0_FORBIDDEN" "$2" "red" "yellow1" "" 'LOCK_ICON' "$1_prompt_segment" "$0_FORBIDDEN" "$2" "red" "yellow1" "" 'LOCK_ICON'
@ -2057,7 +2080,7 @@ powerlevel9k_prepare_prompts() {
# Reset start time # Reset start time
_P9K_TIMER_START=0x7FFFFFFF _P9K_TIMER_START=0x7FFFFFFF
if [[ -s $_P9K_CACHE_CHANNEL ]]; then if [[ $POWERLEVEL9K_USE_CACHE == true && -s $_P9K_CACHE_CHANNEL ]]; then
eval $(<$_P9K_CACHE_CHANNEL) eval $(<$_P9K_CACHE_CHANNEL)
rm -f $_P9K_CACHE_CHANNEL rm -f $_P9K_CACHE_CHANNEL
if [[ -n $POWERLEVEL9K_MAX_CACHE_SIZE && $#_p9k_cache_data -gt $POWERLEVEL9K_MAX_CACHE_SIZE ]]; then if [[ -n $POWERLEVEL9K_MAX_CACHE_SIZE && $#_p9k_cache_data -gt $POWERLEVEL9K_MAX_CACHE_SIZE ]]; then

Loading…
Cancel
Save