-
Notifications
You must be signed in to change notification settings - Fork 3
/
h2-list
503 lines (425 loc) · 17.1 KB
/
h2-list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# $1 - main window name
# $2, $3 - width and height of the window
# $4 - optional status window name
# $5, $6 - width and height of the window
#
# $HLIST_NONSELECTABLE_ELEMENTS - array of indexes (1-based) that cannot be selected
# $REPLY is the output variable - contains index (1-based) or -1 when no selection
# $reply (array) is the second part of the output - use the index (REPLY) to get selected element
#
# This function outputs a list of elements that can be navigated with keyboard.
# Besides vertical navigation, it does horizontal navigation over elements of line.
# Uses curses library
emulate -LR zsh
setopt typesetsilent extendedglob noshortloops
_hlist_has_terminfo=0
zmodload zsh/curses
zmodload zsh/terminfo 2>/dev/null && _hlist_has_terminfo=1
trap "REPLY=-2; reply=(); return" TERM INT QUIT
# Drawing and input
autoload h2-list-draw h2-list-input
# Outputs a message in the bottom of the screen
_hlist_status_msg() {
# -1 for border, -1 for 0-based indexing
zcurses move "$wname_status" 1 2
zcurses clear "$wname_status" eol
zcurses string "$wname_status" "$1"
#status_msg_strlen is localized in caller
status_msg_strlen=$#1
}
# $1 is window name, $2 is the expected cursor state (0 invisible, 1 visible)
# Prefer tput, then module terminfo
_hlist_cursor_visibility() {
# If $1 = plain, then just output the
# codes without any state management
if [ "$1" != "plain" ]; then
# Don't change already set cursor state
[ "$cursor_state[$1]" = "$2" ] && return
cursor_state[$1]="$2"
fi
if type tput 2>/dev/null 1>&2; then
[ "$2" = "1" ] && { tput cvvis; tput cnorm }
[ "$2" = "0" ] && tput civis
elif [ "$_hlist_has_terminfo" = "1" ]; then
[ "$2" = "1" ] && { [ -n $terminfo[cvvis] ] && echo -n $terminfo[cvvis];
[ -n $terminfo[cnorm] ] && echo -n $terminfo[cnorm] }
[ "$2" = "0" ] && [ -n $terminfo[civis] ] && echo -n $terminfo[civis]
fi
}
_hlist_compute_user_vars_difference() {
if [[ "${(t)HLIST_NONSELECTABLE_ELEMENTS}" != "array" &&
"${(t)HLIST_NONSELECTABLE_ELEMENTS}" != "array-local" ]]
then
last_element_difference=0
current_difference=0
else
last_element_difference=$#HLIST_NONSELECTABLE_ELEMENTS
current_difference=0
local idx
for idx in "${(n)HLIST_NONSELECTABLE_ELEMENTS[@]}"; do
[ "$idx" -le "$HLIST_CURRENT_IDX" ] && current_difference+=1 || break
done
fi
}
# List was processed, check if variables aren't off range
_hlist_verify_vars() {
[ "$HLIST_CURRENT_IDX" -gt "$last_element" ] && HLIST_CURRENT_IDX="$last_element"
[[ "$HLIST_CURRENT_IDX" -eq 0 && "$last_element" -ne 0 ]] && HLIST_CURRENT_IDX=1
(( HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=0+((HLIST_CURRENT_IDX-1)/page_height)*page_height+1 ))
}
# Compute the variables which are shown to the user
_hlist_setup_user_vars() {
if [ "$1" = "1" ]; then
# Basic values when there are no non-selectables
HLIST_USER_CURRENT_IDX="$HLIST_CURRENT_IDX"
HLIST_USER_LAST_ELEMENT="$last_element"
else
_hlist_compute_user_vars_difference
HLIST_USER_CURRENT_IDX=$(( HLIST_CURRENT_IDX - current_difference ))
HLIST_USER_LAST_ELEMENT=$(( last_element - last_element_difference ))
fi
}
_hlist_colorify_disp_list() {
local col=$'\x1b[00;34m' reset=$'\x1b[0m'
[ -n "$HLIST_COLORING_COLOR" ] && col="$HLIST_COLORING_COLOR"
[ -n "$HLIST_COLORING_END_COLOR" ] && reset="$HLIST_COLORING_END_COLOR"
if [ "$HLIST_COLORING_MATCH_MULTIPLE" -eq 1 ]; then
disp_list=( "${(@)disp_list//(#mi)$~HLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
else
disp_list=( "${(@)disp_list/(#mi)$~HLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
fi
}
_hlist_mark_current_segment_in_current_element() {
# Check if current element is in HLIST_NONSELECTABLE_ELEMENTS
# If yes, don't mark current segment
[ "${HLIST_NONSELECTABLE_ELEMENTS[(r)$HLIST_CURRENT_IDX]}" = "$HLIST_CURRENT_IDX" ] && return
# There will be no colorifying for current element
integer HLIST_CURRENT_PAGE_IDX=$(( (HLIST_CURRENT_IDX-1)%page_height+1 ))
local element="$list[HLIST_CURRENT_IDX]"
local output=""
[[ "$HLIST_CURRENT_SEGMENT" -lt 1 ]] && HLIST_CURRENT_SEGMENT=1
integer active_segment="$HLIST_CURRENT_SEGMENT"
[[ "$active_segment" -gt "$nseg" ]] && active_segment="$nseg"
# Lets find interesting segment and mark it with bells (\7)
integer count=0
local i
for i in "${(z)element}"; do
count=count+1
if [[ "$count" -eq "$active_segment" ]]; then
# (z) replaces regular newlines with ;
# // in following line takes care of newlines
# from within strings
output+=$'\7'"${i//$'\n'/\\n}"$'\7'" "
else
output+="$i "
fi
done
disp_list2[HLIST_CURRENT_PAGE_IDX]="${output% }"
}
#
# Main code
#
# Check if there is proper input
if [ "$#" -lt 1 ]; then
echo "Usage: h2-list element_1 ..."
return 1
fi
REPLY="-1"
typeset -ga reply
reply=()
local wname_main="$1"
integer main_height="$2"
integer main_width="$3"
local wname_status="$4"
integer status_height="$5"
integer status_width="$6"
integer page_height=main_height-2
integer page_width=main_width-2
local header="$7"
local mark_header="$8"
shift 8
typeset -a list disp_list disp_list2
integer last_element=$#
local action
local final_key
integer selection
integer last_element_difference=0
integer current_difference=0
local prev_search_buffer=""
integer prev_uniq_mode=0
integer prev_start_idx=-1
local MBEGIN MEND MATCH mbegin mend match
integer nseg
typeset -A "cursor_state"
cursor_state=( "main" 1 "status" 1 )
local boldattr
[ "$bold" = "0" ] && boldattr="" || boldattr="+bold"
# Ability to remember the list between calls
if [[ -z "$HLIST_REMEMBER_STATE" || "$HLIST_REMEMBER_STATE" -eq 0 || "$HLIST_REMEMBER_STATE" -eq 2 ]]; then
HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=1
HLIST_CURRENT_IDX=1
HLIST_IS_SEARCH_MODE=0
HLIST_SEARCH_BUFFER=""
HLIST_TEXT_OFFSET=0
HLIST_IS_UNIQ_MODE=0
HLIST_CURRENT_SEGMENT=1 # This variable is responsible for horizontal indexing
typeset -gA HLIST_ACTIVE_SEGMENTS
HLIST_ACTIVE_SEGMENTS=()
# Zero - because it isn't known, unless we
# confirm that first element is selectable
HLIST_USER_CURRENT_IDX=0
[[ ${HLIST_NONSELECTABLE_ELEMENTS[(r)1]} != 1 ]] && HLIST_USER_CURRENT_IDX=1
HLIST_USER_LAST_ELEMENT=$(( last_element - $#HLIST_NONSELECTABLE_ELEMENTS ))
# 2 is init once, then remember
[ "$HLIST_REMEMBER_STATE" -eq 2 ] && HLIST_REMEMBER_STATE=1
fi
if [ "$HLIST_START_IN_SEARCH_MODE" -eq 1 ]; then
HLIST_START_IN_SEARCH_MODE=0
HLIST_IS_SEARCH_MODE=1
fi
if [ -n "$HLIST_SET_SEARCH_TO" ]; then
HLIST_SEARCH_BUFFER="$HLIST_SET_SEARCH_TO"
HLIST_SET_SEARCH_TO=""
fi
if [ "$HLIST_START_IN_UNIQ_MODE" -eq 1 ]; then
HLIST_START_IN_UNIQ_MODE=0
HLIST_IS_UNIQ_MODE=1
fi
#
# Listening for input
#
local key keypad
# Clear input buffer
zcurses timeout main 0
zcurses input main key keypad
zcurses timeout main -1
key=""
keypad=""
# This loop makes script faster on some Zsh's (e.g. 5.0.8)
repeat 1; do
list=( "$@" )
done
last_element="$#list"
while (( 1 )); do
# Do searching (filtering with string)
if [ -n "$HLIST_SEARCH_BUFFER" ]; then
# Compute new list?
if [[ "$HLIST_SEARCH_BUFFER" != "$prev_search_buffer" || "$HLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
prev_search_buffer="$HLIST_SEARCH_BUFFER"
prev_uniq_mode="$HLIST_IS_UNIQ_MODE"
# regenerating list -> regenerating disp_list
prev_start_idx=-1
# Take all elements, including duplicates and non-selectables
typeset +U list
repeat 1; do
list=( "$@" )
done
# Remove non-selectable elements
[ "$#HLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] && for i in "${(nO)HLIST_NONSELECTABLE_ELEMENTS[@]}"; do
list[$i]=()
done
# Remove duplicates
[ "$HLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
last_element="$#list"
# Next do the filtering
local search_buffer="${HLIST_SEARCH_BUFFER%% ##}"
search_buffer="${search_buffer## ##}"
search_buffer="${search_buffer//(#m)[][*?|#~^()><\\]/\\$MATCH}"
local search_pattern=""
local colsearch_pattern=""
if [ -n "$search_buffer" ]; then
# Patterns will be *foo*~^*bar* and (foo|bar)
search_pattern="${search_buffer// ##/*~^*}"
colsearch_pattern="${search_buffer// ##/|}"
# The repeat will make the matching work on a fresh heap
repeat 1; do
list=( "${(@M)list:#(#i)*$~search_pattern*}" )
done
last_element="$#list"
fi
# Called after processing list
_hlist_verify_vars
fi
_hlist_setup_user_vars 1
typeset -a tmp
tmp=( "${(z)list[HLIST_CURRENT_IDX]}" )
nseg="$#tmp"
integer end_idx=$(( HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
[ "$end_idx" -gt "$last_element" ] && end_idx=last_element
if [ "$prev_start_idx" -ne "$HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
prev_start_idx="$HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
disp_list=( "${(@)list[HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
if [ -n "$colsearch_pattern" ]; then
local col=$'\x1b[00;35m' reset=$'\x1b[00;00m'
# The repeat will make the matching work on a fresh heap
repeat 1; do
disp_list=( "${(@)disp_list//(#mi)($~colsearch_pattern)/$col${MATCH}$reset}" )
done
fi
fi
# We need second disp_list to be able to disable colorifying for particular elements
disp_list2=( "${(@)disp_list}" )
# We have display list, lets replace newlines with ";"
# This will also replace newlines from within strings,
# which is an error, but it will be fine when element
# will be active
disp_list2=( "${(@)disp_list2//$'\n'/;}" )
[ "$#disp_list2" -gt 0 ] && _hlist_mark_current_segment_in_current_element
# Output colored list
h2-list-draw "$(( (HLIST_CURRENT_IDX-1) % page_height + 1 ))" \
"$page_height" "$page_width" 1 2 "$HLIST_TEXT_OFFSET" "$wname_main" \
"$disp_list2[@]"
else
# There is no search, but there was in previous loop
# OR
# Uniq mode was entered or left out
# -> compute new list
if [[ -n "$prev_search_buffer" || "$HLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
prev_search_buffer=""
prev_uniq_mode="$HLIST_IS_UNIQ_MODE"
# regenerating list -> regenerating disp_list
prev_start_idx=-1
# Take all elements, including duplicates and non-selectables
typeset +U list
repeat 1; do
list=( "$@" )
done
# Remove non-selectable elements only when in uniq mode
[ "$HLIST_IS_UNIQ_MODE" -eq 1 ] && [ "$#HLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] &&
for i in "${(nO)HLIST_NONSELECTABLE_ELEMENTS[@]}"; do
list[$i]=()
done
# Remove duplicates when in uniq mode
[ "$HLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
last_element="$#list"
# Called after processing list
_hlist_verify_vars
fi
# "1" - shouldn't bother with non-selectables
_hlist_setup_user_vars "$HLIST_IS_UNIQ_MODE"
typeset -a tmp
tmp=( "${(z)list[HLIST_CURRENT_IDX]}" )
nseg="$#tmp"
integer end_idx=$(( HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
[ "$end_idx" -gt "$last_element" ] && end_idx=last_element
if [ "$prev_start_idx" -ne "$HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
prev_start_idx="$HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
disp_list=( "${(@)list[HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
[ -n "$HLIST_COLORING_PATTERN" ] && _hlist_colorify_disp_list
fi
# We need second disp_list to be able to disable colorifying for particular elements
disp_list2=( "${(@)disp_list}" )
# We have display list, lets replace newlines with ";"
# This will also replace newlines from within strings,
# which is an error, but it will be fine when element
# will be active
disp_list2=( "${(@)disp_list2//$'\n'/;}" )
[ "$#disp_list2" -gt 0 ] && _hlist_mark_current_segment_in_current_element
# Output the list
h2-list-draw "$(( (HLIST_CURRENT_IDX-1) % page_height + 1 ))" \
"$page_height" "$page_width" 1 2 "$HLIST_TEXT_OFFSET" "$wname_main" \
"$disp_list2[@]"
fi
[ "$mark_header" = "1" ] && zcurses attr "$wname_main" "$boldattr" magenta/black
zcurses border "$wname_main"
zcurses move "$wname_main" 0 2
zcurses string "$wname_main" "$header"
[ "$mark_header" = "1" ] && zcurses attr "$wname_main" "$boldattr" white/black
_hlist_cursor_visibility "main" "0"
zcurses refresh "$wname_main"
# Status window is optional (1/2)
[ -n "$wname_status" ] && {
local status_msg_strlen
if [ "$HLIST_IS_SEARCH_MODE" = "1" ]; then
local _txt2=""
[ "$HLIST_IS_UNIQ_MODE" -eq 1 ] && _txt2="[-UNIQ-] "
_hlist_status_msg "${_txt2}Filtering with: ${HLIST_SEARCH_BUFFER// /+}"
elif [[ ${HLIST_NONSELECTABLE_ELEMENTS[(r)$HLIST_CURRENT_IDX]} != $HLIST_CURRENT_IDX ||
-n "$HLIST_SEARCH_BUFFER" || "$HLIST_IS_UNIQ_MODE" -eq 1 ]]; then
local _txt="" _txt2=""
[ -n "$HLIST_GREP_STRING" ] && _txt=" [$HLIST_GREP_STRING]"
[ "$HLIST_IS_UNIQ_MODE" -eq 1 ] && _txt2="[-UNIQ-] "
_hlist_status_msg "${_txt2}Current #$HLIST_USER_CURRENT_IDX (of #$HLIST_USER_LAST_ELEMENT entries)$_txt"
else
_hlist_status_msg ""
fi
zcurses border "$wname_status"
# status_msg_strlen is set in _hlist_status_msg()
zcurses move "$wname_status" 1 $(( status_msg_strlen + 2 ))
[ "$HLIST_IS_SEARCH_MODE" -ne 1 ] && _hlist_cursor_visibility "status" "0" || _hlist_cursor_visibility "status" "1"
zcurses refresh "$wname_status"
}
# Draw only?
[ "$HLIST_DRAW_ONLY" = "1" ] && return
# Wait for input
zcurses input main key keypad
# Get the special (i.e. "keypad") key or regular key
if [ -n "$key" ]; then
final_key="$key"
elif [ -n "$keypad" ]; then
final_key="$keypad"
else
[ -n "$wname_status" ] && {
_hlist_status_msg "Inproper input detected"
zcurses refresh "$wname_status"
sleep 2
}
fi
integer track_cur_idx="$HLIST_CURRENT_IDX"
h2-list-input "$HLIST_CURRENT_IDX" "$HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" \
"$page_height" "$page_width" "$last_element" "$nseg" "$HLIST_TEXT_OFFSET" \
"$final_key" "$HLIST_IS_SEARCH_MODE" "$HLIST_SEARCH_BUFFER" \
"$HLIST_IS_UNIQ_MODE" "$HLIST_DISABLE_SEARCH"
selection="$reply[1]"
action="$reply[2]"
HLIST_CURRENT_IDX="$reply[3]"
HLIST_FROM_WHAT_IDX_LIST_IS_SHOWN="$reply[4]"
HLIST_TEXT_OFFSET="$reply[5]"
HLIST_IS_SEARCH_MODE="$reply[6]"
HLIST_SEARCH_BUFFER="$reply[7]"
HLIST_IS_UNIQ_MODE="$reply[8]"
if [ "$HLIST_TRACK_SEGMENTS" = "1" ]; then
# Remember segment change (element change and segment
# change cannot occur at the same time)
HLIST_ACTIVE_SEGMENTS[$track_cur_idx]="$HLIST_CURRENT_SEGMENT"
if [[ "$track_cur_idx" != "$HLIST_CURRENT_IDX" ]]; then
# Restore segment or set to 1
if (( ${+HLIST_ACTIVE_SEGMENTS[$HLIST_CURRENT_IDX]} )); then
HLIST_CURRENT_SEGMENT="${HLIST_ACTIVE_SEGMENTS[$HLIST_CURRENT_IDX]}"
else
HLIST_CURRENT_SEGMENT=1
fi
fi
fi
if [ "$action" = "SELECT" ]; then
REPLY="$selection"
reply=( "$list[@]" )
break
elif [[ "$action" = F[1-4] ]]; then
REPLY="$action"
reply=( "$list[@]" )
break
elif [ "$action" = "LEAVE" ]; then
REPLY="LEAVE"
reply=( "$list[@]" )
break
elif [[ "$action" = "MOVE_LEFT" || "$action" = "MOVE_RIGHT" ||
"$action" = "BR_MOVE_LEFT" || "$action" = "BR_MOVE_RIGHT" ]]; then
REPLY="$action"
reply=( "$list[@]" )
break
elif [ "$action" = "QUIT" ]; then
REPLY=-1
reply=( "$list[@]" )
break
elif [ "$action" = "REDRAW" ]; then
zcurses clear "$wname_main" redraw
# Status window is optional (2/2)
[ -n "$wname_status" ] && zcurses clear "$wname_status" redraw
elif [ "$action" = "SUBWIN" ]; then
REPLY="$action"
reply=( "$list[@]" )
break
fi
done
# vim: set filetype=zsh: