npm install @ferrow/ansi-stripStrip/parse ANSI escape sequences: strip, visibleLength, truncateVisible, tokenize with color preservation.
import { strip, visibleLength, truncateVisible, tokenize } from "ansi-strip";
const colored = "\x1b[32mHello\x1b[0m World";
strip(colored); // "Hello World"
visibleLength(colored); // 11
truncateVisible(colored, 5); // "Hello\x1b[0m" (color reset appended)
tokenize(colored);
// [
// { text: "Hello", codes: ["\x1b[32m"] },
// { text: " World", codes: [] }
// ]Remove all ANSI escape sequences (SGR/colors, CSI sequences, OSC commands).
Return length of text without ANSI codes. Codepoint-aware (counts multi-byte UTF-8 correctly) but not full grapheme-cluster aware.
Detect if text contains ANSI escape sequences.
Truncate text to visible length, preserving ANSI codes and appending reset (if codes were present).
Tokenize text into segments: {text: string, codes: string[]}. Each segment carries the ANSI codes active at that point. Useful for re-rendering with different styles.
Token shape:
interface Token {
text: string; // Visible text (no ANSI)
codes: string[]; // ANSI codes active here
}- Not full grapheme-aware:
visibleLengthcounts UTF-8 codepoints, not grapheme clusters (emoji combining marks will be counted separately). - CSI/OSC coverage: Covers standard SGR (30-39, 90-97, 100-107, 1, 4, etc.), CSI sequences, and OSC (OSC 0-2, OSC 8 links). Some exotic sequences may not match.
- No support for other terminal escape families (like termcap).
Part of the ferrow-toolkit collection · Sponsored by Ferrow