Skip to content

Commit

Permalink
read-key: rejig some lines for internal clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Oct 9, 2024
1 parent c7f31b1 commit ba73e79
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions commands/read-key
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function read_key() (
}

# process
local item option_timeout='' option_keep_line_buffer_newlines='no'
local item option_timeout='' subsequent_timeout='0.0001' option_keep_line_buffer_newlines='no'
while test "$#" -ne 0; do
item="$1"
shift
Expand All @@ -240,7 +240,10 @@ function read_key() (
if ! is-number -- "$option_timeout"; then
option_timeout=600 # ten minutes
fi

# bash v3 compat
option_timeout="$(__get_read_decimal_timeout "$option_timeout")"
subsequent_timeout="$(__get_read_decimal_timeout "$subsequent_timeout")"

# =====================================
# Action
Expand Down Expand Up @@ -275,8 +278,7 @@ function read_key() (
}
function read_and_flush {
# read
local status=0 mini_timeout
mini_timeout=$(__get_read_decimal_timeout '0.0001') # bash v3 compat
local status=0
IFS='' read -rsn1 -t "$option_timeout" input || status=$?
if test "$status" -eq 0; then
add "$input"
Expand All @@ -285,7 +287,7 @@ function read_key() (
# -r allows backslash key [\] to be kept
# -s prevents the input from being echoed
# -n1 reads only one character, which is necessary surprisingly to read non-printable characters
if ! IFS='' read -rsn1 -t "$mini_timeout" input; then
if ! IFS='' read -rsn1 -t "$subsequent_timeout" input; then
break
fi
add "$input"
Expand Down

0 comments on commit ba73e79

Please sign in to comment.