add caching to speed up prompt generation

pull/22/head
romkatv 6 years ago
parent dcd7718c62
commit 968e1a618c

@ -98,6 +98,54 @@ fi
# right-left but reads the opposite, this isn't necessary for the other side.
CURRENT_BG='NONE'
# 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
# to the configuration will require re-sourcing powerlevel9k. For example, if you change
# POWERLEVEL9K_TIME_BACKGROUND in an interactive shell, your next prompt will reflect the change
# only if caching is disabled.
set_default POWERLEVEL9K_USE_CACHE false
# Meaningless unless POWERLEVEL9K_USE_CACHE is 'true'.
#
# Specifies the maximum number of elements in the cache. When the cache grows over this limit,
# it gets cleared. This is meant to avoid memory leaks when a rogue prompt is filling the cache
# with data.
set_default POWERLEVEL9K_MAX_CACHE_SIZE 10000
# Functions producing left and right prompts are called from subshells, so any
# changes to the environment variables they do get wiped out after the prompt is
# printed. In order to cache the results of expensive computations in these functions,
# we use a temporary file to communicate with the parent shell and to ask it to
# change environment variables.
typeset -AH p9k_cache_data=()
typeset P9K_CACHE_CHANNEL=${$(mktemp -u)%/*}/p9k_cache_channel.$$
# Store a key-value pair in the cache.
#
# * $1: Key.
# * $2: Value. Can be empty.
#
# 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() {
if [[ $POWERLEVEL9K_USE_CACHE == true ]]; then
# Prepend dot to the value so that we can easily tell apart empty
# values from missing in p9k_cache_get.
echo -E "p9k_cache_data+=(${(qq)1} .${(qq)2})" >>$P9K_CACHE_CHANNEL
fi
}
# Retrieve a value from the cache.
#
# * $1: Key.
#
# Returns 1 if there is no value with the specified key.
p9k_cache_get() {
local V=${p9k_cache_data[$1]}
[[ -n $V ]] || return 1
echo -E ${V:1}
}
# Begin a left prompt segment
# Takes four arguments:
# * $1: Name of the function that was originally invoked (mandatory).
@ -111,7 +159,12 @@ CURRENT_BG='NONE'
set_default last_left_element_index 1
set_default POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS " "
left_prompt_segment() {
local segment_name="${1}"
local cache_key="${(q)0} ${(q)1} ${(q)2} ${(q)3} ${(q)4} ${(q)5:+1} ${(q)6}"
local cached
if ! cached=$(p9k_cache_get $cache_key); then
local output=''
local segment_name=$1
local current_index=$2
# Check if the segment should be joined with the previous one
local joined
@ -140,10 +193,10 @@ left_prompt_segment() {
[[ -n "${foregroundColor}" ]] && foreground="$(foregroundColor ${foregroundColor})" || foreground="%f"
if [[ $CURRENT_BG != 'NONE' ]] && ! isSameColor "${backgroundColor}" "$CURRENT_BG"; then
echo -n "${background}%F{$CURRENT_BG}"
output+="${background}%F{$CURRENT_BG}"
if [[ $joined == false ]]; then
# Middle segment
echo -n "$(print_icon 'LEFT_SEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
output+="$(print_icon 'LEFT_SEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi
elif isSameColor "$CURRENT_BG" "${backgroundColor}"; then
# Middle segment with same color as previous segment
@ -152,13 +205,13 @@ left_prompt_segment() {
# enough contrast.
local complement
[[ -n "${foregroundColor}" ]] && complement="${foreground}" || complement="$(foregroundColor $DEFAULT_COLOR)"
echo -n "${background}${complement}"
output+="${background}${complement}"
if [[ $joined == false ]]; then
echo -n "$(print_icon 'LEFT_SUBSEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
output+="$(print_icon 'LEFT_SUBSEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi
else
# First segment
echo -n "${background}$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
output+="${background}$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi
local visual_identifier
@ -177,23 +230,36 @@ left_prompt_segment() {
fi
# Print the visual identifier
echo -n "${visual_identifier}"
# Print the content of the segment, if there is any
[[ -n "$5" ]] && echo -n "${foreground}${5}"
echo -n "${POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS}"
output+="${visual_identifier}"
CURRENT_BG="${backgroundColor}"
last_left_element_index=$current_index
cached="${(qq)output} ${(qq)backgroundColor} ${(qq)foreground}"
p9k_cache_set $cache_key $cached
fi
# output backgroundColor foreground
local tuple=("${(@Q)${(z)cached}}")
echo -n $tuple[1]
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.
left_prompt_end() {
local cache_key="$0 ${(q)CURRENT_BG}"
local cached
if ! cached=$(p9k_cache_get $cache_key); then
if [[ -n $CURRENT_BG ]]; then
echo -n "%k$(foregroundColor ${CURRENT_BG})$(print_icon 'LEFT_SEGMENT_SEPARATOR')"
cached+="%k$(foregroundColor ${CURRENT_BG})$(print_icon 'LEFT_SEGMENT_SEPARATOR')"
else
echo -n "%k"
cached+="%k"
fi
echo -n "%f$(print_icon 'LEFT_SEGMENT_END_SEPARATOR')"
cached+="%f$(print_icon 'LEFT_SEGMENT_END_SEPARATOR')"
p9k_cache_set $cache_key $cached
fi
echo -n $cached
CURRENT_BG=''
}
@ -212,6 +278,11 @@ CURRENT_RIGHT_BG='NONE'
set_default last_right_element_index 1
set_default POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS " "
right_prompt_segment() {
local cache_key="${(q)0} ${(q)1} ${(q)2} ${(q)3} ${(q)4} ${(q)5:+1} ${(q)6}"
local cached
if ! cached=$(p9k_cache_get $cache_key); then
local output=''
local segment_name="${1}"
local current_index=$2
@ -245,7 +316,7 @@ right_prompt_segment() {
if [[ "$CURRENT_RIGHT_BG" != "NONE" ]]; then
# This is the closing whitespace for the previous segment
echo -n "${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}%f"
output+="${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}%f"
fi
if [[ $joined == false ]] || [[ "$CURRENT_RIGHT_BG" == "NONE" ]]; then
@ -256,10 +327,10 @@ right_prompt_segment() {
# enough contrast.
local complement
[[ -n "${foregroundColor}" ]] && complement="${foreground}" || complement="$(foregroundColor $DEFAULT_COLOR)"
echo -n "$complement$(print_icon 'RIGHT_SUBSEGMENT_SEPARATOR')%f"
output+="$complement$(print_icon 'RIGHT_SUBSEGMENT_SEPARATOR')%f"
else
# Use the new Background Color as the foreground of the segment separator
echo -n "$(foregroundColor ${backgroundColor})$(print_icon 'RIGHT_SEGMENT_SEPARATOR')%f"
output+="$(foregroundColor ${backgroundColor})$(print_icon 'RIGHT_SEGMENT_SEPARATOR')%f"
fi
fi
@ -278,18 +349,26 @@ right_prompt_segment() {
fi
fi
echo -n "${background}${foreground}"
output+="${background}${foreground}"
# Print whitespace only if segment is not joined or first right segment
[[ $joined == false ]] || [[ "$CURRENT_RIGHT_BG" == "NONE" ]] && echo -n "${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}"
[[ $joined == false ]] || [[ "$CURRENT_RIGHT_BG" == "NONE" ]] && output+="${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}"
cached="${(qq)output} ${(qq)backgroundColor} ${(qq)visual_identifier}"
p9k_cache_set $cache_key $cached
fi
# output backgroundColor visual_identifier
local tuple=("${(@Q)${(z)cached}}")
echo -n $tuple[1]
CURRENT_RIGHT_BG=$tuple[2]
# Print segment content if there is any
[[ -n "$5" ]] && echo -n "${5}"
# Print the visual identifier
echo -n "${visual_identifier}"
echo -n "${tuple[3]}"
CURRENT_RIGHT_BG="${backgroundColor}"
last_right_element_index=$current_index
last_right_element_index=$2
}
################################################################
@ -335,18 +414,22 @@ prompt_aws_eb_env() {
set_default POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE true
set_default POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS false
prompt_background_jobs() {
local background_jobs_number=${$(jobs -l | wc -l)// /}
local wrong_lines=`jobs -l | awk '/pwd now/{ count++ } END {print count}'`
if [[ wrong_lines -gt 0 ]]; then
background_jobs_number=$(( $background_jobs_number - $wrong_lines ))
fi
if [[ background_jobs_number -gt 0 ]]; then
local background_jobs_number_print=""
if [[ "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE" == "true" ]] && ([[ "$background_jobs_number" -gt 1 ]] || [[ "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS" == "true" ]]); then
background_jobs_number_print="$background_jobs_number"
local job_lines=("${(@f)$(jobs -lr)}")
[[ ${(c)#job_lines} == 0 ]] && return
local num_jobs=$#job_lines
local cache_key="$0 $num_jobs"
local cached
if ! cached=$(p9k_cache_get $cache_key); then
cached=""
if [[ "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE" == "true" ]] && ([[ "$num_jobs" -gt 1 ]] || [[ "$POWERLEVEL9K_BACKGROUND_JOBS_VERBOSE_ALWAYS" == "true" ]]); then
cached=$num_jobs
fi
"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "cyan" "$background_jobs_number_print" 'BACKGROUND_JOBS_ICON'
p9k_cache_set $cache_key $cached
fi
"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "cyan" "$cached" 'BACKGROUND_JOBS_ICON'
}
################################################################
@ -797,6 +880,9 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false
prompt_dir() {
# 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 cache_key="$0 $2 ${(q)current_path}"
local cached
if ! cached=$(p9k_cache_get $cache_key); then
# check if the user wants to use absolute paths or "~" paths
[[ ${(L)POWERLEVEL9K_DIR_PATH_ABSOLUTE} != "true" ]] && current_path=${current_path/#$HOME/"~"}
# declare all local variables
@ -1037,7 +1123,11 @@ prompt_dir() {
current_path=${current_path:s/~/$POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}
fi
"$1_prompt_segment" "$0_${current_state}" "$2" "blue" "$DEFAULT_COLOR" "${current_path}" "${dir_states[$current_state]}"
cached="$0_$current_state ${(qq)2} blue ${(qq)DEFAULT_COLOR} ${(qq)current_path} ${(qq)dir_states[$current_state]}"
p9k_cache_set $cache_key $cached
fi
"$1_prompt_segment" "${(@Q)${(z)cached}}"
}
################################################################
@ -1408,6 +1498,9 @@ exit_code_or_status() {
}
prompt_status() {
local cache_key="$0 $2 $RETVAL $RETVALS"
local cached
if ! cached=$(p9k_cache_get $cache_key); then
local ec_text
local ec_sum
local ec
@ -1434,13 +1527,18 @@ prompt_status() {
if (( ec_sum > 0 )); then
if [[ "$POWERLEVEL9K_STATUS_CROSS" == false && "$POWERLEVEL9K_STATUS_VERBOSE" == true ]]; then
"$1_prompt_segment" "$0_ERROR" "$2" "red" "yellow1" "$ec_text" 'CARRIAGE_RETURN_ICON'
cached="$0_ERROR ${(qq)2} red yellow1 ${(qq)ec_text} CARRIAGE_RETURN_ICON"
else
"$1_prompt_segment" "$0_ERROR" "$2" "$DEFAULT_COLOR" "red" "" 'FAIL_ICON'
cached="$0_ERROR ${(qq)2} ${(qq)DEFAULT_COLOR} red '' FAIL_ICON"
fi
elif [[ "$POWERLEVEL9K_STATUS_OK" == true ]] && [[ "$POWERLEVEL9K_STATUS_VERBOSE" == true || "$POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE" == true ]]; then
"$1_prompt_segment" "$0_OK" "$2" "$DEFAULT_COLOR" "green" "" 'OK_ICON'
cached="$0_OK ${(qq)2} ${(qq)DEFAULT_COLOR} green '' OK_ICON"
fi
if (( $#RETVALS < 3 )); then
p9k_cache_set $cache_key $cached
fi
fi
"$1_prompt_segment" "${(@Q)${(z)cached}}"
}
################################################################
@ -1835,6 +1933,14 @@ powerlevel9k_prepare_prompts() {
# Reset start time
_P9K_TIMER_START=0x7FFFFFFF
if [[ -s $P9K_CACHE_CHANNEL ]]; then
eval $(<$P9K_CACHE_CHANNEL)
rm -f $P9K_CACHE_CHANNEL
if [[ -n $POWERLEVEL9K_MAX_CACHE_SIZE && $#p9k_cache_data -gt $POWERLEVEL9K_MAX_CACHE_SIZE ]]; then
p9k_cache_data=()
fi
fi
if [[ "$POWERLEVEL9K_PROMPT_ON_NEWLINE" == true ]]; then
PROMPT='$(print_icon 'MULTILINE_FIRST_PROMPT_PREFIX')%f%b%k$(build_left_prompt)
$(print_icon 'MULTILINE_LAST_PROMPT_PREFIX')'

Loading…
Cancel
Save