From a80a3557e46177feebf6780928647c1e204615f8 Mon Sep 17 00:00:00 2001 From: Timo Reymann Date: Wed, 21 Aug 2024 08:13:04 +0200 Subject: [PATCH] feat(#31): Read from tty device instead of stdin --- src/prompts.sh | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/prompts.sh b/src/prompts.sh index 0f24b4e..5f0745d 100644 --- a/src/prompts.sh +++ b/src/prompts.sh @@ -2,10 +2,14 @@ # @name Prompts # @brief Inquirer.js inspired prompts +_read_stdin() { + read $@ &2; } @@ -17,10 +21,10 @@ _key_input() { local ESC=$'\033' local IFS='' - read -rsn1 a + _read_stdin -rsn1 a # is the first character ESC? if [[ "$ESC" == "$a" ]]; then - read -rsn2 b + _read_stdin -rsn2 b fi local input="${a}${b}" @@ -83,7 +87,7 @@ _increment_selected() { # text=$(with_validate 'input "Please enter at least one character and confirm with enter"' validate_present) input() { _prompt_text "$1"; echo -en "\033[36m\c" >&2 - read -r text + _read_stdin -r text echo -n "${text}" } @@ -111,7 +115,7 @@ confirm() { do echo -e "\033[1D\c " >&2 # shellcheck disable=SC2162 - read -n1 result + _read_stdin -n1 result case "$result" in y|Y) echo -n 1; break; ;; @@ -276,7 +280,7 @@ password() { echo -en "\033[36m" >&2 local password='' local IFS= - while read -r -s -n1 char; do + while _read_stdin -r -s -n1 char; do # ENTER pressed; output \n and break. [[ -z "${char}" ]] && { printf '\n' >&2; break; } # BACKSPACE pressed; remove last character