chalk/readme.md

164 lines
3.6 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.**
![screenshot](screenshot.png)
## 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
2014-02-11 20:33:11 +01:00
- [Used by 300+ modules](https://npmjs.org/browse/depended/chalk)
2013-08-03 02:16:26 +02:00
## Install
2014-04-03 22:57:06 +02:00
```bash
$ 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') + '!') );
// 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
Detect whether the terminal [supports color](https://github.com/sindresorhus/has-color).
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);
2013-12-08 01:02:35 +01:00
//=> {open: '\x1b[31m', close: '\x1b[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
- reset
- bold
- italic
- underline
- inverse
- strikethrough
### Text colors
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- gray
### Background colors
- bgBlack
- bgRed
- bgGreen
- bgYellow
- bgBlue
- bgMagenta
- bgCyan
- bgWhite
## License
2014-04-03 22:57:06 +02:00
[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com)