What is ansi-colors?
The ansi-colors npm package is a utility for styling strings in the terminal with ANSI escape codes. It allows developers to add color and style to text output in command-line applications.
What are ansi-colors's main functionalities?
Text Colors
Apply text color to strings. The example shows how to make text red.
"const colors = require('ansi-colors');\nconsole.log(colors.red('This is red text'));"
Background Colors
Apply background color to strings. The example shows how to give text a red background.
"const colors = require('ansi-colors');\nconsole.log(colors.bgRed('This has a red background'));"
Text Styles
Apply text styles like bold, italic, underline, etc. The example shows how to make text bold.
"const colors = require('ansi-colors');\nconsole.log(colors.bold('This is bold text'));"
Chaining Styles
Chain multiple styles together. The example shows text that is blue, bold, and underlined.
"const colors = require('ansi-colors');\nconsole.log(colors.blue.bold.underline('This is blue, bold, and underlined'));"
Custom Themes
Create custom themes by combining styles. The example defines a custom theme with styles for error and warning messages.
"const colors = require('ansi-colors');\nconst customTheme = {\n error: colors.red.bold,\n warning: colors.yellow.italic\n};\nconsole.log(customTheme.error('Error message'));\nconsole.log(customTheme.warning('Warning message'));"
Other packages similar to ansi-colors
chalk
Chalk is a popular package similar to ansi-colors that provides a chainable API for styling strings. It offers a more extensive API and additional features like template literal support.
kleur
Kleur is a lightweight alternative to ansi-colors, focusing on performance. It has a similar API but does not support older versions of Node.js.
colorette
Colorette is another lightweight package for coloring terminal text. It aims to be fast and simple, with a minimalistic API compared to ansi-colors.
ansi-colors
Collection of ansi colors and styles.
Please consider following this project's author, Brian Woodward, and consider starring the project to show your :heart: and support.
Install
Install with npm:
$ npm install --save ansi-colors
ansi-colors is a Node.js library for adding colors to text in the terminal. A more performant drop-in replacement for chalk, with no dependencies.
Why use this?
See a comparison to other libraries
Usage
const c = require('ansi-colors');
console.log(c.red('This is a red string!'));
console.log(c.green('This is a red string!'));
console.log(c.cyan('This is a cyan string!'));
console.log(c.yellow('This is a yellow string!'));
Features
Colors take multiple arguments.
console.log(c.red('Some', 'red', 'text', 'to', 'display'));
Chained styles
Supports chained styles.
console.log(c.bold.red('this is a bold red message'));
console.log(c.bold.yellow.italic('this is a bold yellow italicized message'));
console.log(c.green.bold.underline('this is a bold green underlined message'));
Nested styles
Supports nested styles.
console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`));
console.log(c.yellow('foo', c.red.bold('red'), 'bar', c.cyan('cyan'), 'baz'));
Conditional color support
Easily enable/disable colors.
const c = require('ansi-colors');
c.enabled = false;
c.enabled = require('color-support').stdout;
console.log(c.red('I will only be colored red if the terminal supports colors'));
printf-like formatting
Uses node's built-in util.format() to achieve printf-like formatting. The first argument is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the converted value from the corresponding argument.
console.log(c.bold.red('%s:%s', 'foo', 'bar', 'baz'));
Even works with nested colors!
console.log(c.bold('%s:%s:%s', 'foo', c.red('bar'), 'baz'));
Strip ANSI codes
Use the .unstyle
method to manually strip ANSI codes from a string.
console.log(c.unstyle(c.blue.bold('foo bar baz')));
Available styles
Note that bright and bright-background colors are not always supported.
Colors
black
blue
cyan
gray
(U.S.) and grey
(everyone else)green
magenta
red
white
yellow
Bright colors
blueBright
cyanBright
greenBright
magentaBright
redBright
whiteBright
yellowBright
Background colors
bgBlack
bgBlue
bgCyan
bgGreen
bgMagenta
bgRed
bgWhite
bgYellow
Bright background colors
bgBlackBright
bgBlueBright
bgCyanBright
bgGreenBright
bgMagentaBright
bgRedBright
bgWhiteBright
bgYellowBright
Modifiers
Symbols
A handful of common useful symbols are available on the c.symbols
property.
console.log(c.symbols);
Available symbols
Windows
- check:
√
- cross:
×
- ellipsis:
'...
- info:
i
- line:
─
- pointer:
'>
- pointerSmall:
»
- question:
?
- questionSmall:
﹖
- warning:
‼
Other platforms
- check:
✔
- cross:
✖
- ellipsis:
…
- info:
ℹ
- line:
─
- pointer:
❯
- pointerSmall:
›
- question:
?
- questionSmall:
﹖
- warning:
⚠
Benchmarks
MacBook Pro, Intel Core i7, 2.3 GHz, 16 GB.
Load time
Time it takes to load the module the first time:
chalk: 11.795ms
clorox: 1.019ms
ansi-colors: 0.867ms
Performance
# All Colors
ansi-colors x 104,559 ops/sec ±0.80% (91 runs sampled)
chalk x 8,869 ops/sec ±2.09% (82 runs sampled)
clorox x 1,315 ops/sec ±2.19% (76 runs sampled)
# Stacked colors
ansi-colors x 36,208 ops/sec ±0.65% (88 runs sampled)
chalk x 1,789 ops/sec ±1.77% (80 runs sampled)
clorox x 528 ops/sec ±2.00% (78 runs sampled)
# Nested colors
ansi-colors x 42,828 ops/sec ±0.36% (90 runs sampled)
chalk x 4,082 ops/sec ±1.93% (83 runs sampled)
clorox x 628 ops/sec ±2.10% (60 runs sampled)
Comparison
Feature | ansi-colors | chalk | clorox | colors |
---|
Nested colors | yes | yes | no | yes |
Chained colors | yes | yes | You must call .toString() on result | yes |
Toggle color support | yes | yes | no | yes |
printf-like formatting | yes | no | no | no |
Includes symbols | yes | no | no | no |
About
Related projects
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
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:
$ npm install -g verbose/verb
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:
$ npm install && npm test
Author
Brian Woodward
License
Copyright © 2018, Brian Woodward.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.7.0, on July 16, 2018.