main/lib/prompt.sh

25 lines
697 B
Bash
Executable File

#!/usr/bin/env bash
# prompt.sh — plain y/N prompts.
# ask "prompt?" [default y|n] -> sets REPLY
# Under ${ALL:-0} (the --yes flag) assume the stated default without
# reading stdin, so a fully non-interactive run never blocks on a read.
ask() {
local p="$1" d="${2:-y}" hint
if [[ "$d" == "y" ]]; then hint="[Y/n]"; else hint="[y/N]"; fi
if (( ${ALL:-0} )); then
REPLY="$d"
return
fi
read -rp "$p $hint " REPLY
[[ -z "$REPLY" ]] && REPLY="$d"
}
# yn "prompt?" [default] -> returns 0 (true) if answer is yes
yn() {
ask "$1" "${2:-y}"
[[ "$REPLY" =~ ^[Yy]$ ]]
}
# confirm_once "prompt?" default -> for destructive single confirm
confirm_once() { yn "$1" "${2:-n}"; }