kleur vs ansi-colors
Terminal Color Libraries Comparison
1 Year
kleuransi-colorsSimilar Packages:
What's Terminal Color Libraries?

Terminal color libraries are essential tools for developers who want to enhance the visual output of their command-line applications. They allow for the easy application of colors and styles to text output, making it more readable and visually appealing. These libraries help in debugging, logging, and providing user feedback in a more engaging manner. By using these libraries, developers can create a more interactive and user-friendly command-line interface, which can significantly improve the overall user experience.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
kleur38,363,5171,65720.3 kB13-MIT
ansi-colors35,553,51844826.1 kB15-MIT
Feature Comparison: kleur vs ansi-colors

Ease of Use

  • kleur:

    kleur also offers an intuitive API but includes additional features like chaining and support for more complex color manipulations. This makes it slightly more versatile for users who need advanced styling options.

  • ansi-colors:

    ansi-colors provides a simple API that allows developers to easily apply colors and styles to strings. It uses a straightforward syntax, making it accessible for beginners and quick to implement in scripts.

Performance

  • kleur:

    kleur is designed with performance in mind as well, but it may introduce slight overhead due to its additional features. However, for most applications, this overhead is negligible compared to the benefits it provides.

  • ansi-colors:

    ansi-colors is optimized for speed and has a minimal footprint, making it ideal for performance-critical applications where every millisecond counts. Its simplicity contributes to its efficiency.

Color Support

  • kleur:

    kleur supports a broader range of colors, including 256 colors and true color (24-bit). This allows for more vibrant and visually appealing outputs, catering to developers who want to create a more immersive terminal experience.

  • ansi-colors:

    ansi-colors supports basic ANSI colors and styles, which is sufficient for many terminal applications. It allows for easy customization of text color and background color, but is limited to standard ANSI options.

Extensibility

  • kleur:

    kleur offers more extensibility with its chaining capabilities and support for custom styles. This makes it easier to create complex styling scenarios, allowing developers to build upon its foundation.

  • ansi-colors:

    ansi-colors is a lightweight library with limited extensibility. It is designed to do one thing well—coloring text—without additional features that could complicate its use.

Community and Maintenance

  • kleur:

    kleur has a growing community and is also actively maintained. Its modern design and features attract more users, which can lead to better support and more frequent updates.

  • ansi-colors:

    ansi-colors has a stable community and is actively maintained, ensuring that it remains compatible with the latest Node.js versions and terminal standards.

How to Choose: kleur vs ansi-colors
  • kleur:

    Choose kleur if you are looking for a more modern and feature-rich library that supports a wider range of color formats and styles. Kleur is designed for better performance and offers a more extensive API for advanced styling options.

  • ansi-colors:

    Choose ansi-colors if you need a lightweight and straightforward solution for adding colors to your terminal output without any additional dependencies. It is particularly useful for simple projects or scripts where minimalism is prioritized.

README for kleur
kleur
The fastest Node.js library for formatting terminal text with ANSI colors~!

Features


As of v3.0 the Chalk-style syntax (magical getter) is no longer used.
Please visit History for migration paths supporting that syntax.


Install

$ npm install --save kleur

Usage

import kleur from 'kleur';

// basic usage
kleur.red('red text');

// chained methods
kleur.blue().bold().underline('howdy partner');

// nested methods
kleur.bold(`${ white().bgRed('[ERROR]') } ${ kleur.red().italic('Something happened')}`);

Chained Methods

const { bold, green } = require('kleur');

console.log(bold().red('this is a bold red message'));
console.log(bold().italic('this is a bold italicized message'));
console.log(bold().yellow().bgRed().italic('this is a bold yellow italicized message'));
console.log(green().bold().underline('this is a bold green underlined message'));

Nested Methods

const { yellow, red, cyan } = require('kleur');

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

Conditional Support

Toggle color support as needed; kleur includes simple auto-detection which may not cover all cases.

Note: Both kleur and kleur/colors share the same detection logic.

import kleur from 'kleur';

// manually disable
kleur.enabled = false;

// or use another library to detect support
kleur.enabled = require('color-support').level > 0;

console.log(kleur.red('I will only be colored red if the terminal supports colors'));

Important:
Colors will be disabled automatically in non TTY contexts. For example, spawning another process or piping output into another process will disable colorization automatically. To force colors in your piped output, you may do so with the FORCE_COLOR=1 environment variable:

$ node app.js #=> COLORS
$ node app.js > log.txt  #=> NO COLORS
$ FORCE_COLOR=1 node app.js > log.txt #=> COLORS
$ FORCE_COLOR=0 node app.js > log.txt #=> NO COLORS

API

Any kleur method returns a String when invoked with input; otherwise chaining is expected.

It's up to the developer to pass the output to destinations like console.log, process.stdout.write, etc.

The methods below are grouped by type for legibility purposes only. They each can be chained or nested with one another.

Colors:

black — red — green — yellow — blue — magenta — cyan — white — gray — grey

Backgrounds:

bgBlack — bgRed — bgGreen — bgYellow — bgBlue — bgMagenta — bgCyan — bgWhite

Modifiers:

reset — bold — dim — italic* — underline — inverse — hidden — strikethrough*

* Not widely supported

Individual Colors

When you only need a few colors, it doesn't make sense to import all of kleur because, as small as it is, kleur is not treeshakeable, and so most of its code will be doing nothing. In order to fix this, you can import from the kleur/colors submodule which fully supports tree-shaking.

The caveat with this approach is that color functions are not chainable~!
Each function receives and colorizes its input. You may combine colors, backgrounds, and modifiers by nesting function calls within other functions.

// or: import * as kleur from 'kleur/colors';
import { red, underline, bgWhite } from 'kleur/colors';

red('red text');
//~> kleur.red('red text');

underline(red('red underlined text'));
//~> kleur.underline().red('red underlined text');

bgWhite(underline(red('red underlined text w/ white background')));
//~> kleur.bgWhite().underline().red('red underlined text w/ white background');

Note: All the same colors, backgrounds, and modifiers are available.

Conditional Support

The kleur/colors submodule also allows you to toggle color support, as needed.
It includes the same initial assumptions as kleur, in an attempt to have colors enabled by default.

Unlike kleur, this setting exists as kleur.$.enabled instead of kleur.enabled:

import * as kleur from 'kleur/colors';
// or: import { $, red } from 'kleur/colors';

// manually disabled
kleur.$.enabled = false;

// or use another library to detect support
kleur.$.enabled = require('color-support').level > 0;

console.log(red('I will only be colored red if the terminal supports colors'));

Benchmarks

Using Node v10.13.0

Load time

chalk        :: 5.303ms
kleur        :: 0.488ms
kleur/colors :: 0.369ms
ansi-colors  :: 1.504ms

Performance

# All Colors
  ansi-colors      x 177,625 ops/sec ±1.47% (92 runs sampled)
  chalk            x 611,907 ops/sec ±0.20% (92 runs sampled)
  kleur            x 742,509 ops/sec ±1.47% (93 runs sampled)
  kleur/colors     x 881,742 ops/sec ±0.19% (98 runs sampled)

# Stacked colors
  ansi-colors      x  23,331 ops/sec ±1.81% (94 runs sampled)
  chalk            x 337,178 ops/sec ±0.20% (98 runs sampled)
  kleur            x  78,299 ops/sec ±1.01% (97 runs sampled)
  kleur/colors     x 104,431 ops/sec ±0.22% (97 runs sampled)

# Nested colors
  ansi-colors      x  67,181 ops/sec ±1.15% (92 runs sampled)
  chalk            x 116,361 ops/sec ±0.63% (94 runs sampled)
  kleur            x 139,514 ops/sec ±0.76% (95 runs sampled)
  kleur/colors     x 145,716 ops/sec ±0.97% (97 runs sampled)

History

This project originally forked ansi-colors.

Beginning with kleur@3.0, the Chalk-style syntax (magical getter) has been replaced with function calls per key:

// Old:
c.red.bold.underline('old');

// New:
c.red().bold().underline('new');

As I work more with Rust, the newer syntax feels so much better & more natural!

If you prefer the old syntax, you may migrate to ansi-colors or newer chalk releases.
Versions below kleur@3.0 have been officially deprecated.

License

MIT © Luke Edwards