Easily add ANSI colors to your text and symbols in the terminal. ansi-colors is the official ansi styling library for gulp. Used by hundreds of projects, including enquirer, vscode, codeql, azure data studio, aws-cdk, redwoodjs, leaflet, mocha, and many others.
Installations
npm install ansi-colors
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=6
Node Version
17.0.0
NPM Version
8.1.0
Score
99.1
Supply Chain
100
Quality
78.4
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (88.1%)
TypeScript (11.9%)
Developer
doowb
Download Statistics
Total Downloads
6,941,380,066
Last Day
2,691,628
Last Week
24,368,376
Last Month
129,804,367
Last Year
1,536,173,395
GitHub Statistics
445 Stars
124 Commits
31 Forks
6 Watching
1 Branches
12 Contributors
Package Meta Information
Latest Version
4.1.3
Package Id
ansi-colors@4.1.3
Unpacked Size
25.53 kB
Size
8.53 kB
File Count
6
NPM Version
8.1.0
Node Version
17.0.0
Total Downloads
Cumulative downloads
Total Downloads
6,941,380,066
Last day
-53.1%
2,691,628
Compared to previous day
Last week
-19.6%
24,368,376
Compared to previous week
Last month
-4.9%
129,804,367
Compared to previous month
Last year
-3.1%
1,536,173,395
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
5
ansi-colors
Easily add ANSI colors to your text and symbols in the terminal. A faster drop-in replacement for chalk, kleur and turbocolor (without the dependencies and rendering bugs).
Please consider following this project's author, Brian Woodward, and consider starring the project to show your :heart: and support.
Install
Install with npm:
1$ npm install --save ansi-colors
Why use this?
ansi-colors is the fastest Node.js library for terminal styling. A more performant drop-in replacement for chalk, with no dependencies.
-
Blazing fast - Fastest terminal styling library in node.js, 10-20x faster than chalk!
-
Drop-in replacement for chalk.
-
No dependencies (Chalk has 7 dependencies in its tree!)
-
Safe - Does not modify the
String.prototype
like colors. -
Supports nested colors, and does not have the nested styling bug that is present in colorette, chalk, and kleur.
-
Supports chained colors.
-
Toggle color support on or off.
Usage
1const c = require('ansi-colors'); 2 3console.log(c.red('This is a red string!')); 4console.log(c.green('This is a red string!')); 5console.log(c.cyan('This is a cyan string!')); 6console.log(c.yellow('This is a yellow string!'));
Chained colors
1console.log(c.bold.red('this is a bold red message')); 2console.log(c.bold.yellow.italic('this is a bold yellow italicized message')); 3console.log(c.green.bold.underline('this is a bold green underlined message'));
Nested colors
1console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`));
Nested styling bug
ansi-colors
does not have the nested styling bug found in colorette, chalk, and kleur.
1const { bold, red } = require('ansi-styles'); 2console.log(bold(`foo ${red.dim('bar')} baz`)); 3 4const colorette = require('colorette'); 5console.log(colorette.bold(`foo ${colorette.red(colorette.dim('bar'))} baz`)); 6 7const kleur = require('kleur'); 8console.log(kleur.bold(`foo ${kleur.red.dim('bar')} baz`)); 9 10const chalk = require('chalk'); 11console.log(chalk.bold(`foo ${chalk.red.dim('bar')} baz`));
Results in the following
(sans icons and labels)
Toggle color support
Easily enable/disable colors.
1const c = require('ansi-colors'); 2 3// disable colors manually 4c.enabled = false; 5 6// or use a library to automatically detect support 7c.enabled = require('color-support').hasBasic; 8 9console.log(c.red('I will only be colored red if the terminal supports colors'));
Strip ANSI codes
Use the .unstyle
method to strip ANSI codes from a string.
1console.log(c.unstyle(c.blue.bold('foo bar baz'))); 2//=> 'foo bar baz'
Available styles
Note that bright and bright-background colors are not always supported.
Colors | Background Colors | Bright Colors | Bright Background Colors |
---|---|---|---|
black | bgBlack | blackBright | bgBlackBright |
red | bgRed | redBright | bgRedBright |
green | bgGreen | greenBright | bgGreenBright |
yellow | bgYellow | yellowBright | bgYellowBright |
blue | bgBlue | blueBright | bgBlueBright |
magenta | bgMagenta | magentaBright | bgMagentaBright |
cyan | bgCyan | cyanBright | bgCyanBright |
white | bgWhite | whiteBright | bgWhiteBright |
gray | |||
grey |
(gray
is the U.S. spelling, grey
is more commonly used in the Canada and U.K.)
Style modifiers
-
dim
-
bold
-
hidden
-
italic
-
underline
-
inverse
-
strikethrough -
reset
Aliases
Create custom aliases for styles.
1const colors = require('ansi-colors'); 2 3colors.alias('primary', colors.yellow); 4colors.alias('secondary', colors.bold); 5 6console.log(colors.primary.secondary('Foo'));
Themes
A theme is an object of custom aliases.
1const colors = require('ansi-colors'); 2 3colors.theme({ 4 danger: colors.red, 5 dark: colors.dim.gray, 6 disabled: colors.gray, 7 em: colors.italic, 8 heading: colors.bold.underline, 9 info: colors.cyan, 10 muted: colors.dim, 11 primary: colors.blue, 12 strong: colors.bold, 13 success: colors.green, 14 underline: colors.underline, 15 warning: colors.yellow 16}); 17 18// Now, we can use our custom styles alongside the built-in styles! 19console.log(colors.danger.strong.em('Error!')); 20console.log(colors.warning('Heads up!')); 21console.log(colors.info('Did you know...')); 22console.log(colors.success.bold('It worked!'));
Performance
Libraries tested
- ansi-colors v3.0.4
- chalk v2.4.1
Mac
MacBook Pro, Intel Core i7, 2.3 GHz, 16 GB.
Load time
Time it takes to load the first time require()
is called:
- ansi-colors -
1.915ms
- chalk -
12.437ms
Benchmarks
# All Colors
ansi-colors x 173,851 ops/sec ±0.42% (91 runs sampled)
chalk x 9,944 ops/sec ±2.53% (81 runs sampled)))
# Chained colors
ansi-colors x 20,791 ops/sec ±0.60% (88 runs sampled)
chalk x 2,111 ops/sec ±2.34% (83 runs sampled)
# Nested colors
ansi-colors x 59,304 ops/sec ±0.98% (92 runs sampled)
chalk x 4,590 ops/sec ±2.08% (82 runs sampled)
Windows
Windows 10, Intel Core i7-7700k CPU @ 4.2 GHz, 32 GB
Load time
Time it takes to load the first time require()
is called:
- ansi-colors -
1.494ms
- chalk -
11.523ms
Benchmarks
# All Colors
ansi-colors x 193,088 ops/sec ±0.51% (95 runs sampled))
chalk x 9,612 ops/sec ±3.31% (77 runs sampled)))
# Chained colors
ansi-colors x 26,093 ops/sec ±1.13% (94 runs sampled)
chalk x 2,267 ops/sec ±2.88% (80 runs sampled))
# Nested colors
ansi-colors x 67,747 ops/sec ±0.49% (93 runs sampled)
chalk x 4,446 ops/sec ±3.01% (82 runs sampled))
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Related projects
You might also be interested in these projects:
- ansi-wrap: Create ansi colors by passing the open and close codes. | homepage
- strip-color: Strip ANSI color codes from a string. No dependencies. | homepage
Contributors
Commits | Contributor |
---|---|
48 | jonschlinkert |
42 | doowb |
6 | lukeed |
2 | Silic0nS0ldier |
1 | dwieeb |
1 | jorgebucaran |
1 | madhavarshney |
1 | chapterjason |
Author
Brian Woodward
License
Copyright © 2019, Brian Woodward. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on July 01, 2019.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 4/23 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
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
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
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-12-23
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