1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-17 13:43:06 -07:00

fix redirection inconsistencies across shells

This commit is contained in:
Connor Olding 2021-09-28 12:09:32 -07:00
parent 1bd6f41be5
commit 9bac51b1a6
2 changed files with 6 additions and 16 deletions

View File

@ -27,11 +27,6 @@
### 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
confirm() {
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
@ -43,10 +38,10 @@ if [ -n "$ZSH_VERSION" ]; then
elif [ -n "$BASH_VERSION" ]; then
confirm() {
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
read -n1 -p "Continue? [y/N] " c
read -n1 -p "Continue? [y/N] " c 2>&1
echo
[ "$c" = y ] || [ "$c" = Y ]
} <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe
} 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
else
confirm() (
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
@ -57,7 +52,7 @@ else
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
) 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
fi
[ -n "${preload+-}" ] || confirm "$@"

View File

@ -13,19 +13,14 @@
### $
### ```
# known inconsistencies:
# 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
read -n1 -sp $'Press any key to continue\n' 2>&1
} 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
else
pause() (
old="$(stty -g)"
@ -33,7 +28,7 @@ else
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
) 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
fi
[ -n "${preload+-}" ] || pause "$@"