speed up time, date and custom prompts. saves 2.5ms overall

pull/22/head
romkatv 6 years ago
parent afc1a07a71
commit a0d2449c43

@ -773,6 +773,9 @@ prompt_custom() {
# Get content of custom segment # Get content of custom segment
local command="POWERLEVEL9K_CUSTOM_${segment_name}" local command="POWERLEVEL9K_CUSTOM_${segment_name}"
local segment_content="$(eval ${(P)command})" local segment_content="$(eval ${(P)command})"
# Note: this would be a better implementation. I'm keeping the old one in case anyone
# relies on its quirks.
# local segment_content=$("${(@Q)${(z)${(P)command}}}")
if [[ -n $segment_content ]]; then if [[ -n $segment_content ]]; then
"$1_prompt_segment" "${0}_${3:u}" "$2" $DEFAULT_COLOR_INVERTED $DEFAULT_COLOR "$segment_content" "CUSTOM_${segment_name}_ICON" "$1_prompt_segment" "${0}_${3:u}" "$2" $DEFAULT_COLOR_INVERTED $DEFAULT_COLOR "$segment_content" "CUSTOM_${segment_name}_ICON"
@ -1554,22 +1557,30 @@ build_test_stats() {
# If set to true, `time` prompt will update every second. # If set to true, `time` prompt will update every second.
set_default POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME false set_default POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME false
set_default POWERLEVEL9K_TIME_FORMAT "%D{%H:%M:%S}"
prompt_time() { prompt_time() {
set_default POWERLEVEL9K_TIME_FORMAT "%D{%H:%M:%S}" "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$POWERLEVEL9K_TIME_FORMAT" "TIME_ICON"
if [[ $POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME == true ]]; then # For the reference, here's how the code should ideally look like. However, it's 2ms slower
local _P9K_TIME=$POWERLEVEL9K_TIME_FORMAT # for a tiny gain in usability. The difference is that the current code will cause time
else # to update when vcs segment goes from grey to green/yellow, but the commented-out code
[[ -v _P9K_REFRESH_PROMPT ]] || typeset -gH _P9K_TIME=$(print -P $POWERLEVEL9K_TIME_FORMAT) # won't (unless POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME is true).
fi #if [[ $POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME == true ]]; then
"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$_P9K_TIME" "TIME_ICON" # local _P9K_TIME=$POWERLEVEL9K_TIME_FORMAT
#else
# [[ -v _P9K_REFRESH_PROMPT ]] || typeset -gH _P9K_TIME=$(print -P $POWERLEVEL9K_TIME_FORMAT)
# typeset -gH _P9K_TIME=$POWERLEVEL9K_TIME_FORMAT
#fi
#"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$_P9K_TIME" "TIME_ICON"
} }
################################################################ ################################################################
# System date # System date
set_default POWERLEVEL9K_DATE_FORMAT "%D{%d.%m.%y}"
prompt_date() { prompt_date() {
set_default POWERLEVEL9K_DATE_FORMAT "%D{%d.%m.%y}" "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$POWERLEVEL9K_DATE_FORMAT" "DATE_ICON"
[[ -v _P9K_REFRESH_PROMPT ]] || typeset -gH _P9K_DATE=$(print -P $POWERLEVEL9K_DATE_FORMAT) # See comments in prompt_time.
"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$_P9K_DATE" "DATE_ICON" # [[ -v _P9K_REFRESH_PROMPT ]] || typeset -gH _P9K_DATE=$(print -P $POWERLEVEL9K_DATE_FORMAT)
# "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR_INVERTED" "$DEFAULT_COLOR" "$_P9K_DATE" "DATE_ICON"
} }
################################################################ ################################################################
@ -2001,7 +2012,7 @@ build_left_prompt() {
# Check if it is a custom command, otherwise interpet it as # Check if it is a custom command, otherwise interpet it as
# a prompt. # a prompt.
if [[ $element[0,7] =~ "custom_" ]]; then if [[ $element == custom_* ]]; then
"prompt_custom" "left" "$index" $element[8,-1] "prompt_custom" "left" "$index" $element[8,-1]
else else
"prompt_$element" "left" "$index" "prompt_$element" "left" "$index"
@ -2029,7 +2040,7 @@ build_right_prompt() {
# Check if it is a custom command, otherwise interpet it as # Check if it is a custom command, otherwise interpet it as
# a prompt. # a prompt.
if [[ $element[0,7] =~ "custom_" ]]; then if [[ $element == custom_* ]]; then
"prompt_custom" "right" "$index" $element[8,-1] "prompt_custom" "right" "$index" $element[8,-1]
else else
"prompt_$element" "right" "$index" "prompt_$element" "right" "$index"

Loading…
Cancel
Save