### Install ansi-sequence-parser Source: https://github.com/blake-mealey/ansi-sequence-parser/blob/main/README.md Instructions for installing the ansi-sequence-parser package using popular package managers: pnpm, yarn, and npm. ```sh pnpm install ansi-sequence-parser yarn add ansi-sequence-parser npm install ansi-sequence-parser ``` -------------------------------- ### Parse ANSI Sequences (Chunked Input) Source: https://github.com/blake-mealey/ansi-sequence-parser/blob/main/README.md Illustrates parsing ANSI escape sequences in chunks by creating a parser instance with `createAnsiSequenceParser`. This approach maintains state between chunks, ensuring correct parsing of sequences that span across multiple input segments. ```ts import { createAnsiSequenceParser } from 'ansi-sequence-parser'; const parser = createAnsiSequenceParser(); const tokensByLine = input.split(/\r?\n/).map((line) => parser.parse(line)); ``` -------------------------------- ### Interpret Colors with Custom Palette Source: https://github.com/blake-mealey/ansi-sequence-parser/blob/main/README.md Demonstrates how to create a color palette with a custom map of named colors. This allows overriding default color definitions or providing specific hex values for ANSI color names. ```ts import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser'; const tokens = parseAnsiSequences(input); const colorPalette = createColorPalette({ black: '#000000', red: '#bb0000', green: '#00bb00', yellow: '#bbbb00', blue: '#0000bb', magenta: '#ff00ff', cyan: '#00bbbb', white: '#eeeeee', brightBlack: '#555555', brightRed: '#ff5555', brightGreen: '#00ff00', brightYellow: '#ffff55', brightBlue: '#5555ff', brightMagenta: '#ff55ff', brightCyan: '#55ffff', brightWhite: '#ffffff', }); ``` -------------------------------- ### Override Default Named Colors with Custom Hex Values Source: https://context7.com/blake-mealey/ansi-sequence-parser/llms.txt Demonstrates how to create a custom color palette by overriding default named colors with specific hex values. This is useful for matching custom terminal themes. It shows how to create a completely new palette or extend the default one. ```typescript import { parseAnsiSequences, createColorPalette, defaultNamedColorsMap } from 'ansi-sequence-parser'; // Create a custom color palette with modified colors const customPalette = createColorPalette({ black: '#1a1a2e', red: '#ff6b6b', green: '#51cf66', yellow: '#ffd43b', blue: '#339af0', magenta: '#cc5de8', cyan: '#22b8cf', white: '#f8f9fa', brightBlack: '#495057', brightRed: '#ff8787', brightGreen: '#69db7c', brightYellow: '#ffe066', brightBlue: '#5c7cfa', brightMagenta: '#da77f2', brightCyan: '#3bc9db', brightWhite: '#ffffff', }); // Or extend the default colors const extendedPalette = createColorPalette({ ...defaultNamedColorsMap, blue: '#0066cc', green: '#00aa00', }); const input = '\x1b[34mCustom Blue\x1b[0m \x1b[32mCustom Green\x1b[0m'; const tokens = parseAnsiSequences(input); tokens.forEach((token) => { if (token.foreground) { console.log(`${token.value}: ${extendedPalette.value(token.foreground)}`); } }); // Output: // Custom Blue: #0066cc // Custom Green: #00aa00 ``` -------------------------------- ### Interpret Colors with Default Palette Source: https://github.com/blake-mealey/ansi-sequence-parser/blob/main/README.md Shows how to use the `createColorPalette` function to interpret ANSI color tokens (named, table, or RGB) into hex color codes. It iterates through parsed tokens and retrieves their foreground and background color values. ```ts import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser'; const tokens = parseAnsiSequences(input); const colorPalette = createColorPalette(); for (const token of tokens) { if (token.foreground) { const foregroundValue = colorPalette.value(token.foreground); } if (token.background) { const backgroundValue = colorPalette.value(token.background); } } ``` -------------------------------- ### Convert ANSI Sequences to HTML with Inline Styles Source: https://context7.com/blake-mealey/ansi-sequence-parser/llms.txt Provides a TypeScript function `ansiToHtml` that converts ANSI-colored terminal output into HTML. It parses ANSI sequences and applies corresponding inline styles for colors and text decorations. Special HTML characters and newlines are also handled. ```typescript import { parseAnsiSequences, createColorPalette, ParseToken, DecorationType } from 'ansi-sequence-parser'; function ansiToHtml(input: string): string { const tokens = parseAnsiSequences(input); const palette = createColorPalette(); return tokens.map((token) => { const styles: string[] = []; if (token.foreground) { styles.push(`color: ${palette.value(token.foreground)}`); } if (token.background) { styles.push(`background-color: ${palette.value(token.background)}`); } // Apply text decorations const decorationMap: Record = { bold: 'font-weight: bold', dim: 'opacity: 0.7', italic: 'font-style: italic', underline: 'text-decoration: underline', reverse: '', // handled separately hidden: 'visibility: hidden', strikethrough: 'text-decoration: line-through', overline: 'text-decoration: overline', }; token.decorations.forEach((decoration) => { if (decorationMap[decoration]) { styles.push(decorationMap[decoration]); } }); const escapedValue = token.value .replace(/&/g, '&') .replace(//g, '>') .replace(/\n/g, '
'); if (styles.length > 0) { return `${escapedValue}`; } return escapedValue; }).join(''); } // Usage const terminalOutput = '\x1b[1;32m✓ Test passed\x1b[0m in \x1b[33m12ms\x1b[0m'; const html = ansiToHtml(terminalOutput); console.log(html); // Output: ✓ Test passed in 12ms ``` -------------------------------- ### Parse ANSI Sequences (Full Input) Source: https://github.com/blake-mealey/ansi-sequence-parser/blob/main/README.md Demonstrates how to parse an entire input string containing ANSI escape sequences into a list of tokens using the `parseAnsiSequences` function. This is suitable for processing complete data at once. ```ts import { parseAnsiSequences } from 'ansi-sequence-parser'; const tokens = parseAnsiSequences(input); ``` -------------------------------- ### Create Color Palette for ANSI Colors in TypeScript Source: https://context7.com/blake-mealey/ansi-sequence-parser/llms.txt Creates a color palette utility to convert parsed ANSI color objects (named, 256-color, or RGB) into hex color strings. This palette is useful for applications that need to render terminal output in environments that support hex color codes, such as HTML. ```typescript import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser'; const input = '\x1b[0;32mGreen\x1b[0;38;2;31;222;162mPastel Green\x1b[0;38;5;87mTable Cyan'; const tokens = parseAnsiSequences(input); const colorPalette = createColorPalette(); // Convert each token's colors to hex values const tokensWithHex = tokens.map((token) => ({ value: token.value, foregroundHex: token.foreground ? colorPalette.value(token.foreground) : null, backgroundHex: token.background ? colorPalette.value(token.background) : null, })); console.log(tokensWithHex); // Output: // [ // { value: 'Green', foregroundHex: '#00bb00', backgroundHex: null }, // { value: 'Pastel Green', foregroundHex: '#1fdea2', backgroundHex: null }, // { value: 'Table Cyan', foregroundHex: '#5fffff', backgroundHex: null } // ] ``` -------------------------------- ### Modify Default Named Colors Map Source: https://github.com/blake-mealey/ansi-sequence-parser/blob/main/README.md Explains how to modify the default named colors map by importing `defaultNamedColorsMap` and spreading it into a new color palette configuration. This allows for selective overrides of default colors. ```ts import { parseAnsiSequences, createColorPalette, defaultNamedColorsMap, } from 'ansi-sequence-parser'; const tokens = parseAnsiSequences(input); const colorPalette = createColorPalette({ ...defaultNamedColorsMap, blue: '#0000cc', }); ``` -------------------------------- ### Create Stateful ANSI Sequence Parser in TypeScript Source: https://context7.com/blake-mealey/ansi-sequence-parser/llms.txt Creates a stateful parser instance that maintains style state across multiple calls. This is ideal for processing streaming terminal output or text line-by-line, where ANSI sequences might span across chunks. The parser processes input in chunks and returns tokens for each chunk. ```typescript import { createAnsiSequenceParser } from 'ansi-sequence-parser'; // Create a stateful parser for multi-line processing const parser = createAnsiSequenceParser(); const multilineInput = `\x1b[0;32msome green text which wraps to the next line\x1b[0m then clears after that`; // Parse line by line while maintaining color state const lines = multilineInput.split(/\r?\n/); const tokensByLine = lines.map((line) => parser.parse(line)); console.log(tokensByLine); // Output: // [ // [{ value: 'some green text', foreground: { type: 'named', name: 'green' }, background: null, decorations: Set {} }], // [{ value: 'which wraps to the next line', foreground: { type: 'named', name: 'green' }, background: null, decorations: Set {} }], // [{ value: 'then clears after that', foreground: null, background: null, decorations: Set {} }] // ] ``` -------------------------------- ### ParseToken Interface Definition Source: https://context7.com/blake-mealey/ansi-sequence-parser/llms.txt Defines the structure of a ParseToken object returned by the ansi-sequence-parser. It includes the text value, foreground and background colors, and active text decorations. Colors can be named, table-based, or RGB. ```typescript import { ParseToken, Color, DecorationType } from 'ansi-sequence-parser'; // ParseToken structure interface ParseToken { value: string; // The text content foreground: Color | null; // Foreground color or null background: Color | null; // Background color or null decorations: Set; // Active decorations: bold, dim, italic, underline, reverse, hidden, strikethrough, overline } // Color types type Color = | { type: 'named'; name: ColorName } // e.g., 'red', 'brightBlue' | { type: 'table'; index: number } // 0-255 color table index | { type: 'rgb'; rgb: [number, number, number] }; // 24-bit RGB // Named colors: black, red, green, yellow, blue, magenta, cyan, white, // brightBlack, brightRed, brightGreen, brightYellow, // brightBlue, brightMagenta, brightCyan, brightWhite ``` -------------------------------- ### Parse ANSI Sequences in TypeScript Source: https://context7.com/blake-mealey/ansi-sequence-parser/llms.txt Parses an entire string containing ANSI escape sequences into an array of tokens. Each token includes the text value and its associated styling (foreground, background, decorations). This function is suitable for processing complete terminal output strings at once. ```typescript import { parseAnsiSequences } from 'ansi-sequence-parser'; // Parse terminal output with colors and decorations const input = '\x1b[0;32m✓\x1b[0m \x1b[0;1mTest passed\x1b[0m'; const tokens = parseAnsiSequences(input); console.log(tokens); // Output: // [ // { value: '✓', foreground: { type: 'named', name: 'green' }, background: null, decorations: Set {} }, // { value: ' ', foreground: null, background: null, decorations: Set {} }, // { value: 'Test passed', foreground: null, background: null, decorations: Set { 'bold' } } // ] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.