3159655fa17c0ed282d8d0bb94d2063fa6b8fe1af29e8fa92b3f23164fbe88a5dbc34d8c1e4530277c83d20f318bab810ff665dbd25713662e0baea45f29ff 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # 🌈Colorette
  2. > Easily set your terminal text color & styles.
  3. - No dependecies
  4. - Automatic color support detection
  5. - Up to [2x faster](#benchmarks) than alternatives
  6. - TypeScript support
  7. - [`NO_COLOR`](https://no-color.org) friendly
  8. - Node >= `10`
  9. > [**Upgrading from Colorette `1.x`?**](https://github.com/jorgebucaran/colorette/issues/70)
  10. ## Quickstart
  11. ```js
  12. import { blue, bold, underline } from "colorette"
  13. console.log(
  14. blue("I'm blue"),
  15. bold(blue("da ba dee")),
  16. underline(bold(blue("da ba daa")))
  17. )
  18. ```
  19. Here's an example using [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals).
  20. ```js
  21. console.log(`
  22. There's a ${underline(blue("house"))},
  23. With a ${bold(blue("window"))},
  24. And a ${blue("corvette")}
  25. And everything is blue
  26. `)
  27. ```
  28. You can also nest styles without breaking existing color sequences.
  29. ```js
  30. console.log(bold(`I'm ${blue(`da ba ${underline("dee")} da ba`)} daa`))
  31. ```
  32. Need to override terminal color detection? You can do that too.
  33. ```js
  34. import { createColors } from "colorette"
  35. const { blue } = createColors({ useColor: false })
  36. console.log(blue("Blue? Nope, nah"))
  37. ```
  38. ## Installation
  39. ```console
  40. npm install colorette
  41. ```
  42. ## API
  43. ### \<color\>()
  44. > See all [supported colors](#supported-colors).
  45. ```js
  46. import { blue } from "colorette"
  47. blue("I'm blue") //=> \x1b[34mI'm blue\x1b[39m
  48. ```
  49. ### createColors()
  50. Override terminal color detection via `createColors({ useColor })`.
  51. ```js
  52. import { createColors } from "colorette"
  53. const { blue } = createColors({ useColor: false })
  54. ```
  55. ### isColorSupported
  56. `true` if your terminal supports color, `false` otherwise. Used internally, but exposed for convenience.
  57. ## Environment
  58. You can override color detection from the CLI by setting the `--no-color` or `--color` flags.
  59. ```console
  60. $ ./example.js --no-color | ./consumer.js
  61. ```
  62. Or if you can't use CLI flags, by setting the `NO_COLOR=` or `FORCE_COLOR=` environment variables.
  63. ```console
  64. $ NO_COLOR= ./example.js | ./consumer.js
  65. ```
  66. ## Supported colors
  67. | Colors | Background Colors | Bright Colors | Bright Background Colors | Modifiers |
  68. | ------- | ----------------- | ------------- | ------------------------ | ----------------- |
  69. | black | bgBlack | blackBright | bgBlackBright | dim |
  70. | red | bgRed | redBright | bgRedBright | **bold** |
  71. | green | bgGreen | greenBright | bgGreenBright | hidden |
  72. | yellow | bgYellow | yellowBright | bgYellowBright | _italic_ |
  73. | blue | bgBlue | blueBright | bgBlueBright | <u>underline</u> |
  74. | magenta | bgMagenta | magentaBright | bgMagentaBright | ~~strikethrough~~ |
  75. | cyan | bgCyan | cyanBright | bgCyanBright | reset |
  76. | white | bgWhite | whiteBright | bgWhiteBright | |
  77. | gray | | | | |
  78. ## [Benchmarks](https://github.com/jorgebucaran/colorette/actions/workflows/bench.yml)
  79. ```console
  80. npm --prefix bench start
  81. ```
  82. ```diff
  83. chalk 1,786,703 ops/sec
  84. kleur 1,618,960 ops/sec
  85. colors 646,823 ops/sec
  86. ansi-colors 786,149 ops/sec
  87. picocolors 2,871,758 ops/sec
  88. + colorette 3,002,751 ops/sec
  89. ```
  90. ## Acknowledgments
  91. 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
  92. ## License
  93. [MIT](LICENSE.md)