npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ansi-sequence-parser

v1.1.1

Published

A parser for ANSI escape sequences

Downloads

3,156,749

Readme

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