1 | # Colorette
|
2 |
|
3 | > Easily set your terminal text color & styles.
|
4 |
|
5 | - No wonky prototype method-chain API.
|
6 | - Automatic color support detection.
|
7 | - Up to [2x faster](#benchmarks) than alternatives.
|
8 | - [`NO_COLOR`](https://no-color.org) friendly. ✅
|
9 |
|
10 | Here's the first example to get you started.
|
11 |
|
12 | ```js
|
13 | import { blue, bold, underline } from "colorette"
|
14 |
|
15 | console.log(
|
16 | blue("I'm blue"),
|
17 | bold(blue("da ba dee")),
|
18 | underline(bold(blue("da ba daa")))
|
19 | )
|
20 | ```
|
21 |
|
22 | Here's an example using [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals).
|
23 |
|
24 | ```js
|
25 | console.log(`
|
26 | There's a ${underline(blue("house"))},
|
27 | With a ${bold(blue("window"))},
|
28 | And a ${blue("corvette")}
|
29 | And everything is blue
|
30 | `)
|
31 | ```
|
32 |
|
33 | Of course, you can nest styles without breaking existing color sequences.
|
34 |
|
35 | ```js
|
36 | console.log(bold(`I'm ${blue(`da ba ${underline("dee")} da ba`)} daa`))
|
37 | ```
|
38 |
|
39 | Need to dynamically override color detection? You can do that too.
|
40 |
|
41 | ```js
|
42 | import { createColors } from "colorette"
|
43 |
|
44 | const { blue } = createColors({ useColor: false })
|
45 |
|
46 | console.log(blue("Blue? Nope, nah"))
|
47 | ```
|
48 |
|
49 | ## Installation
|
50 |
|
51 | ```console
|
52 | npm install colorette
|
53 | ```
|
54 |
|
55 | ## API
|
56 |
|
57 | ### `blue(text)`
|
58 |
|
59 | > See all [supported colors](#supported-colors).
|
60 |
|
61 | ```js
|
62 | blue("I'm blue") //=> \x1b[34mI'm blue\x1b[39m
|
63 | ```
|
64 |
|
65 | ### `isColorSupported`
|
66 |
|
67 | `true` if your terminal supports color or `false` otherwise. Used internally and handled for you, but exposed for convenience.
|
68 |
|
69 | ### `createColors({ useColor })`
|
70 |
|
71 | Create a reusable instance of Colorette. Color support is automatically detected, but you can override it by setting the `useColor` boolean property.
|
72 |
|
73 | ```js
|
74 | import { createColors } from "colorette"
|
75 |
|
76 | const { blue } = createColors({ useColor: false })
|
77 | ```
|
78 |
|
79 | ## Environment
|
80 |
|
81 | You can override automatic color detection from the CLI too via `NO_COLOR=` or `FORCE_COLOR=`.
|
82 |
|
83 | ```console
|
84 | $ FORCE_COLOR= node example.js | ./consumer.js
|
85 | ```
|
86 |
|
87 | ## Supported colors
|
88 |
|
89 | | Colors | Background Colors | Bright Colors | Bright Background Colors | Modifiers |
|
90 | | ------- | ----------------- | ------------- | ------------------------ | ----------------- |
|
91 | | black | bgBlack | blackBright | bgBlackBright | dim |
|
92 | | red | bgRed | redBright | bgRedBright | **bold** |
|
93 | | green | bgGreen | greenBright | bgGreenBright | hidden |
|
94 | | yellow | bgYellow | yellowBright | bgYellowBright | _italic_ |
|
95 | | blue | bgBlue | blueBright | bgBlueBright | <u>underline</u> |
|
96 | | magenta | bgMagenta | magentaBright | bgMagentaBright | ~~strikethrough~~ |
|
97 | | cyan | bgCyan | cyanBright | bgCyanBright | reset |
|
98 | | white | bgWhite | whiteBright | bgWhiteBright | |
|
99 | | gray | | | | |
|
100 |
|
101 | ## [Benchmarks](https://github.com/jorgebucaran/colorette/actions/workflows/bench.yml)
|
102 |
|
103 | ```console
|
104 | npm --prefix bench start
|
105 | ```
|
106 |
|
107 | ## License
|
108 |
|
109 | [MIT](LICENSE.md)
|