chalk/readme.md

169 lines
3.9 KiB
Markdown
Raw Normal View History

2013-11-10 13:25:35 +01:00
# <img width="250" src="logo.png" 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)
2013-11-10 13:25:35 +01:00
2013-12-13 20:21:51 +01:00
[colors.js](https://github.com/Marak/colors.js) is currently 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
- **Doesn't extend String.prototype**
- Expressive API
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 1000+ 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
2014-04-03 22:57:06 +02:00
$ 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
2013-12-13 20:21:51 +01: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
2013-12-13 20:21:51 +01:00
console.log( chalk.blue('Hello world!') );
2013-08-03 02:16:26 +02:00
// combine styled and normal strings
2013-12-13 20:21:51 +01:00
console.log( chalk.blue('Hello'), 'World' + chalk.red('!') );
2013-08-03 02:16:26 +02:00
// compose multiple styles using the chainable API
2013-12-13 20:21:51 +01:00
console.log( chalk.blue.bgRed.bold('Hello world!') );
2013-08-03 18:47:47 +02:00
// nest styles
2013-12-13 20:21:51 +01:00
console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') );
// nest styles of the same type even (colour, underline, background)
console.log( chalk.green('Hello, I'm a green line ' + chalk.blue('with a blue substring') + ' that becomes green again!') );
// pass in multiple arguments
2013-12-13 20:21:51 +01:00
console.log( chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz') );
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
2013-12-13 20:21:51 +01:00
Chain [styles](#styles) and call the last one as a method with a string argument. Order doesn't matter.
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
2014-06-14 03:49:42 +02:00
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-12-13 20:21:51 +01:00
Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with yours.
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
```
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
2013-12-13 20:21:51 +01:00
Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
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
## License
2014-06-04 01:43:07 +02:00
MIT © [Sindre Sorhus](http://sindresorhus.com)