ANSI escape codes for manipulating the terminal
npx @tessl/cli install tessl/npm-ansi-escapes@7.0.0ANSI Escapes provides a comprehensive collection of ANSI escape codes for terminal manipulation, enabling precise control over cursor positioning, screen clearing, text formatting, and terminal display properties. It offers cross-platform compatibility and supports both Node.js and browser environments.
npm install ansi-escapesimport ansiEscapes from 'ansi-escapes';For named imports:
import { cursorTo, cursorUp, clearScreen, beep } from 'ansi-escapes';For CommonJS environments:
const ansiEscapes = require('ansi-escapes');import ansiEscapes from 'ansi-escapes';
// Move cursor to position (10, 5) and clear the line
process.stdout.write(ansiEscapes.cursorTo(10, 5) + ansiEscapes.eraseLine);
// Move cursor up 2 rows and to the left edge
process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft);
// Clear the entire terminal
process.stdout.write(ansiEscapes.clearTerminal);
// Create a clickable link
console.log(ansiEscapes.link('Click here', 'https://example.com'));Browser Usage with Xterm.js:
import ansiEscapes from 'ansi-escapes';
import { Terminal } from 'xterm';
const terminal = new Terminal();
terminal.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft);ANSI Escapes is organized around several key functional areas:
All functions return ANSI escape code strings that can be written directly to terminal outputs or used with terminal emulators.
Core cursor control operations for precise terminal positioning and relative movement.
function cursorTo(x: number, y?: number): string;
function cursorMove(x: number, y?: number): string;
function cursorUp(count?: number): string;
function cursorDown(count?: number): string;
function cursorForward(count?: number): string;
function cursorBackward(count?: number): string;
const cursorLeft: string;
const cursorSavePosition: string;
const cursorRestorePosition: string;
const cursorGetPosition: string;
const cursorNextLine: string;
const cursorPrevLine: string;
const cursorHide: string;
const cursorShow: string;Operations for managing terminal display, clearing screens, and controlling screen buffers.
const clearScreen: string;
const clearTerminal: string;
const enterAlternativeScreen: string;
const exitAlternativeScreen: string;
const scrollUp: string;
const scrollDown: string;
const eraseScreen: string;
const beep: string;Targeted line manipulation for clearing specific portions of terminal lines.
function eraseLines(count: number): string;
const eraseEndLine: string;
const eraseStartLine: string;
const eraseLine: string;
const eraseDown: string;
const eraseUp: string;Modern terminal features including hyperlinks, image display, and iTerm2 integration.
function link(text: string, url: string): string;
function image(data: Uint8Array, options?: ImageOptions): string;
const iTerm: {
setCwd(cwd?: string): string;
annotation(message: string, options?: AnnotationOptions): string;
};interface ImageOptions {
width?: 'auto' | number | string;
height?: 'auto' | number | string;
preserveAspectRatio?: boolean;
}
interface AnnotationOptions {
length?: number;
x?: number;
y?: number;
isHidden?: boolean;
}