beddu/src/03.prompt/request.sh
2025-05-20 17:33:11 +02:00

49 lines
1.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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"
}