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 ansiStyles = colors.styles = require('./styles');
37 var defineProps = Object.defineProperties;
39 colors.supportsColor = require('./system/supports-colors');
41 if (typeof colors.enabled === "undefined") {
42 colors.enabled = colors.supportsColor;
45 colors.stripColors = colors.strip = function(str){
46 return ("" + str).replace(/\x1B\[\d+m/g, '');
50 var stylize = colors.stylize = function stylize (str, style) {
51 if (!colors.enabled) {
55 return ansiStyles[style].open + str + ansiStyles[style].close;
58 var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
59 var escapeStringRegexp = function (str) {
60 if (typeof str !== 'string') {
61 throw new TypeError('Expected a string');
63 return str.replace(matchOperatorsRe, '\\$&');
66 function build(_styles) {
67 var builder = function builder() {
68 return applyStyle.apply(builder, arguments);
70 builder._styles = _styles;
71 // __proto__ is used because we must return a function, but there is
72 // no way to create a function with a different prototype.
73 builder.__proto__ = proto;
77 var styles = (function () {
79 ansiStyles.grey = ansiStyles.gray;
80 Object.keys(ansiStyles).forEach(function (key) {
81 ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
84 return build(this._styles.concat(key));
91 var proto = defineProps(function colors() {}, styles);
93 function applyStyle() {
95 var argsLen = args.length;
96 var str = argsLen !== 0 && String(arguments[0]);
98 for (var a = 1; a < argsLen; a++) {
103 if (!colors.enabled || !str) {
107 var nestedStyles = this._styles;
109 var i = nestedStyles.length;
111 var code = ansiStyles[nestedStyles[i]];
112 str = code.open + str.replace(code.closeRe, code.open) + code.close;
118 function applyTheme (theme) {
119 for (var style in theme) {
121 colors[style] = function(str){
122 if (typeof theme[style] === 'object'){
124 for (var i in theme[style]){
125 out = colors[theme[style][i]](out);
129 return colors[theme[style]](str);
135 colors.setTheme = function (theme) {
136 if (typeof theme === 'string') {
138 colors.themes[theme] = require(theme);
139 applyTheme(colors.themes[theme]);
140 return colors.themes[theme];
152 Object.keys(styles).forEach(function (name) {
155 return build([name]);
162 var sequencer = function sequencer (map, str) {
163 var exploded = str.split(""), i = 0;
164 exploded = exploded.map(map);
165 return exploded.join("");
168 // custom formatter methods
169 colors.trap = require('./custom/trap');
170 colors.zalgo = require('./custom/zalgo');
174 colors.maps.america = require('./maps/america');
175 colors.maps.zebra = require('./maps/zebra');
176 colors.maps.rainbow = require('./maps/rainbow');
177 colors.maps.random = require('./maps/random')
179 for (var map in colors.maps) {
181 colors[map] = function (str) {
182 return sequencer(colors.maps[map], str);
187 defineProps(colors, init());