Comparing version 1.1.3 to 2.0.0
220
index.js
'use strict'; | ||
var escapeStringRegexp = require('escape-string-regexp'); | ||
var ansiStyles = require('ansi-styles'); | ||
var stripAnsi = require('strip-ansi'); | ||
var hasAnsi = require('has-ansi'); | ||
var supportsColor = require('supports-color'); | ||
var defineProps = Object.defineProperties; | ||
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM); | ||
const escapeStringRegexp = require('escape-string-regexp'); | ||
const ansiStyles = require('ansi-styles'); | ||
const supportsColor = require('supports-color'); | ||
const template = require('./templates.js'); | ||
const isSimpleWindowsTerm = process.platform === 'win32' && !process.env.TERM.toLowerCase().startsWith('xterm'); | ||
// `supportsColor.level` → `ansiStyles.color[name]` mapping | ||
const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m']; | ||
// `color-convert` models to exclude from the Chalk API due to conflicts and such | ||
const skipModels = new Set(['gray']); | ||
const styles = Object.create(null); | ||
function applyOptions(obj, options) { | ||
options = options || {}; | ||
// Detect level if not set manually | ||
obj.level = options.level === undefined ? supportsColor.level : options.level; | ||
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0; | ||
} | ||
function Chalk(options) { | ||
// detect mode if not set manually | ||
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled; | ||
// We check for this.template here since calling chalk.constructor() | ||
// by itself will have a `this` of a previously constructed chalk object. | ||
if (!this || !(this instanceof Chalk) || this.template) { | ||
const chalk = {}; | ||
applyOptions(chalk, options); | ||
chalk.template = function () { | ||
const args = [].slice.call(arguments); | ||
return chalkTag.apply(null, [chalk.template].concat(args)); | ||
}; | ||
Object.setPrototypeOf(chalk, Chalk.prototype); | ||
Object.setPrototypeOf(chalk.template, chalk); | ||
chalk.template.constructor = Chalk; | ||
return chalk.template; | ||
} | ||
applyOptions(this, options); | ||
} | ||
// use bright blue on Windows as the normal blue color is illegible | ||
// Use bright blue on Windows as the normal blue color is illegible | ||
if (isSimpleWindowsTerm) { | ||
ansiStyles.blue.open = '\u001b[94m'; | ||
ansiStyles.blue.open = '\u001B[94m'; | ||
} | ||
var styles = (function () { | ||
var ret = {}; | ||
for (const key of Object.keys(ansiStyles)) { | ||
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g'); | ||
Object.keys(ansiStyles).forEach(function (key) { | ||
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g'); | ||
styles[key] = { | ||
get() { | ||
const codes = ansiStyles[key]; | ||
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key); | ||
} | ||
}; | ||
} | ||
ret[key] = { | ||
get: function () { | ||
return build.call(this, this._styles.concat(key)); | ||
} | ||
}; | ||
}); | ||
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g'); | ||
for (const model of Object.keys(ansiStyles.color.ansi)) { | ||
if (skipModels.has(model)) { | ||
continue; | ||
} | ||
return ret; | ||
})(); | ||
styles[model] = { | ||
get() { | ||
const level = this.level; | ||
return function () { | ||
const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments); | ||
const codes = { | ||
open, | ||
close: ansiStyles.color.close, | ||
closeRe: ansiStyles.color.closeRe | ||
}; | ||
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model); | ||
}; | ||
} | ||
}; | ||
} | ||
var proto = defineProps(function chalk() {}, styles); | ||
ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g'); | ||
for (const model of Object.keys(ansiStyles.bgColor.ansi)) { | ||
if (skipModels.has(model)) { | ||
continue; | ||
} | ||
function build(_styles) { | ||
var builder = function () { | ||
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1); | ||
styles[bgModel] = { | ||
get() { | ||
const level = this.level; | ||
return function () { | ||
const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments); | ||
const codes = { | ||
open, | ||
close: ansiStyles.bgColor.close, | ||
closeRe: ansiStyles.bgColor.closeRe | ||
}; | ||
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model); | ||
}; | ||
} | ||
}; | ||
} | ||
const proto = Object.defineProperties(() => {}, styles); | ||
function build(_styles, key) { | ||
const builder = function () { | ||
return applyStyle.apply(builder, arguments); | ||
@@ -44,7 +118,31 @@ }; | ||
builder._styles = _styles; | ||
builder.enabled = this.enabled; | ||
// __proto__ is used because we must return a function, but there is | ||
const self = this; | ||
Object.defineProperty(builder, 'level', { | ||
enumerable: true, | ||
get() { | ||
return self.level; | ||
}, | ||
set(level) { | ||
self.level = level; | ||
} | ||
}); | ||
Object.defineProperty(builder, 'enabled', { | ||
enumerable: true, | ||
get() { | ||
return self.enabled; | ||
}, | ||
set(enabled) { | ||
self.enabled = enabled; | ||
} | ||
}); | ||
// See below for fix regarding invisible grey/dim combination on Windows | ||
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey'; | ||
// `__proto__` is used because we must return a function, but there is | ||
// no way to create a function with a different prototype. | ||
/* eslint-disable no-proto */ | ||
builder.__proto__ = proto; | ||
builder.__proto__ = proto; // eslint-disable-line no-proto | ||
@@ -55,10 +153,10 @@ return builder; | ||
function applyStyle() { | ||
// support varags, but simply cast to string in case there's only one arg | ||
var args = arguments; | ||
var argsLen = args.length; | ||
var str = argsLen !== 0 && String(arguments[0]); | ||
// Support varags, but simply cast to string in case there's only one arg | ||
const args = arguments; | ||
const argsLen = args.length; | ||
let str = argsLen !== 0 && String(arguments[0]); | ||
if (argsLen > 1) { | ||
// don't slice `arguments`, it prevents v8 optimizations | ||
for (var a = 1; a < argsLen; a++) { | ||
// Don't slice `arguments`, it prevents V8 optimizations | ||
for (let a = 1; a < argsLen; a++) { | ||
str += ' ' + args[a]; | ||
@@ -68,20 +166,15 @@ } | ||
if (!this.enabled || !str) { | ||
if (!this.enabled || this.level <= 0 || !str) { | ||
return str; | ||
} | ||
var nestedStyles = this._styles; | ||
var i = nestedStyles.length; | ||
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe, | ||
// see https://github.com/chalk/chalk/issues/58 | ||
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop. | ||
var originalDim = ansiStyles.dim.open; | ||
if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) { | ||
const originalDim = ansiStyles.dim.open; | ||
if (isSimpleWindowsTerm && this.hasGrey) { | ||
ansiStyles.dim.open = ''; | ||
} | ||
while (i--) { | ||
var code = ansiStyles[nestedStyles[i]]; | ||
for (const code of this._styles.slice().reverse()) { | ||
// Replace any instances already present with a re-opening code | ||
@@ -91,5 +184,10 @@ // otherwise only the part of the string until said closing code | ||
str = code.open + str.replace(code.closeRe, code.open) + code.close; | ||
// Close the styling before a linebreak and reopen | ||
// after next line to fix a bleed issue on macOS | ||
// https://github.com/chalk/chalk/pull/92 | ||
str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`); | ||
} | ||
// Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue. | ||
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue | ||
ansiStyles.dim.open = originalDim; | ||
@@ -100,22 +198,22 @@ | ||
function init() { | ||
var ret = {}; | ||
function chalkTag(chalk, strings) { | ||
const args = [].slice.call(arguments, 2); | ||
Object.keys(styles).forEach(function (name) { | ||
ret[name] = { | ||
get: function () { | ||
return build.call(this, [name]); | ||
} | ||
}; | ||
}); | ||
if (!Array.isArray(strings)) { | ||
return strings.toString(); | ||
} | ||
return ret; | ||
const parts = [strings.raw[0]]; | ||
for (let i = 1; i < strings.length; i++) { | ||
parts.push(args[i - 1].toString().replace(/[{}]/g, '\\$&')); | ||
parts.push(strings.raw[i]); | ||
} | ||
return template(chalk, parts.join('')); | ||
} | ||
defineProps(Chalk.prototype, init()); | ||
Object.defineProperties(Chalk.prototype, styles); | ||
module.exports = new Chalk(); | ||
module.exports.styles = ansiStyles; | ||
module.exports.hasColor = hasAnsi; | ||
module.exports.stripColor = stripAnsi; | ||
module.exports = Chalk(); // eslint-disable-line new-cap | ||
module.exports.supportsColor = supportsColor; |
128
package.json
{ | ||
"name": "chalk", | ||
"version": "1.1.3", | ||
"description": "Terminal string styling done right. Much color.", | ||
"license": "MIT", | ||
"repository": "chalk/chalk", | ||
"maintainers": [ | ||
"Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)", | ||
"Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)", | ||
"JD Ballard <i.am.qix@gmail.com> (github.com/qix-)" | ||
], | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && mocha", | ||
"bench": "matcha benchmark.js", | ||
"coverage": "nyc npm test && nyc report", | ||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"color", | ||
"colour", | ||
"colors", | ||
"terminal", | ||
"console", | ||
"cli", | ||
"string", | ||
"str", | ||
"ansi", | ||
"style", | ||
"styles", | ||
"tty", | ||
"formatting", | ||
"rgb", | ||
"256", | ||
"shell", | ||
"xterm", | ||
"log", | ||
"logging", | ||
"command-line", | ||
"text" | ||
], | ||
"dependencies": { | ||
"ansi-styles": "^2.2.1", | ||
"escape-string-regexp": "^1.0.2", | ||
"has-ansi": "^2.0.0", | ||
"strip-ansi": "^3.0.0", | ||
"supports-color": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"matcha": "^0.6.0", | ||
"mocha": "*", | ||
"nyc": "^3.0.0", | ||
"require-uncached": "^1.0.2", | ||
"resolve-from": "^1.0.0", | ||
"semver": "^4.3.3", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"envs": [ | ||
"node", | ||
"mocha" | ||
] | ||
} | ||
"name": "chalk", | ||
"version": "2.0.0", | ||
"description": "Terminal string styling done right. Much color", | ||
"license": "MIT", | ||
"repository": "chalk/chalk", | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc mocha", | ||
"bench": "matcha benchmark.js", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"templates.js" | ||
], | ||
"keywords": [ | ||
"color", | ||
"colour", | ||
"colors", | ||
"terminal", | ||
"console", | ||
"cli", | ||
"string", | ||
"str", | ||
"ansi", | ||
"style", | ||
"styles", | ||
"tty", | ||
"formatting", | ||
"rgb", | ||
"256", | ||
"shell", | ||
"xterm", | ||
"log", | ||
"logging", | ||
"command-line", | ||
"text" | ||
], | ||
"dependencies": { | ||
"ansi-styles": "^3.1.0", | ||
"escape-string-regexp": "^1.0.5", | ||
"supports-color": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"import-fresh": "^2.0.0", | ||
"matcha": "^0.7.0", | ||
"mocha": "*", | ||
"nyc": "^11.0.2", | ||
"resolve-from": "^3.0.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"envs": [ | ||
"node", | ||
"mocha" | ||
] | ||
} | ||
} |
241
readme.md
<h1 align="center"> | ||
<br> | ||
<br> | ||
<img width="360" src="https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk"> | ||
<img width="320" src="https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk"> | ||
<br> | ||
@@ -12,7 +12,4 @@ <br> | ||
[![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) | ||
[![Coverage Status](https://coveralls.io/repos/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/r/chalk/chalk?branch=master) | ||
[![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) | ||
[![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) [![](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/sindresorhus/xo) | ||
[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. | ||
@@ -25,12 +22,13 @@ | ||
## Why | ||
## Highlights | ||
- Expressive API | ||
- Highly performant | ||
- Ability to nest styles | ||
- [256/Truecolor color support](#256-and-truecolor-color-support) | ||
- Auto-detects color support | ||
- Doesn't extend `String.prototype` | ||
- Expressive API | ||
- Ability to nest styles | ||
- Clean and focused | ||
- Auto-detects color support | ||
- Actively maintained | ||
- [Used by ~4500 modules](https://www.npmjs.com/browse/depended/chalk) as of July 15, 2015 | ||
- [Used by ~17,000 packages](https://www.npmjs.com/browse/depended/chalk) as of June 20th, 2017 | ||
@@ -40,5 +38,5 @@ | ||
```console | ||
$ npm install chalk | ||
``` | ||
$ npm install --save chalk | ||
``` | ||
@@ -48,44 +46,71 @@ | ||
```js | ||
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. | ||
```js | ||
var chalk = require('chalk'); | ||
const chalk = require('chalk'); | ||
const log = console.log; | ||
// style a string | ||
chalk.blue('Hello world!'); | ||
// Combine styled and normal strings | ||
log(chalk.blue('Hello') + 'World' + chalk.red('!')); | ||
// combine styled and normal strings | ||
chalk.blue('Hello') + 'World' + chalk.red('!'); | ||
// Compose multiple styles using the chainable API | ||
log(chalk.blue.bgRed.bold('Hello world!')); | ||
// compose multiple styles using the chainable API | ||
chalk.blue.bgRed.bold('Hello world!'); | ||
// Pass in multiple arguments | ||
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz')); | ||
// pass in multiple arguments | ||
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'); | ||
// Nest styles | ||
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!')); | ||
// nest styles | ||
chalk.red('Hello', chalk.underline.bgBlue('world') + '!'); | ||
// nest styles of the same type even (color, underline, background) | ||
chalk.green( | ||
// 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%')} | ||
`); | ||
// ES2015 tagged template literal | ||
log(chalk` | ||
CPU: {red ${cpu.totalPercent}%} | ||
RAM: {green ${ram.used / ram.total * 100}%} | ||
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%} | ||
`); | ||
// 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. | ||
Easily define your own themes: | ||
```js | ||
var chalk = require('chalk'); | ||
var error = chalk.bold.red; | ||
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](http://nodejs.org/docs/latest/api/console.html#console_console_log_data). | ||
Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args): | ||
```js | ||
var name = 'Sindre'; | ||
const name = 'Sindre'; | ||
console.log(chalk.green('Hello %s'), name); | ||
//=> Hello Sindre | ||
//=> 'Hello Sindre' | ||
``` | ||
@@ -100,3 +125,3 @@ | ||
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`. | ||
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`. | ||
@@ -107,53 +132,38 @@ Multiple arguments will be separated by space. | ||
Color support is automatically detected, but you can override it by setting the `enabled` property. You should however only do this in your own code as it applies globally to all chalk consumers. | ||
Color support is automatically detected, as is the level (see `chalk.level`). However, if you'd like to simply enable/disable Chalk, you can do so via the `.enabled` property. | ||
If you need to change this in a reusable module create a new instance: | ||
Chalk is enabled by default unless expicitly disabled via the constructor or `chalk.level` is `0`. | ||
If you need to change this in a reusable module, create a new instance: | ||
```js | ||
var ctx = new chalk.constructor({enabled: false}); | ||
const ctx = new chalk.constructor({enabled: false}); | ||
``` | ||
### chalk.supportsColor | ||
### chalk.level | ||
Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience. | ||
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. | ||
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`. | ||
If you need to change this in a reusable module, create a new instance: | ||
### chalk.styles | ||
Exposes the styles as [ANSI escape codes](https://github.com/chalk/ansi-styles). | ||
Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own. | ||
```js | ||
var 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); | ||
const ctx = new chalk.constructor({level: 0}); | ||
``` | ||
### chalk.hasColor(string) | ||
Levels are as follows: | ||
Check whether a string [has color](https://github.com/chalk/has-ansi). | ||
0. All colors disabled | ||
1. Basic color support (16 colors) | ||
2. 256 color support | ||
3. Truecolor support (16 million colors) | ||
### chalk.stripColor(string) | ||
### chalk.supportsColor | ||
[Strip color](https://github.com/chalk/strip-ansi) from a string. | ||
Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience. | ||
Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported. | ||
Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, add the environment variable `FORCE_COLOR=1` to forcefully enable color or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks. | ||
Example: | ||
Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively. | ||
```js | ||
var chalk = require('chalk'); | ||
var styledString = getText(); | ||
if (!chalk.supportsColor) { | ||
styledString = chalk.stripColor(styledString); | ||
} | ||
``` | ||
## Styles | ||
@@ -166,7 +176,7 @@ | ||
- `dim` | ||
- `italic` *(not widely supported)* | ||
- `italic` *(Not widely supported)* | ||
- `underline` | ||
- `inverse` | ||
- `hidden` | ||
- `strikethrough` *(not widely supported)* | ||
- `strikethrough` *(Not widely supported)* | ||
@@ -179,3 +189,3 @@ ### Colors | ||
- `yellow` | ||
- `blue` *(on Windows the bright version is used as normal blue is illegible)* | ||
- `blue` *(On Windows the bright version is used since normal blue is illegible)* | ||
- `magenta` | ||
@@ -185,2 +195,10 @@ - `cyan` | ||
- `gray` | ||
- `blackBright` | ||
- `redBright` | ||
- `greenBright` | ||
- `yellowBright` | ||
- `blueBright` | ||
- `magentaBright` | ||
- `cyanBright` | ||
- `whiteBright` | ||
@@ -197,12 +215,75 @@ ### Background colors | ||
- `bgWhite` | ||
- `bgBlackBright` | ||
- `bgRedBright` | ||
- `bgGreenBright` | ||
- `bgYellowBright` | ||
- `bgBlueBright` | ||
- `bgMagentaBright` | ||
- `bgCyanBright` | ||
- `bgWhiteBright` | ||
## 256-colors | ||
## Tagged template literal | ||
Chalk does not 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. | ||
Chalk can be used as a [tagged template literal](http://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals). | ||
```js | ||
const chalk = require('chalk'); | ||
const miles = 18; | ||
const calculateFeet = miles => miles * 5280; | ||
console.log(chalk` | ||
There are {bold 5280 feet} in a mile. | ||
In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}. | ||
`); | ||
``` | ||
Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`). | ||
Template styles are chained exactly like normal Chalk styles. The following two statements are equivalent: | ||
```js | ||
console.log(chalk.bold.rgb(10, 100, 200)('Hello!')); | ||
console.log(chalk`{bold.rgb(10,100,200) Hello!}`); | ||
``` | ||
Note that function styles (`rgb()`, `hsl()`, `keyword()`, etc.) may not contain spaces between parameters. | ||
All interpolated values (`` chalk`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped. | ||
## 256 and Truecolor color support | ||
Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps. | ||
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). | ||
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!')` | ||
The following color models can be used: | ||
- [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')` | ||
- [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')` | ||
- [`keyword`](https://www.w3.org/wiki/CSS/Properties/color/keywords) (CSS keywords) - Example: `chalk.keyword('orange').bold('Orange!')` | ||
- [`hsl`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 100, 50).bold('Orange!')` | ||
- [`hsv`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 1, 1).bold('Orange!')` | ||
- [`hwb`](https://en.wikipedia.org/wiki/HWB_color_model) - Example: `chalk.hsl(32, 0, 50).bold('Orange!')` | ||
- `ansi16` | ||
- `ansi256` | ||
## Windows | ||
If you're on Windows, do yourself a favor and use [`cmder`](http://bliker.github.io/cmder/) instead of `cmd.exe`. | ||
If you're on Windows, do yourself a favor and use [`cmder`](http://cmder.net/) instead of `cmd.exe`. | ||
@@ -213,4 +294,4 @@ | ||
- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module | ||
- [ansi-styles](https://github.com/chalk/ansi-styles/) - ANSI escape codes for styling strings in the terminal | ||
- [supports-color](https://github.com/chalk/supports-color/) - Detect whether a terminal supports color | ||
- [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal | ||
- [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color | ||
- [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes | ||
@@ -220,6 +301,14 @@ - [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes | ||
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes | ||
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes | ||
- [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models | ||
## Maintainers | ||
- [Sindre Sorhus](https://github.com/sindresorhus) | ||
- [Josh Junon](https://github.com/qix-) | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21823
3
7
5
313
303
1
+ Addedansi-styles@3.2.1(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedhas-flag@2.0.0(transitive)
+ Addedsupports-color@4.5.0(transitive)
- Removedhas-ansi@^2.0.0
- Removedstrip-ansi@^3.0.0
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
Updatedansi-styles@^3.1.0
Updatedescape-string-regexp@^1.0.5
Updatedsupports-color@^4.0.0