mirror of
https://github.com/notwa/rc
synced 2024-06-29 02:17:12 -07:00
rewrite confirm and pause to improve compatibility and consistency
This commit is contained in:
parent
18eca74b08
commit
e0aac93c38
40
sh/confirm
40
sh/confirm
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env sh
|
||||||
# YES_ZSH
|
# YES_ZSH
|
||||||
# YES_BASH
|
# YES_BASH
|
||||||
# NO_DASH
|
# YES_DASH
|
||||||
# NO_ASH
|
# YES_ASH
|
||||||
|
|
||||||
### @ confirm
|
### @ confirm
|
||||||
### display a simple yes-or-no prompt and return 0-or-1 respectively.
|
### display a simple yes-or-no prompt and return 0-or-1 respectively.
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
### a real world example:
|
### a real world example:
|
||||||
###
|
###
|
||||||
### ```
|
### ```
|
||||||
### % g1 && confirm && git commit -a --amend --no-edit
|
### $ g1 && confirm && git commit -a --amend --no-edit
|
||||||
### daf84e3 document a ton of stuff
|
### daf84e3 document a ton of stuff
|
||||||
### Continue? [y/N] y
|
### Continue? [y/N] y
|
||||||
### [master 92bdf76] document a ton of stuff
|
### [master 92bdf76] document a ton of stuff
|
||||||
|
@ -27,23 +27,37 @@
|
||||||
### 20 files changed, 406 insertions(+), 29 deletions(-)
|
### 20 files changed, 406 insertions(+), 29 deletions(-)
|
||||||
### ```
|
### ```
|
||||||
|
|
||||||
|
# known inconsistencies:
|
||||||
|
# since the fallbacks pipe stderr into stdin,
|
||||||
|
# this will behave differently between zsh and non-zsh shells:
|
||||||
|
# ( echo hi | confirm | wc -c ) 2>/dev/null
|
||||||
|
|
||||||
if [ -n "$ZSH_VERSION" ]; then
|
if [ -n "$ZSH_VERSION" ]; then
|
||||||
confirm() {
|
confirm() {
|
||||||
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
|
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||||
read -q '?Continue? [y/N] '
|
read -q '?Continue? [y/N] '
|
||||||
ret=$?
|
ret=$?
|
||||||
echo
|
echo >/dev/tty
|
||||||
return $ret
|
return $ret
|
||||||
}
|
}
|
||||||
else
|
elif [ -n "$BASH_VERSION" ]; then
|
||||||
confirm() {
|
confirm() {
|
||||||
# specify stdin (1) to avoid taking input from pipes
|
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||||
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
|
read -n1 -p "Continue? [y/N] " c
|
||||||
read -n1 -u 1 -p "Continue? [y/N] " c
|
|
||||||
echo
|
echo
|
||||||
[ "$c" != 'y' ] && [ "$c" != 'Y' ] && return 1
|
[ "$c" = y ] || [ "$c" = Y ]
|
||||||
return 0
|
} <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe
|
||||||
}
|
else
|
||||||
|
confirm() (
|
||||||
|
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||||
|
old="$(stty -g)"
|
||||||
|
trap 'stty "$old"' INT EXIT
|
||||||
|
printf 'Continue? [y/N] '
|
||||||
|
stty -icanon
|
||||||
|
c="$(dd ibs=1 count=1 2>/dev/null)"
|
||||||
|
echo
|
||||||
|
[ "$c" = y ] || [ "$c" = Y ]
|
||||||
|
) <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ -n "${preload+-}" ] || confirm "$@"
|
[ -n "${preload+-}" ] || confirm "$@"
|
||||||
|
|
34
sh/pause
34
sh/pause
|
@ -1,10 +1,10 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env sh
|
||||||
# YES_ZSH
|
# YES_ZSH
|
||||||
# YES_BASH
|
# YES_BASH
|
||||||
# NO_DASH
|
# YES_DASH
|
||||||
# NO_ASH
|
# YES_ASH
|
||||||
|
|
||||||
pause() { ### @-
|
### @ pause
|
||||||
### pause — the companion script of [`confirm`.](#confirm)
|
### pause — the companion script of [`confirm`.](#confirm)
|
||||||
###
|
###
|
||||||
### ```
|
### ```
|
||||||
|
@ -12,10 +12,28 @@ pause() { ### @-
|
||||||
### Press any key to continue
|
### Press any key to continue
|
||||||
### $
|
### $
|
||||||
### ```
|
### ```
|
||||||
[ -n "${ZSH_VERSION:-}" ] \
|
|
||||||
&& read -sk '?Press any key to continue
|
# known inconsistencies:
|
||||||
' || read -n1 -u 1 -sp 'Press any key to continue
|
# since the fallbacks pipe stderr into stdin,
|
||||||
'
|
# this will behave differently between zsh and non-zsh shells:
|
||||||
|
# ( echo hi | pause | wc -c ) 2>/dev/null
|
||||||
|
|
||||||
|
if [ -n "$ZSH_VERSION" ]; then
|
||||||
|
pause() {
|
||||||
|
read -sk $'?Press any key to continue\n'
|
||||||
}
|
}
|
||||||
|
elif [ -n "$BASH_VERSION" ]; then
|
||||||
|
pause() {
|
||||||
|
read -n1 -sp $'Press any key to continue\n'
|
||||||
|
} <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe
|
||||||
|
else
|
||||||
|
pause() (
|
||||||
|
old="$(stty -g)"
|
||||||
|
trap 'stty "$old"' INT EXIT
|
||||||
|
printf 'Press any key to continue\n'
|
||||||
|
stty -icanon -echo
|
||||||
|
dd ibs=1 count=1 2>/dev/null >/dev/null
|
||||||
|
) <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe
|
||||||
|
fi
|
||||||
|
|
||||||
[ -n "${preload+-}" ] || pause "$@"
|
[ -n "${preload+-}" ] || pause "$@"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user