chalk/index.js

160 lines
4.7 KiB
JavaScript
Raw Normal View History

2013-08-03 02:16:26 +02:00
'use strict';
var escapeStringRegexp = require('escape-string-regexp');
2014-06-04 01:43:07 +02:00
var ansiStyles = require('ansi-styles');
2014-06-14 03:49:42 +02:00
var supportsColor = require('supports-color');
2016-06-16 14:36:05 +02:00
2013-08-03 02:16:26 +02:00
var defineProps = Object.defineProperties;
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
2017-06-20 10:02:09 -07:00
// supportsColor.level -> ansiStyles.color[name] mapping
var levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
// color-convert models to exclude from the Chalk API due to conflicts and such.
var skipModels = ['gray'];
function Chalk(options) {
2017-06-20 10:02:09 -07:00
// detect level if not set manually
this.level = !options || options.level === undefined ? supportsColor.level : options.level;
}
2013-08-29 16:14:18 +02:00
// use bright blue on Windows as the normal blue color is illegible
if (isSimpleWindowsTerm) {
ansiStyles.blue.open = '\u001b[94m';
}
2016-01-09 16:20:22 +01:00
var styles = {};
2013-08-03 02:16:26 +02:00
2016-01-09 16:20:22 +01:00
Object.keys(ansiStyles).forEach(function (key) {
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
2016-01-09 16:20:22 +01:00
styles[key] = {
get: function () {
2017-06-20 10:02:09 -07:00
var codes = ansiStyles[key];
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key);
}
};
});
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
Object.keys(ansiStyles.color.ansi).forEach(function (model) {
if (skipModels.indexOf(model) !== -1) {
return;
}
styles[model] = {
get: function () {
var level = this.level;
return function () {
var open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
var codes = {open: open, close: ansiStyles.color.close, closeRe: ansiStyles.color.closeRe};
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
};
}
};
});
ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
Object.keys(ansiStyles.bgColor.ansi).forEach(function (model) {
if (skipModels.indexOf(model) !== -1) {
return;
}
var bgModel = 'bg' + model.charAt(0).toUpperCase() + model.substring(1);
styles[bgModel] = {
get: function () {
var level = this.level;
return function () {
var open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
var codes = {open: open, close: ansiStyles.bgColor.close, closeRe: ansiStyles.bgColor.closeRe};
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
};
2016-01-09 16:20:22 +01:00
}
};
});
2013-08-03 02:16:26 +02:00
2016-10-14 16:32:39 +07:00
// eslint-disable-next-line func-names
var proto = defineProps(function chalk() {}, styles);
2017-06-20 10:02:09 -07:00
function build(_styles, key) {
var builder = function () {
2015-06-02 17:12:01 +02:00
return applyStyle.apply(builder, arguments);
};
var self = this;
2015-06-02 17:12:01 +02:00
builder._styles = _styles;
2017-06-20 10:02:09 -07:00
Object.defineProperty(builder, 'level', {
enumerable: true,
get: function () {
2017-06-20 10:02:09 -07:00
return self.level;
},
2017-06-20 10:02:09 -07:00
set: function (level) {
self.level = level;
}
});
2017-06-20 10:02:09 -07:00
// see below for fix regarding invisible grey/dim combination on windows.
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
2015-06-02 17:12:01 +02:00
// __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 */
2015-06-02 17:12:01 +02:00
builder.__proto__ = proto;
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]);
2015-06-02 17:12:01 +02:00
if (argsLen > 1) {
// don't slice `arguments`, it prevents v8 optimizations
for (var a = 1; a < argsLen; a++) {
str += ' ' + args[a];
}
}
2017-06-20 10:02:09 -07:00
if (!this.level || !str) {
return str;
}
var nestedStyles = this._styles;
var i = nestedStyles.length;
2015-06-02 17:12:01 +02:00
// 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;
2017-06-20 10:02:09 -07:00
if (isSimpleWindowsTerm && this.hasGrey) {
ansiStyles.dim.open = '';
}
while (i--) {
2017-06-20 10:02:09 -07:00
var code = nestedStyles[i];
2015-06-02 17:12:01 +02:00
// 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
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.
ansiStyles.dim.open = originalDim;
return str;
}
2016-01-09 16:20:22 +01:00
defineProps(Chalk.prototype, styles);
2013-12-13 19:36:43 +01:00
module.exports = new Chalk();
module.exports.styles = ansiStyles;
module.exports.supportsColor = supportsColor;