OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 var escapeStringRegexp = require('escape-string-regexp'); |
| 3 var ansiStyles = require('ansi-styles'); |
| 4 var stripAnsi = require('strip-ansi'); |
| 5 var hasAnsi = require('has-ansi'); |
| 6 var supportsColor = require('supports-color'); |
| 7 var defineProps = Object.defineProperties; |
| 8 var chalk = module.exports; |
| 9 |
| 10 function build(_styles) { |
| 11 var builder = function builder() { |
| 12 return applyStyle.apply(builder, arguments); |
| 13 }; |
| 14 builder._styles = _styles; |
| 15 // __proto__ is used because we must return a function, but there is |
| 16 // no way to create a function with a different prototype. |
| 17 builder.__proto__ = proto; |
| 18 return builder; |
| 19 } |
| 20 |
| 21 var styles = (function () { |
| 22 var ret = {}; |
| 23 |
| 24 ansiStyles.grey = ansiStyles.gray; |
| 25 |
| 26 Object.keys(ansiStyles).forEach(function (key) { |
| 27 ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyl
es[key].close), 'g'); |
| 28 |
| 29 ret[key] = { |
| 30 get: function () { |
| 31 return build(this._styles.concat(key)); |
| 32 } |
| 33 }; |
| 34 }); |
| 35 |
| 36 return ret; |
| 37 })(); |
| 38 |
| 39 var proto = defineProps(function chalk() {}, styles); |
| 40 |
| 41 function applyStyle() { |
| 42 // support varags, but simply cast to string in case there's only one ar
g |
| 43 var args = arguments; |
| 44 var argsLen = args.length; |
| 45 var str = argsLen !== 0 && String(arguments[0]); |
| 46 if (argsLen > 1) { |
| 47 // don't slice `arguments`, it prevents v8 optimizations |
| 48 for (var a = 1; a < argsLen; a++) { |
| 49 str += ' ' + args[a]; |
| 50 } |
| 51 } |
| 52 |
| 53 if (!chalk.enabled || !str) { |
| 54 return str; |
| 55 } |
| 56 |
| 57 /*jshint validthis: true*/ |
| 58 var nestedStyles = this._styles; |
| 59 |
| 60 for (var i = 0; i < nestedStyles.length; i++) { |
| 61 var code = ansiStyles[nestedStyles[i]]; |
| 62 // Replace any instances already present with a re-opening code |
| 63 // otherwise only the part of the string until said closing code |
| 64 // will be colored, and the rest will simply be 'plain'. |
| 65 str = code.open + str.replace(code.closeRe, code.open) + code.cl
ose; |
| 66 } |
| 67 |
| 68 return str; |
| 69 } |
| 70 |
| 71 function init() { |
| 72 var ret = {}; |
| 73 |
| 74 Object.keys(styles).forEach(function (name) { |
| 75 ret[name] = { |
| 76 get: function () { |
| 77 return build([name]); |
| 78 } |
| 79 }; |
| 80 }); |
| 81 |
| 82 return ret; |
| 83 } |
| 84 |
| 85 defineProps(chalk, init()); |
| 86 |
| 87 chalk.styles = ansiStyles; |
| 88 chalk.hasColor = hasAnsi; |
| 89 chalk.stripColor = stripAnsi; |
| 90 chalk.supportsColor = supportsColor; |
| 91 |
| 92 // detect mode if not set manually |
| 93 if (chalk.enabled === undefined) { |
| 94 chalk.enabled = chalk.supportsColor; |
| 95 } |
OLD | NEW |