feat(#31): Read from tty device instead of stdin

This commit is contained in:
Timo Reymann
2024-08-21 08:13:04 +02:00
parent c0af934f6d
commit a80a3557e4
+10 -6
View File
@@ -2,10 +2,14 @@
# @name Prompts # @name Prompts
# @brief Inquirer.js inspired prompts # @brief Inquirer.js inspired prompts
_read_stdin() {
read $@ </dev/tty
}
_get_cursor_row() { _get_cursor_row() {
local IFS=';' local IFS=';'
# shellcheck disable=SC2162,SC2034 # shellcheck disable=SC2162,SC2034
read -sdR -p $'\E[6n' ROW COL; _read_stdin -sdR -p $'\E[6n' ROW COL;
echo "${ROW#*[}"; echo "${ROW#*[}";
} }
_cursor_blink_on() { echo -en "\033[?25h" >&2; } _cursor_blink_on() { echo -en "\033[?25h" >&2; }
@@ -17,10 +21,10 @@ _key_input() {
local ESC=$'\033' local ESC=$'\033'
local IFS='' local IFS=''
read -rsn1 a _read_stdin -rsn1 a
# is the first character ESC? # is the first character ESC?
if [[ "$ESC" == "$a" ]]; then if [[ "$ESC" == "$a" ]]; then
read -rsn2 b _read_stdin -rsn2 b
fi fi
local input="${a}${b}" 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) # text=$(with_validate 'input "Please enter at least one character and confirm with enter"' validate_present)
input() { input() {
_prompt_text "$1"; echo -en "\033[36m\c" >&2 _prompt_text "$1"; echo -en "\033[36m\c" >&2
read -r text _read_stdin -r text
echo -n "${text}" echo -n "${text}"
} }
@@ -111,7 +115,7 @@ confirm() {
do do
echo -e "\033[1D\c " >&2 echo -e "\033[1D\c " >&2
# shellcheck disable=SC2162 # shellcheck disable=SC2162
read -n1 result _read_stdin -n1 result
case "$result" in case "$result" in
y|Y) echo -n 1; break; ;; y|Y) echo -n 1; break; ;;
@@ -276,7 +280,7 @@ password() {
echo -en "\033[36m" >&2 echo -en "\033[36m" >&2
local password='' local password=''
local IFS= local IFS=
while read -r -s -n1 char; do while _read_stdin -r -s -n1 char; do
# ENTER pressed; output \n and break. # ENTER pressed; output \n and break.
[[ -z "${char}" ]] && { printf '\n' >&2; break; } [[ -z "${char}" ]] && { printf '\n' >&2; break; }
# BACKSPACE pressed; remove last character # BACKSPACE pressed; remove last character