| 1 |
<sup>yoctocolors 🌈</sup> |
| 2 |
|
| 3 |
> The smallest and fastest command-line coloring package on the internet |
| 4 |
|
| 5 |
*Check out [Chalk](https://github.com/chalk/chalk) if you want something more mature and comprehensive.* |
| 6 |
|
| 7 |
## Highlights |
| 8 |
|
| 9 |
- Tiny |
| 10 |
- Fast |
| 11 |
- Handles nested colors |
| 12 |
- Tree-shakeable |
| 13 |
- No dependencies |
| 14 |
- Actively maintained |
| 15 |
|
| 16 |
## Install |
| 17 |
|
| 18 |
ESM: |
| 19 |
|
| 20 |
```sh |
| 21 |
npm install yoctocolors |
| 22 |
``` |
| 23 |
|
| 24 |
Or CommonJS: |
| 25 |
|
| 26 |
```sh |
| 27 |
npm install yoctocolors-cjs |
| 28 |
``` |
| 29 |
|
| 30 |
## Usage |
| 31 |
|
| 32 |
```js |
| 33 |
import colors from 'yoctocolors'; |
| 34 |
|
| 35 |
console.log(colors.red('Yo!')); |
| 36 |
|
| 37 |
console.log(colors.blue(`Welcome to the ${colors.green('yoctocolors')} package!`)); |
| 38 |
``` |
| 39 |
|
| 40 |
You can also import colors as named imports: |
| 41 |
|
| 42 |
```js |
| 43 |
import {red, blue, green} from 'yoctocolors'; |
| 44 |
|
| 45 |
console.log(red('Yo!')); |
| 46 |
|
| 47 |
console.log(blue(`Welcome to the ${green('yoctocolors')} package!`)); |
| 48 |
``` |
| 49 |
|
| 50 |
*This package supports [basic color detection](https://nodejs.org/api/tty.html#writestreamhascolorscount-env). Colors can be forcefully enabled by setting the `FORCE_COLOR` environment variable to `1` and can be forcefully disabled by setting `NO_COLOR` or `NODE_DISABLE_COLORS` to any value. [More info.](https://nodejs.org/api/tty.html#writestreamgetcolordepthenv)* |
| 51 |
|
| 52 |
## Styles |
| 53 |
|
| 54 |
### Modifiers |
| 55 |
|
| 56 |
- `reset` - Reset the current style. |
| 57 |
- `bold` - Make the text bold. |
| 58 |
- `dim` - Make the text have lower opacity. |
| 59 |
- `italic` - Make the text italic. *(Not widely supported)* |
| 60 |
- `underline` - Put a horizontal line above the text. *(Not widely supported)* |
| 61 |
- `overline` - Put a horizontal line below the text. *(Not widely supported)* |
| 62 |
- `inverse`- Invert background and foreground colors. |
| 63 |
- `hidden` - Print the text but make it invisible. |
| 64 |
- `strikethrough` - Put a horizontal line through the center of the text. *(Not widely supported)* |
| 65 |
|
| 66 |
### Colors |
| 67 |
|
| 68 |
- `black` |
| 69 |
- `red` |
| 70 |
- `green` |
| 71 |
- `yellow` |
| 72 |
- `blue` |
| 73 |
- `magenta` |
| 74 |
- `cyan` |
| 75 |
- `white` |
| 76 |
- `gray` |
| 77 |
- `redBright` |
| 78 |
- `greenBright` |
| 79 |
- `yellowBright` |
| 80 |
- `blueBright` |
| 81 |
- `magentaBright` |
| 82 |
- `cyanBright` |
| 83 |
- `whiteBright` |
| 84 |
|
| 85 |
### Background colors |
| 86 |
|
| 87 |
- `bgBlack` |
| 88 |
- `bgRed` |
| 89 |
- `bgGreen` |
| 90 |
- `bgYellow` |
| 91 |
- `bgBlue` |
| 92 |
- `bgMagenta` |
| 93 |
- `bgCyan` |
| 94 |
- `bgWhite` |
| 95 |
- `bgGray` |
| 96 |
- `bgRedBright` |
| 97 |
- `bgGreenBright` |
| 98 |
- `bgYellowBright` |
| 99 |
- `bgBlueBright` |
| 100 |
- `bgMagentaBright` |
| 101 |
- `bgCyanBright` |
| 102 |
- `bgWhiteBright` |
| 103 |
|
| 104 |
## Prior art |
| 105 |
|
| 106 |
Yes |
| 107 |
|
| 108 |
## Benchmark |
| 109 |
|
| 110 |
```sh |
| 111 |
$ ./benchmark.js |
| 112 |
┌─────────┬────────────────┬─────────────┐ |
| 113 |
│ (index) │ library │ ops/sec │ |
| 114 |
├─────────┼────────────────┼─────────────┤ |
| 115 |
│ 0 │ 'yoctocolors' │ '8,000,000' │ |
| 116 |
│ 1 │ 'colorette' │ '8,000,000' │ |
| 117 |
│ 2 │ 'picocolors' │ '8,000,000' │ |
| 118 |
│ 3 │ 'nanocolors' │ '5,988,024' │ |
| 119 |
│ 4 │ 'chalk' │ '4,807,692' │ |
| 120 |
│ 5 │ 'kleur/colors' │ '4,807,692' │ |
| 121 |
│ 6 │ 'kleur' │ '4,784,689' │ |
| 122 |
│ 7 │ 'ansi-colors' │ '2,178,649' │ |
| 123 |
│ 8 │ 'cli-color' │ '585,138' │ |
| 124 |
└─────────┴────────────────┴─────────────┘ |
| 125 |
``` |
| 126 |
|
| 127 |
*See [benchmark.js](benchmark.js).* |
| 128 |
|
| 129 |
## FAQ |
| 130 |
|
| 131 |
### What is yocto? |
| 132 |
|
| 133 |
[It was the smallest official unit prefix in the metric system until 2022.](https://en.wikipedia.org/wiki/Yocto-) Much smaller than nano. |
| 134 |
|
| 135 |
## Related |
| 136 |
|
| 137 |
- [yoctodelay](https://github.com/sindresorhus/yoctodelay) - Delay a promise a given amount of time |
| 138 |
- [chalk](https://github.com/chalk/chalk) - Terminal string styling |