It's still the same as what I posted earlier. A bit longer than it has to be because of all the ifs and comments. I like the way you used parameter expansion in your version, but your version didn't scroll right on my system - an ordinary terminal emulator running Bash 5 on a Debian Linux.
Here my currently final version again:
function askYesOrNo {
printf "\e[1;31;47m$1 [Y|n]\e[0m " # print $1 in color
printf "\e[s" # save cursor x-pos
while true ; do
read answer
if [ -z "$answer" ] ; then
return 0
elif [ "$(tr -d "NnYy" <<< $answer)" != "$answer" ] ; then
break
fi
# How many lines in this window?
LINES=$(tput lines)
# find cursor y-position ( line number )
printf "\e[6n" ; read -sd R POS
CURPOS=${POS#*[}; CUR_Y=${CURPOS%;*}
if [ "$CUR_Y" -eq "$LINES" ] ; then
# printf "\e[u\e[1A\e[K"
printf "\e[u\eM\e[K"
else
printf "\e[u\e[K"
fi
done
if [ "${answer^}" == "Y" ]; then
return 0
fi
return 1
}
for (( i = 100, max = 110; i <= max; i++ )) ; do
printf " %d\n" $i
if [ $i -eq $max ] ; then
if askYesOrNo "Do you wish to continue?" ; then
(( max += 10 ))
fi
fi
done
Only complaint now is that when the question is asked at the very bottom of the window the cursor still jumps to the next line, the window scrolls but the cursor jumps back to the end of the question. That can be avoided by using read -n 1
- as wisecracker suggested - but I like to use the Enter key to confirm.
Does it run the same way on your system?