-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslib.lzui
318 lines (276 loc) · 10.6 KB
/
syslib.lzui
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
#
# Library file (*.lzui)
#
# System (internal) functions
#
ZUI[syslib_sourced]="1"
# FUNCTION: -zui_sys_get_tfield_cursor_boundaries {{{
# Gets cursor minimum and maximum x position for
# given line and given (via id) text field. Stores
# them into two parameters given by name. Testable,
# returns if the cursor positions could have been
# established.
#
# Example call:
# -zui_sys_get_tfield_cursor_boundaries "cidx_start" "cidx_end" "${list[10]}" "${reply[@]}"
#
# $1 - output parameter name - cursor start index
# $2 - output parameter name - cursor end index
# $3 - current $list (zui-list working variable) element
# $4, ..., $11 - decoded __text field
function -zui_sys_get_tfield_cursor_boundaries() {
local __out1="$1" __out2="$2" __elem="$3" __id="$4"
local -a __output
zui-process-buffer "$__elem"
integer __i __size="${#ZUI_PB_WORDS}" __idx=0
local __seg __text REPLY
for (( __i=1; __i <= __size; __i ++ )); do
__idx+="${#ZUI_PB_SPACES[__i]}"
__seg="${ZUI_PB_WORDS[__i]}"
if -zui_std_is_hyperlink "$__seg"; then
__text="${__seg//(#b)$'\01'[^$'\01']#$'\01'[^$'\01']#$'\01'[^$'\01']#$'\01'[^$'\01']#$'\01'[^$'\02']#$'\02'([^$'\02']#)$'\02'/${(Q)match[1]}}"
-zui_util_strip_codes "$__text"
__idx+="${(m)#REPLY}"
elif -zui_std_decode_text_field "$__seg" __output; then
if [[ "$__id" = "${__output[1]}" ]]; then
# Add characters that precede
if [[ "$__seg" = (#b)(*)$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\02']#$'\02'* ]]; then
__idx+=${(m)#match[1]}
fi
# Minimum x position
: ${(P)__out1::=$__idx}
__text="${(P)__output[4]}"
__text="${__text[${(P)__output[3]},-1]}"
__text="${__text[1,${(P)__output[2]}]}"
__idx+="${(m)#__text}"
# Maximum x position
: ${(P)__out2::=$__idx}
return 0
else
# Add characters that precede and follow
if [[ "$__seg" = (#b)(*)$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\032']#$'\032'[^$'\02']#$'\02'(*) ]]; then
__idx+=${(m)#match[1]}+${(m)#match[2]}
fi
__idx+="${(P)__output[2]}"
fi
elif -zui_std_decode_list_box "$__seg" __output; then
if [[ "$__id" = "${__output[1]}" ]]; then
# Add characters that precede
if [[ "$__seg" = (#b)(*)$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\02']#$'\02'* ]]; then
__idx+=${(m)#match[1]}
fi
# Minimum x position
: ${(P)__out1::=$__idx}
# Just take width
__idx+="${__output[2]}"
# Maximum x position
: ${(P)__out2::=$__idx}
return 0
else
# Add characters that precede and follow
if [[ "$__seg" = (#b)(*)$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\034']#$'\034'[^$'\02']#$'\02'(*) ]]; then
__idx+=${(m)#match[1]}+${(m)#match[2]}
fi
__idx+="${(P)__output[2]}"
fi
elif -zui_std_get_stext "$__seg" __text; then
-zui_util_strip_codes "$__text"
__idx+="${(m)#REPLY}"
else
-zui_util_strip_codes "$__seg"
__idx+="${(m)#REPLY}"
fi
done
__idx+="${#ZUI_PB_SPACES[__i]}"
# Store incorrect data
: ${(P)__out1::=$__idx}
: ${(P)__out2::=$__idx}
return 1
} # }}}
# FUNCTION: -zui_sys_map_cursor_on_string {{{
# Maps visual cursor position onto in-string cursor
# position. Supports characters with double and greater
# width
#
# $1 - string start display index
# $2 - string
# $3 - cursor position
# $4 - output parameter name
function -zui_sys_map_cursor_on_string() {
local __idx="$1" __str="$2" __curx="$3" __var_name="$4"
integer __i __count=${#__str} __width_sum=0
for (( __i = 1; __i <= __count; __i ++ )); do
if (( __width_sum - 1 >= __curx )); then
(( __idx += __i - 1 - 1 ))
: ${(P)__var_name::=$__idx}
return 0
fi
__width_sum+=${(m)#__str[__idx+__i-1]}
done
(( __idx += __i - 1 - 1 ))
: ${(P)__var_name::=$__idx}
return 1
} # }}}
# FUNCTION: -zui_sys_index_tail_width {{{
# Returns index in given string that results
# in given width, when displaying the string
# starting from that index
#
# $1 - string
# $2 - expected width
# $3 - output parameter name for the index
function -zui_sys_index_tail_width() {
local __str="$1" __width="$2" __var_name="$3"
integer __i __count=${#__str} __width_sum=0
for (( __i = __count; __i >= 1; __i -- )); do
__width_sum+=${(m)#__str[__i]}
if (( __width_sum >= __width )); then
: ${(P)__var_name::=$__i}
return 0
fi
done
: ${(P)__var_name::=1}
return 1
} # }}}
# FUNCTION: -zui_sys_gather_lanchors {{{
# Appends mod${1}_ice${2}_lanchors into array given by name
#
# $1 - module index
# $2 - instance index
# $3 - target array name
function -zui_sys_gather_lanchors() {
local __var_name1="mod${1}_ice${2}_lanchors" __var_name2="${3-reply}"
if [[ -z "$1" || -z "$2" ]]; then
echo "-zui_std_gather_lanchors must obtain module and instance indices"
return 1
fi
# Append and store
local -a __output
__output=( "${(P@)__var_name2}" "${(P@)__var_name1}" )
: ${(PA)__var_name2::=${__output[@]}}
} # }}}
# FUNCTION: -zui_sys_decode_list_update {{{
# Deserializes on-the-fly update package.
# To be used rather only internally.
#
# $1 - the package
# $2 - optional target parameter name
function -zui_sys_decode_list_update() {
local -a __segments
[[ -n "$1" ]] && __segments=( "${(z@)1}" ) || __segments=( "" "" 0 0 )
local __var_name="${2-reply}"
: ${(PA)__var_name::="${(Q)__segments[@]}"}
} # }}}
# FUNCTION: -zui_sys_decode_hops {{{
# Deserializes on-the-fly hops substitution.
# For internal usage.
#
# $1 - the hops' package
# $2 - optional target parameter name
function -zui_sys_decode_hops() {
local -a __hops
[[ -n "$1" ]] && __hops=( "${(z@)1}" ) || __hops=( )
local __var_name="${2-reply}"
: ${(PA)__var_name::=${(Q)__hops[@]}}
} # }}}
# FUNCTION: -zui_sys_decode_nonselectables {{{
# Deserializes on-the-fly nonselectables
# substitution. For internal usage.
#
# $1 - the nonselectables' package
# $2 - optional target parameter name
function -zui_sys_decode_nonselectables() {
local -a __nonselectables
[[ -n "$1" ]] && __nonselectables=( "${(z@)1}" ) || __nonselectables=( )
local __var_name="${2-reply}"
: ${(PA)__var_name::="${(Q)__nonselectables[@]}"}
} # }}}
# FUNCTION: -zui_sys_decode_lanchors {{{
# Deserializes on-the-fly local anchors.
# For internal usage.
#
# $1 - the anchors' package
# $2 - optional target parameter name
function -zui_sys_decode_lanchors() {
local -a __lanchors
[[ -n "$1" ]] && __lanchors=( "${(z@)1}" ) || __lanchors=( )
local __var_name="${2-reply}"
: ${(PA)__var_name::="${(Q)__lanchors[@]}"}
} # }}}
# FUNCTION: -zui_sys_add_message {{{
# Appends given message to ZUI_MESSAGES
#
# $1 - message type
# $2 - timestamp
# $3, $4, ... - message bits / lines
function -zui_sys_add_message() {
local tpe="$1" ts="${2:-$(date +%s)}" timestr=""
if [[ -n "${ZUI[log_time_format]}" ]]; then
if (( ${+builtins[strftime]} )); then
strftime -s timestr "${ZUI[log_time_format]}" "$ts"
else
# Slow and not respecting $ts fallback
# but will rescue users with weird zsh
timestr=$(date "+${ZUI[log_time_format]}")
fi
fi
# Clean space-only elements
set -- "${@[3,-1]/(#s)[[:space:]]##(#e)/}"
integer index msg_len=0
for (( index = 1; index <= ${#}; index ++ )); do
msg_len+=${#@[index]}
done
ZUI_MESSAGES+=( "${(q)msg_len} ${(q)tpe} ${(q)ts} ${(q)timestr} ${(j: :)${(q)@}}" )
(( ++ ZUI[message_count] ))
# House-keeping
(( ${#ZUI_MESSAGES} >= ZUI[log_size] + 10 )) && { ZUI_MESSAGES=( "${(@)ZUI_MESSAGES[1+10,-1]}" ); }
} # }}}
# FUNCTION: -zui_sys_get_match_line {{{
# Returns at which index in $__list the search
# result that's currently highlighted is located.
#
# Allows to jump to search result, not only see it.
#
# $__slist - input variable with copy of $__list
# $1 - output variable to hold the established target line number
function -zui_sys_get_match_line() {
local __var_name="${1:-REPLY}" __i
[[ "${#ZUILIST_NONSELECTABLE_ELEMENTS}" -gt 0 ]] && for __i in "${(nO)ZUILIST_NONSELECTABLE_ELEMENTS[@]}"; do
__slist[$__i]=()
done
[[ "${ZUI[UNIQ_MODE]}" -eq 1 ]] && typeset -gU __slist
local search_buffer="${ZUI[SEARCH_BUFFER]%% ##}"
search_buffer="${search_buffer## ##}"
search_buffer="${search_buffer//(#m)[][*?|#~^()><\\]/\\$MATCH}"
if [[ -n "$search_buffer" ]]; then
# Base bit of this pattern is ((#s)[^$'\01']#THEWORD*|*$'\02'[^$'\01']#THEWORD*) - the word occuring not inside hyper link
# Pattern will be: BIT1~^BIT2...
local search_pattern=${search_buffer//(#b)([^ ]##)/(#B)((#s)[^$'\01'$'\032'$'\034']#${match[1]}*|*[$'\02'][^$'\01'$'\032'$'\034']#${match[1]}*)~^}
search_pattern="${search_pattern// ##/}"
search_pattern="${search_pattern%\~\^}"
search_pattern="(#b)(($search_pattern)|(*))"
integer __line=0 __correct=0
local -a __slist2
repeat 1; do
__slist=( "${(@)__slist//(#i)$~search_pattern/$(( ++ __line, ${#match[2]} > 0 ? __line : 0 ))}" )
__slist=( "${(@)__slist:#0}" )
__slist2=( "${(@)__slist:#}" )
done
# Use the same element but from line-number-only array
__line=${__slist2[${ZUI[CURRENT_IDX]}]}
# Correct for possible empty entries that weren't counted
# because // substitution ignores empty elements, always
integer __index=${__slist[(i)$__line]}
local -a __empty
__empty=( "${(@M)__slist[1,__index]:#}" )
__line+=${#__empty}
[[ "${ZUI[UNIQ_MODE]}" -ne 1 ]] && [[ "${#ZUILIST_NONSELECTABLE_ELEMENTS}" -gt 0 ]] && for __i in "${(no)ZUILIST_NONSELECTABLE_ELEMENTS[@]}"; do
(( __i <= __line + __correct )) && __correct+=1
done
__line+=__correct
: ${(P)__var_name::=$__line}
return 0
fi
return 1
} # }}}
# vim:ft=zsh