Skip to content

Commit 1538998

Browse files
committed
lib/zfsbootmenu-core: allow timed_prompt to delay indefinitely
1 parent e3b1ff3 commit 1538998

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

zfsbootmenu/lib/zfsbootmenu-core.sh

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,22 +1261,31 @@ has_resume_device() {
12611261
}
12621262

12631263
# getopts arguments:
1264-
# -d prompt countdown/delay
1265-
# -p prompt with countdown timer (%0.xd)
1264+
# -d prompt countdown/delay; non-positive values will wait forever
1265+
# -p prompt with countdown timer (use %0.Xd as a placeholder for time)
12661266
# -m+ message to be printed above the prompt, usable multiple times
12671267
# -r message to be prefixed with [RETURN] (accept)
12681268
# -e message to be prefixed with [ESCAPE] (reject)
12691269
#
1270-
# -m, -r, -e are print in the order they are passed into the function
1270+
# -m, -r, -e are printed in the order they are passed to the function
12711271

12721272
timed_prompt() {
1273-
local prompt delay message opt OPTIND
1273+
local prompt delay message infinite opt OPTIND
12741274

1275+
infinite=
12751276
while getopts "d:p:m:r:e:" opt; do
12761277
case "${opt}" in
12771278
d)
1278-
delay="${OPTARG}"
1279-
[ "${delay}" -gt 0 ] || return 0
1279+
delay="${OPTARG:-0}"
1280+
if [ "${delay}" -gt 0 ] >/dev/null 2>&1; then
1281+
:
1282+
elif [ "${delay}" -le 0 ] >/dev/null 2>&1; then
1283+
delay="30"
1284+
infinite="yes"
1285+
else
1286+
zdebug "delay argument for timed_prompt is not numeric"
1287+
return 0
1288+
fi
12801289
;;
12811290
p)
12821291
prompt="${OPTARG}"
@@ -1295,8 +1304,12 @@ timed_prompt() {
12951304
esac
12961305
done
12971306

1298-
[ -n "${delay}" ] || delay="30"
1299-
[ -n "${prompt}" ] || prompt="Press $( colorize green "[RETURN]") or wait $( colorize yellow "%0.${#delay}d" ) seconds to continue"
1307+
[ "${delay:-0}" -gt 0 ] >/dev/null 2>&1 || delay="30"
1308+
1309+
if [ -z "${prompt}" ]; then
1310+
prompt="Press $( colorize green "[RETURN]") to continue"
1311+
[ -z "${infinite}" ] && prompt+=" or wait $( colorize yellow "%0.${#delay}d" ) seconds"
1312+
fi
13001313

13011314
# Add a blank line between any messages and the prompt
13021315
message+=( "" )
@@ -1325,6 +1338,10 @@ timed_prompt() {
13251338
shift
13261339
done
13271340

1341+
local readargs
1342+
readargs=( -s -N 1 )
1343+
[ -z "${infinite}" ] && readargs+=( -t 1 )
1344+
13281345
for (( i=delay; i>0; i-- )); do
13291346
# shellcheck disable=SC2059
13301347
mes="$( printf "${prompt}" "${i}" )"
@@ -1335,14 +1352,17 @@ timed_prompt() {
13351352
echo -ne "${mes}"
13361353

13371354
# shellcheck disable=SC2162
1338-
IFS='' read -s -N 1 -t 1 key
1355+
IFS='' read "${readargs[@]}" key
13391356
# escape key
13401357
if [ "$key" = $'\e' ]; then
13411358
return 1
13421359
# enter key
13431360
elif [ "$key" = $'\x0a' ]; then
13441361
return 0
13451362
fi
1363+
1364+
# If delay is infinite, don't let it tick down
1365+
[ -n "${infinite}" ] && i=$(( i + 1 ))
13461366
done
13471367

13481368
return 0

0 commit comments

Comments
 (0)