Blame view

node_modules/chalk/index.js 3.08 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  'use strict';
  var escapeStringRegexp = require('escape-string-regexp');
  var ansiStyles = require('ansi-styles');
  var stripAnsi = require('strip-ansi');
  var hasAnsi = require('has-ansi');
  var supportsColor = require('supports-color');
  var defineProps = Object.defineProperties;
  var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
  
  function Chalk(options) {
  	// detect mode if not set manually
  	this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
  }
  
  // use bright blue on Windows as the normal blue color is illegible
  if (isSimpleWindowsTerm) {
  	ansiStyles.blue.open = '\u001b[94m';
  }
  
  var styles = (function () {
  	var ret = {};
  
  	Object.keys(ansiStyles).forEach(function (key) {
  		ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
  
  		ret[key] = {
  			get: function () {
  				return build.call(this, this._styles.concat(key));
  			}
  		};
  	});
  
  	return ret;
  })();
  
  var proto = defineProps(function chalk() {}, styles);
  
  function build(_styles) {
  	var builder = function () {
  		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 */
  	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]);
  
  	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;
  
  	// 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]];
  
  		// 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;
  	}
  
  	// Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
  	ansiStyles.dim.open = originalDim;
  
  	return str;
  }
  
  function init() {
  	var ret = {};
  
  	Object.keys(styles).forEach(function (name) {
  		ret[name] = {
  			get: function () {
  				return build.call(this, [name]);
  			}
  		};
  	});
  
  	return ret;
  }
  
  defineProps(Chalk.prototype, init());
  
  module.exports = new Chalk();
  module.exports.styles = ansiStyles;
  module.exports.hasColor = hasAnsi;
  module.exports.stripColor = stripAnsi;
  module.exports.supportsColor = supportsColor;