273063ad59a9ce125349100647204ef42ad83e1c

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Merge pull request #4663 from psprint/master

znt: faster startup of tools using colorifying (e.g. n-history), optimized heap usage for older Zsh's

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/zsh-navigation-tools/README.md b/plugins/zsh-navigation-tools/README.md
  2index 670a85ece9f073d70ed4df1f1f13860ac02ac71c..630b2e840e6ee1445fbab305beb47bc0a6462cfb 100644
  3--- a/plugins/zsh-navigation-tools/README.md
  4+++ b/plugins/zsh-navigation-tools/README.md
  5@@ -105,8 +105,7 @@ colorize output of the tools, via their config files (check out e.g. n-cd.conf,
  6 it uses this).
  7 
  8 ## Performance
  9-ZNT are fastest with Zsh before 5.0.8 and starting from 5.2 (the version yet to
 10-be released).
 11+ZNT are fastest with Zsh before 5.0.8 and starting from 5.2
 12 
 13 
 14 vim:filetype=conf
 15diff --git a/plugins/zsh-navigation-tools/n-list b/plugins/zsh-navigation-tools/n-list
 16index 26431a0d9c24ce581c2abda128c75a3e8a3da406..d13e048bf2c591b7081357559885279ed0600993 100644
 17--- a/plugins/zsh-navigation-tools/n-list
 18+++ b/plugins/zsh-navigation-tools/n-list
 19@@ -116,15 +116,15 @@ _nlist_setup_user_vars() {
 20     fi
 21 }
 22 
 23-_nlist_coloring_list_into_col_list() {
 24+_nlist_colorify_disp_list() {
 25     local col=$'\x1b[00;34m' reset=$'\x1b[0m'
 26     [ -n "$NLIST_COLORING_COLOR" ] && col="$NLIST_COLORING_COLOR"
 27     [ -n "$NLIST_COLORING_END_COLOR" ] && reset="$NLIST_COLORING_END_COLOR"
 28 
 29     if [ "$NLIST_COLORING_MATCH_MULTIPLE" -eq 1 ]; then
 30-        col_list=( "${(@)list//(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
 31+        disp_list=( "${(@)disp_list//(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
 32     else
 33-        col_list=( "${(@)list/(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
 34+        disp_list=( "${(@)disp_list/(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
 35     fi
 36 }
 37 
 38@@ -154,7 +154,7 @@ integer inner_width=term_width-3
 39 integer page_height=inner_height
 40 integer page_width=inner_width
 41 
 42-typeset -a list col_list disp_list
 43+typeset -a list disp_list
 44 integer last_element=$#
 45 local action
 46 local final_key
 47@@ -224,19 +224,17 @@ zcurses timeout main -1
 48 key=""
 49 keypad=""
 50 
 51-list=( "$@" )
 52-last_element="$#list"
 53+# This loop makes script faster on some Zsh's (e.g. 5.0.8)
 54+repeat 1; do
 55+    list=( "$@" )
 56+done
 57 
 58-integer is_colored=0
 59-if [[ -z "$NLIST_SEARCH_BUFFER" && -n "$NLIST_COLORING_PATTERN" ]]; then
 60-    is_colored=1
 61-    _nlist_coloring_list_into_col_list
 62-fi
 63+last_element="$#list"
 64 
 65 while (( 1 )); do
 66     # Do searching (filtering with string)
 67     if [ -n "$NLIST_SEARCH_BUFFER" ]; then
 68-        # Compute new list, col_list ?
 69+        # Compute new list?
 70         if [[ "$NLIST_SEARCH_BUFFER" != "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
 71             prev_search_buffer="$NLIST_SEARCH_BUFFER"
 72             prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
 73@@ -290,7 +288,7 @@ while (( 1 )); do
 74                 disp_list=( "${(@)disp_list//(#mi)($~colsearch_pattern)/$red${MATCH}$reset}" )
 75             fi
 76 
 77-            # We have display list, lets replace newlines with "\n" when needed (1/3)
 78+            # We have display list, lets replace newlines with "\n" when needed (1/2)
 79             [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
 80         fi
 81 
 82@@ -302,7 +300,7 @@ while (( 1 )); do
 83         # There is no search, but there was in previous loop
 84         # OR
 85         # Uniq mode was entered or left out
 86-        # -> compute new list (maybe also col_list)
 87+        # -> compute new list
 88         if [[ -n "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
 89             prev_search_buffer=""
 90             prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
 91@@ -322,13 +320,6 @@ while (( 1 )); do
 92             # Remove duplicates when in uniq mode
 93             [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
 94 
 95-            # Apply coloring pattern (when not with search query)
 96-            is_colored=0
 97-            if [ -n "$NLIST_COLORING_PATTERN" ]; then
 98-                is_colored=1
 99-                _nlist_coloring_list_into_col_list 
100-            fi
101-
102             last_element="$#list"
103             # Called after processing list
104             _nlist_verify_vars
105@@ -340,22 +331,14 @@ while (( 1 )); do
106         integer end_idx=$(( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
107         [ "$end_idx" -gt "$last_element" ] && end_idx=last_element
108 
109-        if [ "$is_colored" -eq 0 ]; then
110-            if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
111-                prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
112-                disp_list=( "${(@)list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
113+        if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
114+            prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
115+            disp_list=( "${(@)list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
116 
117-                # We have display list, lets replace newlines with "\n" when needed (2/3)
118-                [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
119diff --git a/plugins/zsh-navigation-tools/n-panelize b/plugins/zsh-navigation-tools/n-panelize
120index a70565c79b03e32c616a78d0d4138fb72557e975..01d01cb9eba4219c5670b25b22b6b453028752f7 100644
121--- a/plugins/zsh-navigation-tools/n-panelize
122+++ b/plugins/zsh-navigation-tools/n-panelize
123@@ -32,7 +32,11 @@ if [ -t 0 ]; then
124         return 1
125     fi
126 
127-    list=( `"$@"` )
128+    # This loop makes script faster on some Zsh's (e.g. 5.0.8)
129+    repeat 1; do
130+        list=( `"$@"` )
131+    done
132+
133     # TODO: $? doesn't reach user
134     [ "$?" -eq 127 ] && return $?
135 else
136@@ -42,7 +46,10 @@ else
137         return 1
138     fi
139 
140-    list=( "${(@f)"$(<&0)"}" )
141+    # This loop makes script faster on some Zsh's (e.g. 5.0.8)
142+    repeat 1; do
143+        list=( "${(@f)"$(<&0)"}" )
144+    done
145 
146     if [[ ! -c /dev/tty ]]; then
147         exec <&2