Fixes #41.
Built on #45 (tty_wrap and setRawMode, #31), which is itself built on #17: this branch carries their commits, and only the last commit (0860a65) is new. Review that one after them.
A terminal program that enters the alternate screen, hides the cursor or turns on mouse reporting leaves the user's terminal that way when it traps: a trap runs no program code, only atexit handlers, and the runtime restores only termios. tty.WriteStream.prototype.setResetOnExit(sequence) registers bytes for the stream's terminal (written to a copy of its fd), and the runtime writes them once: at exit after the last of stdout, on a trap before the trap's message (so the message lands on the main screen), and on a fatal SIGINT/SIGTERM while raw mode is on. '' clears it. No JavaScript runs on a trap.
tests/npm/node_tty_reset (driven by tests/npm/tty_reset.py in a PTY) checks that the sequence reaches the terminal once, after the program's output, when the program returns, calls process.exit(3), throws an uncaught error, traps (there before the trap's message) or dies by SIGTERM, and never once it has been cleared. cargo test --no-fail-fast passes on macOS arm64 on top of c204858, apart from node_tz (#32).
Activity
3Thanks for this. #45 is merged now, so this can be rebased onto main. Before merging, two correctness points and one API question:
- There's one process-wide slot for the reset sequence (node.c, around line 886), so calling setResetOnExit on a second stream silently replaces the first. Either keep one per terminal or make it explicit that it's process-wide.
- The exit write goes through a dup of the terminal fd and gives up on EAGAIN. If that open file is O_NONBLOCK (it is once stdin is read in raw mode), the reset can be lost. It needs a blocking write, or a short poll-and-retry.
- API: setResetOnExit is a new, non-Node method on tty.WriteStream, and it adds a hook into the core runtime's trap path. Issue #41 also floats process.setTerminalReset. I'd like to settle on one surface first. A narrower one is preferable: a single runtime-level 'bytes to write to the terminal on exit or trap' that tty code can set.
@rofrol is attempting to deploy a commit to the Cap Software Inc Team on Vercel.
A member of the Team first needs to authorize it.