No description
Find a file
2017-01-17 12:32:10 -08:00
media move the logo into a /media 2015-06-30 19:56:59 +02:00
.editorconfig minor tweaks 2016-01-17 12:44:51 +01:00
.gitattributes various tweaks 2014-06-04 01:47:53 +02:00
.gitignore minor tweaks 2015-05-26 16:06:57 +02:00
.travis.yml remove stable from list of versions to test 2017-01-17 03:52:10 -08:00
benchmark.js add XO 2015-08-20 03:05:43 +07:00
code-of-conduct.md update code of conduct 2016-05-02 15:36:24 +07:00
contributing.md add Code of Conduct 2016-01-15 00:06:24 +01:00
example.js add example file 2016-10-26 13:00:12 +07:00
index.js add comments explaining 256 color configuration values 2017-01-17 04:02:56 -08:00
license various tweaks 2014-06-04 01:47:53 +02:00
package.json use supportsColor.level instead of an enabled bool 2017-01-17 01:47:55 -08:00
readme.md restructure readme examples 2017-01-17 12:32:10 -08:00
test.js add tests 2017-01-17 02:48:17 -08:00



chalk


Terminal string styling done right

Build Status Coverage Status XO code style

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. Although there are other ones, they either do too much or not enough.

Chalk is a clean and focused alternative.

Why

  • Highly performant
  • Doesn't extend String.prototype
  • Expressive API
  • Ability to nest styles
  • Clean and focused
  • Auto-detects color support
  • Actively maintained
  • Used by ~10,000 modules as of August 2nd, 2016

Install

$ npm install --save chalk

Usage

const chalk = require('chalk');

console.log(chalk.blue('Hello world!'));

Chalk comes with an easy to use composable API where you just chain and nest the styles you want.

const chalk = require('chalk');
const log = console.log;

// combine styled and normal strings
log(chalk.blue('Hello') + 'World' + chalk.red('!'));

// compose multiple styles using the chainable API
log(chalk.blue.bgRed.bold('Hello world!'));

// pass in multiple arguments
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));

// nest styles
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));

// nest styles of the same type even (color, underline, background)
log(chalk.green(
	'I am a green line ' +
	chalk.blue.underline.bold('with a blue substring') +
	' that becomes green again!'
));

// ES2015 template literal
log(`
CPU: ${chalk.red('90%')}
RAM: ${chalk.green('40%')}
DISK: ${chalk.yellow('70%')}
`);

// Use RGB colors in terminal emulators that support it.
log(chalk.keyword('orange')('Yay for orange colored text!'));
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
log(chalk.hex('#DEADED').bold('Bold gray!'));

Easily define your own themes.

const chalk = require('chalk');

const error = chalk.bold.red;
const warning = chalk.keyword('orange');

console.log(error('Error!'));
console.log(warning('Warning!'));

Take advantage of console.log string substitution.

const name = 'Sindre';
console.log(chalk.green('Hello %s'), name);
//=> 'Hello Sindre'

API

chalk.<style>[.<style>...](string, [string...])

Example: chalk.red.bold.underline('Hello', 'world');

Chain 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.

Multiple arguments will be separated by space.

chalk.level

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.

If you need to change this in a reusable module create a new instance:

const ctx = new chalk.constructor({level: 0});

Levels are as follows:

  1. All colors disabled
  2. Basic color support (16 colors)
  3. 256 color support
  4. RGB/Truecolor support (16 million colors)

chalk.supportsColor

Detect whether the terminal supports color. Used internally and handled for you, but exposed for convenience.

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.

chalk.styles

Exposes the styles as ANSI escape codes.

Generally not useful, but you might need just the .open or .close escape code if you're mixing externally styled strings with your own.

const chalk = require('chalk');

console.log(chalk.styles.red);
//=> {open: '\u001b[31m', close: '\u001b[39m'}

console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);

Styles

Modifiers

  • reset
  • bold
  • dim
  • italic (not widely supported)
  • underline
  • inverse
  • hidden
  • strikethrough (not widely supported)

Colors

  • black
  • red
  • green
  • yellow
  • blue (on Windows the bright version is used since normal blue is illegible)
  • magenta
  • cyan
  • white
  • gray

Background colors

  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite

256/16 million (Truecolor) color support

Chalk supports 256 colors and, when manually specified, Truecolor (16 million colors) on all supported terminal emulators.

For supported methods (listed below), the color will be 'fit' to the color level supported (i.e. RGB colors will be downsampled to 16 colors if only basic support is enabled).

Some examples:

  • chalk.hex('#DEADED').underline('Hello, world!')
  • chalk.keyword('orange')('Some orange text')
  • chalk.rgb(15, 100, 204).inverse('Hello!')

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).

  • chalk.bgHex('#DEADED').underline('Hello, world!')
  • chalk.bgKeyword('orange')('Some orange text')
  • chalk.bgRgb(15, 100, 204).inverse('Hello!')

As of this writing, these are the supported color models that are exposed in Chalk:

  • rgb
  • hex
  • keyword (CSS keywords)
  • hsl
  • hsv
  • hwb
  • cmyk
  • xyz
  • lab
  • lch
  • ansi16
  • ansi256
  • hcg
  • apple (see qix-/color-convert#30)

For a complete list of color models, see color-convert's list of conversions.

Windows

If you're on Windows, do yourself a favor and use cmder instead of cmd.exe.

  • chalk-cli - CLI for this module
  • ansi-styles - ANSI escape codes for styling strings in the terminal
  • supports-color - Detect whether a terminal supports color
  • strip-ansi - Strip ANSI escape codes
  • has-ansi - Check if a string has ANSI escape codes
  • ansi-regex - Regular expression for matching ANSI escape codes
  • wrap-ansi - Wordwrap a string with ANSI escape codes
  • slice-ansi - Slice a string with ANSI escape codes
  • color-convert - Converts colors between different models

License

MIT © Sindre Sorhus