chalk/index.js

215 lines
5.9 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');
const supportsColor = require('supports-color');
2016-06-16 14:36:05 +02:00
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);
function applyOptions(obj, options) {
options = options || {};
2017-06-20 19:18:21 +02:00
// Detect level if not set manually
2017-06-29 16:46:19 -07:00
obj.level = options.level === undefined ? supportsColor.level : options.level;
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
}
function Chalk(options) {
2017-06-30 12:42:24 +02:00
// We check for this.template here since calling `chalk.constructor()`
// by itself will have a `this` of a previously constructed chalk object
2017-06-29 16:46:19 -07:00
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);
}
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];
2017-06-20 10:02:09 -07:00
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key);
}
};
2017-06-20 19:18:21 +02:00
}
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() {
const level = this.level;
2017-06-20 10:02:09 -07:00
return function () {
2017-06-20 19:18:21 +02:00
const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
const codes = {
open,
close: ansiStyles.color.close,
closeRe: ansiStyles.color.closeRe
};
2017-06-20 10:02:09 -07:00
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
};
}
};
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() {
const level = this.level;
2017-06-20 10:02:09 -07:00
return function () {
2017-06-20 19:18:21 +02:00
const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
const codes = {
open,
close: ansiStyles.bgColor.close,
closeRe: ansiStyles.bgColor.closeRe
};
2017-06-20 10:02:09 -07:00
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
};
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);
2017-06-20 10:02:09 -07:00
function build(_styles, key) {
2017-06-20 19:18:21 +02:00
const builder = function () {
2015-06-02 17:12:01 +02:00
return applyStyle.apply(builder, arguments);
};
builder._styles = _styles;
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;
}
function applyStyle() {
2017-06-20 19:18:21 +02:00
// 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]);
2015-06-02 17:12:01 +02:00
if (argsLen > 1) {
2017-06-20 19:18:21 +02:00
// Don't slice `arguments`, it prevents V8 optimizations
for (let a = 1; a < argsLen; a++) {
str += ' ' + args[a];
}
}
2017-06-29 13:34:00 -07:00
if (!this.enabled || this.level <= 0 || !str) {
return str;
}
// 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'.
str = code.open + str.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
2017-06-20 19:18:21 +02:00
str = str.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;
return str;
}
2017-06-29 16:46:19 -07:00
function chalkTag(chalk, strings) {
const args = [].slice.call(arguments, 2);
if (!Array.isArray(strings)) {
return strings.toString();
}
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(''));
}
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 = supportsColor;