-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.sh
More file actions
258 lines (192 loc) · 6.76 KB
/
Copy pathselect.sh
File metadata and controls
258 lines (192 loc) · 6.76 KB
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
#!/usr/bin/bash
if [ -n "$select_isSourced" ]; then return; fi
. "validate.sh" && validate_shellVersion
declare -gri select_isSourced=1
################################################################################
# Screen #######################################################################
select_getScreen_dimensions() {
local dimensions=()
IFS='[;' read -rs -p $'\e[18t' -d 't' -a dimensions
echo "${dimensions[@]:2}"
return 0
} ##############################################################################
select_getScreen_maxLines() {
local dimensions="$(select_getScreen_dimensions)"
echo "${dimensions%% *}"
return 0
} ##############################################################################
select_getScreen_maxCols() {
local dimensions="$(select_getScreen_dimensions)"
echo "${dimensions##* }"
return 0
} ##############################################################################
select_setScreen_clean() {
echo -ne "\033[2J"
return 0
} ##############################################################################
select_setScreen_new() {
echo -ne "\033[?1049h"
stty -echo
tput civis
return 0
} ##############################################################################
select_setScreen_old() {
tput cnorm
stty sane
echo -ne "\033[?1049l"
return 0
} ##############################################################################
################################################################################
################################################################################
# Cursor ######################################################################
select_getCursor_position() {
local position=()
IFS='[;' read -rs -p $'\e[6n' -d 'R' -a position
echo "${position[@]:1}"
return 0
} ##############################################################################
select_getCursor_line() {
local position="$(select_getCursor_position)"
echo "${position%% *}"
return 0
} ##############################################################################
select_getCursor_col() {
local position="$(select_getCursor_position)"
echo "${position##* }"
return 0
} ##############################################################################
select_setCursor_line() {
local line="$1"
local maxLines="$(select_getScreen_maxLines)"
( [[ ! "$line" =~ ^[0-9]+$ ]] || (( "$line" > "$maxLines" )) ) && \
readarray -t select_error_message <<< "$(log_fatal -f \
"Line must be in the range of [1, ${maxLines}]: ${line}")" && \
exit 1
(( "$line" == 0 )) && line=1
echo -ne "\033[${line};0H"
return 0
} ##############################################################################
select_setCursor_text() {
local add_format=0
while (( $# > 0 )); do
case "$1" in
"-f"|"--add-format")
add_format=1
shift;;
*)
break;;
esac
done
(( add_format )) && echo -ne "\033[0m\033[47m\033[30m\033[1m${@}\033[0m" && \
return 0
echo -ne "\033[0m${@}"
return 0
} ##############################################################################
################################################################################
################################################################################
# Clean ########################################################################
select_clean() {
(( select_isCleaned )) && return 1
select_setScreen_old
sh_restoreOptions "$1"
select_isCleaned=1
(( ${#select_error_message[@]} != 0 )) && \
for i in "${select_error_message[@]}"; do
echo -e "$i" >&2
done
return 0
} ##############################################################################
select_clean_trap() {
select_clean "$@" || exit 1
trap '' EXIT SIGTERM SIGINT SIGHUP
exit 1
} ##############################################################################
################################################################################
################################################################################
# Select #######################################################################
select_text() {
. "sh.sh"
. "log.sh"
local shellOptions_old_varName="${FUNCNAME[0]}_shellOptions_old"
sh_saveOptions "$shellOptions_old_varName"
sh_restoreOptions sh_initialOptions
set -eu -o pipefail
select_setScreen_new
trap "select_clean_trap ${shellOptions_old_varName}" \
EXIT SIGTERM SIGINT SIGHUP
select_isCleaned=0
select_error_message=()
select_selection=""
local texts=("$@")
local normalizedTexts=("$@")
local maxLines="$(select_getScreen_maxLines)"
local maxCols="$(select_getScreen_maxCols)"
local line=1
local line_old=1
local direction=""
local maxPages="$((${#normalizedTexts[@]} / maxLines))"
local page=0
local page_topText_i=0
local page_maxLines=0
(( ${#normalizedTexts[@]} == 0 )) && log_fatal -f "Text is required"
for (( i = 0; i < ${#normalizedTexts[@]}; ++i )); do
normalizedTexts[i]="${normalizedTexts[i]::maxCols}"
done
(( maxPages * maxLines != ${#normalizedTexts[@]} )) && \
maxPages="$((maxPages + 1))"
while :; do
page_topText_i="$((page * maxLines))"
line=1
line_old=1
page_maxLines=0
select_setScreen_clean
select_setCursor_line 1
for i in "${normalizedTexts[@]: page_topText_i: maxLines}"; do
page_maxLines="$((page_maxLines + 1))"
select_setCursor_line "$page_maxLines"
select_setCursor_text "$i"
done
select_setCursor_line 1
select_setCursor_text -f "${normalizedTexts[page_topText_i]}"
while :; do
line_old="$line"
read -rs -n 1 direction
case "$direction" in
"j")
(( line == page_maxLines )) && continue
line="$((line + 1))";;
"k")
(( line == 1 )) && continue
line="$((line - 1))";;
"g")
(( line == 1 )) && continue
line=1;;
"G")
(( line == page_maxLines )) && continue
line="$page_maxLines";;
"h")
(( page == 0 )) && continue
page="$((page - 1))"
continue 2;;
"l")
(( page == maxPages - 1 )) && continue
page="$((page + 1))"
continue 2;;
"")
select_selection="${texts[page_topText_i + line - 1]}"
break 2;;
"q")
break 2;;
*)
continue;;
esac
select_setCursor_line "$line_old"
select_setCursor_text "${normalizedTexts[page_topText_i + line_old - 1]}"
select_setCursor_line "$line"
select_setCursor_text -f "${normalizedTexts[page_topText_i + line - 1]}"
done
done
select_clean "$shellOptions_old_varName"
return 0
} ##############################################################################
################################################################################