xonecas.io
70160
Ce

cel-tui / commit 66b707a

feat(core): terminal hardening — crash cleanup and resize clear

ProcessTerminal now:
- Registers handlers for exit, SIGINT, SIGTERM, uncaughtException,
  and unhandledRejection to restore terminal state (raw mode, mouse
  mode, cursor visibility) on any exit path
- cleanup() is idempotent — safe to call multiple times
- Properly removes stdin data listener on stop (was only removing
  resize listener)
- Clears screen (ESC[2J + cursor home) on resize before triggering
  re-render, preventing scrollback artifacts

xonecas2026-04-04 66b707ae9fd4f05c5eebf7edb2f5d448a26b5918 parent eb4da2c

2 files changed

TODO.md+2−2
@@ -29,7 +29,7 @@ What's implemented, what's missing, and what needs hardening. Reference `spec.md
2929 - ✅ Reactive rendering (`cel.render()` batched via `process.nextTick`)
3030 - ✅ Full render on first paint, cursor home positioning
3131 - ✅ Differential rendering — `emitDiff()` uses `CellBuffer.diff()` to emit only changed cells
32-- ❌ Full clear + re-render on terminal resize (currently re-renders but doesn't clear scrollback)
32+- ✅ Full clear + re-render on terminal resize (clears screen before re-render)
3333
3434 ## Painting
3535
@@ -84,7 +84,7 @@ What's implemented, what's missing, and what needs hardening. Reference `spec.md
8484
8585 - ✅ `ProcessTerminal` (raw mode, SGR mouse, cursor hide/show)
8686 - ✅ `MockTerminal` (captured output, simulated input/resize)
87-- ❌ Restore terminal state on crash/unhandled exception (cleanup handler)
87+- ✅ Restore terminal state on crash/unhandled exception (cleanup handler)
8888 - ❌ Bracketed paste mode support
8989 - ❌ Kitty keyboard protocol detection
9090
packages/core/src/terminal.ts+47−2
@@ -27,6 +27,9 @@ export interface Terminal {
2727 export class ProcessTerminal implements Terminal {
2828 private wasRaw = false;
2929 private resizeHandler?: () => void;
30+ private inputHandler?: (data: string) => void;
31+ private cleanupBound?: () => void;
32+ private stopped = false;
3033
3134 get columns(): number {
3235 return process.stdout.columns || 80;
@@ -41,7 +44,13 @@ export class ProcessTerminal implements Terminal {
4144 }
4245
4346 start(onInput: (data: string) => void, onResize: () => void): void {
44- this.resizeHandler = onResize;
47+ this.stopped = false;
48+ this.inputHandler = onInput;
49+ this.resizeHandler = () => {
50+ // Clear screen on resize to avoid scrollback artifacts
51+ this.write("\x1b[2J\x1b[H");
52+ onResize();
53+ };
4554 this.wasRaw = process.stdin.isRaw || false;
4655
4756 if (process.stdin.setRawMode) {
@@ -50,14 +59,40 @@ export class ProcessTerminal implements Terminal {
5059 process.stdin.setEncoding("utf8");
5160 process.stdin.resume();
5261 process.stdin.on("data", onInput);
53- process.stdout.on("resize", onResize);
62+ process.stdout.on("resize", this.resizeHandler);
5463
5564 // Enable mouse SGR mode
5665 this.write("\x1b[?1006h");
5766 this.hideCursor();
67+
68+ // Register cleanup handlers for crash/exit scenarios
69+ this.cleanupBound = () => this.cleanup();
70+ process.on("exit", this.cleanupBound);
71+ process.on("SIGINT", this.cleanupBound);
72+ process.on("SIGTERM", this.cleanupBound);
73+ process.on("uncaughtException", (err) => {
74+ this.cleanup();
75+ console.error(err);
76+ process.exit(1);
77+ });
78+ process.on("unhandledRejection", (err) => {
79+ this.cleanup();
80+ console.error(err);
81+ process.exit(1);
82+ });
5883 }
5984
6085 stop(): void {
86+ this.cleanup();
87+ }
88+
89+ /**
90+ * Restore terminal state. Safe to call multiple times.
91+ */
92+ private cleanup(): void {
93+ if (this.stopped) return;
94+ this.stopped = true;
95+
6196 // Disable mouse mode
6297 this.write("\x1b[?1006l");
6398 this.showCursor();
@@ -65,11 +100,21 @@ export class ProcessTerminal implements Terminal {
65100 if (this.resizeHandler) {
66101 process.stdout.removeListener("resize", this.resizeHandler);
67102 }
103+ if (this.inputHandler) {
104+ process.stdin.removeListener("data", this.inputHandler);
105+ }
68106
69107 process.stdin.pause();
70108 if (process.stdin.setRawMode) {
71109 process.stdin.setRawMode(this.wasRaw);
72110 }
111+
112+ // Remove process handlers
113+ if (this.cleanupBound) {
114+ process.removeListener("exit", this.cleanupBound);
115+ process.removeListener("SIGINT", this.cleanupBound);
116+ process.removeListener("SIGTERM", this.cleanupBound);
117+ }
73118 }
74119
75120 hideCursor(): void {