mirror of
https://github.com/mjsarfatti/beddu.git
synced 2025-06-27 01:18:01 +02:00
Initial commit - v1.0
This commit is contained in:
commit
76d136f64c
20 changed files with 1350 additions and 0 deletions
47
src/03.prompt/ask.sh
Normal file
47
src/03.prompt/ask.sh
Normal file
|
@ -0,0 +1,47 @@
|
|||
#! /usr/bin/env bash
|
||||
# ask.sh - Get free text input from the user
|
||||
|
||||
# @depends on:
|
||||
# - pen.sh
|
||||
# - _symbols.sh
|
||||
# - cursor.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"
|
||||
}
|
||||
|
||||
# Export the ask function so it's available to subshells
|
||||
export -f ask
|
Loading…
Add table
Add a link
Reference in a new issue