-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlists.sh
More file actions
277 lines (256 loc) · 8.79 KB
/
lists.sh
File metadata and controls
277 lines (256 loc) · 8.79 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env bash
# Multi-UI Scripting - List Selection Functions
# https://github.com/lunarcloud/script-dialog
# LGPL-2.1 license
# Variables set in init.sh and used here
# shellcheck disable=SC2034 # line variable in read loops
# shellcheck disable=SC2154
#######################################
# Display a list of multiply-selectable items
# GLOBALS:
# GUI_ICON
# GUI_TITLE
# XDG_ICO_QUESTION
# QUESTION_SYMBOL
# INTERFACE
# RECMD_LINES
# RECMD_COLS
# RECMD_SCROLL
# APP_NAME
# ACTIVITY
# ZENITY_ICON_ARG
# ZENITY_HEIGHT (optional)
# ZENITY_WIDTH (optional)
# ARGUMENTS:
# The file whose text to display
# Number of options
# First item's value
# First item's description
# First item's default checked status (ON or OFF)
# (repeat $3, $4, $5 for all items)
# OUTPUTS:
# Array of selected items
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function checklist() {
if [ -z ${GUI_ICON+x} ]; then
GUI_ICON=$XDG_ICO_QUESTION
fi
_calculate-gui-title
TEXT=$1
NUM_OPTIONS=$2
shift
shift
_calculate-tui-size
# Adjust height to account for the number of options
# Each option takes 1 line, plus we need space for prompt and UI elements
local needed_lines=$((NUM_OPTIONS + 6)) # 6 lines for prompt, borders, and buttons
# Dialog interface displays the text in the body, whiptail shows it in the title
# Add extra lines for dialog to show the message text
if [ "$INTERFACE" == "dialog" ]; then
local text_lines=0
while IFS= read -r line; do
text_lines=$((text_lines + 1))
done <<< "$TEXT"
needed_lines=$((needed_lines + text_lines))
fi
if [ "$needed_lines" -gt "$RECMD_LINES" ]; then
RECMD_LINES=$needed_lines
# Enforce maximum constraint
if [ "$RECMD_LINES" -gt "$MAX_LINES" ]; then
RECMD_LINES=$MAX_LINES
RECMD_SCROLL=true
fi
fi
local exit_status=0
if [ "$INTERFACE" == "whiptail" ]; then
# shellcheck disable=SC2046 # Intentional word splitting for conditional argument
mapfile -t CHOSEN_ITEMS < <( whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --checklist "${QUESTION_SYMBOL}$TEXT" "$RECMD_LINES" "$RECMD_COLS" "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3)
exit_status=$?
elif [ "$INTERFACE" == "dialog" ]; then
local DIALOG_OUTPUT
# shellcheck disable=SC2046 # Intentional word splitting for conditional argument
DIALOG_OUTPUT=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --separate-output --checklist "${QUESTION_SYMBOL}$TEXT" "$RECMD_LINES" "$RECMD_COLS" "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3)
exit_status=$?
IFS=$'\n' read -r -d '' -a CHOSEN_LIST < <( echo "${DIALOG_OUTPUT[@]}" )
local CHOSEN_ITEMS=()
for value in "${CHOSEN_LIST[@]}"
do
CHOSEN_ITEMS+=( "\"$value\"" )
done
elif [ "$INTERFACE" == "zenity" ]; then
OPTIONS=()
while test ${#} -gt 0; do
if [ "$3" == "ON" ]; then
OPTIONS+=("TRUE")
else
OPTIONS+=("FALSE")
fi
OPTIONS+=("$1")
OPTIONS+=("$2")
shift
shift
shift
done
local ZENITY_OUTPUT
ZENITY_OUTPUT=$(zenity --title "$GUI_TITLE" "$ZENITY_ICON_ARG" "$GUI_ICON" --height="${ZENITY_HEIGHT-512}" ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --list --text "$TEXT" --checklist --column "" --column "Value" --column "Description" "${OPTIONS[@]}")
exit_status=$?
IFS=$'|' read -r -d '' -a CHOSEN_LIST < <( echo "$ZENITY_OUTPUT" )
local CHOSEN_ITEMS=()
for value in "${CHOSEN_LIST[@]}"
do
CHOSEN_ITEMS+=( "\"$value\"" )
done
elif [ "$INTERFACE" == "kdialog" ]; then
mapfile -t CHOSEN_ITEMS < <( kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --checklist "$TEXT" "$@")
exit_status=$?
else
printf "%s\n $TEXT:\n" "${QUESTION_SYMBOL}$ACTIVITY" 3>&1 1>&2 2>&3
local CHOSEN_ITEMS=()
while test ${#} -gt 0; do
if yesno "$2 (default: $3)?"; then
CHOSEN_ITEMS+=( "\"$1\"" )
elif [ "$3" == "ON" ]; then
CHOSEN_ITEMS+=( "\"$1\"" )
fi
shift
shift
shift
done
fi
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
echo "${CHOSEN_ITEMS[@]}"
}
#######################################
# Display a list of singularly-selectable items
# GLOBALS:
# GUI_ICON
# GUI_TITLE
# XDG_ICO_QUESTION
# QUESTION_SYMBOL
# INTERFACE
# RECMD_LINES
# RECMD_COLS
# RECMD_SCROLL
# APP_NAME
# ACTIVITY
# ZENITY_ICON_ARG
# ZENITY_HEIGHT (optional)
# ZENITY_WIDTH (optional)
# ARGUMENTS:
# The file whose text to display
# Number of options
# First item's value
# First item's description
# First item's default selected status (ON or OFF)
# (repeat $3, $4, $5 for all items)
# OUTPUTS:
# Value text of the selected item (or the default item)
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function radiolist() {
if [ -z ${GUI_ICON+x} ]; then
GUI_ICON=$XDG_ICO_QUESTION
fi
_calculate-gui-title
TEXT=$1
NUM_OPTIONS=$2
shift
shift
_calculate-tui-size
# Adjust height to account for the number of options
# Each option takes 1 line, plus we need space for prompt and UI elements
local needed_lines=$((NUM_OPTIONS + 6)) # 6 lines for prompt, borders, and buttons
# Dialog interface displays the text in the body, whiptail shows it in the title
# Add extra lines for dialog to show the message text
if [ "$INTERFACE" == "dialog" ]; then
local text_lines=0
while IFS= read -r line; do
text_lines=$((text_lines + 1))
done <<< "$TEXT"
needed_lines=$((needed_lines + text_lines))
fi
if [ "$needed_lines" -gt "$RECMD_LINES" ]; then
RECMD_LINES=$needed_lines
# Enforce maximum constraint
if [ "$RECMD_LINES" -gt "$MAX_LINES" ]; then
RECMD_LINES=$MAX_LINES
RECMD_SCROLL=true
fi
fi
local exit_status=0
if [ "$INTERFACE" == "whiptail" ]; then
# shellcheck disable=SC2046 # Intentional word splitting for conditional argument
CHOSEN_ITEM=$( whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --radiolist "${QUESTION_SYMBOL}$TEXT" "$RECMD_LINES" "$RECMD_COLS" "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3)
exit_status=$?
# For TUI interfaces, empty response indicates cancel
if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
elif [ "$INTERFACE" == "dialog" ]; then
# shellcheck disable=SC2046 # Intentional word splitting for conditional argument
CHOSEN_ITEM=$( dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --radiolist "${QUESTION_SYMBOL}$TEXT" "$RECMD_LINES" "$RECMD_COLS" "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3)
exit_status=$?
# For TUI interfaces, empty response indicates cancel
if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
elif [ "$INTERFACE" == "zenity" ]; then
OPTIONS=()
while test ${#} -gt 0; do
if [ "$3" == "ON" ]; then
OPTIONS+=("TRUE")
else
OPTIONS+=("FALSE")
fi
OPTIONS+=("$1")
OPTIONS+=("$2")
shift
shift
shift
done
CHOSEN_ITEM=$( zenity --title "$GUI_TITLE" "$ZENITY_ICON_ARG" "$GUI_ICON" --height="${ZENITY_HEIGHT-512}" ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --list --text "$TEXT" --radiolist --column "" --column "Value" --column "Description" "${OPTIONS[@]}" 2>/dev/null)
exit_status=$?
# For GUI interfaces, empty response indicates cancel
if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
elif [ "$INTERFACE" == "kdialog" ]; then
CHOSEN_ITEM=$( kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --radiolist "$TEXT" "$@")
exit_status=$?
# For GUI interfaces, empty response indicates cancel
if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
else
echo -e "${QUESTION_SYMBOL}$ACTIVITY: " 3>&1 1>&2 2>&3
OPTIONS=()
local DEFAULT=""
while test ${#} -gt 0; do
local DEFAULT_NOTATION=""
if [ "$3" == "ON" ]; then
DEFAULT+="\"$1\""
DEFAULT_NOTATION="*"
fi
OPTIONS+=("\t- ${underline}$1${normal}$DEFAULT_NOTATION ($2)\n")
shift
shift
shift
done
read -rp "$(echo -e "${OPTIONS[*]}${QUESTION_SYMBOL}${bold}$TEXT: ${normal}")" CHOSEN_ITEM
exit_status=$?
if [[ "$CHOSEN_ITEM" == "" ]]; then
CHOSEN_ITEM="$DEFAULT"
fi
# For CLI interface, only check exit status
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
fi
echo "$CHOSEN_ITEM"
}