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

View file

@ -1,49 +0,0 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091
# ask.sh - Get free 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 get a free text answer from the user
#
# Usage:
# ask outvar text
# Example:
# ask name "What is your name?"
# echo "Hello, $name!"
ask() {
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"
}