chalk/index.js

100 lines
2.9 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');
2013-08-03 02:16:26 +02:00
var defineProps = Object.defineProperties;
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
function Chalk(options) {
// detect mode if not set manually
2015-02-17 15:24:13 +07:00
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
}
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 () {
return build.call(this, this._styles ? this._styles.concat(key) : [key]);
}
};
});
2013-08-03 02:16:26 +02:00
var proto = defineProps(function chalk() {}, styles);
2015-06-02 17:12:01 +02:00
function build(_styles) {
var builder = function () {
2015-06-02 17:12:01 +02:00
return applyStyle.apply(builder, arguments);
};
builder._styles = _styles;
builder.enabled = this.enabled;
// __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];
}
}
if (!this.enabled || !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;
if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
ansiStyles.dim.open = '';
}
while (i--) {
var code = ansiStyles[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;
// Close the coloring before a line break and reopening after next line
// To fix a bleed issue on macs
// see https://github.com/chalk/chalk/pull/92
str = str.replace(/\n/gm, code.close + '\n' + 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;