readline-sync/lib/read.sh

39 lines
1,007 B
Bash
Raw Normal View History

2015-03-12 03:03:04 +01:00
# Silent Read : emulate `read -s` of bash/zsh
2015-03-05 09:39:46 +01:00
read_s() {
2015-03-01 18:00:20 +01:00
stty --file=/dev/tty -echo echonl 2>/dev/null || \
stty -F /dev/tty -echo echonl 2>/dev/null || \
2015-03-03 18:30:41 +01:00
stty -f /dev/tty -echo echonl || exit 1
2015-03-11 09:06:27 +01:00
IFS= read -r input </dev/tty || exit 1
2015-03-01 18:00:20 +01:00
stty --file=/dev/tty echo -echonl 2>/dev/null || \
stty -F /dev/tty echo -echonl 2>/dev/null || \
2015-03-03 18:30:41 +01:00
stty -f /dev/tty echo -echonl || exit 1
2015-03-01 18:07:28 +01:00
}
2015-03-11 09:06:27 +01:00
# getopt(s)
while [ $# -ge 1 ]; do
case "$1" in
"--noechoback") noechoback=1;;
"--keyin") keyin=1;;
"--display") shift; display=$1;;
esac
shift
done
if [ -n "$display" ]; then
printf '%s' "$display" >/dev/tty
fi
if [ "$noechoback" = "1" ]; then
2015-03-01 18:07:28 +01:00
# Try `-s` option. *ksh have it that not `--silent`. Therefore, don't try it.
if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then
2015-03-11 09:06:27 +01:00
IFS= read -rs input </dev/tty 2>/dev/null && printf '\n' >/dev/tty || read_s
2015-03-01 18:07:28 +01:00
else
2015-03-05 09:39:46 +01:00
read_s
2015-03-01 18:07:28 +01:00
fi
2014-07-12 00:37:25 +02:00
else
2015-03-11 09:06:27 +01:00
IFS= read -r input </dev/tty || exit 1
2014-07-12 00:37:25 +02:00
fi
2015-03-11 09:06:27 +01:00
printf '%s' "'$input'"
2014-06-27 03:08:05 +02:00
exit 0