react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / chalk / index.js
81152 views'use strict';1var escapeStringRegexp = require('escape-string-regexp');2var ansiStyles = require('ansi-styles');3var stripAnsi = require('strip-ansi');4var hasAnsi = require('has-ansi');5var supportsColor = require('supports-color');6var defineProps = Object.defineProperties;78function Chalk(options) {9// detect mode if not set manually10this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;11}1213// use bright blue on Windows as the normal blue color is illegible14if (process.platform === 'win32') {15ansiStyles.blue.open = '\u001b[94m';16}1718function build(_styles) {19var builder = function builder() {20return applyStyle.apply(builder, arguments);21};22builder._styles = _styles;23builder.enabled = this.enabled;24// __proto__ is used because we must return a function, but there is25// no way to create a function with a different prototype.26builder.__proto__ = proto;27return builder;28}2930var styles = (function () {31var ret = {};3233Object.keys(ansiStyles).forEach(function (key) {34ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');3536ret[key] = {37get: function () {38return build.call(this, this._styles.concat(key));39}40};41});4243return ret;44})();4546var proto = defineProps(function chalk() {}, styles);4748function applyStyle() {49// support varags, but simply cast to string in case there's only one arg50var args = arguments;51var argsLen = args.length;52var str = argsLen !== 0 && String(arguments[0]);53if (argsLen > 1) {54// don't slice `arguments`, it prevents v8 optimizations55for (var a = 1; a < argsLen; a++) {56str += ' ' + args[a];57}58}5960if (!this.enabled || !str) {61return str;62}6364/*jshint validthis: true */65var nestedStyles = this._styles;6667var i = nestedStyles.length;68while (i--) {69var code = ansiStyles[nestedStyles[i]];70// Replace any instances already present with a re-opening code71// otherwise only the part of the string until said closing code72// will be colored, and the rest will simply be 'plain'.73str = code.open + str.replace(code.closeRe, code.open) + code.close;74}7576return str;77}7879function init() {80var ret = {};8182Object.keys(styles).forEach(function (name) {83ret[name] = {84get: function () {85return build.call(this, [name]);86}87};88});8990return ret;91}9293defineProps(Chalk.prototype, init());9495module.exports = new Chalk();96module.exports.styles = ansiStyles;97module.exports.hasColor = hasAnsi;98module.exports.stripColor = stripAnsi;99module.exports.supportsColor = supportsColor;100101102