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

parse-ansi

v1.0.3

Published

🤖 Parse ANSI text into an array of ansi-tags and text-chunks.

Downloads

1,388

Readme

parse-ansi

🤖 Parse ansi into an array of ansi-tags and text-chunks.

Build Status Coverage Status NPM Version XO code style

Parse-ANSI takes an ANSI string as input:

const text = "🤖\u001B[31m DANGER\u001B[0m Will Robbinson"
console.log(text)

Danger Will Robbinson

When you run your text through the ANSI parser...

const parseAnsi = require('parse-ansi')
const parsed = parseAnsi(text)
console.log(parsed)

...Parse-ANSI outputs value, position and style data.

{
    raw: '🤖\u001B[31m DANGER\u001B[0m Will Robbinson',    
    plain: '🤖 DANGER Will Robbinson',
    textArea: {columns: 24, rows: 1},
    chunks: [{
        type: 'text',
        value: '🤖',
        position: {x: 0, y: 0, n: 0, raw: 0},
        style: {}
    }, {
        type: 'ansi',
        value: {tag: 'red', ansi: '\u001b[31m'},
        position: {x: 2, y: 0, n: 2, raw: 2}
    }, {
        type: 'text',
        value: ' DANGER',
        position: {x: 2,y: 0,n: 2, raw: 7},
        style: {
            foregroundColor: 'red'
        }
    }, {
        type: 'ansi',
        value: {tag: 'reset', ansi: '\u001b[0m'},
        position: {x: 9, y: 0, n: 9, raw: 14}
    }, {
        type: 'text',
        value: ' Will Robbinson',
        position: {x: 9, y: 0, n: 9, raw: 18},
        style: {}
    }]
}

This data can be used to convert ANSI sequences to other formatsm such as HTML, Image, SVG, etc.

Chunk

Each object in the output array is called a "chunk". Each chunk represents one or more of the following data types.

  1. ansi - ANSI escape sequence
  2. newline - Newline character
  3. text - Text chunk of like-styles

Style

The style object contains a list of styles associated with the current chunk. Each style represents an ANSI Escape sequence that is mapped to friendly name called an ANSI-Tag.

  • Styles are only included in text chunks.
  • Styles that are off/closed, are not present in the style object.

The following style object describes a chunk of red text:

{
    foregroundColor: 'red'
}

This object shows alls styles in combination:

{
    backgroundColor: 'bgRed',
    foregroundColor: 'white',
    dim: true,
    bold: true,
    italic: true,
    underline: true,
    strikethrough: true,
    inverse: true
}

Styles that are closed or reset are not included in the style object.

For example:

             // Turn on Bold
const text = '\u001b[1m BOLD' +
    // Turn off all styles
    '\u001b[0m NORMAL'

const parsed = parseAnsi(text)

const styles = parsed
    .filter(chunk => chunk.style)
    .map(chunk => chunk.style)

console.log(styles)

The above code should log:

[
    // Turn on Bold
    { bold: true },

    // Turn off all styles
    {}
]

Position

{x: 2, y: 0, n: 2, raw: 2}

The position object contains 4 kinds of position:

  1. x - Plain-text column at which the chunk starts
  2. y - Plain-text crow at which the chunk starts
  3. n - Linear, 1-dimensional plain-text position at which the chunk starts.
  4. raw - Linear 1-dimensional position at which the chunk starts for ANSI or plain-text. This is the real JavaScript position string position of the chunk.

Value

Text Value

The value of a text chunk is a JavaScript string. The value of a text chunk should never contain any ANSI escape sequences.

{
    type: 'text',
    value: ' DANGER',
}

ANSI Value

The value of an ansi chunk is an object.

  • value.tag - Friendly-named ansi-tag.
  • value.ansi - Raw ANSI string value.
{
    type: 'ansi',
    value: {
        tag: 'red',
        ansi: '\u001b[31m'
    }
}

You can find the list ansi-tags in types/types.ansi-seqs-to-ansi-tags.js.

Install

$ yarn add parse-ansi