znt-usetty-wrapper
675 bytes
1emulate -L zsh
2
3zmodload zsh/curses
4
5test_fd0() {
6 true <&0
7}
8
9local restore=0 FD
10
11# Reattach to terminal
12if [ ! -t 0 ]; then
13 # Check if can reattach to terminal in any way
14 if [[ ! -c /dev/tty && ! -t 2 ]]; then
15 echo "No terminal available (no /dev/tty and no terminal at stderr)"
16 return 1
17 fi
18
19 if test_fd0 2>/dev/null; then
20 exec {FD}<&0
21 restore=2
22 else
23 restore=1
24 fi
25
26 if [[ ! -c /dev/tty ]]; then
27 exec <&2
28 else
29 exec </dev/tty
30 fi
31fi
32
33# Run the command
34"$@"
35
36# Restore FD state
37(( restore == 1 )) && exec <&-
38(( restore == 2 )) && exec <&$FD && exec {FD}<&-
39
40# vim: set filetype=zsh: