CLI Cursor Toggle

5.0.0 · active · verified Wed Apr 22

cli-cursor is a lightweight utility that provides a simple API for showing, hiding, and toggling the command-line interface cursor. Its primary function is to manage cursor visibility in terminal applications, with a notable feature of gracefully restoring the cursor's state upon process exit, preventing a permanently hidden cursor. The current stable version is 5.0.0, which requires Node.js 18 or higher. Releases are typically driven by updates to Node.js LTS versions and the transition to pure ESM, rather than a fixed cadence. Key differentiators include its robust cursor restoration mechanism (powered by `restore-cursor`) and a minimalist, focused API, making it a reliable choice for CLI tools needing precise cursor control.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates hiding and toggling the CLI cursor, with graceful restoration on exit and explicit showing before the application terminates.

import cliCursor from 'cli-cursor';
import process from 'node:process';

console.log('Cursor is visible. Hiding now...');
cliCursor.hide();

// Simulate some work
setTimeout(() => {
  console.log('Performing some background task...');
}, 1000);

// Toggle cursor visibility based on a condition
const shouldShowCursor = process.env.SHOW_CURSOR === 'true';

setTimeout(() => {
  console.log(`Toggling cursor to ${shouldShowCursor ? 'visible' : 'hidden'}...`);
  cliCursor.toggle(shouldShowCursor);

  // Ensure cursor is shown before exiting in case of manual toggle logic
  // This is handled gracefully by `restore-cursor` on process exit, but explicit is sometimes clearer.
  setTimeout(() => {
    cliCursor.show(); // Ensure cursor is visible before application ends
    console.log('Cursor should now be visible. Exiting.');
  }, 1500);

}, 2000);

view raw JSON →