Skip to content

Commit

Permalink
ask/choose/confirm: fix broken tests, although ask test has a clear…
Browse files Browse the repository at this point in the history
…ing issue when testing

get-terminal-position-support: fix crash when stdin not accessible

get-terminal-tty-support: expand debug information to look for ways to ensure ask is only clearing input if it needed to

is-interactive: simplify conditional logic

read-key, is-shapeshifter: add (ctrl|alt)+(left|right) macos keys

dorothy, dorothy-warnings: only output warnings by default if there are new warnings
  • Loading branch information
balupton committed Oct 24, 2024
1 parent 099bb6b commit 0dfebda
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 80 deletions.
4 changes: 4 additions & 0 deletions commands.beta/is-shapeshifter
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ function is_shapeshifter_test() (
$'\e[2~' # insert
$'\e[3~' # delete
$'\e[5~' # page-up
$'\e[1;5D' # page-up
$'\eb' # page-up
$'\e[6~' # page-down
$'\e[1;5C' # page-down
$'\ef' # page-down
$'\177' # backspace
$'\b' # backspace
# $'\n' # enter <-- don't do enter, as that is normal
Expand Down
6 changes: 4 additions & 2 deletions commands/ask
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function ask_test() (
eval-tester --name='receive default response by timeout and no input' --stdout='a default response' \
-- ask --skip-default --question='What is your response?' --default='a default response'

local down=$'\e[B'

{
sleep 3
__print_line
Expand Down Expand Up @@ -51,7 +53,7 @@ function ask_test() (

# move down and change to custom response
sleep 3
printf $'\eOB'
__print_string "$down"
sleep 3
__print_line

Expand Down Expand Up @@ -289,7 +291,7 @@ function ask_() (
# treat empty string and newline as default
:
elif [[ $__input_result =~ ^[[:cntrl:][:space:]]*$ ]]; then
# if it is only control characters, e.g. escape, and whitespace characters, then treat as empty
# if it is only control characters (e.g. escape) and whitespace characters, then treat as empty
RESULT=''
else
# treat everything else as manual __input_result
Expand Down
45 changes: 25 additions & 20 deletions commands/choose
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function choose_test() (

local up=$'\e[A' down=$'\e[B' right=$'\e[C' left=$'\e[D' home=$'\e[H' end=$'\e[F' insert=$'\e[2~' delete=$'\e[3~' page_up=$'\e[5~' page_down=$'\e[6~' backspace=$'\177' escape=$'\e' tab=$'\t' backtab=$'\e[Z' all=$'\x01' # enter=$'\x0a' space=' '

local \
timeout_optional='[ timed out: not required ]' \
timeout_required='[ input failure: timed out: required ]' \
timeout_defaults='[ timed out: used default ]'

# TESTS WITHOUT INTERACTION
# as we are checking stderr, do not use color for these tests, as gets too complicated

Expand All @@ -29,66 +34,66 @@ function choose_test() (
-- env COLOR=no choose 'q' 'd' --multi --index --skip-default --default=b --default=c -- a b c

# timeout optional
eval-tester --name='receive no response by timeout with no input and optional' --stderr='q [timed out: not required]' \
eval-tester --name='receive no response by timeout with no input and optional' --stderr="q $timeout_optional" \
-- env COLOR=no choose 'q' 'd' --timeout=5 -- a b c

eval-tester --name='receive no --index response by timeout with no input and optional' --stderr='q [timed out: not required]' \
eval-tester --name='receive no --index response by timeout with no input and optional' --stderr="q $timeout_optional" \
-- env COLOR=no choose 'q' 'd' --index --timeout=5 -- a b c

# timeout required
eval-tester --name='receive timeout response by timeout with no input and reqyured' --status='60' --stderr='q [input failure: timed out: required]' \
eval-tester --name='receive timeout response by timeout with no input and reqyured' --status='60' --stderr="q $timeout_required" \
-- env COLOR=no choose 'q' 'd' --required --timeout=5 -- a b c

eval-tester --name='receive timeout --index response by timeout with no input and reqyured' --status='60' --stderr='q [input failure: timed out: required]' \
eval-tester --name='receive timeout --index response by timeout with no input and reqyured' --status='60' --stderr="q $timeout_required" \
-- env COLOR=no choose 'q' 'd' --index --required --timeout=5 -- a b c

# single mode, single defalut
eval-tester --name='receive default response by timeout with no input and optional' --stdout=b --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default response by timeout with no input and optional' --stdout=b --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --timeout=5 --default=b -- a b c

eval-tester --name='receive default --index response by timeout with no input and optional' --stdout=1 --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default --index response by timeout with no input and optional' --stdout=1 --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --index --timeout=5 --default=b -- a b c

eval-tester --name='receive default response by timeout with no input and required' --stdout=b --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default response by timeout with no input and required' --stdout=b --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --timeout=5 --default=b --required -- a b c

eval-tester --name='receive default --index response by timeout with no input and required' --stdout=1 --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default --index response by timeout with no input and required' --stdout=1 --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --index --timeout=5 --default=b --required -- a b c

# single mode, so only the first match is selected
eval-tester --name='receive default first response by timeout with no input and optional' --stdout=b --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default first response by timeout with no input and optional' --stdout=b --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --timeout=5 --default-fuzzy=b -- a b bb c

eval-tester --name='receive default first --index response by timeout with no input and optional' --stdout=1 --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default first --index response by timeout with no input and optional' --stdout=1 --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --index --timeout=5 --default-fuzzy=b -- a b bb c

eval-tester --name='receive default first response by timeout with no input and required' --stdout=b --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default first response by timeout with no input and required' --stdout=b --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --timeout=5 --default-fuzzy=b --required -- a b bb c

eval-tester --name='receive default first --index response by timeout with no input and required' --stdout=1 --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default first --index response by timeout with no input and required' --stdout=1 --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --index --timeout=5 --default-fuzzy=b --required -- a b bb c

# multi mode
eval-tester --name='receive default responses by timeout with no input and multi and optional' --stdout=$'b\nbb' --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default responses by timeout with no input and multi and optional' --stdout=$'b\nbb' --stderr="q $timeout_defaults" \
-- env COLOR=no choose --multi 'q' 'd' --timeout=5 --default-fuzzy=b -- a b bb c

eval-tester --name='receive default --index responses by timeout with no input and multi and optional' --stdout=$'1\n2' --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default --index responses by timeout with no input and multi and optional' --stdout=$'1\n2' --stderr="q $timeout_defaults" \
-- env COLOR=no choose --multi 'q' 'd' --index --timeout=5 --default-fuzzy=b -- a b bb c

eval-tester --name='receive default responses by timeout with no input and multi and required' --stdout=$'b\nbb' --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default responses by timeout with no input and multi and required' --stdout=$'b\nbb' --stderr="q $timeout_defaults" \
-- env COLOR=no choose --multi 'q' 'd' --timeout=5 --default-fuzzy=b --required -- a b bb c

eval-tester --name='receive default --index responses by timeout with no input and multi and required' --stdout=$'1\n2' --stderr='q [timed out: using defaults]' \
eval-tester --name='receive default --index responses by timeout with no input and multi and required' --stdout=$'1\n2' --stderr="q $timeout_defaults" \
-- env COLOR=no choose --multi 'q' 'd' --index --timeout=5 --default-fuzzy=b --required -- a b bb c

# multiline defaults
eval-tester --name='receive multi-line default first response by timeout with no input' --stdout=$'b\nB' --stderr='q [timed out: using defaults]' \
eval-tester --name='receive multi-line default first response by timeout with no input' --stdout=$'b\nB' --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --timeout=2 --default=$'b\nB' --defaults=$'c\nd' -- a $'b\nB' c d
eval-tester --name='receive multi-line default first --index response by timeout with no input' --stdout=1 --stderr='q [timed out: using defaults]' \
eval-tester --name='receive multi-line default first --index response by timeout with no input' --stdout=1 --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --index --timeout=2 --default=$'b\nB' --defaults=$'c\nd' -- a $'b\nB' c d
eval-tester --name='receive multi-line default responses by timeout with no input' --stdout=$'b\nB\nc\nd' --stderr='q [timed out: using defaults]' \
eval-tester --name='receive multi-line default responses by timeout with no input' --stdout=$'b\nB\nc\nd' --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --multi --timeout=2 --default=$'b\nB' --defaults=$'c\nd' -- a $'b\nB' c d
eval-tester --name='receive multi-line default responses by timeout with no input' --stdout=$'1\n2\n3' --stderr='q [timed out: using defaults]' \
eval-tester --name='receive multi-line default responses by timeout with no input' --stdout=$'1\n2\n3' --stderr="q $timeout_defaults" \
-- env COLOR=no choose 'q' 'd' --multi --index --timeout=2 --default=$'b\nB' --defaults=$'c\nd' -- a $'b\nB' c d

# TESTS WITH INTERACTION
Expand Down
4 changes: 3 additions & 1 deletion commands/confirm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function confirm_test() (

# --ignore-stderr otherwise only \n^[[68;1R is output

local escape=$'\e'

{
sleep 3
__print_line
Expand Down Expand Up @@ -53,7 +55,7 @@ function confirm_test() (
{
# press escape key
sleep 3
printf $'\x1b'
__print_string "$escape"
} | eval-tester --name='receive abort response by escape key' --status=125 --ignore-stderr \
-- confirm --ppid=-1 --bool --timeout=5 -- 'What is your response?'

Expand Down
4 changes: 2 additions & 2 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -1987,8 +1987,8 @@ function dorothy_() (
done

# check for warnings
if dorothy-warnings --check; then
dorothy-warnings
if dorothy-warnings check; then
dorothy-warnings list
failures+=('dorothy-warnings')
fi

Expand Down
24 changes: 17 additions & 7 deletions commands/dorothy-warnings
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ function dorothy_warnings() (
Returns [0] always.
--warn | warn
Output the warnings file with appropropriate messaging, only if there are new warnings from last time.
Returns [0] always.
This is the default action.
--list | list
Output the warnings file with appropropriate messaging.
Returns [0] always.
This is the default action.
Expand All @@ -45,6 +50,7 @@ function dorothy_warnings() (
'--check' | 'check' | '--exists' | 'exists') action='check' ;;
'--clear' | 'clear' | '--remove' | 'remove' | '--reset' | 'reset') action='clear' ;;
'--warn' | 'warn') action='warn' ;;
'--list' | 'list') action='warn' ;;
'--add' | 'add')
action='add'
option_args+=("$@")
Expand All @@ -59,19 +65,22 @@ function dorothy_warnings() (
# Act

function dorothy_warnings_check {
test -s "$DOROTHY/state/warnings.txt"
[[ -s "$DOROTHY/state/warnings.txt" ]]
return
}
function dorothy_warnings_clear {
: >"$DOROTHY/state/warnings.txt"
}
function dorothy_warnings_list {
echo-style --stderr \
--notice1='Dorothy has encountered warnings:' --newline \
--="$(echo-file -- "$DOROTHY/state/warnings.txt")" --newline \
--notice1='For help with these warnings, see: ' --code-notice1='https://github.com/bevry/dorothy/issues/185' --newline \
--notice1='To clear these warnings, run: ' --code-notice1='dorothy-warnings clear'
}
function dorothy_warnings_warn {
if test -s "$DOROTHY/state/warnings.txt"; then
echo-style --stderr \
--notice1='Dorothy has encountered warnings:' --newline \
--="$(echo-file -- "$DOROTHY/state/warnings.txt")" --newline \
--notice1='For help with these warnings, see: ' --code-notice1='https://github.com/bevry/dorothy/issues/185' --newline \
--notice1='To clear these warnings, run: ' --code-notice1='dorothy-warnings clear'
if [[ -N "$DOROTHY/state/warnings.txt" ]]; then
dorothy_warnings_list
fi
}
function dorothy_warnings_add {
Expand All @@ -82,6 +91,7 @@ function dorothy_warnings() (
'check') dorothy_warnings_check ;;
'clear') dorothy_warnings_clear ;;
'warn') dorothy_warnings_warn ;;
'list') dorothy_warnings_warn ;;
'add') dorothy_warnings_add ;;
*) help "An unrecognised action was provided: $action" ;;
esac
Expand Down
2 changes: 1 addition & 1 deletion commands/get-terminal-position-support
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function get_terminal_position_support() (
# shorter timeouts aren't suitable as slower machines take a while for the response
# the read will complete immediately upon a response thanks to [-d R] which completes reading when the R is read, which is the final character of the response
local _ line column
IFS='[;' read -t 2 -srd R -p $'\e[6n' _ line column <"$option_device_file"
IFS='[;' read -t 2 -srd R -p $'\e[6n' _ line column 2>/dev/null <"$option_device_file" || :
if test "$option_quiet" = 'yes'; then
if test -n "${line-}" -a -n "${column-}"; then
return 0
Expand Down
Loading

0 comments on commit 0dfebda

Please sign in to comment.