Installations
npm install ansicolor
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
20.11.1
NPM Version
10.2.4
Contributors
Unable to fetch Contributors
Languages
JavaScript (99.59%)
Shell (0.41%)
Developer
Download Statistics
Total Downloads
7,965,617
Last Day
9,240
Last Week
43,332
Last Month
191,906
Last Year
2,112,981
GitHub Statistics
121 Stars
295 Commits
17 Forks
7 Watching
2 Branches
10 Contributors
Bundle Size
5.88 kB
Minified
2.42 kB
Minified + Gzipped
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
7,965,617
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
ansicolor
A JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.
1npm install ansicolor
What For
- String coloring with ANSI escape codes
- Solves the style hierarchy problem (when other similar tools fail)
- Parsing/removing ANSI style data from strings
- Converting ANSI styles to CSS or a Chrome DevTools-compatible output
- A middleware for your platform-agnostic logging system
Why Another One?
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!
Crash Course
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'
Nice Mode (not recommended)
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)
Supported Styles
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', ...
Removing ANSI Styles From Strings
1const { strip } = require ('ansicolor') 2 3strip ('\u001b[0m\u001b[4m\u001b[42m\u001b[31mfoo\u001b[39m\u001b[49m\u001b[24mfoo\u001b[0m')) // 'foofoo'
Checking If Strings Contain ANSI Codes
1const { isEscaped, green } = require ('ansicolor') 2 3isEscaped ('text') // false 4isEscaped (green ('text')) // true
Converting to CSS/HTML
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' } ])
Custom Color Themes
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}
Chrome DevTools Compatibility
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!
Projects That Use ansicolor
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: The Unlicense: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/ci.yml:29
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
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:9: update your workflow using https://app.stepsecurity.io/secureworkflow/xpl/ansicolor/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/xpl/ansicolor/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:38: update your workflow using https://app.stepsecurity.io/secureworkflow/xpl/ansicolor/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/xpl/ansicolor/ci.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:19
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:48
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 npmCommand dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 18 are checked with a SAST tool
Reason
14 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
Score
3.1
/10
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 MoreOther packages similar to 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!