Gracefully restore the CLI cursor on exit
npx @tessl/cli install tessl/npm-restore-cursor@5.1.0Restore Cursor provides a utility function that gracefully restores the CLI cursor on process exit, preventing hidden cursors from remaining hidden if a process crashes. It automatically detects TTY context and does nothing when run in non-interactive environments.
npm install restore-cursorimport restoreCursor from "restore-cursor";For CommonJS:
const restoreCursor = require("restore-cursor");import restoreCursor from "restore-cursor";
// Call once to register the exit handler
restoreCursor();
// The cursor will be automatically restored when the process exits
// even if the process crashes or is terminated with SIGTERM/SIGINTRestore Cursor has a minimal, focused architecture designed around a single responsibility:
onetime for single execution and signal-exit for comprehensive exit handlingThe design prioritizes simplicity and reliability, ensuring cursor restoration works across different exit conditions without requiring complex configuration.
Registers an exit handler that restores the CLI cursor visibility using ANSI escape sequences.
/**
* Gracefully restore the CLI cursor on exit.
*
* Prevent the cursor you have hidden interactively from remaining hidden if the process crashes.
* It does nothing if run in a non-TTY context.
*
* @returns void - No return value
* @example
* ```typescript
* import restoreCursor from "restore-cursor";
*
* restoreCursor();
* ```
*/
function restoreCursor(): void;Key Features:
process.stderr if it's a TTYprocess.stdout if it's a TTYonetime wrapper to ensure handler is only registered once, even if called multiple timessignal-exit to handle various process termination scenarios including:
{alwaysLast: true} option to ensure cursor restoration happens after other cleanup\u001B[?25h to show the cursorUsage Patterns:
// Safe to call multiple times - only registers handler once
restoreCursor();
restoreCursor(); // No additional effect
// Works in any TTY context
console.log("Hiding cursor...");
process.stdout.write("\u001B[?25l"); // Hide cursor
restoreCursor(); // Will restore on exit
// Safe in non-TTY environments (does nothing)
restoreCursor(); // No effect when stdout/stderr are not TTYDependencies:
onetime@^7.0.0: Ensures function runs only oncesignal-exit@^4.1.0: Handles various process exit scenarios