1 | # Nano Colors
|
---|
2 |
|
---|
3 | <img align="right" width="64" height="60"
|
---|
4 | src="./img/logo.svg"
|
---|
5 | title="Nano Colors logo by Roman Shamin">
|
---|
6 |
|
---|
7 | A tiny and fast Node.js library for formatting terminal text with ANSI colors.
|
---|
8 |
|
---|
9 | * It is **2 times faster** than `chalk`. Both loading and calls.
|
---|
10 | * **No dependencies.** It takes **5 times less space** in `node_modules`
|
---|
11 | than `chalk`.
|
---|
12 | * **Actively maintained.** Used in many big projects
|
---|
13 | like PostCSS or Browserslist.
|
---|
14 | * **Auto-detects color support.** You can also toggle color mode manually.
|
---|
15 | * **Tree-shakable.** We use a dual [ESM]/[CJS] package.
|
---|
16 | * Supports Node.js ≥ 6, Deno and universal Node.js/browser projects.
|
---|
17 |
|
---|
18 | ```js
|
---|
19 | import { green, bold } from 'nanocolors'
|
---|
20 |
|
---|
21 | console.log(
|
---|
22 | green(`Task ${bold('1')} was finished`)
|
---|
23 | )
|
---|
24 | ```
|
---|
25 |
|
---|
26 | <p align="center">
|
---|
27 | <img src="./img/example.png" alt="Nano Colors output" width="600">
|
---|
28 | </p>
|
---|
29 |
|
---|
30 | API was heavily inspired
|
---|
31 | by [**@jorgebucaran**](https://github.com/jorgebucaran/)’s
|
---|
32 | [`colorette`](https://github.com/jorgebucaran/colorette) with few cool hacks
|
---|
33 | copied from [**@lukeed**](https://github.com/lukeed/)’s
|
---|
34 | [`kleur`](https://github.com/lukeed/kleur).
|
---|
35 |
|
---|
36 | <a href="https://evilmartians.com/?utm_source=nanocolors">
|
---|
37 | <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
|
---|
38 | alt="Sponsored by Evil Martians" width="236" height="54">
|
---|
39 | </a>
|
---|
40 |
|
---|
41 | [ESM]: https://github.com/ai/nanocolors/blob/main/index.js
|
---|
42 | [CJS]: https://github.com/ai/nanocolors/blob/main/index.cjs
|
---|
43 |
|
---|
44 |
|
---|
45 | ## Benchmarks
|
---|
46 |
|
---|
47 | Function calling time:
|
---|
48 |
|
---|
49 | ```
|
---|
50 | $ ./test/benchmark.js
|
---|
51 | chalk 11,608,010 ops/sec
|
---|
52 | cli-color 752,419 ops/sec
|
---|
53 | ansi-colors 3,601,857 ops/sec
|
---|
54 | kleur 15,185,239 ops/sec
|
---|
55 | kleur/colors 21,113,231 ops/sec
|
---|
56 | colorette 19,712,884 ops/sec
|
---|
57 | nanocolors 21,176,376 ops/sec
|
---|
58 | ```
|
---|
59 |
|
---|
60 | Library loading time:
|
---|
61 |
|
---|
62 | ```
|
---|
63 | $ ./test/loading.js
|
---|
64 | chalk 3.465 ms
|
---|
65 | cli-color 21.849 ms
|
---|
66 | ansi-colors 1.101 ms
|
---|
67 | kleur 1.628 ms
|
---|
68 | kleur/colors 0.508 ms
|
---|
69 | colorette 2.610 ms
|
---|
70 | nanocolors 0.486 ms
|
---|
71 | ```
|
---|
72 |
|
---|
73 | The space in `node_modules` including sub-dependencies:
|
---|
74 |
|
---|
75 | ```
|
---|
76 | $ ./test/size.js
|
---|
77 | Data from packagephobia.com
|
---|
78 | chalk 101 kB
|
---|
79 | cli-color 1249 kB
|
---|
80 | ansi-colors 25 kB
|
---|
81 | kleur 21 kB
|
---|
82 | colorette 16 kB
|
---|
83 | nanocolors 16 kB
|
---|
84 | ```
|
---|
85 |
|
---|
86 | Test configuration: ThinkPad X1 Carbon Gen 9, Fedora 34, Node.js 16.8.
|
---|
87 |
|
---|
88 | ## Replacing `chalk`
|
---|
89 |
|
---|
90 | 1. Replace import and use named exports:
|
---|
91 |
|
---|
92 | ```diff
|
---|
93 | - import chalk from 'chalk'
|
---|
94 | + import { red, bold } from 'nanocolors'
|
---|
95 | ```
|
---|
96 |
|
---|
97 | 2. Unprefix calls:
|
---|
98 |
|
---|
99 | ```diff
|
---|
100 | - chalk.red(text)
|
---|
101 | + red(text)
|
---|
102 | ```
|
---|
103 |
|
---|
104 | 3. Replace chains to nested calls:
|
---|
105 |
|
---|
106 | ```diff
|
---|
107 | - chalk.red.bold(text)
|
---|
108 | + red(bold(text))
|
---|
109 | ```
|
---|
110 |
|
---|
111 |
|
---|
112 | ## API
|
---|
113 |
|
---|
114 | ### Individual Colors
|
---|
115 |
|
---|
116 | Nano Colors exports functions:
|
---|
117 |
|
---|
118 | | Colors | Background Colors | Modifiers |
|
---|
119 | | --------- | ------------------- | ----------------- |
|
---|
120 | | `black` | `bgBlack` | dim |
|
---|
121 | | `red` | `bgRed` | **bold** |
|
---|
122 | | `green` | `bgGreen` | hidden |
|
---|
123 | | `yellow` | `bgYellow` | _italic_ |
|
---|
124 | | `blue` | `bgBlue` | <u>underline</u> |
|
---|
125 | | `magenta` | `bgMagenta` | ~~strikethrough~~ |
|
---|
126 | | `cyan` | `bgCyan` | reset |
|
---|
127 | | `white` | `bgWhite` | |
|
---|
128 | | `gray` | | |
|
---|
129 |
|
---|
130 | Functions are not chainable. You need to wrap it inside each other:
|
---|
131 |
|
---|
132 | ```js
|
---|
133 | import { black, bgYellow } from 'nanocolors'
|
---|
134 |
|
---|
135 | console.log(bgYellow(black(' WARN ')))
|
---|
136 | ```
|
---|
137 |
|
---|
138 | Functions will use colors only if Nano Colors auto-detect that current
|
---|
139 | environment supports colors.
|
---|
140 |
|
---|
141 | You can get support level in `isColorSupported`:
|
---|
142 |
|
---|
143 | ```js
|
---|
144 | import { isColorSupported } from 'nanocolors'
|
---|
145 |
|
---|
146 | if (isColorSupported) {
|
---|
147 | console.log('With colors')
|
---|
148 | }
|
---|
149 | ```
|
---|
150 |
|
---|
151 |
|
---|
152 | ### Conditional Support
|
---|
153 |
|
---|
154 | You can manually switch colors on/off and override color support auto-detection:
|
---|
155 |
|
---|
156 | ```js
|
---|
157 | import { createColors } from 'nanocolors'
|
---|
158 |
|
---|
159 | const { red } = createColors(options.enableColors)
|
---|
160 | ```
|
---|
161 |
|
---|
162 | On `undefined` argument, `createColors` will use value
|
---|
163 | from color support auto-detection.
|
---|
164 |
|
---|
165 |
|
---|
166 | ### Deno
|
---|
167 |
|
---|
168 | Nano Colors has build-in [Deno support](https://deno.land/x/nanocolors).
|
---|
169 |
|
---|
170 | ```js
|
---|
171 | import { red } from 'https://deno.land/x/nanocolors/mod.ts'
|
---|
172 | ```
|
---|