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

ansicolour

v1.1.0

Published

Fork of ansicolor with CSS closer to that used in devtools and a few fixes

Downloads

26

Readme

ansicolorBETA

A quality library for the ANSI color/style management.

npm install ansicolor

Why another one?

Other tools lack consistency, failing to solve the simple hierarchy problem:

require ('colors') // most popular color utility

console.log (('foo'.cyan + 'bar').red)

pic

WTF, bar is not rendered red! It sucks. Ansicolor arranges styles in stack and reconstructs proper linear form from that stack:

require ('ansicolor').nice // .nice for unsafe String extensions

console.log (('foo'.cyan + 'bar').red)

pic

Nice!

Cross-platform rendering

Other tools provide output (rendering), but not input (parsing). Inspection of ANSI colors in arbitrary strings is essential when implementing cross-platform logging — that works not only in terminal, but in browsers too. Modern browsers support color logging with console.log, but it does not understand ANSI colors — having a proprietary CSS-based format instead.

Ansicolor solves that problem by converting color codes to argument lists that are understandable by browser's consoles:

parsed = color.parse ('foo' + ('bar'.red.underline.bright.inverse + 'baz').bgGreen)

parsed.browserConsoleArguments /* = [
    "%cfoo%cbar%cbaz",
    "",
    "font-weight: bold;font-style: underline;background:rgba(255,51,0,1);color:rgba(0,204,0,1);",
    "background:rgba(0,204,0,1);"
] */

console.log (...parsed.browserConsoleArguments) // prints with colors in Chrome!

Crash course

String wrapping (safe):

color = require ('ansicolor')

console.log ('foo' + color.green (color.inverse (color.bgBrightCyan ('bar')) + 'baz') + 'qux')

String wrapping (unsafe):

require ('ansicolor').nice

console.log ('foo'.red.bright + 'bar'.bgYellow.underline.dim)

All supported options:

'foreground colors'
    .black.red.green.yellow.blue.magenta.cyan.white
'background colors'
    .bgBlack.bgRed.bgGreen.bgYellow.bgBlue.bgMagenta.bgCyan.bgWhite
'bright background colors'
    .bgBrightBlack.bgBrightRed.bgBrightGreen.bgBrightYellow.bgBrightBlue.bgBrightMagenta.bgBrightCyan.bgBrightWhite
'styles'
    .bright.dim.italic.underline.inverse // italic may lack support on your platform

Converting to CSS

Parsing arbitrary strings styled with ANSI escape codes:

parsed = color.parse ('foo'.bgBrightRed + 'bar')
                            

Will return a pseudo-array of styled spans (iterable with for ... of and convertable to an array with spread operator):

[{ css: 'background:rgba(255,51,0,1);', text: 'foo' },
 { css: '',                             text: 'bar' } ])]

Converting parsed array to argument list (acceptable by Chrome's console.log):

console.log (...parsed.browserConsoleArguments)

Happy logging!