Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80657 views
1
/*
2
colors.js
3
4
Copyright (c) 2010
5
6
Marak Squires
7
Alexis Sellier (cloudhead)
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
27
*/
28
29
var isHeadless = false;
30
31
if (typeof module !== 'undefined') {
32
isHeadless = true;
33
}
34
35
if (!isHeadless) {
36
var exports = {};
37
var module = {};
38
var colors = exports;
39
exports.mode = "browser";
40
} else {
41
exports.mode = "console";
42
}
43
44
//
45
// Prototypes the string object to have additional method calls that add terminal colors
46
//
47
var addProperty = function (color, func) {
48
exports[color] = function (str) {
49
return func.apply(str);
50
};
51
String.prototype.__defineGetter__(color, func);
52
};
53
54
55
56
function stylize(str, style) {
57
58
var styles;
59
60
if (exports.mode === 'console') {
61
styles = {
62
//styles
63
'bold' : ['\033[1m', '\033[22m'],
64
'italic' : ['\033[3m', '\033[23m'],
65
'underline' : ['\033[4m', '\033[24m'],
66
'inverse' : ['\033[7m', '\033[27m'],
67
//grayscale
68
'white' : ['\033[37m', '\033[39m'],
69
'grey' : ['\033[90m', '\033[39m'],
70
'black' : ['\033[30m', '\033[39m'],
71
//colors
72
'blue' : ['\033[34m', '\033[39m'],
73
'cyan' : ['\033[36m', '\033[39m'],
74
'green' : ['\033[32m', '\033[39m'],
75
'magenta' : ['\033[35m', '\033[39m'],
76
'red' : ['\033[31m', '\033[39m'],
77
'yellow' : ['\033[33m', '\033[39m']
78
};
79
} else if (exports.mode === 'browser') {
80
styles = {
81
//styles
82
'bold' : ['<b>', '</b>'],
83
'italic' : ['<i>', '</i>'],
84
'underline' : ['<u>', '</u>'],
85
'inverse' : ['<span style="background-color:black;color:white;">', '</span>'],
86
//grayscale
87
'white' : ['<span style="color:white;">', '</span>'],
88
'grey' : ['<span style="color:grey;">', '</span>'],
89
'black' : ['<span style="color:black;">', '</span>'],
90
//colors
91
'blue' : ['<span style="color:blue;">', '</span>'],
92
'cyan' : ['<span style="color:cyan;">', '</span>'],
93
'green' : ['<span style="color:green;">', '</span>'],
94
'magenta' : ['<span style="color:magenta;">', '</span>'],
95
'red' : ['<span style="color:red;">', '</span>'],
96
'yellow' : ['<span style="color:yellow;">', '</span>']
97
};
98
} else if (exports.mode === 'none') {
99
return str;
100
} else {
101
console.log('unsupported mode, try "browser", "console" or "none"');
102
}
103
return styles[style][0] + str + styles[style][1];
104
}
105
106
function applyTheme(theme) {
107
108
//
109
// Remark: This is a list of methods that exist
110
// on String that you should not overwrite.
111
//
112
var stringPrototypeBlacklist = [
113
'__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor',
114
'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt',
115
'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring',
116
'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
117
];
118
119
Object.keys(theme).forEach(function (prop) {
120
if (stringPrototypeBlacklist.indexOf(prop) !== -1) {
121
console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name');
122
} else {
123
addProperty(prop, function () {
124
return exports[theme[prop]](this);
125
});
126
}
127
});
128
}
129
130
131
//
132
// Iterate through all default styles and colors
133
//
134
var x = ['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
135
x.forEach(function (style) {
136
137
// __defineGetter__ at the least works in more browsers
138
// http://robertnyman.com/javascript/javascript-getters-setters.html
139
// Object.defineProperty only works in Chrome
140
addProperty(style, function () {
141
return stylize(this, style);
142
});
143
});
144
145
function sequencer(map) {
146
return function () {
147
if (!isHeadless) {
148
return this.replace(/( )/, '$1');
149
}
150
var exploded = this.split(""), i = 0;
151
exploded = exploded.map(map);
152
return exploded.join("");
153
};
154
}
155
156
var rainbowMap = (function () {
157
var rainbowColors = ['red', 'yellow', 'green', 'blue', 'magenta']; //RoY G BiV
158
return function (letter, i, exploded) {
159
if (letter === " ") {
160
return letter;
161
} else {
162
return stylize(letter, rainbowColors[i++ % rainbowColors.length]);
163
}
164
};
165
})();
166
167
exports.themes = {};
168
169
exports.addSequencer = function (name, map) {
170
addProperty(name, sequencer(map));
171
};
172
173
exports.addSequencer('rainbow', rainbowMap);
174
exports.addSequencer('zebra', function (letter, i, exploded) {
175
return i % 2 === 0 ? letter : letter.inverse;
176
});
177
178
exports.setTheme = function (theme) {
179
if (typeof theme === 'string') {
180
try {
181
exports.themes[theme] = require(theme);
182
applyTheme(exports.themes[theme]);
183
return exports.themes[theme];
184
} catch (err) {
185
console.log(err);
186
return err;
187
}
188
} else {
189
applyTheme(theme);
190
}
191
};
192
193
194
addProperty('stripColors', function () {
195
return ("" + this).replace(/\u001b\[\d+m/g, '');
196
});
197
198
// please no
199
function zalgo(text, options) {
200
var soul = {
201
"up" : [
202
'̍', '̎', '̄', '̅',
203
'̿', '̑', '̆', '̐',
204
'͒', '͗', '͑', '̇',
205
'̈', '̊', '͂', '̓',
206
'̈', '͊', '͋', '͌',
207
'̃', '̂', '̌', '͐',
208
'̀', '́', '̋', '̏',
209
'̒', '̓', '̔', '̽',
210
'̉', 'ͣ', 'ͤ', 'ͥ',
211
'ͦ', 'ͧ', 'ͨ', 'ͩ',
212
'ͪ', 'ͫ', 'ͬ', 'ͭ',
213
'ͮ', 'ͯ', '̾', '͛',
214
'͆', '̚'
215
],
216
"down" : [
217
'̖', '̗', '̘', '̙',
218
'̜', '̝', '̞', '̟',
219
'̠', '̤', '̥', '̦',
220
'̩', '̪', '̫', '̬',
221
'̭', '̮', '̯', '̰',
222
'̱', '̲', '̳', '̹',
223
'̺', '̻', '̼', 'ͅ',
224
'͇', '͈', '͉', '͍',
225
'͎', '͓', '͔', '͕',
226
'͖', '͙', '͚', '̣'
227
],
228
"mid" : [
229
'̕', '̛', '̀', '́',
230
'͘', '̡', '̢', '̧',
231
'̨', '̴', '̵', '̶',
232
'͜', '͝', '͞',
233
'͟', '͠', '͢', '̸',
234
'̷', '͡', ' ҉'
235
]
236
},
237
all = [].concat(soul.up, soul.down, soul.mid),
238
zalgo = {};
239
240
function randomNumber(range) {
241
var r = Math.floor(Math.random() * range);
242
return r;
243
}
244
245
function is_char(character) {
246
var bool = false;
247
all.filter(function (i) {
248
bool = (i === character);
249
});
250
return bool;
251
}
252
253
function heComes(text, options) {
254
var result = '', counts, l;
255
options = options || {};
256
options["up"] = options["up"] || true;
257
options["mid"] = options["mid"] || true;
258
options["down"] = options["down"] || true;
259
options["size"] = options["size"] || "maxi";
260
text = text.split('');
261
for (l in text) {
262
if (is_char(l)) {
263
continue;
264
}
265
result = result + text[l];
266
counts = {"up" : 0, "down" : 0, "mid" : 0};
267
switch (options.size) {
268
case 'mini':
269
counts.up = randomNumber(8);
270
counts.min = randomNumber(2);
271
counts.down = randomNumber(8);
272
break;
273
case 'maxi':
274
counts.up = randomNumber(16) + 3;
275
counts.min = randomNumber(4) + 1;
276
counts.down = randomNumber(64) + 3;
277
break;
278
default:
279
counts.up = randomNumber(8) + 1;
280
counts.mid = randomNumber(6) / 2;
281
counts.down = randomNumber(8) + 1;
282
break;
283
}
284
285
var arr = ["up", "mid", "down"];
286
for (var d in arr) {
287
var index = arr[d];
288
for (var i = 0 ; i <= counts[index]; i++) {
289
if (options[index]) {
290
result = result + soul[index][randomNumber(soul[index].length)];
291
}
292
}
293
}
294
}
295
return result;
296
}
297
return heComes(text);
298
}
299
300
301
// don't summon zalgo
302
addProperty('zalgo', function () {
303
return zalgo(this);
304
});
305