1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-28 18:17:11 -07:00
rc/sh/confirm

49 lines
1.1 KiB
Plaintext
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-08-01 09:45:19 -07:00
###
### a real world example:
###
### ```
### % g1 && confirm && git commit -a --amend --no-edit
### daf84e3 document a ton of stuff
### Continue? [y/N] y
### [master 92bdf76] document a ton of stuff
### Date: Sun Aug 1 09:27:25 2021 -0700
### 20 files changed, 406 insertions(+), 29 deletions(-)
### ```
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 "$@"