Improved speed of prompt_vcs by 50%-66%.

This commit is contained in:
Ben Hilburn 2016-08-11 15:40:05 -04:00
parent 520eed1248
commit c4fdc8f708
2 changed files with 30 additions and 9 deletions

View file

@ -122,6 +122,20 @@ if [[ "$OS" == 'OSX' ]]; then
fi fi
fi fi
# Determine if the passed segment is used in the prompt
#
# Pass the name of the segment to this function to test for its presence in
# either the LEFT or RIGHT prompt arrays.
# * $1: The segment to be tested.
segment_in_use() {
local key=$1
if [[ -n "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(r)$key]}" ]] || [[ -n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)$key]}" ]]; then
return 0
else
return 1
fi
}
# Print a deprecation warning if an old segment is in use. # Print a deprecation warning if an old segment is in use.
# Takes the name of an associative array that contains the # Takes the name of an associative array that contains the
# deprecated segments as keys, the values contain the new # deprecated segments as keys, the values contain the new
@ -131,7 +145,7 @@ print_deprecation_warning() {
raw_deprecated_segments=(${(kvP@)1}) raw_deprecated_segments=(${(kvP@)1})
for key in ${(@k)raw_deprecated_segments}; do for key in ${(@k)raw_deprecated_segments}; do
if [[ -n "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(r)$key]}" ]] || [[ -n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)$key]}" ]]; then if segment_in_use $key; then
# segment is deprecated # segment is deprecated
print -P "%F{yellow}Warning!%f The '$key' segment is deprecated. Use '%F{blue}${raw_deprecated_segments[$key]}%f' instead. For more informations, have a look at the CHANGELOG.md." print -P "%F{yellow}Warning!%f The '$key' segment is deprecated. Use '%F{blue}${raw_deprecated_segments[$key]}%f' instead. For more informations, have a look at the CHANGELOG.md."
fi fi

View file

@ -837,7 +837,7 @@ prompt_todo() {
set_default POWERLEVEL9K_VCS_ACTIONFORMAT_FOREGROUND "red" set_default POWERLEVEL9K_VCS_ACTIONFORMAT_FOREGROUND "red"
# Default: Just display the first 8 characters of our changeset-ID. # Default: Just display the first 8 characters of our changeset-ID.
set_default POWERLEVEL9K_VCS_INTERNAL_HASH_LENGTH "8" set_default POWERLEVEL9K_VCS_INTERNAL_HASH_LENGTH "8"
prompt_vcs() { powerlevel9k_vcs_init() {
if [[ -n "$POWERLEVEL9K_CHANGESET_HASH_LENGTH" ]]; then if [[ -n "$POWERLEVEL9K_CHANGESET_HASH_LENGTH" ]]; then
POWERLEVEL9K_VCS_INTERNAL_HASH_LENGTH="$POWERLEVEL9K_CHANGESET_HASH_LENGTH" POWERLEVEL9K_VCS_INTERNAL_HASH_LENGTH="$POWERLEVEL9K_CHANGESET_HASH_LENGTH"
fi fi
@ -849,8 +849,7 @@ prompt_vcs() {
VCS_WORKDIR_HALF_DIRTY=false VCS_WORKDIR_HALF_DIRTY=false
# The vcs segment can have three different states - defaults to 'clean'. # The vcs segment can have three different states - defaults to 'clean'.
local current_state="" typeset -gAH vcs_states
typeset -AH vcs_states
vcs_states=( vcs_states=(
'clean' 'green' 'clean' 'green'
'modified' 'yellow' 'modified' 'yellow'
@ -890,6 +889,12 @@ prompt_vcs() {
if [[ "$POWERLEVEL9K_SHOW_CHANGESET" == true ]]; then if [[ "$POWERLEVEL9K_SHOW_CHANGESET" == true ]]; then
zstyle ':vcs_info:*' get-revision true zstyle ':vcs_info:*' get-revision true
fi fi
}
prompt_vcs() {
VCS_WORKDIR_DIRTY=false
VCS_WORKDIR_HALF_DIRTY=false
current_state=""
# Actually invoke vcs_info manually to gather all information. # Actually invoke vcs_info manually to gather all information.
vcs_info vcs_info
@ -954,11 +959,8 @@ prompt_pyenv() {
################################################################ ################################################################
# Prompt processing and drawing # Prompt processing and drawing
################################################################ ################################################################
# Main prompt # Main prompt
build_left_prompt() { build_left_prompt() {
defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs)
local index=1 local index=1
for element in "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[@]}"; do for element in "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[@]}"; do
# Remove joined information in direct calls # Remove joined information in direct calls
@ -980,8 +982,6 @@ build_left_prompt() {
# Right prompt # Right prompt
build_right_prompt() { build_right_prompt() {
defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time)
local index=1 local index=1
for element in "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]}"; do for element in "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]}"; do
# Remove joined information in direct calls # Remove joined information in direct calls
@ -1050,6 +1050,9 @@ powerlevel9k_init() {
print -P "\t%F{red}WARNING!%f %F{blue}export LANG=\"en_US.UTF-8\"%f at the top of your \~\/.zshrc is sufficient." print -P "\t%F{red}WARNING!%f %F{blue}export LANG=\"en_US.UTF-8\"%f at the top of your \~\/.zshrc is sufficient."
fi fi
defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs)
defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time)
# Display a warning if deprecated segments are in use. # Display a warning if deprecated segments are in use.
typeset -AH deprecated_segments typeset -AH deprecated_segments
# old => new # old => new
@ -1067,6 +1070,10 @@ powerlevel9k_init() {
# initialize colors # initialize colors
autoload -U colors && colors autoload -U colors && colors
if segment_in_use "vcs"; then
powerlevel9k_vcs_init
fi
# initialize hooks # initialize hooks
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook