6 - Copyright (c) Marak Squires
8 Additional functionality
9 - Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 module['exports'] = colors;
36 var util = require('util');
37 var ansiStyles = colors.styles = require('./styles');
38 var defineProps = Object.defineProperties;
39 var newLineRegex = new RegExp(/[\r\n]+/g);
41 colors.supportsColor = require('./system/supports-colors').supportsColor;
43 if (typeof colors.enabled === 'undefined') {
44 colors.enabled = colors.supportsColor() !== false;
47 colors.enable = function() {
48 colors.enabled = true;
51 colors.disable = function() {
52 colors.enabled = false;
55 colors.stripColors = colors.strip = function(str) {
56 return ('' + str).replace(/\x1B\[\d+m/g, '');
59 // eslint-disable-next-line no-unused-vars
60 var stylize = colors.stylize = function stylize(str, style) {
61 if (!colors.enabled) {
65 var styleMap = ansiStyles[style];
67 // Stylize should work for non-ANSI styles, too
68 if(!styleMap && style in colors){
69 // Style maps like trap operate as functions on strings;
70 // they don't have properties like open or close.
71 return colors[style](str);
74 return styleMap.open + str + styleMap.close;
77 var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
78 var escapeStringRegexp = function(str) {
79 if (typeof str !== 'string') {
80 throw new TypeError('Expected a string');
82 return str.replace(matchOperatorsRe, '\\$&');
85 function build(_styles) {
86 var builder = function builder() {
87 return applyStyle.apply(builder, arguments);
89 builder._styles = _styles;
90 // __proto__ is used because we must return a function, but there is
91 // no way to create a function with a different prototype.
92 builder.__proto__ = proto;
96 var styles = (function() {
98 ansiStyles.grey = ansiStyles.gray;
99 Object.keys(ansiStyles).forEach(function(key) {
100 ansiStyles[key].closeRe =
101 new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
104 return build(this._styles.concat(key));
111 var proto = defineProps(function colors() {}, styles);
113 function applyStyle() {
114 var args = Array.prototype.slice.call(arguments);
116 var str = args.map(function(arg) {
117 // Use weak equality check so we can colorize null/undefined in safe mode
118 if (arg != null && arg.constructor === String) {
121 return util.inspect(arg);
125 if (!colors.enabled || !str) {
129 var newLinesPresent = str.indexOf('\n') != -1;
131 var nestedStyles = this._styles;
133 var i = nestedStyles.length;
135 var code = ansiStyles[nestedStyles[i]];
136 str = code.open + str.replace(code.closeRe, code.open) + code.close;
137 if (newLinesPresent) {
138 str = str.replace(newLineRegex, function(match) {
139 return code.close + match + code.open;
147 colors.setTheme = function(theme) {
148 if (typeof theme === 'string') {
149 console.log('colors.setTheme now only accepts an object, not a string. ' +
150 'If you are trying to set a theme from a file, it is now your (the ' +
151 'caller\'s) responsibility to require the file. The old syntax ' +
152 'looked like colors.setTheme(__dirname + ' +
153 '\'/../themes/generic-logging.js\'); The new syntax looks like '+
154 'colors.setTheme(require(__dirname + ' +
155 '\'/../themes/generic-logging.js\'));');
158 for (var style in theme) {
160 colors[style] = function(str) {
161 if (typeof theme[style] === 'object') {
163 for (var i in theme[style]) {
164 out = colors[theme[style][i]](out);
168 return colors[theme[style]](str);
176 Object.keys(styles).forEach(function(name) {
179 return build([name]);
186 var sequencer = function sequencer(map, str) {
187 var exploded = str.split('');
188 exploded = exploded.map(map);
189 return exploded.join('');
192 // custom formatter methods
193 colors.trap = require('./custom/trap');
194 colors.zalgo = require('./custom/zalgo');
198 colors.maps.america = require('./maps/america')(colors);
199 colors.maps.zebra = require('./maps/zebra')(colors);
200 colors.maps.rainbow = require('./maps/rainbow')(colors);
201 colors.maps.random = require('./maps/random')(colors);
203 for (var map in colors.maps) {
205 colors[map] = function(str) {
206 return sequencer(colors.maps[map], str);
211 defineProps(colors, init());