Socket
Socket
Sign inDemoInstall

ansi-sequence-parser

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ansi-sequence-parser

A parser for ANSI escape sequences


Version published
Weekly downloads
734K
decreased by-14.88%
Maintainers
1
Install size
21.4 kB
Created
Weekly downloads
 

Changelog

Source

1.1.1

Patch Changes

  • b5ccaf4: fix: don't infinite loop when parsing an unclosed sequence

Readme

Source

ansi-sequence-parser

Parse ANSI escape sequences into a readable format for things like generating pretty HTML.

Install with your favourite package manager:

pnpm install ansi-sequence-parser
yarn add ansi-sequence-parser
npm install ansi-sequence-parser

Parsing

Token format:

interface ParseToken {
  // The text content of the token
  value: string;
  // The foreground color
  foreground: Color | null;
  // The background color
  background: Color | null;
  // A Set of the applied decorations
  decorations: Set<DecorationType>;
}

Parse full input at once:

import { parseAnsiSequences } from 'ansi-sequence-parser';

const 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:

import { createAnsiSequenceParser } from 'ansi-sequence-parser';

const parser = createAnsiSequenceParser();

const tokensByLine = input.split(/\r?\n/).map((line) => parser.parse(line));

Colors

Colors format:

// A named ANSI color, e.g. `magenta` or `brightBlue`
export interface NamedColor {
  type: 'named';
  name: ColorName;
}

// A color-table lookup
export interface TableColor {
  type: 'table';
  index: number;
}

// An RGB color
export interface RgbColor {
  type: 'rgb';
  rgb: [number, number, number];
}

export 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:

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);
  }
}

You can also specify a custom named colors map:

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',
});

If you want to modify the default named colors map, you can import the defaultNamedColorsMap:

import {
  parseAnsiSequences,
  createColorPalette,
  defaultNamedColorsMap,
} from 'ansi-sequence-parser';

const tokens = parseAnsiSequences(input);
const colorPalette = createColorPalette({
  ...defaultNamedColorsMap,
  blue: '#0000cc',
});

Keywords

FAQs

Last updated on 26 Jul 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc