1 | <div align="center">
|
2 | <img src="shots/logo.png" alt="kleur" height="120" />
|
3 | </div>
|
4 |
|
5 | <div align="center">
|
6 | <a href="https://npmjs.org/package/kleur">
|
7 | <img src="https://badgen.now.sh/npm/v/kleur" alt="version" />
|
8 | </a>
|
9 | <a href="https://github.com/lukeed/kleur/actions?query=workflow%3ACI">
|
10 | <img src="https://github.com/lukeed/kleur/workflows/CI/badge.svg?event=push" alt="CI" />
|
11 | </a>
|
12 | <a href="https://npmjs.org/package/kleur">
|
13 | <img src="https://badgen.now.sh/npm/dm/kleur" alt="downloads" />
|
14 | </a>
|
15 | <a href="https://packagephobia.now.sh/result?p=kleur">
|
16 | <img src="https://packagephobia.now.sh/badge?p=kleur" alt="install size" />
|
17 | </a>
|
18 | </div>
|
19 |
|
20 | <div align="center">The fastest Node.js library for formatting terminal text with ANSI colors~!</div>
|
21 |
|
22 | ## Features
|
23 |
|
24 | * No dependencies
|
25 | * Super [lightweight](#load-time) & [performant](#performance)
|
26 | * Supports [nested](#nested-methods) & [chained](#chained-methods) colors
|
27 | * No `String.prototype` modifications
|
28 | * Conditional [color support](#conditional-support)
|
29 | * [Fully treeshakable](#individual-colors)
|
30 | * Familiar [API](#api)
|
31 |
|
32 | ---
|
33 |
|
34 | As of `v3.0` the Chalk-style syntax (magical getter) is no longer used.<br>Please visit [History](#history) for migration paths supporting that syntax.
|
35 |
|
36 | ---
|
37 |
|
38 |
|
39 | ## Install
|
40 |
|
41 | ```
|
42 | $ npm install --save kleur
|
43 | ```
|
44 |
|
45 |
|
46 | ## Usage
|
47 |
|
48 | ```js
|
49 | import kleur from 'kleur';
|
50 |
|
51 | // basic usage
|
52 | kleur.red('red text');
|
53 |
|
54 | // chained methods
|
55 | kleur.blue().bold().underline('howdy partner');
|
56 |
|
57 | // nested methods
|
58 | kleur.bold(`${ white().bgRed('[ERROR]') } ${ kleur.red().italic('Something happened')}`);
|
59 | ```
|
60 |
|
61 | ### Chained Methods
|
62 |
|
63 | ```js
|
64 | const { bold, green } = require('kleur');
|
65 |
|
66 | console.log(bold().red('this is a bold red message'));
|
67 | console.log(bold().italic('this is a bold italicized message'));
|
68 | console.log(bold().yellow().bgRed().italic('this is a bold yellow italicized message'));
|
69 | console.log(green().bold().underline('this is a bold green underlined message'));
|
70 | ```
|
71 |
|
72 | <img src="shots/1.png" width="300" />
|
73 |
|
74 | ### Nested Methods
|
75 |
|
76 | ```js
|
77 | const { yellow, red, cyan } = require('kleur');
|
78 |
|
79 | console.log(yellow(`foo ${red().bold('red')} bar ${cyan('cyan')} baz`));
|
80 | console.log(yellow('foo ' + red().bold('red') + ' bar ' + cyan('cyan') + ' baz'));
|
81 | ```
|
82 |
|
83 | <img src="shots/2.png" width="300" />
|
84 |
|
85 |
|
86 | ### Conditional Support
|
87 |
|
88 | Toggle color support as needed; `kleur` includes simple auto-detection which may not cover all cases.
|
89 |
|
90 | > **Note:** Both `kleur` and `kleur/colors` share the same detection logic.
|
91 |
|
92 | ```js
|
93 | import kleur from 'kleur';
|
94 |
|
95 | // manually disable
|
96 | kleur.enabled = false;
|
97 |
|
98 | // or use another library to detect support
|
99 | kleur.enabled = require('color-support').level > 0;
|
100 |
|
101 | console.log(kleur.red('I will only be colored red if the terminal supports colors'));
|
102 | ```
|
103 |
|
104 | > **Important:** <br>Colors will be disabled automatically in non [TTY contexts](https://nodejs.org/api/process.html#process_a_note_on_process_i_o). 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:
|
105 |
|
106 | ```sh
|
107 | $ node app.js #=> COLORS
|
108 | $ node app.js > log.txt #=> NO COLORS
|
109 | $ FORCE_COLOR=1 node app.js > log.txt #=> COLORS
|
110 | $ FORCE_COLOR=0 node app.js > log.txt #=> NO COLORS
|
111 | ```
|
112 |
|
113 | ## API
|
114 |
|
115 | Any `kleur` method returns a `String` when invoked with input; otherwise chaining is expected.
|
116 |
|
117 | > It's up to the developer to pass the output to destinations like `console.log`, `process.stdout.write`, etc.
|
118 |
|
119 | The methods below are grouped by type for legibility purposes only. They each can be [chained](#chained-methods) or [nested](#nested-methods) with one another.
|
120 |
|
121 | ***Colors:***
|
122 | > black — red — green — yellow — blue — magenta — cyan — white — gray — grey
|
123 |
|
124 | ***Backgrounds:***
|
125 | > bgBlack — bgRed — bgGreen — bgYellow — bgBlue — bgMagenta — bgCyan — bgWhite
|
126 |
|
127 | ***Modifiers:***
|
128 | > reset — bold — dim — italic* — underline — inverse — hidden — strikethrough*
|
129 |
|
130 | <sup>* <em>Not widely supported</em></sup>
|
131 |
|
132 |
|
133 | ## Individual Colors
|
134 |
|
135 | 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.
|
136 |
|
137 | The caveat with this approach is that color functions **are not** chainable~!<br>Each function receives and colorizes its input. You may combine colors, backgrounds, and modifiers by nesting function calls within other functions.
|
138 |
|
139 | ```js
|
140 | // or: import * as kleur from 'kleur/colors';
|
141 | import { red, underline, bgWhite } from 'kleur/colors';
|
142 |
|
143 | red('red text');
|
144 | //~> kleur.red('red text');
|
145 |
|
146 | underline(red('red underlined text'));
|
147 | //~> kleur.underline().red('red underlined text');
|
148 |
|
149 | bgWhite(underline(red('red underlined text w/ white background')));
|
150 | //~> kleur.bgWhite().underline().red('red underlined text w/ white background');
|
151 | ```
|
152 |
|
153 | > **Note:** All the same [colors, backgrounds, and modifiers](#api) are available.
|
154 |
|
155 | ***Conditional Support***
|
156 |
|
157 | The `kleur/colors` submodule also allows you to toggle color support, as needed.<br>
|
158 | It includes the same initial assumptions as `kleur`, in an attempt to have colors enabled by default.
|
159 |
|
160 | Unlike `kleur`, this setting exists as `kleur.$.enabled` instead of `kleur.enabled`:
|
161 |
|
162 | ```js
|
163 | import * as kleur from 'kleur/colors';
|
164 | // or: import { $, red } from 'kleur/colors';
|
165 |
|
166 | // manually disabled
|
167 | kleur.$.enabled = false;
|
168 |
|
169 | // or use another library to detect support
|
170 | kleur.$.enabled = require('color-support').level > 0;
|
171 |
|
172 | console.log(red('I will only be colored red if the terminal supports colors'));
|
173 | ```
|
174 |
|
175 |
|
176 | ## Benchmarks
|
177 |
|
178 | > Using Node v10.13.0
|
179 |
|
180 | ### Load time
|
181 |
|
182 | ```
|
183 | chalk :: 5.303ms
|
184 | kleur :: 0.488ms
|
185 | kleur/colors :: 0.369ms
|
186 | ansi-colors :: 1.504ms
|
187 | ```
|
188 |
|
189 | ### Performance
|
190 |
|
191 | ```
|
192 | # All Colors
|
193 | ansi-colors x 177,625 ops/sec ±1.47% (92 runs sampled)
|
194 | chalk x 611,907 ops/sec ±0.20% (92 runs sampled)
|
195 | kleur x 742,509 ops/sec ±1.47% (93 runs sampled)
|
196 | kleur/colors x 881,742 ops/sec ±0.19% (98 runs sampled)
|
197 |
|
198 | # Stacked colors
|
199 | ansi-colors x 23,331 ops/sec ±1.81% (94 runs sampled)
|
200 | chalk x 337,178 ops/sec ±0.20% (98 runs sampled)
|
201 | kleur x 78,299 ops/sec ±1.01% (97 runs sampled)
|
202 | kleur/colors x 104,431 ops/sec ±0.22% (97 runs sampled)
|
203 |
|
204 | # Nested colors
|
205 | ansi-colors x 67,181 ops/sec ±1.15% (92 runs sampled)
|
206 | chalk x 116,361 ops/sec ±0.63% (94 runs sampled)
|
207 | kleur x 139,514 ops/sec ±0.76% (95 runs sampled)
|
208 | kleur/colors x 145,716 ops/sec ±0.97% (97 runs sampled)
|
209 | ```
|
210 |
|
211 |
|
212 | ## History
|
213 |
|
214 | This project originally forked [`ansi-colors`](https://github.com/doowb/ansi-colors).
|
215 |
|
216 | Beginning with `kleur@3.0`, the Chalk-style syntax (magical getter) has been replaced with function calls per key:
|
217 |
|
218 | ```js
|
219 | // Old:
|
220 | c.red.bold.underline('old');
|
221 |
|
222 | // New:
|
223 | c.red().bold().underline('new');
|
224 | ```
|
225 | > <sup><em>As I work more with Rust, the newer syntax feels so much better & more natural!</em></sup>
|
226 |
|
227 | If you prefer the old syntax, you may migrate to `ansi-colors` or newer `chalk` releases.<br>Versions below `kleur@3.0` have been officially deprecated.
|
228 |
|
229 |
|
230 | ## License
|
231 |
|
232 | MIT © [Luke Edwards](https://lukeed.com)
|