mirror of
https://github.com/mjsarfatti/beddu.git
synced 2025-06-26 08:58:02 +02:00
Ask -> Request, while Seek makes the answer optional
This commit is contained in:
parent
5ff6a50d32
commit
ce6bfcd04c
6 changed files with 93 additions and 33 deletions
12
README.md
12
README.md
|
@ -78,10 +78,15 @@ pen "This is $(pen yellow "yellow"), and this is $(pen bold "bold")"
|
|||
### Interactive Functions
|
||||
|
||||
```bash
|
||||
# Ask for input
|
||||
ask name "What's your name?"
|
||||
# Kindly ask for input (empty answer is accepted)
|
||||
seek name "What's your name?"
|
||||
pen "Hello, $name!"
|
||||
|
||||
# Firmly ask for input (empty answer NOT accepted)
|
||||
request name "No really, what's your name?"
|
||||
pen "There you go, $name!"
|
||||
|
||||
|
||||
# Yes/no confirmation (defaults to "yes")
|
||||
if confirm "Continue?"; then
|
||||
pen green "Continuing..."
|
||||
|
@ -131,7 +136,8 @@ check "Task completed!" # We can directly `check`, `warn`, or `throw` after a `s
|
|||
|
||||
### User Interaction
|
||||
|
||||
- `ask [retval] PROMPT` - Get text input from user, saves the answer in `$retval`
|
||||
- `seek [retval] PROMPT` - Get (optional) text input from user, saves the answer in `$retval`
|
||||
- `request [retval] PROMPT` - Like above, but doesn't accept an empty response, saves the answer in `$retval`
|
||||
- `confirm [OPTIONS] PROMPT` - Get yes/no input
|
||||
- `--default-yes` - Set default answer to "yes" (default behavior)
|
||||
- `--default-no` - Set default answer to "no"
|
||||
|
|
BIN
demo/asciinema.png
Normal file
BIN
demo/asciinema.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 115 KiB |
|
@ -102,7 +102,7 @@ demo() {
|
|||
pen $_pink italic "-- Interactive functions --"
|
||||
line
|
||||
|
||||
ask name "How can I call you?"
|
||||
request name "How can I call you?"
|
||||
pen "Hello, $(pen bold cyan "${name:?}")"
|
||||
line
|
||||
|
||||
|
|
62
dist/beddu.sh
vendored
62
dist/beddu.sh
vendored
|
@ -2,7 +2,7 @@
|
|||
# shellcheck disable=all
|
||||
#
|
||||
# beddu.sh - A lightweight bash framework for interactive scripts and pretty output
|
||||
# Version: v1.0.0
|
||||
# Version: v1.0.0-1-g5ff6a50-dirty
|
||||
#
|
||||
# Copyright © 2025 Manuele Sarfatti
|
||||
# Licensed under the MIT license
|
||||
|
@ -190,29 +190,6 @@ warn() {
|
|||
pen italic "$@"
|
||||
}
|
||||
|
||||
ask() {
|
||||
local -n outvar="$1"
|
||||
local prompt
|
||||
local answer
|
||||
prompt=$(
|
||||
pen -n blue "${_q:-?} "
|
||||
pen "${2}"
|
||||
pen -n blue "${_a:-❯} "
|
||||
)
|
||||
show_cursor
|
||||
while true; do
|
||||
read -r -p "$prompt" answer
|
||||
case "$answer" in
|
||||
"")
|
||||
echo
|
||||
warn "Please type your answer."
|
||||
;;
|
||||
*) break ;;
|
||||
esac
|
||||
done
|
||||
outvar="$answer"
|
||||
}
|
||||
|
||||
choose() {
|
||||
local -n outvar="$1"
|
||||
local prompt
|
||||
|
@ -309,3 +286,40 @@ confirm() {
|
|||
esac
|
||||
done
|
||||
}
|
||||
|
||||
request() {
|
||||
local -n outvar="$1"
|
||||
local prompt
|
||||
local answer
|
||||
prompt=$(
|
||||
pen -n blue "${_q:-?} "
|
||||
pen "${2}"
|
||||
pen -n blue "${_a:-❯} "
|
||||
)
|
||||
show_cursor
|
||||
while true; do
|
||||
read -r -p "$prompt" answer
|
||||
case "$answer" in
|
||||
"")
|
||||
echo
|
||||
warn "Please type your answer."
|
||||
;;
|
||||
*) break ;;
|
||||
esac
|
||||
done
|
||||
outvar="$answer"
|
||||
}
|
||||
|
||||
seek() {
|
||||
local -n outvar="$1"
|
||||
local prompt
|
||||
local answer
|
||||
prompt=$(
|
||||
pen -n blue "${_q:-?} "
|
||||
pen "${2}"
|
||||
pen -n blue "${_a:-❯} "
|
||||
)
|
||||
show_cursor
|
||||
read -r -p "$prompt" answer
|
||||
outvar="$answer"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC1091
|
||||
# ask.sh - Get free text input from the user
|
||||
# request.sh - Get required text input from the user
|
||||
|
||||
[[ $BEDDU_ASK_LOADED ]] && return
|
||||
readonly BEDDU_ASK_LOADED=true
|
||||
|
@ -11,14 +11,14 @@ 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
|
||||
# Ask a question and require a free text answer from the user
|
||||
#
|
||||
# Usage:
|
||||
# ask outvar text
|
||||
# request outvar text
|
||||
# Example:
|
||||
# ask name "What is your name?"
|
||||
# request name "What is your name?"
|
||||
# echo "Hello, $name!"
|
||||
ask() {
|
||||
request() {
|
||||
local -n outvar="$1" # Declare nameref
|
||||
local prompt
|
||||
local answer
|
40
src/03.prompt/seek.sh
Normal file
40
src/03.prompt/seek.sh
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC1091
|
||||
# seek.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:
|
||||
# seek outvar text
|
||||
# Example:
|
||||
# seek name "What is your name?"
|
||||
# echo "Hello, $name!"
|
||||
seek() {
|
||||
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
|
||||
read -r -p "$prompt" answer
|
||||
|
||||
# shellcheck disable=SC2034
|
||||
outvar="$answer"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue