From 8ff58ae0ec9205c1a64ce8668605bff320bd77c2 Mon Sep 17 00:00:00 2001 From: Sebastian Gniazdowski Date: Mon, 24 Oct 2016 13:35:13 +0200 Subject: [PATCH] Optimization: generate disp_list only on page change or query change --- history-search-multi-word | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/history-search-multi-word b/history-search-multi-word index 4a693ea..d1d22ad 100644 --- a/history-search-multi-word +++ b/history-search-multi-word @@ -27,6 +27,8 @@ autoload is-at-least typeset -g __hsmw_hcw_index typeset -g __hsmw_hcw_widget_name __hsmw_hcw_restart __hsmw_hcw_call_count typeset -g __hsmw_page_size __hsmw_hl_color __hsmw_synhl __hsmw_active +typeset -ga __hsmw_disp_list +typeset -gi __hsmw_page_start_idx typeset -gaU __hsmw_hcw_found (( __hsmw_hcw_call_count ++ )) @@ -52,6 +54,7 @@ _hsmw_main() { __hsmw_hcw_found=( ) __hsmw_hcw_finished="0" __hsmw_hcw_restart="0" + __hsmw_page_start_idx=0 else # Consecutive call [[ "$WIDGET" = *-word ]] && (( __hsmw_hcw_index ++ )) @@ -99,22 +102,25 @@ _hsmw_main() { # Prepare display # - typeset -a disp_list - disp_list=( "${(@)__hsmw_hcw_found[page_start_idx,page_start_idx+page_size-1]}" ) + (( page_start_idx != __hsmw_page_start_idx )) && { + __hsmw_disp_list=( "${(@)__hsmw_hcw_found[page_start_idx,page_start_idx+page_size-1]}" ) - # All entries should have multilines replaced - disp_list=( "${(@)disp_list//$'\n'/\\n}" ) - # ... and truncated to display width, and - # also preceeded by two spaces - disp_list=( "${(@)disp_list/(#m)*/ ${MATCH[1,COLUMNS-8]}}" ) + # All entries should have multilines replaced + __hsmw_disp_list=( "${(@)__hsmw_disp_list//$'\n'/\\n}" ) + # ... and truncated to display width, and + # also preceeded by two spaces + __hsmw_disp_list=( "${(@)__hsmw_disp_list/(#m)*/ ${MATCH[1,COLUMNS-8]}}" ) + + __hsmw_page_start_idx=page_start_idx + } # # Detect where "> .." entry starts, add the ">" mark # - local txt_before="${(F)${(@)disp_list[1,on_page_idx-1]}}" - local entry="${disp_list[on_page_idx]}" - local text="${(F)disp_list}" + local txt_before="${(F)${(@)__hsmw_disp_list[1,on_page_idx-1]}}" + local entry="${__hsmw_disp_list[on_page_idx]}" + local text="${(F)__hsmw_disp_list}" integer replace_idx=${#txt_before}+2 (( replace_idx == 2 )) && replace_idx=1 text[replace_idx]=">" @@ -131,7 +137,7 @@ _hsmw_main() { if [[ "$__hsmw_synhl" = "yes" ]]; then integer pre_index=0 local line - for line in "${disp_list[@]}"; do + for line in "${__hsmw_disp_list[@]}"; do reply=( ) -hsmw-highlight-process "$line" region_highlight+=( "${reply[@]//(#b)([[:digit:]]##) ([[:digit:]]##)/$(( ${match[1]} + pre_index + offset )) $(( ${match[2]} + pre_index + offset ))}" )