main/lib/prompt.sh

19 lines
506 B
Bash
Executable File

#!/usr/bin/env bash
# prompt.sh — plain y/N prompts.
# ask "prompt?" [default y|n] -> sets REPLY
ask() {
local p="$1" d="${2:-y}" hint
if [[ "$d" == "y" ]]; then hint="[Y/n]"; else hint="[y/N]"; 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}"; }