1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-26 09:07:12 -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(-) ### 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 2; } [ $# -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 elif [ -n "$BASH_VERSION" ]; then
confirm() { confirm() {
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; } [ $# -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 echo
[ "$c" = y ] || [ "$c" = Y ] [ "$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 else
confirm() ( confirm() (
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; } [ $# -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)" c="$(dd ibs=1 count=1 2>/dev/null)"
echo echo
[ "$c" = y ] || [ "$c" = Y ] [ "$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 fi
[ -n "${preload+-}" ] || confirm "$@" [ -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 if [ -n "$ZSH_VERSION" ]; then
pause() { pause() {
read -sk $'?Press any key to continue\n' read -sk $'?Press any key to continue\n'
} }
elif [ -n "$BASH_VERSION" ]; then elif [ -n "$BASH_VERSION" ]; then
pause() { pause() {
read -n1 -sp $'Press any key to continue\n' read -n1 -sp $'Press any key to continue\n' 2>&1
} <&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 else
pause() ( pause() (
old="$(stty -g)" old="$(stty -g)"
@ -33,7 +28,7 @@ else
printf 'Press any key to continue\n' printf 'Press any key to continue\n'
stty -icanon -echo stty -icanon -echo
dd ibs=1 count=1 2>/dev/null >/dev/null 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 fi
[ -n "${preload+-}" ] || pause "$@" [ -n "${preload+-}" ] || pause "$@"