Skip to content

Commit 953c724

Browse files
committed
Richer color handling in timed_prompt (formerly warning_prompt)
1 parent eb39ea6 commit 953c724

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

90zfsbootmenu/zfsbootmenu-lib.sh

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -729,35 +729,45 @@ has_resume_device() {
729729
return 1
730730
}
731731

732-
# arg1: warning message
733-
# arg2: persistence in seconds (optional, default 30)
732+
# arg1..argN: lines of warning message
734733
# prints: warning message
735-
# returns: nothing
734+
# returns: 1 if user pressed ESC, 0 otherwise
736735

737-
warning_prompt() {
738-
local prompt x y
736+
timed_prompt() {
737+
local prompt x y cnum
739738

740739
[ $# -gt 0 ] || return
741740
[ -n "${delay}" ] || delay="30"
742741
[ -n "${prompt}" ] || prompt="Press [ENTER] or wait %0.2d seconds to continue"
743742

743+
# shellcheck disable=SC2154
744+
case "${color}" in
745+
red) cnum=1 ;;
746+
green) cnum=2 ;;
747+
yellow) cnum=3 ;;
748+
blue) cnum=4 ;;
749+
magenta) cnum=5 ;;
750+
cyan) cnum=6 ;;
751+
*) cnum="" ;;
752+
esac
753+
744754
tput civis
745755
HEIGHT=$( tput lines )
746756
WIDTH=$( tput cols )
747757
tput clear
748758

749759
x=$(( (HEIGHT - 0) / 2))
750760

761+
[ -n "${cnum}" ] && tput setaf "${cnum}"
751762
while [ $# -gt 0 ]; do
752763
local line=${1}
753764
y=$(( (WIDTH - ${#line}) / 2 ))
754765
tput cup $x $y
755-
line="${line//\[/\\033\[0;32m\[}"
756-
line="${line//\]/\]\\033\[0m}"
757766
echo -n -e "${line}"
758767
x=$(( x + 1 ))
759768
shift
760769
done
770+
[ -n "${cnum}" ] && tput sgr0
761771

762772
IFS=''
763773
for (( i=delay; i>0; i-- )); do
@@ -853,8 +863,8 @@ set_rw_pool() {
853863
pool="${1}"
854864
[ -n "${pool}" ] || return 1
855865

856-
if [ -r "${BASE}/degraded" ] && grep -q "${pool}" "${BASE}/degraded"; then
857-
warning_prompt "Operation prohibited: ZFSBootMenu cannot import '${pool}' read-write" "10"
866+
if grep -q "${pool}" "${BASE}/degraded" >/dev/null 2>&1; then
867+
color=red delay=10 timed_prompt "Operation prohibited" "Pool '${pool}' cannot be imported read-write"
858868
return 1
859869
fi
860870

90zfsbootmenu/zfsbootmenu.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ while IFS=$'\t' read -r _pool _property; do
102102
done <<<"$( zpool get all -H -o name,property )"
103103

104104
if [ "${unsupported}" -ne 0 ]; then
105-
warning_prompt "Unsupported features detected, upgrade ZFS modules in ZFSBootMenu"
105+
color=red timed_prompt "Unsupported features detected" "Upgrade ZFS modules in ZFSBootMenu"
106106
fi
107107

108108
# Attempt to find the bootfs property
@@ -121,7 +121,7 @@ if [[ -n "${BOOTFS}" ]]; then
121121
# Draw a countdown menu
122122
# shellcheck disable=SC2154
123123
if [[ ${menu_timeout} -gt 0 ]]; then
124-
if delay=10 prompt="Booting in %0.2d seconds" warning_prompt "[ENTER] to boot ${BOOTFS}" "[ESC] boot menu" ; then
124+
if delay="${menu_timeout}" prompt="Booting in %0.2d seconds" timed_prompt "[ENTER] to boot ${BOOTFS}" "[ESC] boot menu" ; then
125125
# Clear screen before a possible password prompt
126126
tput clear
127127
if ! key_wrapper "${BOOTFS}" ; then
@@ -262,8 +262,8 @@ while true; do
262262
if [ "${leftover_space}" -le 0 ]; then
263263
avail_space="$( zfs list -H -o available "${selected_snap%/*}" )"
264264
be_size="$( zfs list -H -o refer "${selected_snap}" )"
265-
delay=10 warning_prompt "Insufficient space in '${selected_snap%/*}' for duplication" \
266-
"${avail_space} available, ${be_size} needed"
265+
color=red delay=10 timed_prompt "Insufficient space for duplication" \
266+
"'${selected_snap%/*}' has ${avail_space} free but needs ${be_size}"
267267
continue
268268
fi
269269
;;

0 commit comments

Comments
 (0)