From a2113444d130e2319c83923b684b21b3b9f9a002 Mon Sep 17 00:00:00 2001 From: anseki Date: Mon, 2 Mar 2015 02:07:28 +0900 Subject: [PATCH] Use `read -s` if it is supported. --- lib/read.sh | 20 +++++++++++++++++--- package.json | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/read.sh b/lib/read.sh index 2f4eafc..f974814 100644 --- a/lib/read.sh +++ b/lib/read.sh @@ -1,13 +1,27 @@ -if [ "$1" = "noechoback" ]; then +silentRead() { + # `typeset` doesn't work in `func()` of ksh. + # `function fnc` for local-var of ksh is not compatible. + # Therefore, `_input` is not local-var on ksh. + local _input="" 2>/dev/null || typeset _input="" stty --file=/dev/tty -echo echonl 2>/dev/null || \ stty -F /dev/tty -echo echonl 2>/dev/null || \ stty -f /dev/tty -echo echonl 2>/dev/null || \ exit 1 - IFS= read -r INPUT /dev/null || \ stty -F /dev/tty echo -echonl 2>/dev/null || \ stty -f /dev/tty echo -echonl 2>/dev/null - # printf '\n' >/dev/tty + printf '%s' "$_input" +} + +if [ "$1" = "noechoback" ]; then + # Try `-s` option. *ksh have it that not `--silent`. Therefore, don't try it. + if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then + IFS= read -rs INPUT /dev/null || INPUT=`silentRead` || exit 1 + printf '\n' >/dev/tty + else + INPUT=`silentRead` || exit 1 + fi else IFS= read -r INPUT