You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
9.2 KiB

4 years ago
  1. # ansi-colors [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [![NPM version](https://img.shields.io/npm/v/ansi-colors.svg?style=flat)](https://www.npmjs.com/package/ansi-colors) [![NPM monthly downloads](https://img.shields.io/npm/dm/ansi-colors.svg?style=flat)](https://npmjs.org/package/ansi-colors) [![NPM total downloads](https://img.shields.io/npm/dt/ansi-colors.svg?style=flat)](https://npmjs.org/package/ansi-colors) [![Linux Build Status](https://img.shields.io/travis/doowb/ansi-colors.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/ansi-colors)
  2. > Easily add ANSI colors to your text and symbols in the terminal. A faster drop-in replacement for chalk, kleur and turbocolor (without the dependencies and rendering bugs).
  3. Please consider following this project's author, [Brian Woodward](https://github.com/doowb), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save ansi-colors
  8. ```
  9. ![image](https://user-images.githubusercontent.com/383994/39635445-8a98a3a6-4f8b-11e8-89c1-068c45d4fff8.png)
  10. ## Why use this?
  11. ansi-colors is _the fastest Node.js library for terminal styling_. A more performant drop-in replacement for chalk, with no dependencies.
  12. * _Blazing fast_ - Fastest terminal styling library in node.js, 10-20x faster than chalk!
  13. * _Drop-in replacement_ for [chalk](https://github.com/chalk/chalk).
  14. * _No dependencies_ (Chalk has 7 dependencies in its tree!)
  15. * _Safe_ - Does not modify the `String.prototype` like [colors](https://github.com/Marak/colors.js).
  16. * Supports [nested colors](#nested-colors), **and does not have the [nested styling bug](#nested-styling-bug) that is present in [colorette](https://github.com/jorgebucaran/colorette), [chalk](https://github.com/chalk/chalk), and [kleur](https://github.com/lukeed/kleur)**.
  17. * Supports [chained colors](#chained-colors).
  18. * [Toggle color support](#toggle-color-support) on or off.
  19. ## Usage
  20. ```js
  21. const c = require('ansi-colors');
  22. console.log(c.red('This is a red string!'));
  23. console.log(c.green('This is a red string!'));
  24. console.log(c.cyan('This is a cyan string!'));
  25. console.log(c.yellow('This is a yellow string!'));
  26. ```
  27. ![image](https://user-images.githubusercontent.com/383994/39653848-a38e67da-4fc0-11e8-89ae-98c65ebe9dcf.png)
  28. ## Chained colors
  29. ```js
  30. console.log(c.bold.red('this is a bold red message'));
  31. console.log(c.bold.yellow.italic('this is a bold yellow italicized message'));
  32. console.log(c.green.bold.underline('this is a bold green underlined message'));
  33. ```
  34. ![image](https://user-images.githubusercontent.com/383994/39635780-7617246a-4f8c-11e8-89e9-05216cc54e38.png)
  35. ## Nested colors
  36. ```js
  37. console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`));
  38. ```
  39. ![image](https://user-images.githubusercontent.com/383994/39635817-8ed93d44-4f8c-11e8-8afd-8c3ea35f5fbe.png)
  40. ### Nested styling bug
  41. `ansi-colors` does not have the nested styling bug found in [colorette](https://github.com/jorgebucaran/colorette), [chalk](https://github.com/chalk/chalk), and [kleur](https://github.com/lukeed/kleur).
  42. ```js
  43. const { bold, red } = require('ansi-styles');
  44. console.log(bold(`foo ${red.dim('bar')} baz`));
  45. const colorette = require('colorette');
  46. console.log(colorette.bold(`foo ${colorette.red(colorette.dim('bar'))} baz`));
  47. const kleur = require('kleur');
  48. console.log(kleur.bold(`foo ${kleur.red.dim('bar')} baz`));
  49. const chalk = require('chalk');
  50. console.log(chalk.bold(`foo ${chalk.red.dim('bar')} baz`));
  51. ```
  52. **Results in the following**
  53. (sans icons and labels)
  54. ![image](https://user-images.githubusercontent.com/383994/47280326-d2ee0580-d5a3-11e8-9611-ea6010f0a253.png)
  55. ## Toggle color support
  56. Easily enable/disable colors.
  57. ```js
  58. const c = require('ansi-colors');
  59. // disable colors manually
  60. c.enabled = false;
  61. // or use a library to automatically detect support
  62. c.enabled = require('color-support').hasBasic;
  63. console.log(c.red('I will only be colored red if the terminal supports colors'));
  64. ```
  65. ## Strip ANSI codes
  66. Use the `.unstyle` method to strip ANSI codes from a string.
  67. ```js
  68. console.log(c.unstyle(c.blue.bold('foo bar baz')));
  69. //=> 'foo bar baz'
  70. ```
  71. ## Available styles
  72. **Note** that bright and bright-background colors are not always supported.
  73. | Colors | Background Colors | Bright Colors | Bright Background Colors |
  74. | ------- | ----------------- | ------------- | ------------------------ |
  75. | black | bgBlack | blackBright | bgBlackBright |
  76. | red | bgRed | redBright | bgRedBright |
  77. | green | bgGreen | greenBright | bgGreenBright |
  78. | yellow | bgYellow | yellowBright | bgYellowBright |
  79. | blue | bgBlue | blueBright | bgBlueBright |
  80. | magenta | bgMagenta | magentaBright | bgMagentaBright |
  81. | cyan | bgCyan | cyanBright | bgCyanBright |
  82. | white | bgWhite | whiteBright | bgWhiteBright |
  83. | gray | | | |
  84. | grey | | | |
  85. _(`gray` is the U.S. spelling, `grey` is more commonly used in the Canada and U.K.)_
  86. ### Style modifiers
  87. * dim
  88. * **bold**
  89. * hidden
  90. * _italic_
  91. * underline
  92. * inverse
  93. * ~~strikethrough~~
  94. * reset
  95. ## Performance
  96. **Libraries tested**
  97. * ansi-colors v3.0.4
  98. * chalk v2.4.1
  99. ### Mac
  100. > MacBook Pro, Intel Core i7, 2.3 GHz, 16 GB.
  101. **Load time**
  102. Time it takes to load the first time `require()` is called:
  103. * ansi-colors - `1.915ms`
  104. * chalk - `12.437ms`
  105. **Benchmarks**
  106. ```
  107. # All Colors
  108. ansi-colors x 173,851 ops/sec ±0.42% (91 runs sampled)
  109. chalk x 9,944 ops/sec ±2.53% (81 runs sampled)))
  110. # Chained colors
  111. ansi-colors x 20,791 ops/sec ±0.60% (88 runs sampled)
  112. chalk x 2,111 ops/sec ±2.34% (83 runs sampled)
  113. # Nested colors
  114. ansi-colors x 59,304 ops/sec ±0.98% (92 runs sampled)
  115. chalk x 4,590 ops/sec ±2.08% (82 runs sampled)
  116. ```
  117. ### Windows
  118. > Windows 10, Intel Core i7-7700k CPU @ 4.2 GHz, 32 GB
  119. **Load time**
  120. Time it takes to load the first time `require()` is called:
  121. * ansi-colors - `1.494ms`
  122. * chalk - `11.523ms`
  123. **Benchmarks**
  124. ```
  125. # All Colors
  126. ansi-colors x 193,088 ops/sec ±0.51% (95 runs sampled))
  127. chalk x 9,612 ops/sec ±3.31% (77 runs sampled)))
  128. # Chained colors
  129. ansi-colors x 26,093 ops/sec ±1.13% (94 runs sampled)
  130. chalk x 2,267 ops/sec ±2.88% (80 runs sampled))
  131. # Nested colors
  132. ansi-colors x 67,747 ops/sec ±0.49% (93 runs sampled)
  133. chalk x 4,446 ops/sec ±3.01% (82 runs sampled))
  134. ```
  135. ## About
  136. <details>
  137. <summary><strong>Contributing</strong></summary>
  138. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  139. </details>
  140. <details>
  141. <summary><strong>Running Tests</strong></summary>
  142. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  143. ```sh
  144. $ npm install && npm test
  145. ```
  146. </details>
  147. <details>
  148. <summary><strong>Building docs</strong></summary>
  149. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  150. To generate the readme, run the following command:
  151. ```sh
  152. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  153. ```
  154. </details>
  155. ### Related projects
  156. You might also be interested in these projects:
  157. * [ansi-wrap](https://www.npmjs.com/package/ansi-wrap): Create ansi colors by passing the open and close codes. | [homepage](https://github.com/jonschlinkert/ansi-wrap "Create ansi colors by passing the open and close codes.")
  158. * [strip-color](https://www.npmjs.com/package/strip-color): Strip ANSI color codes from a string. No dependencies. | [homepage](https://github.com/jonschlinkert/strip-color "Strip ANSI color codes from a string. No dependencies.")
  159. ### Contributors
  160. | **Commits** | **Contributor** |
  161. | --- | --- |
  162. | 42 | [doowb](https://github.com/doowb) |
  163. | 38 | [jonschlinkert](https://github.com/jonschlinkert) |
  164. | 6 | [lukeed](https://github.com/lukeed) |
  165. | 2 | [Silic0nS0ldier](https://github.com/Silic0nS0ldier) |
  166. | 1 | [dwieeb](https://github.com/dwieeb) |
  167. | 1 | [jorgebucaran](https://github.com/jorgebucaran) |
  168. | 1 | [madhavarshney](https://github.com/madhavarshney) |
  169. | 1 | [chapterjason](https://github.com/chapterjason) |
  170. ### Author
  171. **Brian Woodward**
  172. * [GitHub Profile](https://github.com/doowb)
  173. * [Twitter Profile](https://twitter.com/doowb)
  174. * [LinkedIn Profile](https://linkedin.com/in/woodwardbrian)
  175. Please consider supporting me on Patreon, or [start your own Patreon page](https://patreon.com/invite/bxpbvm)!
  176. <a href="https://www.patreon.com/jonschlinkert">
  177. <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" height="50">
  178. </a>
  179. ### License
  180. Copyright © 2019, [Brian Woodward](https://github.com/doowb).
  181. Released under the [MIT License](LICENSE).
  182. ***
  183. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on March 03, 2019._