Gathering detailed insights and metrics for ansi-sequence-parser
Gathering detailed insights and metrics for ansi-sequence-parser
Gathering detailed insights and metrics for ansi-sequence-parser
Gathering detailed insights and metrics for ansi-sequence-parser
npm install ansi-sequence-parser
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5 Stars
18 Commits
1 Forks
2 Watching
2 Branches
1 Contributors
Updated on 19 Dec 2023
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-9.9%
135,855
Compared to previous day
Last week
2.3%
844,332
Compared to previous week
Last month
13.9%
3,475,628
Compared to previous month
Last year
78.2%
42,064,342
Compared to previous year
4
ansi-sequence-parser
Parse ANSI escape sequences into a readable format for things like generating pretty HTML.
Install with your favourite package manager:
1pnpm install ansi-sequence-parser 2yarn add ansi-sequence-parser 3npm install ansi-sequence-parser
Token format:
1interface ParseToken { 2 // The text content of the token 3 value: string; 4 // The foreground color 5 foreground: Color | null; 6 // The background color 7 background: Color | null; 8 // A Set of the applied decorations 9 decorations: Set<DecorationType>; 10}
Parse full input at once:
1import { parseAnsiSequences } from 'ansi-sequence-parser'; 2 3const tokens = parseAnsiSequences(input);
If you want to parse your input in multiple chunks, make sure to create a parser so that you can maintain state between chunks:
1import { createAnsiSequenceParser } from 'ansi-sequence-parser'; 2 3const parser = createAnsiSequenceParser(); 4 5const tokensByLine = input.split(/\r?\n/).map((line) => parser.parse(line));
Colors format:
1// A named ANSI color, e.g. `magenta` or `brightBlue` 2export interface NamedColor { 3 type: 'named'; 4 name: ColorName; 5} 6 7// A color-table lookup 8export interface TableColor { 9 type: 'table'; 10 index: number; 11} 12 13// An RGB color 14export interface RgbColor { 15 type: 'rgb'; 16 rgb: [number, number, number]; 17} 18 19export type Color = NamedColor | TableColor | RgbColor;
In order to interpret all of the above color types as a hex code, you can create a color palette:
1import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser'; 2 3const tokens = parseAnsiSequences(input); 4const colorPalette = createColorPalette(); 5 6for (const token of tokens) { 7 if (token.foreground) { 8 const foregroundValue = colorPalette.value(token.foreground); 9 } 10 if (token.background) { 11 const backgroundValue = colorPalette.value(token.background); 12 } 13}
You can also specify a custom named colors map:
1import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser'; 2 3const tokens = parseAnsiSequences(input); 4const colorPalette = createColorPalette({ 5 black: '#000000', 6 red: '#bb0000', 7 green: '#00bb00', 8 yellow: '#bbbb00', 9 blue: '#0000bb', 10 magenta: '#ff00ff', 11 cyan: '#00bbbb', 12 white: '#eeeeee', 13 brightBlack: '#555555', 14 brightRed: '#ff5555', 15 brightGreen: '#00ff00', 16 brightYellow: '#ffff55', 17 brightBlue: '#5555ff', 18 brightMagenta: '#ff55ff', 19 brightCyan: '#55ffff', 20 brightWhite: '#ffffff', 21});
If you want to modify the default named colors map, you can import the defaultNamedColorsMap
:
1import { 2 parseAnsiSequences, 3 createColorPalette, 4 defaultNamedColorsMap, 5} from 'ansi-sequence-parser'; 6 7const tokens = parseAnsiSequences(input); 8const colorPalette = createColorPalette({ 9 ...defaultNamedColorsMap, 10 blue: '#0000cc', 11});
No vulnerabilities found.
No security vulnerabilities found.