-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupTerminal.js
More file actions
46 lines (40 loc) · 1.1 KB
/
Copy pathsetupTerminal.js
File metadata and controls
46 lines (40 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Shared terminal setup and teardown for Ink-based TUI mode.
* Use when running mood record or mood results (full-screen takeover).
*/
export function setupTerminal() {
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
// Clear screen and hide cursor
process.stdout.write('\x1b[2J\x1b[H');
process.stdout.write('\x1b[?25l');
}
export function teardownTerminal() {
process.stdout.write('\x1b[?25h'); // Show cursor
process.stdin.setRawMode(false);
process.stdin.pause();
}
export function setupErrorHandlers() {
process.on('uncaughtException', (error) => {
teardownTerminal();
console.error('\n\nError:', error.message);
process.exit(1);
});
process.on('unhandledRejection', (error) => {
teardownTerminal();
console.error('\n\nUnhandled Rejection:', error);
process.exit(1);
});
}
/**
* Returns a cleanup function for normal app exit (SIGINT/SIGTERM).
* Calls teardownTerminal, unmounts the Ink instance, and exits.
*/
export function createAppCleanup(instance) {
return () => {
teardownTerminal();
instance.unmount();
process.exit(0);
};
}