用工小程序前端代码
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.

341 lines
13 KiB

6 months ago
  1. <h1 align="center">
  2. <br>
  3. <br>
  4. <img width="320" src="media/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) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) ![TypeScript-ready](https://img.shields.io/npm/types/chalk.svg) [![run on repl.it](https://repl.it/badge/github/chalk/chalk)](https://repl.it/github/chalk/chalk)
  11. <img src="https://cdn.jsdelivr.net/gh/chalk/ansi-styles@8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" width="900">
  12. <br>
  13. ---
  14. <div align="center">
  15. <p>
  16. <p>
  17. <sup>
  18. Sindre Sorhus' open source work is supported by the community on <a href="https://github.com/sponsors/sindresorhus">GitHub Sponsors</a> and <a href="https://stakes.social/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15">Dev</a>
  19. </sup>
  20. </p>
  21. <sup>Special thanks to:</sup>
  22. <br>
  23. <br>
  24. <a href="https://standardresume.co/tech">
  25. <img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="160"/>
  26. </a>
  27. <br>
  28. <br>
  29. <a href="https://retool.com/?utm_campaign=sindresorhus">
  30. <img src="https://sindresorhus.com/assets/thanks/retool-logo.svg" width="230"/>
  31. </a>
  32. <br>
  33. <br>
  34. <a href="https://doppler.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=chalk&utm_source=github">
  35. <div>
  36. <img src="https://dashboard.doppler.com/imgs/logo-long.svg" width="240" alt="Doppler">
  37. </div>
  38. <b>All your environment variables, in one place</b>
  39. <div>
  40. <span>Stop struggling with scattered API keys, hacking together home-brewed tools,</span>
  41. <br>
  42. <span>and avoiding access controls. Keep your team and servers in sync with Doppler.</span>
  43. </div>
  44. </a>
  45. <br>
  46. <a href="https://uibakery.io/?utm_source=chalk&utm_medium=sponsor&utm_campaign=github">
  47. <div>
  48. <img src="https://sindresorhus.com/assets/thanks/uibakery-logo.jpg" width="270" alt="UI Bakery">
  49. </div>
  50. </a>
  51. </p>
  52. </div>
  53. ---
  54. <br>
  55. ## Highlights
  56. - Expressive API
  57. - Highly performant
  58. - Ability to nest styles
  59. - [256/Truecolor color support](#256-and-truecolor-color-support)
  60. - Auto-detects color support
  61. - Doesn't extend `String.prototype`
  62. - Clean and focused
  63. - Actively maintained
  64. - [Used by ~50,000 packages](https://www.npmjs.com/browse/depended/chalk) as of January 1, 2020
  65. ## Install
  66. ```console
  67. $ npm install chalk
  68. ```
  69. ## Usage
  70. ```js
  71. const chalk = require('chalk');
  72. console.log(chalk.blue('Hello world!'));
  73. ```
  74. Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
  75. ```js
  76. const chalk = require('chalk');
  77. const log = console.log;
  78. // Combine styled and normal strings
  79. log(chalk.blue('Hello') + ' World' + chalk.red('!'));
  80. // Compose multiple styles using the chainable API
  81. log(chalk.blue.bgRed.bold('Hello world!'));
  82. // Pass in multiple arguments
  83. log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
  84. // Nest styles
  85. log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
  86. // Nest styles of the same type even (color, underline, background)
  87. log(chalk.green(
  88. 'I am a green line ' +
  89. chalk.blue.underline.bold('with a blue substring') +
  90. ' that becomes green again!'
  91. ));
  92. // ES2015 template literal
  93. log(`
  94. CPU: ${chalk.red('90%')}
  95. RAM: ${chalk.green('40%')}
  96. DISK: ${chalk.yellow('70%')}
  97. `);
  98. // ES2015 tagged template literal
  99. log(chalk`
  100. CPU: {red ${cpu.totalPercent}%}
  101. RAM: {green ${ram.used / ram.total * 100}%}
  102. DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
  103. `);
  104. // Use RGB colors in terminal emulators that support it.
  105. log(chalk.keyword('orange')('Yay for orange colored text!'));
  106. log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
  107. log(chalk.hex('#DEADED').bold('Bold gray!'));
  108. ```
  109. Easily define your own themes:
  110. ```js
  111. const chalk = require('chalk');
  112. const error = chalk.bold.red;
  113. const warning = chalk.keyword('orange');
  114. console.log(error('Error!'));
  115. console.log(warning('Warning!'));
  116. ```
  117. Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
  118. ```js
  119. const name = 'Sindre';
  120. console.log(chalk.green('Hello %s'), name);
  121. //=> 'Hello Sindre'
  122. ```
  123. ## API
  124. ### chalk.`<style>[.<style>...](string, [string...])`
  125. Example: `chalk.red.bold.underline('Hello', 'world');`
  126. 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`.
  127. Multiple arguments will be separated by space.
  128. ### chalk.level
  129. Specifies the level of color support.
  130. Color support is automatically detected, but you can override it by setting the `level` property. You should however only do this in your own code as it applies globally to all Chalk consumers.
  131. If you need to change this in a reusable module, create a new instance:
  132. ```js
  133. const ctx = new chalk.Instance({level: 0});
  134. ```
  135. | Level | Description |
  136. | :---: | :--- |
  137. | `0` | All colors disabled |
  138. | `1` | Basic color support (16 colors) |
  139. | `2` | 256 color support |
  140. | `3` | Truecolor support (16 million colors) |
  141. ### chalk.supportsColor
  142. Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
  143. Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, use the environment variable `FORCE_COLOR=1` (level 1), `FORCE_COLOR=2` (level 2), or `FORCE_COLOR=3` (level 3) to forcefully enable color, or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
  144. Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
  145. ### chalk.stderr and chalk.stderr.supportsColor
  146. `chalk.stderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `chalk.supportsColor` apply to this too. `chalk.stderr.supportsColor` is exposed for convenience.
  147. ## Styles
  148. ### Modifiers
  149. - `reset` - Resets the current color chain.
  150. - `bold` - Make text bold.
  151. - `dim` - Emitting only a small amount of light.
  152. - `italic` - Make text italic. *(Not widely supported)*
  153. - `underline` - Make text underline. *(Not widely supported)*
  154. - `inverse`- Inverse background and foreground colors.
  155. - `hidden` - Prints the text, but makes it invisible.
  156. - `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)*
  157. - `visible`- Prints the text only when Chalk has a color level > 0. Can be useful for things that are purely cosmetic.
  158. ### Colors
  159. - `black`
  160. - `red`
  161. - `green`
  162. - `yellow`
  163. - `blue`
  164. - `magenta`
  165. - `cyan`
  166. - `white`
  167. - `blackBright` (alias: `gray`, `grey`)
  168. - `redBright`
  169. - `greenBright`
  170. - `yellowBright`
  171. - `blueBright`
  172. - `magentaBright`
  173. - `cyanBright`
  174. - `whiteBright`
  175. ### Background colors
  176. - `bgBlack`
  177. - `bgRed`
  178. - `bgGreen`
  179. - `bgYellow`
  180. - `bgBlue`
  181. - `bgMagenta`
  182. - `bgCyan`
  183. - `bgWhite`
  184. - `bgBlackBright` (alias: `bgGray`, `bgGrey`)
  185. - `bgRedBright`
  186. - `bgGreenBright`
  187. - `bgYellowBright`
  188. - `bgBlueBright`
  189. - `bgMagentaBright`
  190. - `bgCyanBright`
  191. - `bgWhiteBright`
  192. ## Tagged template literal
  193. Chalk can be used as a [tagged template literal](https://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
  194. ```js
  195. const chalk = require('chalk');
  196. const miles = 18;
  197. const calculateFeet = miles => miles * 5280;
  198. console.log(chalk`
  199. There are {bold 5280 feet} in a mile.
  200. In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
  201. `);
  202. ```
  203. Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`).
  204. Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent:
  205. ```js
  206. console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
  207. console.log(chalk.bold.rgb(10, 100, 200)`Hello!`);
  208. console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
  209. ```
  210. Note that function styles (`rgb()`, `hsl()`, `keyword()`, etc.) may not contain spaces between parameters.
  211. All interpolated values (`` chalk`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
  212. ## 256 and Truecolor color support
  213. Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps.
  214. Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying `{level: n}` as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 31 (ANSI escape for red).
  215. Examples:
  216. - `chalk.hex('#DEADED').underline('Hello, world!')`
  217. - `chalk.keyword('orange')('Some orange text')`
  218. - `chalk.rgb(15, 100, 204).inverse('Hello!')`
  219. Background versions of these models are prefixed with `bg` and the first level of the module capitalized (e.g. `keyword` for foreground colors and `bgKeyword` for background colors).
  220. - `chalk.bgHex('#DEADED').underline('Hello, world!')`
  221. - `chalk.bgKeyword('orange')('Some orange text')`
  222. - `chalk.bgRgb(15, 100, 204).inverse('Hello!')`
  223. The following color models can be used:
  224. - [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
  225. - [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
  226. - [`keyword`](https://www.w3.org/wiki/CSS/Properties/color/keywords) (CSS keywords) - Example: `chalk.keyword('orange').bold('Orange!')`
  227. - [`hsl`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 100, 50).bold('Orange!')`
  228. - [`hsv`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsv(32, 100, 100).bold('Orange!')`
  229. - [`hwb`](https://en.wikipedia.org/wiki/HWB_color_model) - Example: `chalk.hwb(32, 0, 50).bold('Orange!')`
  230. - [`ansi`](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) - Example: `chalk.ansi(31).bgAnsi(93)('red on yellowBright')`
  231. - [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
  232. ## Windows
  233. If you're on Windows, do yourself a favor and use [Windows Terminal](https://github.com/microsoft/terminal) instead of `cmd.exe`.
  234. ## Origin story
  235. [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) and the package is unmaintained. Although there are other packages, they either do too much or not enough. Chalk is a clean and focused alternative.
  236. ## chalk for enterprise
  237. Available as part of the Tidelift Subscription.
  238. The maintainers of chalk and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-chalk?utm_source=npm-chalk&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
  239. ## Related
  240. - [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
  241. - [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
  242. - [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color
  243. - [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
  244. - [strip-ansi-stream](https://github.com/chalk/strip-ansi-stream) - Strip ANSI escape codes from a stream
  245. - [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
  246. - [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
  247. - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
  248. - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
  249. - [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models
  250. - [chalk-animation](https://github.com/bokub/chalk-animation) - Animate strings in the terminal
  251. - [gradient-string](https://github.com/bokub/gradient-string) - Apply color gradients to strings
  252. - [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
  253. - [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
  254. ## Maintainers
  255. - [Sindre Sorhus](https://github.com/sindresorhus)
  256. - [Josh Junon](https://github.com/qix-)