znt-usetty-wrapper 675 B

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