chalk/readme.md

201 lines
4.6 KiB
Markdown
Raw Normal View History

# <img width="300" src="https://cdn.rawgit.com/sindresorhus/chalk/77ae94f63ab1ac61389b190e5a59866569d1a376/logo.svg" alt="chalk">
2013-08-03 02:16:26 +02:00
2013-12-13 20:21:51 +01:00
> Terminal string styling done right
2013-08-03 02:16:26 +02:00
2014-04-03 22:57:06 +02:00
[![Build Status](https://travis-ci.org/sindresorhus/chalk.svg?branch=master)](https://travis-ci.org/sindresorhus/chalk)
2014-06-26 00:18:23 +02:00
![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg)
2013-11-10 13:25:35 +01:00
[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.
2013-08-03 02:16:26 +02:00
**Chalk is a clean and focused alternative.**
2014-06-04 01:38:03 +02:00
![screenshot](https://github.com/sindresorhus/ansi-styles/raw/master/screenshot.png)
2013-08-03 02:16:26 +02:00
## Why
2014-07-04 21:29:01 +02:00
- Highly performant
- Doesn't extend `String.prototype`
2013-08-03 02:16:26 +02:00
- Expressive API
2014-06-24 17:23:43 +02:00
- Ability to nest styles
2013-08-03 18:47:47 +02:00
- Clean and focused
2013-08-03 02:16:26 +02:00
- Auto-detects color support
- Actively maintained
- [Used by 1700+ modules](https://npmjs.org/browse/depended/chalk)
2013-08-03 02:16:26 +02:00
## Install
2014-06-04 01:43:07 +02:00
```sh
npm install --save chalk
2014-02-11 20:33:11 +01:00
```
2013-08-03 02:16:26 +02:00
2014-04-03 22:57:06 +02:00
## Usage
2013-08-03 02:16:26 +02:00
Chalk comes with an easy to use composable API where you just chain and nest
the styles you want.
2013-08-03 02:16:26 +02:00
```js
var chalk = require('chalk');
// style a string
chalk.blue('Hello world!');
2013-08-03 02:16:26 +02:00
// combine styled and normal strings
chalk.blue('Hello'), 'World' + chalk.red('!');
2013-08-03 02:16:26 +02:00
// compose multiple styles using the chainable API
chalk.blue.bgRed.bold('Hello world!');
2013-08-03 18:47:47 +02:00
2014-06-24 17:23:43 +02:00
// pass in multiple arguments
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
2014-06-24 17:23:43 +02:00
2013-08-03 18:47:47 +02:00
// nest styles
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
2014-06-24 17:23:43 +02:00
// nest styles of the same type even (color, underline, background)
chalk.green(
'I am a green line ' +
chalk.blue.underline.bold('with a blue substring') +
' that becomes green again!'
);
2013-08-03 02:16:26 +02:00
```
Easily define your own themes.
2013-08-03 02:16:26 +02:00
```js
var chalk = require('chalk');
var error = chalk.bold.red;
console.log(error('Error!'));
```
Take advantage of console.log [string
substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
```js
var name = 'Sindre';
console.log(chalk.green('Hello %s'), name);
//=> Hello Sindre
```
2013-08-03 02:16:26 +02:00
## API
2013-10-19 18:04:30 +02:00
### chalk.`<style>[.<style>...](string, [string...])`
2013-08-03 02:16:26 +02:00
2013-12-13 20:21:51 +01:00
Example: `chalk.red.bold.underline('Hello', 'world');`
2013-08-03 02:16:26 +02:00
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`.
2013-12-13 20:21:51 +01:00
Multiple arguments will be separated by space.
2013-08-03 02:16:26 +02:00
### chalk.enabled
Color support is automatically detected, but you can override it.
### chalk.supportsColor
Detect whether the terminal [supports
color](https://github.com/sindresorhus/supports-color).
2013-08-03 02:16:26 +02:00
Can be overridden by the user with the flags `--color` and `--no-color`.
Used internally and handled for you, but exposed for convenience.
### chalk.styles
Exposes the styles as [ANSI escape
codes](https://github.com/sindresorhus/ansi-styles).
2013-08-03 02:16:26 +02:00
Generally not useful, but you might need just the `.open` or `.close` escape
2014-08-30 23:16:28 +02:00
code if you're mixing externally styled strings with your own.
2013-12-13 20:21:51 +01:00
2013-08-03 02:16:26 +02:00
```js
var chalk = require('chalk');
2013-08-03 18:47:47 +02:00
2013-08-03 02:16:26 +02:00
console.log(chalk.styles.red);
2014-06-04 01:43:07 +02:00
//=> {open: '\u001b[31m', close: '\u001b[39m'}
2013-08-03 18:47:47 +02:00
2013-12-08 01:02:35 +01:00
console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
2013-08-03 02:16:26 +02:00
```
2014-06-24 21:34:11 +02:00
### chalk.hasColor(string)
Check whether a string [has color](https://github.com/sindresorhus/has-ansi).
2013-08-03 03:38:42 +02:00
### chalk.stripColor(string)
2013-08-03 02:16:26 +02:00
2013-12-08 01:02:35 +01:00
[Strip color](https://github.com/sindresorhus/strip-ansi) from a string.
2013-08-03 02:16:26 +02:00
Can be useful in combination with `.supportsColor` to strip color on externally
styled text when it's not supported.
2013-12-13 20:21:51 +01:00
Example:
```js
var chalk = require('chalk');
2014-02-11 20:33:11 +01:00
var styledString = getText();
2013-12-13 20:21:51 +01:00
if (!chalk.supportsColor) {
2014-02-11 20:33:11 +01:00
styledString = chalk.stripColor(styledString);
2013-12-13 20:21:51 +01:00
}
```
2013-08-03 02:16:26 +02:00
## Styles
### General
2014-06-04 01:38:03 +02:00
- `reset`
- `bold`
- `dim`
- `italic` *(not widely supported)*
- `underline`
- `inverse`
- `hidden`
- `strikethrough` *(not widely supported)*
2013-08-03 02:16:26 +02:00
### Text colors
2014-06-04 01:38:03 +02:00
- `black`
- `red`
- `green`
- `yellow`
- `blue`
- `magenta`
- `cyan`
- `white`
- `gray`
2013-08-03 02:16:26 +02:00
### Background colors
2014-06-04 01:38:03 +02:00
- `bgBlack`
- `bgRed`
- `bgGreen`
- `bgYellow`
- `bgBlue`
- `bgMagenta`
- `bgCyan`
- `bgWhite`
2013-08-03 02:16:26 +02:00
## 256-colors
Chalk does not support 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.
2013-08-03 02:16:26 +02:00
## License
2014-06-04 01:43:07 +02:00
MIT © [Sindre Sorhus](http://sindresorhus.com)