chalk/index.js

221 lines
6 KiB
JavaScript
Raw Normal View History

2013-08-03 02:16:26 +02:00
'use strict';
2017-06-20 19:18:21 +02:00
const escapeStringRegexp = require('escape-string-regexp');
const ansiStyles = require('ansi-styles');
2018-12-26 02:37:03 +01:00
const {stdout: stdoutColor} = require('supports-color');
2017-06-29 16:46:19 -07:00
const template = require('./templates.js');
2017-06-29 20:11:09 -07:00
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
2017-06-20 19:18:21 +02:00
// `supportsColor.level` → `ansiStyles.color[name]` mapping
const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
2017-06-20 21:17:16 +02:00
2017-06-20 19:18:21 +02:00
// `color-convert` models to exclude from the Chalk API due to conflicts and such
2017-06-20 21:17:16 +02:00
const skipModels = new Set(['gray']);
2017-06-20 10:02:09 -07:00
2017-06-29 16:46:19 -07:00
const styles = Object.create(null);
2018-12-26 02:37:03 +01:00
function applyOptions(object, options = {}) {
2018-09-18 15:33:20 +07:00
if (options.level > 3 || options.level < 0) {
throw new Error('The `level` option should be an integer from 0 to 3');
}
2017-06-29 16:46:19 -07:00
2017-06-20 19:18:21 +02:00
// Detect level if not set manually
2018-12-26 02:37:03 +01:00
const colorLevel = stdoutColor ? stdoutColor.level : 0;
object.level = options.level === undefined ? colorLevel : options.level;
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
2017-06-29 16:46:19 -07:00
}
class ChalkClass {
constructor(options) {
return chalkFactory(options);
}
}
2017-06-29 16:46:19 -07:00
function chalkFactory(options) {
const chalk = {};
applyOptions(chalk, options);
2017-06-29 16:46:19 -07:00
chalk.template = (...args) => chalkTag(chalk.template, ...args);
2017-06-29 16:46:19 -07:00
Object.setPrototypeOf(chalk, Chalk.prototype);
Object.setPrototypeOf(chalk.template, chalk);
2017-06-29 16:46:19 -07:00
chalk.template.constructor = () => {
throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
};
chalk.template.Instance = ChalkClass;
return chalk.template;
}
2017-06-29 16:46:19 -07:00
function Chalk(options) {
return chalkFactory(options);
}
2013-08-29 16:14:18 +02:00
2017-06-20 19:18:21 +02:00
// Use bright blue on Windows as the normal blue color is illegible
if (isSimpleWindowsTerm) {
2017-06-20 19:18:21 +02:00
ansiStyles.blue.open = '\u001B[94m';
}
2017-06-20 19:18:21 +02:00
for (const key of Object.keys(ansiStyles)) {
2016-01-09 16:20:22 +01:00
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
2016-01-09 16:20:22 +01:00
styles[key] = {
2017-06-20 19:18:21 +02:00
get() {
const codes = ansiStyles[key];
2018-12-26 02:37:03 +01:00
return build.call(this, [...(this._styles || []), codes], this._empty, key);
2017-06-20 10:02:09 -07:00
}
};
2017-06-20 19:18:21 +02:00
}
2017-06-20 10:02:09 -07:00
styles.visible = {
get() {
return build.call(this, this._styles || [], true, 'visible');
}
};
2017-06-20 10:02:09 -07:00
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
2017-06-20 19:18:21 +02:00
for (const model of Object.keys(ansiStyles.color.ansi)) {
2017-06-20 21:17:16 +02:00
if (skipModels.has(model)) {
2017-06-20 19:18:21 +02:00
continue;
2017-06-20 10:02:09 -07:00
}
styles[model] = {
2017-06-20 19:18:21 +02:00
get() {
2018-09-18 14:05:08 +07:00
const {level} = this;
return function (...args) {
const open = ansiStyles.color[levelMapping[level]][model](...args);
2017-06-20 19:18:21 +02:00
const codes = {
open,
close: ansiStyles.color.close,
closeRe: ansiStyles.color.closeRe
};
2018-12-26 02:37:03 +01:00
return build.call(this, [...(this._styles || []), codes], this._empty, model);
2017-06-20 10:02:09 -07:00
};
}
};
2017-06-20 19:18:21 +02:00
}
2017-06-20 10:02:09 -07:00
ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
2017-06-20 19:18:21 +02:00
for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
2017-06-20 21:17:16 +02:00
if (skipModels.has(model)) {
2017-06-20 19:18:21 +02:00
continue;
2017-06-20 10:02:09 -07:00
}
2017-06-20 21:17:16 +02:00
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
2017-06-20 10:02:09 -07:00
styles[bgModel] = {
2017-06-20 19:18:21 +02:00
get() {
2018-09-18 14:05:08 +07:00
const {level} = this;
return function (...args) {
const open = ansiStyles.bgColor[levelMapping[level]][model](...args);
2017-06-20 19:18:21 +02:00
const codes = {
open,
close: ansiStyles.bgColor.close,
closeRe: ansiStyles.bgColor.closeRe
};
2018-12-26 02:37:03 +01:00
return build.call(this, [...(this._styles || []), codes], this._empty, model);
2017-06-20 10:02:09 -07:00
};
2016-01-09 16:20:22 +01:00
}
};
2017-06-20 19:18:21 +02:00
}
2013-08-03 02:16:26 +02:00
2017-06-20 21:17:16 +02:00
const proto = Object.defineProperties(() => {}, styles);
function build(_styles, _empty, key) {
2018-09-18 14:05:08 +07:00
const builder = (...args) => applyStyle.call(builder, ...args);
2015-06-02 17:12:01 +02:00
builder._styles = _styles;
builder._empty = _empty;
2017-06-20 19:18:21 +02:00
const self = this;
2017-06-29 13:34:00 -07:00
2017-06-20 10:02:09 -07:00
Object.defineProperty(builder, 'level', {
enumerable: true,
2017-06-20 19:18:21 +02:00
get() {
2017-06-20 10:02:09 -07:00
return self.level;
},
2017-06-20 19:18:21 +02:00
set(level) {
2017-06-20 10:02:09 -07:00
self.level = level;
}
});
2017-06-29 13:34:00 -07:00
Object.defineProperty(builder, 'enabled', {
enumerable: true,
get() {
return self.enabled;
},
set(enabled) {
self.enabled = enabled;
}
});
2017-06-20 19:18:21 +02:00
// See below for fix regarding invisible grey/dim combination on Windows
2017-06-20 10:02:09 -07:00
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
2017-06-20 19:18:21 +02:00
// `__proto__` is used because we must return a function, but there is
2017-06-30 12:42:24 +02:00
// no way to create a function with a different prototype
2017-06-20 19:18:21 +02:00
builder.__proto__ = proto; // eslint-disable-line no-proto
2015-06-02 17:12:01 +02:00
return builder;
}
2018-09-18 14:05:08 +07:00
function applyStyle(...args) {
2018-12-26 02:37:03 +01:00
let string = args.join(' ');
2018-12-26 02:37:03 +01:00
if (!this.enabled || this.level <= 0 || !string) {
return this._empty ? '' : string;
}
// 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.
2017-06-20 19:18:21 +02:00
const originalDim = ansiStyles.dim.open;
2017-06-20 10:02:09 -07:00
if (isSimpleWindowsTerm && this.hasGrey) {
ansiStyles.dim.open = '';
}
2017-06-20 21:17:16 +02:00
for (const code of this._styles.slice().reverse()) {
// Replace any instances already present with a re-opening code
// otherwise only the part of the string until said closing code
// will be colored, and the rest will simply be 'plain'.
2018-12-26 02:37:03 +01:00
string = code.open + string.replace(code.closeRe, code.open) + code.close;
2016-01-17 12:38:10 +01:00
// Close the styling before a linebreak and reopen
2016-06-14 18:20:49 +02:00
// after next line to fix a bleed issue on macOS
2016-01-17 12:38:10 +01:00
// https://github.com/chalk/chalk/pull/92
2018-12-26 02:37:03 +01:00
string = string.replace(/\r?\n/g, `${code.close}$&${code.open}`);
}
2017-06-20 21:17:16 +02:00
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
ansiStyles.dim.open = originalDim;
2018-12-26 02:37:03 +01:00
return string;
}
2018-09-18 14:05:08 +07:00
function chalkTag(chalk, ...strings) {
const [firstString] = strings;
2018-09-18 14:05:08 +07:00
if (!Array.isArray(firstString)) {
// If chalk() was called by itself or with a string,
// return the string itself as a string.
2018-09-18 14:05:08 +07:00
return strings.join(' ');
2017-06-29 16:46:19 -07:00
}
2018-09-18 14:05:08 +07:00
const args = strings.slice(1);
const parts = [firstString.raw[0]];
2017-06-29 16:46:19 -07:00
2018-09-18 14:05:08 +07:00
for (let i = 1; i < firstString.length; i++) {
2018-12-26 02:37:03 +01:00
parts.push(
String(args[i - 1]).replace(/[{}\\]/g, '\\$&'),
String(firstString.raw[i])
);
2017-06-29 16:46:19 -07:00
}
return template(chalk, parts.join(''));
}
2017-06-20 21:17:16 +02:00
Object.defineProperties(Chalk.prototype, styles);
2013-12-13 19:36:43 +01:00
2017-06-29 16:46:19 -07:00
module.exports = Chalk(); // eslint-disable-line new-cap
module.exports.supportsColor = stdoutColor;
2017-10-17 22:10:43 -05:00
module.exports.default = module.exports; // For TypeScript