ansi-colors vs chalk
Terminal String Styling
ansi-colorschalkSimilar Packages:

Terminal String Styling

ansi-colors and chalk are popular Node.js libraries used for styling terminal output with colors, backgrounds, and various text effects. They enhance the visual presentation of command-line applications, making it easier to convey information, highlight warnings, or display errors in a more engaging way. Both libraries provide simple APIs for applying styles to strings, but they differ in features, size, and design philosophy. ansi-colors is a lightweight library that focuses on providing a comprehensive set of color and style functions without any dependencies. It allows developers to easily apply ANSI styles to strings using a straightforward syntax. On the other hand, chalk is a more feature-rich library that offers a chainable API for styling text, supports nested styles, and provides a more modern and expressive way to work with colors in the terminal. While chalk is slightly larger in size, it offers additional features like color manipulation, theming, and support for true color (16 million colors), making it a popular choice for projects that require more advanced styling capabilities.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
ansi-colors045126.1 kB19-MIT
chalk023,02344.3 kB56 months agoMIT

Feature Comparison: ansi-colors vs chalk

Size and Performance

  • ansi-colors:

    ansi-colors is a very small library (around 1KB minified) that provides a wide range of color and style functions without any dependencies. Its lightweight nature means it has minimal impact on performance, making it an excellent choice for scripts and applications where speed and efficiency are important.

  • chalk:

    chalk is larger than ansi-colors, but it offers a rich set of features that justify the size. The performance impact is generally negligible for most applications, but developers should be aware that the additional features may introduce some overhead, especially in performance-critical environments.

API Design

  • ansi-colors:

    ansi-colors provides a simple and intuitive API for applying colors and styles to strings. It uses a straightforward function-based approach, allowing developers to easily apply styles without any complex setup. The API is designed to be easy to use and understand, making it accessible for developers of all skill levels.

  • chalk:

    chalk offers a more modern and expressive API that supports chaining and nested styles. Its design allows for more complex styling with less code, making it a favorite among developers who appreciate a more elegant and flexible approach to styling terminal output. The API is well-documented and designed for ease of use.

Styling Capabilities

  • ansi-colors:

    ansi-colors supports a wide range of ANSI colors, styles, and effects, including bold, italic, underline, and various foreground and background colors. It provides a comprehensive set of functions for styling text, but it does not support advanced features like true color or nested styles out of the box.

  • chalk:

    chalk supports a wide range of styling capabilities, including true color (16 million colors), nested styles, and custom themes. It is more versatile and feature-rich compared to ansi-colors, making it suitable for applications that require more advanced styling options. chalk also allows for more creative and complex styling, which can enhance the visual appeal of terminal output.

True Color Support

  • ansi-colors:

    ansi-colors does not natively support true color, but it allows for the use of 256 colors and provides a simple way to define custom colors using ANSI escape codes. This makes it flexible, but it may require more manual work to achieve true color effects.

  • chalk:

    chalk supports true color (16 million colors) out of the box, allowing for much more precise and vibrant color styling. This feature makes chalk a better choice for applications that need high-quality color output and more detailed color customization.

Example Code

  • ansi-colors:

    Basic usage of ansi-colors

    const colors = require('ansi-colors');
    
    console.log(colors.red('This is red text')); // Red text
    console.log(colors.bgBlue('This has a blue background')); // Blue background
    console.log(colors.bold('This is bold text')); // Bold text
    console.log(colors.underline('This is underlined text')); // Underlined text
    
  • chalk:

    Basic usage of chalk

    const chalk = require('chalk');
    
    console.log(chalk.red('This is red text')); // Red text
    console.log(chalk.bgBlue('This has a blue background')); // Blue background
    console.log(chalk.bold('This is bold text')); // Bold text
    console.log(chalk.underline('This is underlined text')); // Underlined text
    
    // Nested styles
    console.log(chalk.red('This is red') + ' and ' + chalk.blue('this is blue')); // Red and blue text
    
    // True color example
    console.log(chalk.rgb(255, 0, 0)('This is red using true color')); // True color red
    

How to Choose: ansi-colors vs chalk

  • ansi-colors:

    Choose ansi-colors if you need a lightweight, dependency-free solution for adding basic to advanced ANSI colors and styles to your terminal output. It is ideal for projects where simplicity and performance are priorities, and you want to avoid adding extra weight to your application.

  • chalk:

    Choose chalk if you require a more feature-rich and expressive API for styling terminal output. It is particularly useful for projects that need advanced styling capabilities, such as nested styles, color manipulation, and theming. While it has a larger footprint, the additional features may be worth it for more complex applications.

README for ansi-colors

ansi-colors Donate NPM version NPM monthly downloads NPM total downloads Linux Build Status

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:

$ npm install --save ansi-colors

image

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.

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!'));

image

Chained colors

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'));

image

Nested colors

console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`));

image

Nested styling bug

ansi-colors does not have the nested styling bug found in colorette, chalk, and kleur.

const { bold, red } = require('ansi-styles');
console.log(bold(`foo ${red.dim('bar')} baz`));

const colorette = require('colorette');
console.log(colorette.bold(`foo ${colorette.red(colorette.dim('bar'))} baz`));

const kleur = require('kleur');
console.log(kleur.bold(`foo ${kleur.red.dim('bar')} baz`));

const chalk = require('chalk');
console.log(chalk.bold(`foo ${chalk.red.dim('bar')} baz`));

Results in the following

(sans icons and labels)

image

Toggle color support

Easily enable/disable colors.

const c = require('ansi-colors');

// disable colors manually
c.enabled = false;

// or use a library to automatically detect support
c.enabled = require('color-support').hasBasic;

console.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.

console.log(c.unstyle(c.blue.bold('foo bar baz')));
//=> 'foo bar baz'

Available styles

Note that bright and bright-background colors are not always supported.

ColorsBackground ColorsBright ColorsBright Background Colors
blackbgBlackblackBrightbgBlackBright
redbgRedredBrightbgRedBright
greenbgGreengreenBrightbgGreenBright
yellowbgYellowyellowBrightbgYellowBright
bluebgBlueblueBrightbgBlueBright
magentabgMagentamagentaBrightbgMagentaBright
cyanbgCyancyanBrightbgCyanBright
whitebgWhitewhiteBrightbgWhiteBright
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.

const colors = require('ansi-colors');

colors.alias('primary', colors.yellow);
colors.alias('secondary', colors.bold);

console.log(colors.primary.secondary('Foo'));

Themes

A theme is an object of custom aliases.

const colors = require('ansi-colors');

colors.theme({
  danger: colors.red,
  dark: colors.dim.gray,
  disabled: colors.gray,
  em: colors.italic,
  heading: colors.bold.underline,
  info: colors.cyan,
  muted: colors.dim,
  primary: colors.blue,
  strong: colors.bold,
  success: colors.green,
  underline: colors.underline,
  warning: colors.yellow
});

// Now, we can use our custom styles alongside the built-in styles!
console.log(colors.danger.strong.em('Error!'));
console.log(colors.warning('Heads up!'));
console.log(colors.info('Did you know...'));
console.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:

$ 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:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

CommitsContributor
48jonschlinkert
42doowb
6lukeed
2Silic0nS0ldier
1dwieeb
1jorgebucaran
1madhavarshney
1chapterjason

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.