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.

readme.md 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <h1 align="center">
  2. <br>
  3. <br>
  4. <img width="360" src="https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk">
  5. <br>
  6. <br>
  7. <br>
  8. </h1>
  9. > Terminal string styling done right
  10. [![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk)
  11. [![Coverage Status](https://coveralls.io/repos/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/r/chalk/chalk?branch=master)
  12. [![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4)
  13. [colors.js](https://github.com/Marak/colors.js) used to be the most popular string styling module, but it has serious deficiencies like extending `String.prototype` which causes all kinds of [problems](https://github.com/yeoman/yo/issues/68). Although there are other ones, they either do too much or not enough.
  14. **Chalk is a clean and focused alternative.**
  15. ![](https://github.com/chalk/ansi-styles/raw/master/screenshot.png)
  16. ## Why
  17. - Highly performant
  18. - Doesn't extend `String.prototype`
  19. - Expressive API
  20. - Ability to nest styles
  21. - Clean and focused
  22. - Auto-detects color support
  23. - Actively maintained
  24. - [Used by ~4500 modules](https://www.npmjs.com/browse/depended/chalk) as of July 15, 2015
  25. ## Install
  26. ```
  27. $ npm install --save chalk
  28. ```
  29. ## Usage
  30. Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
  31. ```js
  32. var chalk = require('chalk');
  33. // style a string
  34. chalk.blue('Hello world!');
  35. // combine styled and normal strings
  36. chalk.blue('Hello') + 'World' + chalk.red('!');
  37. // compose multiple styles using the chainable API
  38. chalk.blue.bgRed.bold('Hello world!');
  39. // pass in multiple arguments
  40. chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
  41. // nest styles
  42. chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
  43. // nest styles of the same type even (color, underline, background)
  44. chalk.green(
  45. 'I am a green line ' +
  46. chalk.blue.underline.bold('with a blue substring') +
  47. ' that becomes green again!'
  48. );
  49. ```
  50. Easily define your own themes.
  51. ```js
  52. var chalk = require('chalk');
  53. var error = chalk.bold.red;
  54. console.log(error('Error!'));
  55. ```
  56. Take advantage of console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
  57. ```js
  58. var name = 'Sindre';
  59. console.log(chalk.green('Hello %s'), name);
  60. //=> Hello Sindre
  61. ```
  62. ## API
  63. ### chalk.`<style>[.<style>...](string, [string...])`
  64. Example: `chalk.red.bold.underline('Hello', 'world');`
  65. Chain [styles](#styles) and call the last one as a method with a string argument. Order doesn't matter, and later styles take precedent in case of a conflict. This simply means that `Chalk.red.yellow.green` is equivalent to `Chalk.green`.
  66. Multiple arguments will be separated by space.
  67. ### chalk.enabled
  68. Color support is automatically detected, but you can override it by setting the `enabled` property. You should however only do this in your own code as it applies globally to all chalk consumers.
  69. If you need to change this in a reusable module create a new instance:
  70. ```js
  71. var ctx = new chalk.constructor({enabled: false});
  72. ```
  73. ### chalk.supportsColor
  74. Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
  75. Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, add an environment variable `FORCE_COLOR` with any value to force color. Trumps `--no-color`.
  76. ### chalk.styles
  77. Exposes the styles as [ANSI escape codes](https://github.com/chalk/ansi-styles).
  78. Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own.
  79. ```js
  80. var chalk = require('chalk');
  81. console.log(chalk.styles.red);
  82. //=> {open: '\u001b[31m', close: '\u001b[39m'}
  83. console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
  84. ```
  85. ### chalk.hasColor(string)
  86. Check whether a string [has color](https://github.com/chalk/has-ansi).
  87. ### chalk.stripColor(string)
  88. [Strip color](https://github.com/chalk/strip-ansi) from a string.
  89. Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
  90. Example:
  91. ```js
  92. var chalk = require('chalk');
  93. var styledString = getText();
  94. if (!chalk.supportsColor) {
  95. styledString = chalk.stripColor(styledString);
  96. }
  97. ```
  98. ## Styles
  99. ### Modifiers
  100. - `reset`
  101. - `bold`
  102. - `dim`
  103. - `italic` *(not widely supported)*
  104. - `underline`
  105. - `inverse`
  106. - `hidden`
  107. - `strikethrough` *(not widely supported)*
  108. ### Colors
  109. - `black`
  110. - `red`
  111. - `green`
  112. - `yellow`
  113. - `blue` *(on Windows the bright version is used as normal blue is illegible)*
  114. - `magenta`
  115. - `cyan`
  116. - `white`
  117. - `gray`
  118. ### Background colors
  119. - `bgBlack`
  120. - `bgRed`
  121. - `bgGreen`
  122. - `bgYellow`
  123. - `bgBlue`
  124. - `bgMagenta`
  125. - `bgCyan`
  126. - `bgWhite`
  127. ## 256-colors
  128. Chalk does not support anything other than the base eight colors, which guarantees it will work on all terminals and systems. Some terminals, specifically `xterm` compliant ones, will support the full range of 8-bit colors. For this the lower level [ansi-256-colors](https://github.com/jbnicolai/ansi-256-colors) package can be used.
  129. ## Windows
  130. If you're on Windows, do yourself a favor and use [`cmder`](http://bliker.github.io/cmder/) instead of `cmd.exe`.
  131. ## Related
  132. - [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
  133. - [ansi-styles](https://github.com/chalk/ansi-styles/) - ANSI escape codes for styling strings in the terminal
  134. - [supports-color](https://github.com/chalk/supports-color/) - Detect whether a terminal supports color
  135. - [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
  136. - [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
  137. - [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
  138. - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
  139. ## License
  140. MIT © [Sindre Sorhus](http://sindresorhus.com)