Ask -> Request, while Seek makes the answer optional

This commit is contained in:
Manuele Sarfatti 2025-05-20 17:33:11 +02:00
parent 5ff6a50d32
commit ce6bfcd04c
6 changed files with 93 additions and 33 deletions

49
src/03.prompt/request.sh Normal file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091
# request.sh - Get required text input from the user
[[ $BEDDU_ASK_LOADED ]] && return
readonly BEDDU_ASK_LOADED=true
SCRIPT_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
source "$SCRIPT_DIR/../00.utils/_symbols.sh"
source "$SCRIPT_DIR/../00.utils/movements.sh"
source "$SCRIPT_DIR/../01.core/pen.sh"
source "$SCRIPT_DIR/../02.ui/warn.sh"
# Ask a question and require a free text answer from the user
#
# Usage:
# request outvar text
# Example:
# request name "What is your name?"
# echo "Hello, $name!"
request() {
local -n outvar="$1" # Declare nameref
local prompt
local answer
# Set prompt with default indicator
prompt=$(
pen -n blue "${_q:-?} "
pen "${2}"
pen -n blue "${_a:-} "
)
show_cursor
# Get response
while true; do
read -r -p "$prompt" answer
case "$answer" in
"")
echo
warn "Please type your answer."
;;
*) break ;;
esac
done
# shellcheck disable=SC2034
outvar="$answer"
}