Kleur

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

README

kleur
version CI downloads install size
The fastest Node.js library for formatting terminal text with ANSI colors~!

Features


No dependencies
Supports nested & chained colors
No String.prototype modifications
Conditional color support
Familiar API


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



Install


  1. ```
  2. $ npm install --save kleur
  3. ```


Usage


  1. ```js
  2. import kleur from 'kleur';

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

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

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

Chained Methods


  1. ```js
  2. const { bold, green } = require('kleur');

  3. console.log(bold().red('this is a bold red message'));
  4. console.log(bold().italic('this is a bold italicized message'));
  5. console.log(bold().yellow().bgRed().italic('this is a bold yellow italicized message'));
  6. console.log(green().bold().underline('this is a bold green underlined message'));
  7. ```


Nested Methods


  1. ```js
  2. const { yellow, red, cyan } = require('kleur');

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



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.


  1. ```js
  2. import kleur from 'kleur';

  3. // manually disable
  4. kleur.enabled = false;

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

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

Important: <br>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 theFORCE_COLOR=1 environment variable:


  1. ```sh
  2. $ node app.js #=> COLORS
  3. $ node app.js > log.txt  #=> NO COLORS
  4. $ FORCE_COLOR=1 node app.js > log.txt #=> COLORS
  5. $ FORCE_COLOR=0 node app.js > log.txt #=> NO COLORS
  6. ```

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.

  1. ```js
  2. // or: import * as kleur from 'kleur/colors';
  3. import { red, underline, bgWhite } from 'kleur/colors';

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

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

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

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:

  1. ```js
  2. import * as kleur from 'kleur/colors';
  3. // or: import { $, red } from 'kleur/colors';

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

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

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


Benchmarks


Using Node v10.13.0


Load time


  1. ```
  2. chalk        :: 5.303ms
  3. kleur        :: 0.488ms
  4. kleur/colors :: 0.369ms
  5. ansi-colors  :: 1.504ms
  6. ```

Performance


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

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

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


History


This project originally forked [ansi-colors](https://github.com/doowb/ansi-colors).

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

  1. ```js
  2. // Old:
  3. c.red.bold.underline('old');

  4. // New:
  5. c.red().bold().underline('new');
  6. ```

<sup><em>As I work more with Rust, the newer syntax feels so much better & more natural!</em></sup>


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