Skip to content

Commit 45816c7

Browse files
committed
core, prompt: Move @go.select_option to lib/prompt
More user input functions will be added to `lib/prompt` in the next commit.
1 parent 9acd484 commit 45816c7

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

go-core.bash

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -229,43 +229,6 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
229229
return "$result"
230230
}
231231

232-
# Prompts the user to select one item from a list of options.
233-
#
234-
# This is a thin wrapper around the `select` builtin command for
235-
# straightforward, single-option user prompts. If you need to do anything more
236-
# complex, use the `select` builtin command directly.
237-
#
238-
# This will prompt the user for a single input, returned in the caller-declared
239-
# variable identified by `result_var`. If the user enters an invalid option,
240-
# this will notify the user and prompt again. If the user terminates input (via
241-
# EOF, i.e. Ctrl-D), `result_var` will remain unchanged and the function will
242-
# return nonzero.
243-
#
244-
# Globals:
245-
# PS3 environment variable defining the selection prompt
246-
#
247-
# Arguments:
248-
# result_var: Name of the caller-declared variable used to store the option
249-
# ...: Strings representing options available for the user to select
250-
#
251-
# Returns:
252-
# zero if `result_var` contains the user's selection, nonzero otherwise
253-
@go.select_option() {
254-
local __go_selected_option
255-
select __go_selected_option in "${@:2}"; do
256-
case "$__go_selected_option" in
257-
'')
258-
@go.printf '"%s" is not a valid option.\n' "$REPLY" >&2
259-
;;
260-
*)
261-
printf -v "$1" -- '%s' "$__go_selected_option"
262-
break
263-
;;
264-
esac
265-
done
266-
[[ -n "$__go_selected_option" ]]
267-
}
268-
269232
# Searches through plugin directories using a helper function
270233
#
271234
# The search will begin in `_GO_SCRIPTS_DIR/plugins`. As long as `search_func`

lib/prompt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#! /usr/bin/env bash
2+
#
3+
# User input prompts
4+
#
5+
# Exports:
6+
# @go.select_option
7+
# Prompts the user to select one item from a list of options
8+
9+
. "$_GO_USE_MODULES" 'validation'
10+
11+
# Prompts the user to select one item from a list of options.
12+
#
13+
# This is a thin wrapper around the `select` builtin command for
14+
# straightforward, single-option user prompts. If you need to do anything more
15+
# complex, use the `select` builtin command directly.
16+
#
17+
# This will prompt the user for a single input, returned in the caller-declared
18+
# variable identified by `result_var`. If the user enters an invalid option,
19+
# this will notify the user and prompt again. If the user terminates input (via
20+
# EOF, i.e. Ctrl-D), `result_var` will remain unchanged and the function will
21+
# return nonzero.
22+
#
23+
# Globals:
24+
# PS3 environment variable defining the selection prompt
25+
#
26+
# Arguments:
27+
# result_var: Name of the caller-declared variable used to store the option
28+
# ...: Strings representing options available for the user to select
29+
#
30+
# Returns:
31+
# zero if `result_var` contains the user's selection, nonzero otherwise
32+
@go.select_option() {
33+
@go.validate_identifier_or_die 'Result variable name' "$1"
34+
35+
local __go_selected_option
36+
select __go_selected_option in "${@:2}"; do
37+
case "$__go_selected_option" in
38+
'')
39+
@go.printf '"%s" is not a valid option.\n' "$REPLY" >&2
40+
;;
41+
*)
42+
printf -v "$1" -- '%s' "$__go_selected_option"
43+
break
44+
;;
45+
esac
46+
done
47+
[[ -n "$__go_selected_option" ]]
48+
}

libexec/demo-core.d/select-option

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# Use this program to get a feel for the core `@go.select_option` function, and
1313
# for an example of how to use it in your own scripts.
1414

15+
. "$_GO_USE_MODULES" 'prompt'
16+
1517
select_option_demo() {
1618
local options=("$@")
1719
local selected

tests/core/select-option.bats renamed to tests/prompt/select-option.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ load ../environment
44

55
setup() {
66
test_filter
7-
@go.create_test_go_script 'declare selection' \
7+
@go.create_test_go_script '. "$_GO_USE_MODULES" "prompt"' \
8+
'declare selection' \
89
'if @go.select_option "selection" "$@"; then' \
910
' printf "\nSelection: \"%s\"\n" "$selection"' \
1011
'else' \

0 commit comments

Comments
 (0)