1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 07:29:04 -08:00
rc/sh/confirm

37 lines
859 B
Text
Raw Normal View History

2013-06-28 05:22:14 -07:00
#!/usr/bin/env bash
2021-07-29 00:37:35 -07:00
# YES_ZSH
# YES_BASH
# NO_DASH
2021-07-29 00:37:35 -07:00
2021-07-30 17:57:08 -07:00
### @ confirm
### display a simple yes-or-no prompt and return 0-or-1 respectively.
###
### ```
### $ confirm && echo yay || echo nay
### Continue? [y/N] y
### yay
### $ confirm && echo yay || echo nay
### Continue? [y/N] n
### nay
### ```
2021-07-30 16:31:21 -07:00
if [ -n "$ZSH_VERSION" ]; then
2013-07-01 18:05:17 -07:00
confirm() {
2021-07-29 00:37:35 -07:00
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
2015-05-23 01:16:56 -07:00
read -q '?Continue? [y/N] '
2013-07-01 18:05:17 -07:00
ret=$?
echo
return $ret
}
2013-06-28 05:22:14 -07:00
else
2013-07-01 18:05:17 -07:00
confirm() {
# specify stdin (1) to avoid taking input from pipes
2021-07-29 00:37:35 -07:00
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
2015-05-23 01:16:56 -07:00
read -n1 -u 1 -p "Continue? [y/N] " c
2013-07-01 18:05:17 -07:00
echo
[ "$c" != 'y' ] && [ "$c" != 'Y' ] && return 1
return 0
}
2013-06-28 05:22:14 -07:00
fi
2021-07-29 00:37:35 -07:00
[ "${SOURCING:-0}" -gt 0 ] || confirm "$@"