The illegible blue color has been fixed in Windows 10 build 16257: https://blogs.msdn.microsoft.com/commandline/2017/08/02/updating-the-windows-console-colors/ The workaround causes all kinds of problems, so better to remove it than adding conditionals for older Windows versions.
208 lines
5.8 KiB
JavaScript
208 lines
5.8 KiB
JavaScript
'use strict';
|
|
const escapeStringRegexp = require('escape-string-regexp');
|
|
const ansiStyles = require('ansi-styles');
|
|
const {stdout: stdoutColor} = 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(object, options = {}) {
|
|
if (options.level > 3 || options.level < 0) {
|
|
throw new Error('The `level` option should be an integer from 0 to 3');
|
|
}
|
|
|
|
// Detect level if not set manually
|
|
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
|
object.level = options.level === undefined ? colorLevel : options.level;
|
|
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
|
|
}
|
|
|
|
function Chalk(options) {
|
|
// 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 = (...args) => chalkTag(chalk.template, ...args);
|
|
|
|
Object.setPrototypeOf(chalk, Chalk.prototype);
|
|
Object.setPrototypeOf(chalk.template, chalk);
|
|
|
|
chalk.template.constructor = Chalk;
|
|
|
|
return chalk.template;
|
|
}
|
|
|
|
applyOptions(this, options);
|
|
}
|
|
|
|
for (const key of Object.keys(ansiStyles)) {
|
|
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
|
|
|
styles[key] = {
|
|
get() {
|
|
const codes = ansiStyles[key];
|
|
return build.call(this, [...(this._styles || []), codes], this._empty, key);
|
|
}
|
|
};
|
|
}
|
|
|
|
styles.visible = {
|
|
get() {
|
|
return build.call(this, this._styles || [], true, 'visible');
|
|
}
|
|
};
|
|
|
|
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
|
|
for (const model of Object.keys(ansiStyles.color.ansi)) {
|
|
if (skipModels.has(model)) {
|
|
continue;
|
|
}
|
|
|
|
styles[model] = {
|
|
get() {
|
|
const {level} = this;
|
|
return function (...args) {
|
|
const open = ansiStyles.color[levelMapping[level]][model](...args);
|
|
const codes = {
|
|
open,
|
|
close: ansiStyles.color.close,
|
|
closeRe: ansiStyles.color.closeRe
|
|
};
|
|
return build.call(this, [...(this._styles || []), codes], this._empty, model);
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
|
|
for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|
if (skipModels.has(model)) {
|
|
continue;
|
|
}
|
|
|
|
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
|
styles[bgModel] = {
|
|
get() {
|
|
const {level} = this;
|
|
return function (...args) {
|
|
const open = ansiStyles.bgColor[levelMapping[level]][model](...args);
|
|
const codes = {
|
|
open,
|
|
close: ansiStyles.bgColor.close,
|
|
closeRe: ansiStyles.bgColor.closeRe
|
|
};
|
|
return build.call(this, [...(this._styles || []), codes], this._empty, model);
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
const proto = Object.defineProperties(() => {}, styles);
|
|
|
|
function build(_styles, _empty, key) {
|
|
const builder = (...args) => applyStyle.call(builder, ...args);
|
|
builder._styles = _styles;
|
|
builder._empty = _empty;
|
|
|
|
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
|
|
builder.__proto__ = proto; // eslint-disable-line no-proto
|
|
|
|
return builder;
|
|
}
|
|
|
|
function applyStyle(...args) {
|
|
let string = args.join(' ');
|
|
|
|
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.
|
|
const originalDim = ansiStyles.dim.open;
|
|
if (isSimpleWindowsTerm && this.hasGrey) {
|
|
ansiStyles.dim.open = '';
|
|
}
|
|
|
|
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'.
|
|
string = code.open + string.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
|
|
string = string.replace(/\r?\n/g, `${code.close}$&${code.open}`);
|
|
}
|
|
|
|
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
|
|
ansiStyles.dim.open = originalDim;
|
|
|
|
return string;
|
|
}
|
|
|
|
function chalkTag(chalk, ...strings) {
|
|
const [firstString] = strings;
|
|
|
|
if (!Array.isArray(firstString)) {
|
|
// If chalk() was called by itself or with a string,
|
|
// return the string itself as a string.
|
|
return strings.join(' ');
|
|
}
|
|
|
|
const args = strings.slice(1);
|
|
const parts = [firstString.raw[0]];
|
|
|
|
for (let i = 1; i < firstString.length; i++) {
|
|
parts.push(
|
|
String(args[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
|
String(firstString.raw[i])
|
|
);
|
|
}
|
|
|
|
return template(chalk, parts.join(''));
|
|
}
|
|
|
|
Object.defineProperties(Chalk.prototype, styles);
|
|
|
|
module.exports = Chalk(); // eslint-disable-line new-cap
|
|
module.exports.supportsColor = stdoutColor;
|
|
module.exports.default = module.exports; // For TypeScript
|