Skip to content

Commit dfbd12a

Browse files
committed
New function to read user input that works cross platform
1 parent 12b6ddf commit dfbd12a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

EasyKey.git/ezk-locations.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ wait_for_keypress() {
44
stty -raw
55
}
66

7-
wait_for_keypress
7+
echo -n "Make your choice: " && wait_for_keypress
88
echo "$REPLY"

shellmenu.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ choice () {
484484
echo
485485
echo "Press 'q' to quit"
486486
echo
487-
read -p "Make your choice: " -n 1 -r
487+
echo -n "Your choice: " && wait_for_keypress
488488
echo
489489

490490
if [[ $REPLY == "q" ]]; then
@@ -495,7 +495,7 @@ choice () {
495495
importantLog "Huh ($REPLY)?"
496496
fi
497497
if $waitstatus; then
498-
read -p $'\n<Press any key to return>' -n 1 -r
498+
echo -n "<Press any key to return>" && wait_for_keypress
499499
else
500500
waitonexit # back to default after method execution
501501
fi
@@ -624,3 +624,15 @@ draw_rounded_square() {
624624

625625
}
626626

627+
######################################################
628+
# Reading single key input that works on most
629+
# shells. Usage example:
630+
# echo -n "Your choice: " && wait_for_keypress
631+
# Outputs:
632+
# REPLY - contains user selection (the key pressed)
633+
######################################################
634+
wait_for_keypress() {
635+
stty raw
636+
REPLY=$(dd bs=1 count=1 2> /dev/null)
637+
stty -raw
638+
}

0 commit comments

Comments
 (0)