kleur - 最快的Node.js库,用于利用ANSI颜色格式化终端文本
kleur - 最快的Node.js库,用于利用ANSI颜色格式化终端文本
Node.js 命令行实用程序
共1088Star
详细介绍
Features
- No dependencies
- Super lightweight & performant
- Supports nested & chained colors
- No
String.prototype
modifications - Supports
printf
formatting - Conditional color support
- Familiar API
Heavily inspired by ansi-colors
. See Credits for more info!
Install
$ npm install --save kleur
Usage
const kleur = require('kleur');
// basic usage
kleur.red('red text');
// or variadic arguments
kleur.red('this', 'is', 'also', 'red');
// or printf formatting
kleur.red('%s, %s!', 'hello', 'world');
// chained methods
kleur.blue.bold.underline('howdy partner');
// nested methods
kleur.bold(`${ kleur.bgRed.white('[ERROR]') } ${ kleur.red.italic('Something happened')}`);
Chained Methods
console.log(kleur.bold.red('this is a bold red message'));
console.log(kleur.bold.italic('this is a bold italicized message'));
console.log(kleur.bold.yellow.bgRed.italic('this is a bold yellow italicized message'));
console.log(kleur.green.bold.underline('this is a bold green underlined message'));
Nested Methods
const { yellow, red, cyan } = require('kleur');
// with template literals
console.log(yellow(`foo ${red.bold('red')} bar ${cyan('cyan')} baz`));
// or variadic arguments
console.log(yellow('foo', red.bold('red'), 'bar', cyan('cyan'), 'baz'));
printf
Formatting
See
util.format
for documentation
const { yellow, bgGreen, bold } = require('kleur');
// basic usage
console.log(bold.cyan('%s, %s!', 'Hello', 'World', '-Anonymous'));
// or with nested colors
console.log( bold('%s-like %s... %s!', 'printf', bgGreen.black('formatting'), yellow('YAY')) );
Clear Formatting
Manually strip all ANSI codes from a given string.
let str = kleur.blue('Howdy partner');
//=> styled
kleur.clear(str);
//=> 'Howdy partner'
Conditional Support
Toggle color support as needed; kleur
assumes it's always enabled.
const kleur = require('kleur');
// manually disable
kleur.enabled = false;
// or use a library to detect support
kleur.enabled = require('color-support').stdout;
console.log(kleur.red('I will only be colored red if the terminal supports colors'));
API
Any kleur
method returns a String
(when invoked, not chained). 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 legbility purposes only. They each can be chained or nested with one another.
Colors:
black — red — green — yellow — blue — magenta — cyan — white — gray
Backgrounds:
bgBlack — bgRed — bgGreen — bgYellow — bgBlue — bgMagenta — bgCyan — bgWhite
Modifiers:
reset — bold — dim — italic* — underline — inverse — hidden — strikethrough*
* Not widely supported
Benchmarks
Using Node v8.9.0
Load time
ansi-colors: 1.150ms
chalk: 8.440ms
clorox: 0.471ms
kleur: 0.611ms
Performance
# All Colors
ansi-colors x 60,646 ops/sec ±0.49% (96 runs sampled)
chalk x 7,228 ops/sec ±3.25% (73 runs sampled)
clorox x 86,631 ops/sec ±0.59% (94 runs sampled)
kleur x 95,595 ops/sec ±0.24% (96 runs sampled)
# Stacked colors
ansi-colors x 13,576 ops/sec ±0.42% (93 runs sampled)
chalk x 1,669 ops/sec ±4.56% (71 runs sampled)
clorox x 26,166 ops/sec ±1.44% (91 runs sampled)
kleur x 28,674 ops/sec ±0.29% (93 runs sampled)
# Nested colors
ansi-colors x 28,712 ops/sec ±0.60% (96 runs sampled)
chalk x 3,446 ops/sec ±4.59% (69 runs sampled)
Clorox x 40,821 ops/sec ±1.90% (94 runs sampled)
kleur x 43,242 ops/sec ±0.17% (97 runs sampled)
Credits
This project (inadvertently) is very similar to Brian Woodward's awesome ansi-colors
project. My original implementation involved writing into a global state — first by writing into an output string, and then by saving the keys
array into the $
directly. Both approaches were leaky & allowed for accidental chains/overwrites. In turn, I borrowed ansi-colors
's approach in writing keys
state into each chain directly.
Aside from the performance boost, kleur
exists as a separate module because I've no need for bright color variants nor the symbols. And since those are defining features of ansi-colors
, they're not something that can be removed.
The benchmark suite is also imported directly from ansi-colors
License
MIT © Luke Edwards
-
269 Star
-
20 Star
-
308 Star
-
0 Star
-
680 Star
-
310 Star
-
935 Star
-
12978 Star
-
12456 Star