From d87711279f901f1bd09f2d3294c6743a936865b9 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 21 Jan 2026 06:54:32 +0100 Subject: [PATCH] feat(tui): add graceful exit handling with terminal restoration - Handle SIGINT/SIGTERM signals properly - Unmount Ink app before exit - Restore cursor visibility and clear screen on exit - Use try/finally to ensure cleanup runs Co-Authored-By: Claude Opus 4.5 --- src/tui/index.tsx | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/tui/index.tsx b/src/tui/index.tsx index a88169b9..93062223 100644 --- a/src/tui/index.tsx +++ b/src/tui/index.tsx @@ -65,7 +65,24 @@ export async function startTUI(): Promise { // Clear the terminal for full-screen experience process.stdout.write('\x1b[2J\x1b[H'); - const { waitUntilExit } = render(); + const { waitUntilExit, unmount } = render(); - await waitUntilExit(); + // Handle graceful exit on SIGINT/SIGTERM + const cleanup = () => { + unmount(); + // Restore terminal state + process.stdout.write('\x1b[?25h'); // Show cursor + process.stdout.write('\x1b[2J\x1b[H'); // Clear screen + }; + + process.on('SIGINT', cleanup); + process.on('SIGTERM', cleanup); + + try { + await waitUntilExit(); + } finally { + process.off('SIGINT', cleanup); + process.off('SIGTERM', cleanup); + cleanup(); + } }