Toggle the CLI cursor visibility in terminal applications
npx @tessl/cli install tessl/npm-cli-cursor@5.0.0CLI Cursor provides a simple, reliable API for controlling the visibility of the CLI cursor in terminal applications. It uses ANSI escape sequences to show and hide the cursor, with automatic TTY detection to prevent errors in non-terminal environments.
npm install cli-cursorimport cliCursor from "cli-cursor";For CommonJS environments:
const cliCursor = require("cli-cursor");import cliCursor from "cli-cursor";
// Hide the cursor for a loading animation
cliCursor.hide();
console.log("Loading...");
// Show the cursor when done
setTimeout(() => {
cliCursor.show();
console.log("Complete!");
}, 2000);
// Toggle based on a condition
const isLoading = true;
cliCursor.toggle(isLoading); // Will hide cursor if isLoading is trueDisplays the CLI cursor using ANSI escape sequence \u001B[?25h.
/**
* Show cursor
* @param {NodeJS.WritableStream} [stream=process.stderr] - Stream to write to
* @returns {void}
*/
cliCursor.show(stream?): voidParameters:
stream (optional): NodeJS.WritableStream - The writable stream to write ANSI sequences to. Defaults to process.stderr.Behavior:
stream.isTTY is trueConceals the CLI cursor using ANSI escape sequence \u001B[?25l and ensures graceful restoration on process exit.
/**
* Hide cursor
* @param {NodeJS.WritableStream} [stream=process.stderr] - Stream to write to
* @returns {void}
*/
cliCursor.hide(stream?): voidParameters:
stream (optional): NodeJS.WritableStream - The writable stream to write ANSI sequences to. Defaults to process.stderr.Behavior:
restoreCursor() from the restore-cursor dependency for graceful exit handlingstream.isTTY is trueToggles cursor visibility or forces a specific state based on a boolean parameter.
/**
* Toggle cursor visibility
* @param {boolean} [force] - Force show (true) or hide (false) state
* @param {NodeJS.WritableStream} [stream] - Stream to write to
* @returns {void}
*/
cliCursor.toggle(force?, stream?): voidParameters:
force (optional): boolean - When provided, forces cursor to show (true) or hide (false) regardless of current statestream (optional): NodeJS.WritableStream - The writable stream passed to show/hide methodsBehavior:
force parameter is provided, it represents the desired hidden state (true = show cursor, false = hide cursor)force is not provided, toggles between current show/hide stateshow() or hide() methods with the provided streamUsage Examples:
// Toggle between states
cliCursor.toggle(); // If currently visible, will hide; if hidden, will show
// Force specific states
cliCursor.toggle(true); // Force show cursor (force=true means "force to show")
cliCursor.toggle(false); // Force hide cursor (force=false means "force to hide")
// Use with custom stream
cliCursor.toggle(true, process.stdout);The package includes automatic TTY detection to ensure safe operation across different environments: