Gathering detailed insights and metrics for ansicolor
Gathering detailed insights and metrics for ansicolor
Gathering detailed insights and metrics for ansicolor
Gathering detailed insights and metrics for ansicolor
A JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.
npm install ansicolor
Typescript
Module System
Node Version
NPM Version
99.5
Supply Chain
100
Quality
78.5
Maintenance
100
Vulnerability
100
License
JavaScript (99.59%)
Shell (0.41%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
9,969,475
Last Day
6,606
Last Week
113,543
Last Month
424,652
Last Year
3,261,672
Unlicense License
125 Stars
295 Commits
16 Forks
6 Watchers
2 Branches
10 Contributors
Updated on Sep 03, 2025
Latest Version
2.0.3
Package Id
ansicolor@2.0.3
Unpacked Size
103.17 kB
Size
27.29 kB
File Count
17
NPM Version
10.2.4
Node Version
20.11.1
Published on
Mar 03, 2024
Cumulative downloads
Total Downloads
Last Day
91%
6,606
Compared to previous day
Last Week
5%
113,543
Compared to previous week
Last Month
-3.1%
424,652
Compared to previous month
Last Year
67.6%
3,261,672
Compared to previous year
A JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.
1npm install ansicolor
Other tools lack consistency, failing to solve a simple hierarchy problem:
1require ('colors') // a popular color utility 2 3console.log (('foo'.cyan + 'bar').red)
WTF?! The bar
word above should be rendered in red, but it's not! That sucks. It's because ANSI codes are linear, not hierarchical (as with XML/HTML). A special kind of magic is needed to make this work. Ansicolor does that magic for you:
1require ('ansicolor').nice // .nice for unsafe String extensions 2 3console.log (('foo'.cyan + 'bar').red)
Nice!
Importing (as methods):
1import { green, inverse, bgLightCyan, underline, dim } from 'ansicolor'
1const { green, inverse, bgLightCyan, underline, dim } = require ('ansicolor')
Usage:
1console.log ('foo' + green (inverse (bgLightCyan ('bar')) + 'baz') + 'qux')
1console.log (underline.bright.green ('foo' + dim.red.bgLightCyan ('bar'))) // method chaining
Importing (as object):
1import { ansicolor, ParsedSpan } from 'ansicolor' // along with type definitions
1import ansicolor from 'ansicolor'
1const ansi = require ('ansicolor').nice
The ('ansicolor').nice
export defines styling APIs on the String
prototype directly. It uses an ad-hoc DSL (sort of) for infix-style string coloring. The nice
is convenient, but not safe, avoid using it in public modules, as it alters global objects, and that might cause potential hard-to-debug compatibility issues.
1console.log ('foo'.red.bright + 'bar'.bgYellow.underline.dim)
1'foreground colors' 2 .red.green.yellow.blue.magenta.cyan.white.darkGray.black
1'light foreground colors' 2 .lightRed.lightGreen.lightYellow.lightBlue.lightMagenta.lightCyan.lightGray
1'background colors' 2 .bgRed.bgGreen.bgYellow.bgBlue.bgMagenta.bgCyan.bgWhite.bgDarkGray.bgBlack
1'light background colors' 2 .bgLightRed.bgLightGreen.bgLightYellow.bgLightBlue.bgLightMagenta.bgLightCyan.bgLightGray
1'styles' 2 .bright.dim.italic.underline.inverse // your platform should support italic
You also can obtain all those style names (for reflection purposes):
1const { names } = require ('ansicolor') 2 3names // ['red', 'green', ...
1const { strip } = require ('ansicolor') 2 3strip ('\u001b[0m\u001b[4m\u001b[42m\u001b[31mfoo\u001b[39m\u001b[49m\u001b[24mfoo\u001b[0m')) // 'foofoo'
1const { isEscaped, green } = require ('ansicolor') 2 3isEscaped ('text') // false 4isEscaped (green ('text')) // true
Inspection of ANSI styles in arbitrary strings is essential when implementing platform-agnostic logging — that piece of code is available under command line interface and in a browser as well. Here's an example of how you would parse a colored string into an array-like structure. That structure can be traversed later to build HTML/JSON/XML or any other markup/syntax.
1const { parse } = require ('ansicolor') 2 3const parsed = parse ('foo'.bgLightRed.bright.italic + 'bar'.red.dim)
The ansi.parse ()
method will return a pseudo-array of styled spans, you can iterate over it with a for ... of
loop and convert it to an array with the spread operator (...
). Also, there's the .spans
property for obtaining the already-spread array directly:
1assert.deepEqual (parsed.spans /* or [...parsed] */, 2 3 [ { css: 'font-weight: bold;font-style: italic;background:rgba(255,51,0,1);', 4 italic: true, 5 bold: true, 6 color: { bright: true }, 7 bgColor: { name: 'lightRed' }, 8 text: 'foo' }, 9 10 { css: 'color:rgba(204,0,0,0.5);', 11 color: { name: 'red', dim: true }, 12 text: 'bar' } ])
You can change default RGB values (won't work in terminals, affects only the ANSI→CSS transformation part):
1const ansi = require ('ansicolor') 2 3ansi.rgb = { 4 5 black: [0, 0, 0], 6 darkGray: [100, 100, 100], 7 lightGray: [200, 200, 200], 8 white: [255, 255, 255], 9 10 red: [204, 0, 0], 11 lightRed: [255, 51, 0], 12 13 green: [0, 204, 0], 14 lightGreen: [51, 204, 51], 15 16 yellow: [204, 102, 0], 17 lightYellow: [255, 153, 51], 18 19 blue: [0, 0, 255], 20 lightBlue: [26, 140, 255], 21 22 magenta: [204, 0, 204], 23 lightMagenta: [255, 0, 255], 24 25 cyan: [0, 153, 255], 26 lightCyan: [0, 204, 255], 27}
Web browsers usually implement their own proprietary CSS-based color formats for console.log
and most of them fail to display standard ANSI colors. Ansicolor offers you a helper method to convert ANSI-styled strings to browser-compatible argument lists acceptable by Chrome's console.log
:
1const { bgGreen, red, parse } = require ('ansicolor') 2 3const string = 'foo' + bgGreen (red.underline.bright.inverse ('bar') + 'baz') 4const parsed = parse (string) 5 6console.log (...parsed.asChromeConsoleLogArguments) // prints with colors in Chrome!
Here's what the format looks like:
1parsed.asChromeConsoleLogArguments // [ "%cfoo%cbar%cbaz", 2 // "", 3 // "font-weight: bold;text-decoration: underline;background:rgba(255,51,0,1);color:rgba(0,204,0,1);", 4 // "background:rgba(0,204,0,1);" 5 // ]
Play with this feature online: demo page. Open the DevTools console and type expressions in the input box to see colored console output.
Happy logging!
ansicolor
No vulnerabilities found.
ansicolors
Functions that surround a string with ansicolor codes so it prints in color.
@types/ansicolors
TypeScript definitions for ansicolors
colorfy
Colorfy colorize your console with pretty ansi color codes. It has static color methods and supports a chained colorization of your tty console output.
ansicolor-utils
A util for ANSI colors. Encoding and Decoding ANSI colors & more!