Skip to content

Commit 636b474

Browse files
committed
ui:ask: Move backend-interna to where they belong
1 parent 1f005c3 commit 636b474

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

sources/fs-root/opt/batocera-emulationstation/lib/ui/backend-api.shl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
# - Abort: Status!=1
3939
#
4040
# @arg $1 string text to use
41-
# @arg $2 ref name of an array to use
41+
# @arg $2 ref name of the array containing choices
4242
# @arg $3 int offset to start at (default 0)
4343
# @stdout index number of selected choice
4444
function ui#requestChoiceImpl
@@ -81,6 +81,15 @@ function ui#requestDirectoryImpl
8181
function ui#requestFileImpl
8282
{ echo "NOT SUPPORTED" >&2 && exit 1; }
8383

84+
# @description
85+
# Ask user for arbitrary input with no specific requirement on form or structure whatsoever.
86+
# @arg $1 string question text
87+
# @arg $2 string inital value
88+
# @stdout user input
89+
# @exitcode 0 when user input wasn't empy
90+
function ui#requestInputImpl
91+
{ echo "NOT SUPPORTED" >&2 && exit 1; }
92+
8493
# @endsection
8594

8695
# -----------

sources/fs-root/opt/batocera-emulationstation/lib/ui/gui.shl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,11 @@ function ui#requestFileImpl
100100
"${__typeFilter[@]}"
101101
}
102102

103+
# @description GUI-style implementation of asking for arbitrary input.
104+
# @see [ui#requestInputImpl](./backend-api.shl.md#uirequestInputImpl)
105+
function ui#requestInputImpl
106+
{
107+
'ui#baseDialog' --entry --text="$1" --entry-text="$2"
108+
}
109+
103110
# @endsection

sources/fs-root/opt/batocera-emulationstation/lib/ui/tty.shl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ function ui#requestDirectoryImpl
6262
echo -n "$REPLY"
6363
}
6464

65-
# @description TTY-style implementation of asking for a file.
65+
# @description TTY-style implementation of asking for a file.
66+
# **Note:** This function uses `()` instead of `{}` for the function body
67+
# because it heavily modifies the execution environment to configure `read`.
68+
#
6669
# @see [ui#requestFileImpl](./backend-api.shl.md#uirequestFileImpl)
6770
# TODO: add a note 'leave empty to cancel' to prompt message
6871
function ui#requestFileImpl
@@ -85,4 +88,15 @@ function ui#requestFileImpl
8588
echo -n "$REPLY"
8689
)
8790

91+
# @description TTY-style implementation of asking for arbitrary input with `read`.
92+
# @see [ui#requestInputImpl](./backend-api.shl.md#uirequestInputImpl)
93+
function ui#requestInputImpl
94+
{
95+
local REPLY
96+
read -rep "$1"$'\n'": " -i "$2"
97+
REPLY="${REPLY%%[[:space:]]}"
98+
echo -n "$REPLY"
99+
[ -n "$REPLY" ] || return 1
100+
}
101+
88102
# @endsection

sources/fs-root/opt/batocera-emulationstation/lib/user-interface.shl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@ function ui:ask {
7373
local _default="$2"
7474

7575
local _question=$(lc "%%msg%% (default: '%%_default%%')" msg _default)
76-
if "ui#isGraphical"; then
77-
REPLY="$('ui#baseDialog' --entry --text="$_question" --entry-text="$_default")"
78-
else
79-
echo '' >&2
80-
IFS='' read -ri "$2" -e -p "$_question"$'\n'": "
76+
if ! "ui#asReply" ui#requestInputImpl "$_question" "${__default}"; then
77+
[ "$1" = "--optional" ] || ui errorAbort 'An answer is required for this question.'
8178
fi
8279

83-
"ui#resultOut" "${REPLY:-"$2"}"
80+
"ui#resultOut" "${REPLY}"
8481
}
8582

8683
# @description
@@ -240,6 +237,9 @@ function ui#verify_isDir
240237
# @description
241238
# Verify that a given string is a file path+name whose ending matches a pattern list.
242239
# The check will automatically append `$` at the end of the pattern.
240+
# **Note:** This function uses `()` instead of `{}` for the function body
241+
# because it changes shell options to configure case insensitive matching.
242+
#
243243
# @stdout empty in case of verification failure, the full file path otherwise.
244244
# @arg $1 regex OR pattern of expected file endings as accepted by `[[ =~ ]]` (`T1|T2`)
245245
# @arg $2 path supposed file path to check

0 commit comments

Comments
 (0)