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
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!
npm install ansicolor
Typescript
Module System
Node Version
NPM Version
JavaScript (99.59%)
Shell (0.41%)
Total Downloads
7,965,617
Last Day
9,240
Last Week
43,332
Last Month
191,906
Last Year
2,112,981
121 Stars
295 Commits
17 Forks
7 Watching
2 Branches
10 Contributors
Minified
Minified + Gzipped
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
Publised On
03 Mar 2024
Cumulative downloads
Total Downloads
Last day
0.6%
9,240
Compared to previous day
Last week
-12%
43,332
Compared to previous week
Last month
12.5%
191,906
Compared to previous month
Last year
-4.1%
2,112,981
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.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
Found 4/16 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
14 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More