Targeted line manipulation operations for erasing specific portions of terminal lines and managing multiple lines.
Erase multiple lines from the current cursor position.
/**
* Erase from the current cursor position up the specified amount of rows
* @param count - Number of lines to erase
* @returns ANSI escape sequence string
*/
function eraseLines(count: number): string;Usage Examples:
import { eraseLines } from 'ansi-escapes';
// Erase the current line and 2 lines above it
process.stdout.write(eraseLines(3));
// Erase just the current line
process.stdout.write(eraseLines(1));
// Common pattern: erase previous output before showing new content
function updateProgress(lines, newContent) {
process.stdout.write(eraseLines(lines));
console.log(newContent);
}The eraseLines function erases from the current cursor position upward, clearing the specified number of lines and positioning the cursor at the beginning of the topmost erased line.
Erase specific portions of the current line.
/**
* Erase from the current cursor position to the end of the current line
*/
const eraseEndLine: string;
/**
* Erase from the current cursor position to the start of the current line
*/
const eraseStartLine: string;
/**
* Erase the entire current line
*/
const eraseLine: string;Usage Examples:
import { eraseEndLine, eraseStartLine, eraseLine } from 'ansi-escapes';
// Clear from cursor to end of line
process.stdout.write('Loading... 50%');
process.stdout.write(eraseEndLine);
process.stdout.write('100% Complete!');
// Clear from start of line to cursor
process.stdout.write('Progress: ');
process.stdout.write(eraseStartLine);
process.stdout.write('Status: Ready');
// Clear entire current line
process.stdout.write(eraseLine);
process.stdout.write('New line content');Erase portions of the screen relative to the cursor position.
/**
* Erase the screen from the current line down to the bottom of the screen
*/
const eraseDown: string;
/**
* Erase the screen from the current line up to the top of the screen
*/
const eraseUp: string;Usage Examples:
import { eraseDown, eraseUp } from 'ansi-escapes';
// Clear everything below the current line
process.stdout.write(eraseDown);
// Clear everything above the current line
process.stdout.write(eraseUp);
// Common pattern: clear below cursor before showing new output
function showResults(results) {
process.stdout.write(eraseDown);
results.forEach(result => console.log(result));
}Common patterns for building interactive terminal interfaces:
Progress Indicator:
import { eraseLine, cursorLeft } from 'ansi-escapes';
function showProgress(percent) {
process.stdout.write(cursorLeft + eraseLine);
process.stdout.write(`Progress: ${'='.repeat(percent / 2)}> ${percent}%`);
}
// Usage
for (let i = 0; i <= 100; i += 10) {
showProgress(i);
// ... do work ...
}Multi-line Status Updates:
import { eraseLines, cursorLeft } from 'ansi-escapes';
function updateStatus(status) {
// Erase previous status (3 lines) and show new one
process.stdout.write(eraseLines(3) + cursorLeft);
console.log(`Status: ${status.message}`);
console.log(`Progress: ${status.progress}%`);
console.log(`Time: ${status.elapsed}s`);
}Interactive Menu Clearing:
import { eraseDown } from 'ansi-escapes';
function showMenu(options) {
// Clear any previous menu content
process.stdout.write(eraseDown);
console.log('Select an option:');
options.forEach((option, i) => {
console.log(`${i + 1}. ${option}`);
});
}