1 | # 🌈Colorette
|
---|
2 |
|
---|
3 | > Easily set your terminal text color & styles.
|
---|
4 |
|
---|
5 | - No dependecies
|
---|
6 | - Automatic color support detection
|
---|
7 | - Up to [2x faster](#benchmarks) than alternatives
|
---|
8 | - TypeScript support
|
---|
9 | - [`NO_COLOR`](https://no-color.org) friendly
|
---|
10 | - Node >= `10`
|
---|
11 |
|
---|
12 | > [**Upgrading from Colorette `1.x`?**](https://github.com/jorgebucaran/colorette/issues/70)
|
---|
13 |
|
---|
14 | ## Quickstart
|
---|
15 |
|
---|
16 | ```js
|
---|
17 | import { blue, bold, underline } from "colorette"
|
---|
18 |
|
---|
19 | console.log(
|
---|
20 | blue("I'm blue"),
|
---|
21 | bold(blue("da ba dee")),
|
---|
22 | underline(bold(blue("da ba daa")))
|
---|
23 | )
|
---|
24 | ```
|
---|
25 |
|
---|
26 | Here's an example using [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals).
|
---|
27 |
|
---|
28 | ```js
|
---|
29 | console.log(`
|
---|
30 | There's a ${underline(blue("house"))},
|
---|
31 | With a ${bold(blue("window"))},
|
---|
32 | And a ${blue("corvette")}
|
---|
33 | And everything is blue
|
---|
34 | `)
|
---|
35 | ```
|
---|
36 |
|
---|
37 | You can also nest styles without breaking existing color sequences.
|
---|
38 |
|
---|
39 | ```js
|
---|
40 | console.log(bold(`I'm ${blue(`da ba ${underline("dee")} da ba`)} daa`))
|
---|
41 | ```
|
---|
42 |
|
---|
43 | Need to override terminal color detection? You can do that too.
|
---|
44 |
|
---|
45 | ```js
|
---|
46 | import { createColors } from "colorette"
|
---|
47 |
|
---|
48 | const { blue } = createColors({ useColor: false })
|
---|
49 |
|
---|
50 | console.log(blue("Blue? Nope, nah"))
|
---|
51 | ```
|
---|
52 |
|
---|
53 | ## Installation
|
---|
54 |
|
---|
55 | ```console
|
---|
56 | npm install colorette
|
---|
57 | ```
|
---|
58 |
|
---|
59 | ## API
|
---|
60 |
|
---|
61 | ### \<color\>()
|
---|
62 |
|
---|
63 | > See all [supported colors](#supported-colors).
|
---|
64 |
|
---|
65 | ```js
|
---|
66 | import { blue } from "colorette"
|
---|
67 |
|
---|
68 | blue("I'm blue") //=> \x1b[34mI'm blue\x1b[39m
|
---|
69 | ```
|
---|
70 |
|
---|
71 | ### createColors()
|
---|
72 |
|
---|
73 | Override terminal color detection via `createColors({ useColor })`.
|
---|
74 |
|
---|
75 | ```js
|
---|
76 | import { createColors } from "colorette"
|
---|
77 |
|
---|
78 | const { blue } = createColors({ useColor: false })
|
---|
79 | ```
|
---|
80 |
|
---|
81 | ### isColorSupported
|
---|
82 |
|
---|
83 | `true` if your terminal supports color, `false` otherwise. Used internally, but exposed for convenience.
|
---|
84 |
|
---|
85 | ## Environment
|
---|
86 |
|
---|
87 | You can override color detection from the CLI by setting the `--no-color` or `--color` flags.
|
---|
88 |
|
---|
89 | ```console
|
---|
90 | $ ./example.js --no-color | ./consumer.js
|
---|
91 | ```
|
---|
92 |
|
---|
93 | Or if you can't use CLI flags, by setting the `NO_COLOR=` or `FORCE_COLOR=` environment variables.
|
---|
94 |
|
---|
95 | ```console
|
---|
96 | $ NO_COLOR= ./example.js | ./consumer.js
|
---|
97 | ```
|
---|
98 |
|
---|
99 | ## Supported colors
|
---|
100 |
|
---|
101 | | Colors | Background Colors | Bright Colors | Bright Background Colors | Modifiers |
|
---|
102 | | ------- | ----------------- | ------------- | ------------------------ | ----------------- |
|
---|
103 | | black | bgBlack | blackBright | bgBlackBright | dim |
|
---|
104 | | red | bgRed | redBright | bgRedBright | **bold** |
|
---|
105 | | green | bgGreen | greenBright | bgGreenBright | hidden |
|
---|
106 | | yellow | bgYellow | yellowBright | bgYellowBright | _italic_ |
|
---|
107 | | blue | bgBlue | blueBright | bgBlueBright | <u>underline</u> |
|
---|
108 | | magenta | bgMagenta | magentaBright | bgMagentaBright | ~~strikethrough~~ |
|
---|
109 | | cyan | bgCyan | cyanBright | bgCyanBright | reset |
|
---|
110 | | white | bgWhite | whiteBright | bgWhiteBright | |
|
---|
111 | | gray | | | | |
|
---|
112 |
|
---|
113 | ## [Benchmarks](https://github.com/jorgebucaran/colorette/actions/workflows/bench.yml)
|
---|
114 |
|
---|
115 | ```console
|
---|
116 | npm --prefix bench start
|
---|
117 | ```
|
---|
118 |
|
---|
119 | ```diff
|
---|
120 | chalk 1,786,703 ops/sec
|
---|
121 | kleur 1,618,960 ops/sec
|
---|
122 | colors 646,823 ops/sec
|
---|
123 | ansi-colors 786,149 ops/sec
|
---|
124 | picocolors 2,871,758 ops/sec
|
---|
125 | + colorette 3,002,751 ops/sec
|
---|
126 | ```
|
---|
127 |
|
---|
128 | ## Acknowledgments
|
---|
129 |
|
---|
130 | Colorette started out in 2015 by [@jorgebucaran](https://github.com/jorgebucaran) as a lightweight alternative to [Chalk](https://github.com/chalk/chalk) and was introduced originally as [Clor](https://github.com/jorgebucaran/colorette/commit/b01b5b9961ceb7df878583a3002e836fae9e37ce). Our terminal color detection logic borrows heavily from [@sindresorhus](https://github.com/sindresorhus) and [@Qix-](https://github.com/Qix-) work on Chalk. The idea of slicing strings to clear bleeding sequences was adapted from a similar technique used by [@alexeyraspopov](https://github.com/alexeyraspopov) in [picocolors](https://github.com/alexeyraspopov/picocolors). Thank you to all our contributors! <3
|
---|
131 |
|
---|
132 | ## License
|
---|
133 |
|
---|
134 | [MIT](LICENSE.md)
|
---|