colors 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/MIT-LICENSE.txt CHANGED
@@ -1,4 +1,7 @@
1
- Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires
1
+ Copyright (c) 2010
2
+
3
+ Marak Squires
4
+ Alexis Sellier (cloudhead)
2
5
 
3
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
7
  of this software and associated documentation files (the "Software"), to deal
package/ReadMe.md CHANGED
@@ -1,16 +1,14 @@
1
- <h1>colors.js - get color and style in your node.js console like what</h1>
1
+ # colors.js - get color and style in your node.js console ( and browser ) like what
2
2
 
3
3
  <img src="http://i.imgur.com/goJdO.png" border = "0"/>
4
4
 
5
- var sys = require('sys');
6
- var colors = require('./colors');
7
5
 
8
- sys.puts('hello'.green); // outputs green text
9
- sys.puts('i like cake and pies'.underline.red) // outputs red underlined text
10
- sys.puts('inverse the color'.inverse); // inverses the color
11
- sys.puts('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
12
-
13
- <h2>colors and styles!</h2>
6
+ ## Installation
7
+
8
+ npm install colors
9
+
10
+ ## colors and styles!
11
+
14
12
  - bold
15
13
  - italic
16
14
  - underline
@@ -23,8 +21,57 @@
23
21
  - red
24
22
  - grey
25
23
  - blue
24
+ - rainbow
25
+ - zebra
26
+ - random
27
+
28
+ ## Usage
29
+
30
+ ``` js
31
+ var colors = require('./colors');
32
+
33
+ console.log('hello'.green); // outputs green text
34
+ console.log('i like cake and pies'.underline.red) // outputs red underlined text
35
+ console.log('inverse the color'.inverse); // inverses the color
36
+ console.log('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
37
+ ```
38
+
39
+ # Creating Custom themes
40
+
41
+ ```js
42
+
43
+ var colors = require('colors');
44
+
45
+ colors.setTheme({
46
+ silly: 'rainbow',
47
+ input: 'grey',
48
+ verbose: 'cyan',
49
+ prompt: 'grey',
50
+ info: 'green',
51
+ data: 'grey',
52
+ help: 'cyan',
53
+ warn: 'yellow',
54
+ debug: 'blue',
55
+ error: 'red'
56
+ });
57
+
58
+ // outputs red text
59
+ console.log("this is an error".error);
60
+
61
+ // outputs yellow text
62
+ console.log("this is a warning".warn);
63
+ ```
64
+
65
+
66
+ ### Contributors
26
67
 
68
+ Marak (Marak Squires)
69
+ Alexis Sellier (cloudhead)
70
+ mmalecki (Maciej Małecki)
71
+ nicoreed (Nico Reed)
72
+ morganrallen (Morgan Allen)
73
+ JustinCampbell (Justin Campbell)
74
+ ded (Dustin Diaz)
27
75
 
28
- ### Authors
29
76
 
30
- #### Alexis Sellier (cloudhead) , Marak Squires , Justin Campbell, Dustin Diaz (@ded)
77
+ #### , Marak Squires , Justin Campbell, Dustin Diaz (@ded)
package/colors.js CHANGED
@@ -1,7 +1,10 @@
1
1
  /*
2
2
  colors.js
3
3
 
4
- Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires
4
+ Copyright (c) 2010
5
+
6
+ Marak Squires
7
+ Alexis Sellier (cloudhead)
5
8
 
6
9
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
10
  of this software and associated documentation files (the "Software"), to deal
@@ -23,169 +26,328 @@ THE SOFTWARE.
23
26
 
24
27
  */
25
28
 
26
- // prototypes the string object to have additional method calls that add terminal colors
27
- var isHeadless = (typeof module !== 'undefined');
28
- ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
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
+
52
+ if (Object.defineProperty) {
53
+ Object.defineProperty(String.prototype, color, {
54
+ get : func,
55
+ configurable: true,
56
+ enumerable: false
57
+ });
58
+ } else {
59
+ String.prototype.__defineGetter__(color, func);
60
+ }
61
+ };
62
+
63
+
64
+
65
+ function stylize(str, style) {
66
+
67
+ var styles;
68
+
69
+ if (exports.mode === 'console') {
70
+ styles = {
71
+ //styles
72
+ 'bold' : ['\x1B[1m', '\x1B[22m'],
73
+ 'italic' : ['\x1B[3m', '\x1B[23m'],
74
+ 'underline' : ['\x1B[4m', '\x1B[24m'],
75
+ 'inverse' : ['\x1B[7m', '\x1B[27m'],
76
+ 'strikethrough' : ['\x1B[9m', '\x1B[29m'],
77
+ //text colors
78
+ //grayscale
79
+ 'white' : ['\x1B[37m', '\x1B[39m'],
80
+ 'grey' : ['\x1B[90m', '\x1B[39m'],
81
+ 'black' : ['\x1B[30m', '\x1B[39m'],
82
+ //colors
83
+ 'blue' : ['\x1B[34m', '\x1B[39m'],
84
+ 'cyan' : ['\x1B[36m', '\x1B[39m'],
85
+ 'green' : ['\x1B[32m', '\x1B[39m'],
86
+ 'magenta' : ['\x1B[35m', '\x1B[39m'],
87
+ 'red' : ['\x1B[31m', '\x1B[39m'],
88
+ 'yellow' : ['\x1B[33m', '\x1B[39m'],
89
+ //background colors
90
+ //grayscale
91
+ 'whiteBG' : ['\x1B[47m', '\x1B[49m'],
92
+ 'greyBG' : ['\x1B[49;5;8m', '\x1B[49m'],
93
+ 'blackBG' : ['\x1B[40m', '\x1B[49m'],
94
+ //colors
95
+ 'blueBG' : ['\x1B[44m', '\x1B[49m'],
96
+ 'cyanBG' : ['\x1B[46m', '\x1B[49m'],
97
+ 'greenBG' : ['\x1B[42m', '\x1B[49m'],
98
+ 'magentaBG' : ['\x1B[45m', '\x1B[49m'],
99
+ 'redBG' : ['\x1B[41m', '\x1B[49m'],
100
+ 'yellowBG' : ['\x1B[43m', '\x1B[49m']
101
+ };
102
+ } else if (exports.mode === 'browser') {
103
+ styles = {
104
+ //styles
105
+ 'bold' : ['<b>', '</b>'],
106
+ 'italic' : ['<i>', '</i>'],
107
+ 'underline' : ['<u>', '</u>'],
108
+ 'inverse' : ['<span style="background-color:black;color:white;">', '</span>'],
109
+ 'strikethrough' : ['<del>', '</del>'],
110
+ //text colors
111
+ //grayscale
112
+ 'white' : ['<span style="color:white;">', '</span>'],
113
+ 'grey' : ['<span style="color:gray;">', '</span>'],
114
+ 'black' : ['<span style="color:black;">', '</span>'],
115
+ //colors
116
+ 'blue' : ['<span style="color:blue;">', '</span>'],
117
+ 'cyan' : ['<span style="color:cyan;">', '</span>'],
118
+ 'green' : ['<span style="color:green;">', '</span>'],
119
+ 'magenta' : ['<span style="color:magenta;">', '</span>'],
120
+ 'red' : ['<span style="color:red;">', '</span>'],
121
+ 'yellow' : ['<span style="color:yellow;">', '</span>'],
122
+ //background colors
123
+ //grayscale
124
+ 'whiteBG' : ['<span style="background-color:white;">', '</span>'],
125
+ 'greyBG' : ['<span style="background-color:gray;">', '</span>'],
126
+ 'blackBG' : ['<span style="background-color:black;">', '</span>'],
127
+ //colors
128
+ 'blueBG' : ['<span style="background-color:blue;">', '</span>'],
129
+ 'cyanBG' : ['<span style="background-color:cyan;">', '</span>'],
130
+ 'greenBG' : ['<span style="background-color:green;">', '</span>'],
131
+ 'magentaBG' : ['<span style="background-color:magenta;">', '</span>'],
132
+ 'redBG' : ['<span style="background-color:red;">', '</span>'],
133
+ 'yellowBG' : ['<span style="background-color:yellow;">', '</span>']
134
+ };
135
+ } else if (exports.mode === 'none') {
136
+ return str + '';
137
+ } else {
138
+ console.log('unsupported mode, try "browser", "console" or "none"');
139
+ }
140
+ return styles[style][0] + str + styles[style][1];
141
+ }
142
+
143
+ function applyTheme(theme) {
144
+
145
+ //
146
+ // Remark: This is a list of methods that exist
147
+ // on String that you should not overwrite.
148
+ //
149
+ var stringPrototypeBlacklist = [
150
+ '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor',
151
+ 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt',
152
+ 'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring',
153
+ 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
154
+ ];
155
+
156
+ Object.keys(theme).forEach(function (prop) {
157
+ if (stringPrototypeBlacklist.indexOf(prop) !== -1) {
158
+ console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name');
159
+ }
160
+ else {
161
+ if (typeof(theme[prop]) === 'string') {
162
+ addProperty(prop, function () {
163
+ return exports[theme[prop]](this);
164
+ });
165
+ }
166
+ else {
167
+ addProperty(prop, function () {
168
+ var ret = this;
169
+ for (var t = 0; t < theme[prop].length; t++) {
170
+ ret = exports[theme[prop][t]](ret);
171
+ }
172
+ return ret;
173
+ });
174
+ }
175
+ }
176
+ });
177
+ }
178
+
179
+
180
+ //
181
+ // Iterate through all default styles and colors
182
+ //
183
+ var x = ['bold', 'underline', 'strikethrough', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta', 'greyBG', 'blackBG', 'yellowBG', 'redBG', 'greenBG', 'blueBG', 'whiteBG', 'cyanBG', 'magentaBG'];
184
+ x.forEach(function (style) {
29
185
 
30
186
  // __defineGetter__ at the least works in more browsers
31
187
  // http://robertnyman.com/javascript/javascript-getters-setters.html
32
188
  // Object.defineProperty only works in Chrome
33
- String.prototype.__defineGetter__(style, function () {
34
- return isHeadless ?
35
- stylize(this, style) : // for those running in node (headless environments)
36
- this.replace(/( )/, '$1'); // and for those running in browsers:
37
- // re: ^ you'd think 'return this' works (but doesn't) so replace coerces the string to be a real string
189
+ addProperty(style, function () {
190
+ return stylize(this, style);
38
191
  });
39
192
  });
40
193
 
41
- // prototypes string with method "rainbow"
42
- // rainbow will apply a the color spectrum to a string, changing colors every letter
43
- String.prototype.__defineGetter__('rainbow', function () {
44
- if (!isHeadless) {
45
- return this.replace(/( )/, '$1');
46
- }
47
- var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
48
- var exploded = this.split("");
49
- var i=0;
50
- exploded = exploded.map(function(letter) {
51
- if (letter==" ") {
52
- return letter;
194
+ function sequencer(map) {
195
+ return function () {
196
+ if (!isHeadless) {
197
+ return this.replace(/( )/, '$1');
53
198
  }
54
- else {
55
- return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
199
+ var exploded = this.split(""), i = 0;
200
+ exploded = exploded.map(map);
201
+ return exploded.join("");
202
+ };
203
+ }
204
+
205
+ var rainbowMap = (function () {
206
+ var rainbowColors = ['red', 'yellow', 'green', 'blue', 'magenta']; //RoY G BiV
207
+ return function (letter, i, exploded) {
208
+ if (letter === " ") {
209
+ return letter;
210
+ } else {
211
+ return stylize(letter, rainbowColors[i++ % rainbowColors.length]);
56
212
  }
57
- });
58
- return exploded.join("");
213
+ };
214
+ })();
215
+
216
+ exports.themes = {};
217
+
218
+ exports.addSequencer = function (name, map) {
219
+ addProperty(name, sequencer(map));
220
+ };
221
+
222
+ exports.addSequencer('rainbow', rainbowMap);
223
+ exports.addSequencer('zebra', function (letter, i, exploded) {
224
+ return i % 2 === 0 ? letter : letter.inverse;
59
225
  });
60
226
 
61
- function stylize(str, style) {
62
- var styles = {
63
- //styles
64
- 'bold' : [1, 22],
65
- 'italic' : [3, 23],
66
- 'underline' : [4, 24],
67
- 'inverse' : [7, 27],
68
- //grayscale
69
- 'white' : [37, 39],
70
- 'grey' : [90, 39],
71
- 'black' : [90, 39],
72
- //colors
73
- 'blue' : [34, 39],
74
- 'cyan' : [36, 39],
75
- 'green' : [32, 39],
76
- 'magenta' : [35, 39],
77
- 'red' : [31, 39],
78
- 'yellow' : [33, 39]
79
- };
80
- return '\033[' + styles[style][0] + 'm' + str +
81
- '\033[' + styles[style][1] + 'm';
227
+ exports.setTheme = function (theme) {
228
+ if (typeof theme === 'string') {
229
+ try {
230
+ exports.themes[theme] = require(theme);
231
+ applyTheme(exports.themes[theme]);
232
+ return exports.themes[theme];
233
+ } catch (err) {
234
+ console.log(err);
235
+ return err;
236
+ }
237
+ } else {
238
+ applyTheme(theme);
239
+ }
82
240
  };
83
241
 
84
- // don't summon zalgo
85
- String.prototype.__defineGetter__('zalgo', function () {
86
- return zalgo(this);
242
+
243
+ addProperty('stripColors', function () {
244
+ return ("" + this).replace(/\x1B\[\d+m/g, '');
87
245
  });
88
246
 
89
247
  // please no
90
248
  function zalgo(text, options) {
91
249
  var soul = {
92
250
  "up" : [
93
- '̍','̎','̄','̅',
94
- '̿','̑','̆','̐',
95
- '͒','͗','͑','̇',
96
- '̈','̊','͂','̓',
97
- '̈','͊','͋','͌',
98
- '̃','̂','̌','͐',
99
- '̀','́','̋','̏',
100
- '̒','̓','̔','̽',
101
- '̉','ͣ','ͤ','ͥ',
102
- 'ͦ','ͧ','ͨ','ͩ',
103
- 'ͪ','ͫ','ͬ','ͭ',
104
- 'ͮ','ͯ','̾','͛',
105
- '͆','̚'
106
- ],
251
+ '̍', '̎', '̄', '̅',
252
+ '̿', '̑', '̆', '̐',
253
+ '͒', '͗', '͑', '̇',
254
+ '̈', '̊', '͂', '̓',
255
+ '̈', '͊', '͋', '͌',
256
+ '̃', '̂', '̌', '͐',
257
+ '̀', '́', '̋', '̏',
258
+ '̒', '̓', '̔', '̽',
259
+ '̉', 'ͣ', 'ͤ', 'ͥ',
260
+ 'ͦ', 'ͧ', 'ͨ', 'ͩ',
261
+ 'ͪ', 'ͫ', 'ͬ', 'ͭ',
262
+ 'ͮ', 'ͯ', '̾', '͛',
263
+ '͆', '̚'
264
+ ],
107
265
  "down" : [
108
- '̖','̗','̘','̙',
109
- '̜','̝','̞','̟',
110
- '̠','̤','̥','̦',
111
- '̩','̪','̫','̬',
112
- '̭','̮','̯','̰',
113
- '̱','̲','̳','̹',
114
- '̺','̻','̼','ͅ',
115
- '͇','͈','͉','͍',
116
- '͎','͓','͔','͕',
117
- '͖','͙','͚','̣'
118
- ],
266
+ '̖', '̗', '̘', '̙',
267
+ '̜', '̝', '̞', '̟',
268
+ '̠', '̤', '̥', '̦',
269
+ '̩', '̪', '̫', '̬',
270
+ '̭', '̮', '̯', '̰',
271
+ '̱', '̲', '̳', '̹',
272
+ '̺', '̻', '̼', 'ͅ',
273
+ '͇', '͈', '͉', '͍',
274
+ '͎', '͓', '͔', '͕',
275
+ '͖', '͙', '͚', '̣'
276
+ ],
119
277
  "mid" : [
120
- '̕','̛','̀','́',
121
- '͘','̡','̢','̧',
122
- '̨','̴','̵','̶',
123
- '͜','͝','͞',
124
- '͟','͠','͢','̸',
125
- '̷','͡',' ҉'
126
- ]
278
+ '̕', '̛', '̀', '́',
279
+ '͘', '̡', '̢', '̧',
280
+ '̨', '̴', '̵', '̶',
281
+ '͜', '͝', '͞',
282
+ '͟', '͠', '͢', '̸',
283
+ '̷', '͡', ' ҉'
284
+ ]
127
285
  },
128
286
  all = [].concat(soul.up, soul.down, soul.mid),
129
287
  zalgo = {};
130
288
 
131
289
  function randomNumber(range) {
132
- r = Math.floor(Math.random()*range);
290
+ var r = Math.floor(Math.random() * range);
133
291
  return r;
134
- };
292
+ }
135
293
 
136
294
  function is_char(character) {
137
295
  var bool = false;
138
- all.filter(function(i){
139
- bool = (i == character);
296
+ all.filter(function (i) {
297
+ bool = (i === character);
140
298
  });
141
299
  return bool;
142
300
  }
143
301
 
144
- function heComes(text, options){
145
- result = '';
146
- options = options || {};
147
- options["up"] = options["up"] || true;
148
- options["mid"] = options["mid"] || true;
149
- options["down"] = options["down"] || true;
150
- options["size"] = options["size"] || "maxi";
151
- var counts;
152
- text = text.split('');
153
- for(var l in text){
154
- if(is_char(l)) { continue; }
155
- result = result + text[l];
156
-
157
- counts = {"up" : 0, "down" : 0, "mid" : 0};
158
-
159
- switch(options.size) {
160
- case 'mini':
161
- counts.up = randomNumber(8);
162
- counts.min= randomNumber(2);
163
- counts.down = randomNumber(8);
164
- break;
165
- case 'maxi':
166
- counts.up = randomNumber(16) + 3;
167
- counts.min = randomNumber(4) + 1;
168
- counts.down = randomNumber(64) + 3;
169
- break;
170
- default:
171
- counts.up = randomNumber(8) + 1;
172
- counts.mid = randomNumber(6) / 2;
173
- counts.down= randomNumber(8) + 1;
174
- break;
175
- }
302
+ function heComes(text, options) {
303
+ var result = '', counts, l;
304
+ options = options || {};
305
+ options["up"] = options["up"] || true;
306
+ options["mid"] = options["mid"] || true;
307
+ options["down"] = options["down"] || true;
308
+ options["size"] = options["size"] || "maxi";
309
+ text = text.split('');
310
+ for (l in text) {
311
+ if (is_char(l)) {
312
+ continue;
313
+ }
314
+ result = result + text[l];
315
+ counts = {"up" : 0, "down" : 0, "mid" : 0};
316
+ switch (options.size) {
317
+ case 'mini':
318
+ counts.up = randomNumber(8);
319
+ counts.min = randomNumber(2);
320
+ counts.down = randomNumber(8);
321
+ break;
322
+ case 'maxi':
323
+ counts.up = randomNumber(16) + 3;
324
+ counts.min = randomNumber(4) + 1;
325
+ counts.down = randomNumber(64) + 3;
326
+ break;
327
+ default:
328
+ counts.up = randomNumber(8) + 1;
329
+ counts.mid = randomNumber(6) / 2;
330
+ counts.down = randomNumber(8) + 1;
331
+ break;
332
+ }
176
333
 
177
- var arr = ["up", "mid", "down"];
178
- for(var d in arr){
179
- var index = arr[d];
180
- for (var i = 0 ; i <= counts[index]; i++)
181
- {
182
- if(options[index]) {
183
- result = result + soul[index][randomNumber(soul[index].length)];
184
- }
185
- }
334
+ var arr = ["up", "mid", "down"];
335
+ for (var d in arr) {
336
+ var index = arr[d];
337
+ for (var i = 0 ; i <= counts[index]; i++) {
338
+ if (options[index]) {
339
+ result = result + soul[index][randomNumber(soul[index].length)];
186
340
  }
187
341
  }
188
- return result;
189
- };
342
+ }
343
+ }
344
+ return result;
345
+ }
190
346
  return heComes(text);
191
347
  }
348
+
349
+
350
+ // don't summon zalgo
351
+ addProperty('zalgo', function () {
352
+ return zalgo(this);
353
+ });
package/example.html CHANGED
@@ -4,17 +4,73 @@
4
4
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
5
5
  <title>Colors Example</title>
6
6
  <script src="colors.js"></script>
7
- <script type="text/javascript">
8
- console.log('Rainbows are fun!'.rainbow);
9
- console.log('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse);
10
- console.log('Chains are also cool.'.bold.italic.underline.red);
11
- </script>
12
7
  </head>
13
8
  <body>
14
9
  <script>
15
- document.write('Rainbows are fun!'.rainbow + '<br>');
16
- document.write('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse + '<br>');
17
- document.write('Chains are also cool.'.bold.italic.underline.red);
10
+
11
+ var test = colors.red("hopefully colorless output");
12
+
13
+ document.write('Rainbows are fun!'.rainbow + '<br/>');
14
+ document.write('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
15
+ document.write('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
16
+ //document.write('zalgo time!'.zalgo);
17
+ document.write(test.stripColors);
18
+ document.write("a".grey + " b".black);
19
+
20
+ document.write("Zebras are so fun!".zebra);
21
+
22
+ document.write(colors.rainbow('Rainbows are fun!'));
23
+ document.write("This is " + "not".strikethrough + " fun.");
24
+
25
+ document.write(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
26
+ document.write(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
27
+ //document.write(colors.zalgo('zalgo time!'));
28
+ document.write(colors.stripColors(test));
29
+ document.write(colors.grey("a") + colors.black(" b"));
30
+
31
+ colors.addSequencer("america", function(letter, i, exploded) {
32
+ if(letter === " ") return letter;
33
+ switch(i%3) {
34
+ case 0: return letter.red;
35
+ case 1: return letter.white;
36
+ case 2: return letter.blue;
37
+ }
38
+ });
39
+
40
+ colors.addSequencer("random", (function() {
41
+ var available = ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
42
+
43
+ return function(letter, i, exploded) {
44
+ return letter === " " ? letter : letter[available[Math.round(Math.random() * (available.length - 1))]];
45
+ };
46
+ })());
47
+
48
+ document.write("AMERICA! F--K YEAH!".america);
49
+ document.write("So apparently I've been to Mars, with all the little green men. But you know, I don't recall.".random);
50
+
51
+ //
52
+ // Custom themes
53
+ //
54
+
55
+ colors.setTheme({
56
+ silly: 'rainbow',
57
+ input: 'grey',
58
+ verbose: 'cyan',
59
+ prompt: 'grey',
60
+ info: 'green',
61
+ data: 'grey',
62
+ help: 'cyan',
63
+ warn: 'yellow',
64
+ debug: 'blue',
65
+ error: 'red'
66
+ });
67
+
68
+ // outputs red text
69
+ document.write("this is an error".error);
70
+
71
+ // outputs yellow text
72
+ document.write("this is a warning".warn);
73
+
18
74
  </script>
19
75
  </body>
20
76
  </html>
package/example.js CHANGED
@@ -1,7 +1,77 @@
1
- var sys = require('sys');
2
1
  var colors = require('./colors');
3
2
 
4
- sys.puts('Rainbows are fun!'.rainbow);
5
- sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
6
- sys.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
7
- // sys.puts('zalgo time!'.zalgo);
3
+ //colors.mode = "browser";
4
+
5
+ var test = colors.red("hopefully colorless output");
6
+ console.log('Rainbows are fun!'.rainbow);
7
+ console.log('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
8
+ console.log('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
9
+ //console.log('zalgo time!'.zalgo);
10
+ console.log(test.stripColors);
11
+ console.log("a".grey + " b".black);
12
+ console.log("Zebras are so fun!".zebra);
13
+ console.log('background color attack!'.black.whiteBG)
14
+
15
+ //
16
+ // Remark: .strikethrough may not work with Mac OS Terminal App
17
+ //
18
+ console.log("This is " + "not".strikethrough + " fun.");
19
+ console.log(colors.rainbow('Rainbows are fun!'));
20
+ console.log(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
21
+ console.log(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
22
+ //console.log(colors.zalgo('zalgo time!'));
23
+ console.log(colors.stripColors(test));
24
+ console.log(colors.grey("a") + colors.black(" b"));
25
+
26
+ colors.addSequencer("america", function(letter, i, exploded) {
27
+ if(letter === " ") return letter;
28
+ switch(i%3) {
29
+ case 0: return letter.red;
30
+ case 1: return letter.white;
31
+ case 2: return letter.blue;
32
+ }
33
+ });
34
+
35
+ colors.addSequencer("random", (function() {
36
+ var available = ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
37
+
38
+ return function(letter, i, exploded) {
39
+ return letter === " " ? letter : letter[available[Math.round(Math.random() * (available.length - 1))]];
40
+ };
41
+ })());
42
+
43
+ console.log("AMERICA! F--K YEAH!".america);
44
+ console.log("So apparently I've been to Mars, with all the little green men. But you know, I don't recall.".random);
45
+
46
+ //
47
+ // Custom themes
48
+ //
49
+
50
+ // Load theme with JSON literal
51
+ colors.setTheme({
52
+ silly: 'rainbow',
53
+ input: 'grey',
54
+ verbose: 'cyan',
55
+ prompt: 'grey',
56
+ info: 'green',
57
+ data: 'grey',
58
+ help: 'cyan',
59
+ warn: 'yellow',
60
+ debug: 'blue',
61
+ error: 'red'
62
+ });
63
+
64
+ // outputs red text
65
+ console.log("this is an error".error);
66
+
67
+ // outputs yellow text
68
+ console.log("this is a warning".warn);
69
+
70
+ // outputs grey text
71
+ console.log("this is an input".input);
72
+
73
+ // Load a theme from file
74
+ colors.setTheme('./themes/winston-dark.js');
75
+
76
+ console.log("this is an input".input);
77
+
package/package.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "name": "colors",
3
3
  "description": "get colors in your node.js console like what",
4
- "version": "0.5.0",
4
+ "version": "0.6.1",
5
5
  "author": "Marak Squires",
6
+ "homepage": "https://github.com/Marak/colors.js",
7
+ "bugs": "https://github.com/Marak/colors.js/issues",
8
+ "keywords": [ "ansi", "terminal", "colors" ],
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "http://github.com/Marak/colors.js.git"
9
12
  },
10
- "engine": [
11
- "node >=0.1.90"
12
- ],
13
+ "engines": {
14
+ "node": ">=0.1.90"
15
+ },
13
16
  "main": "colors"
14
17
  }
package/test.js ADDED
@@ -0,0 +1,70 @@
1
+ var assert = require('assert'),
2
+ colors = require('./colors');
3
+
4
+ var s = 'string';
5
+
6
+ function a(s, code) {
7
+ return '\x1B[' + code.toString() + 'm' + s + '\x1B[39m';
8
+ }
9
+
10
+ function aE(s, color, code) {
11
+ assert.equal(s[color], a(s, code));
12
+ assert.equal(colors[color](s), a(s, code));
13
+ assert.equal(s[color], colors[color](s));
14
+ assert.equal(s[color].stripColors, s);
15
+ assert.equal(s[color].stripColors, colors.stripColors(s));
16
+ }
17
+
18
+ function h(s, color) {
19
+ return '<span style="color:' + color + ';">' + s + '</span>';
20
+ }
21
+
22
+ var stylesColors = ['white', 'black', 'blue', 'cyan', 'green', 'magenta', 'red', 'yellow'];
23
+ var stylesAll = stylesColors.concat(['bold', 'italic', 'underline', 'inverse', 'rainbow']);
24
+
25
+ colors.mode = 'console';
26
+ assert.equal(s.bold, '\x1B[1m' + s + '\x1B[22m');
27
+ assert.equal(s.italic, '\x1B[3m' + s + '\x1B[23m');
28
+ assert.equal(s.underline, '\x1B[4m' + s + '\x1B[24m');
29
+ assert.equal(s.strikethrough, '\x1B[9m' + s + '\x1B[29m');
30
+ assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m');
31
+ assert.ok(s.rainbow);
32
+ aE(s, 'white', 37);
33
+ aE(s, 'grey', 90);
34
+ aE(s, 'black', 30);
35
+ aE(s, 'blue', 34);
36
+ aE(s, 'cyan', 36);
37
+ aE(s, 'green', 32);
38
+ aE(s, 'magenta', 35);
39
+ aE(s, 'red', 31);
40
+ aE(s, 'yellow', 33);
41
+ assert.equal(s, 'string');
42
+
43
+ colors.setTheme({error:'red'});
44
+
45
+ assert.equal(typeof("astring".red),'string');
46
+ assert.equal(typeof("astring".error),'string');
47
+
48
+ colors.mode = 'browser';
49
+ assert.equal(s.bold, '<b>' + s + '</b>');
50
+ assert.equal(s.italic, '<i>' + s + '</i>');
51
+ assert.equal(s.underline, '<u>' + s + '</u>');
52
+ assert.equal(s.strikethrough, '<del>' + s + '</del>');
53
+ assert.equal(s.inverse, '<span style="background-color:black;color:white;">' + s + '</span>');
54
+ assert.ok(s.rainbow);
55
+ stylesColors.forEach(function (color) {
56
+ assert.equal(s[color], h(s, color));
57
+ assert.equal(colors[color](s), h(s, color));
58
+ });
59
+
60
+ assert.equal(typeof("astring".red),'string');
61
+ assert.equal(typeof("astring".error),'string');
62
+
63
+ colors.mode = 'none';
64
+ stylesAll.forEach(function (style) {
65
+ assert.equal(s[style], s);
66
+ assert.equal(colors[style](s), s);
67
+ });
68
+
69
+ assert.equal(typeof("astring".red),'string');
70
+ assert.equal(typeof("astring".error),'string');
@@ -0,0 +1,12 @@
1
+ module['exports'] = {
2
+ silly: 'rainbow',
3
+ input: 'black',
4
+ verbose: 'cyan',
5
+ prompt: 'grey',
6
+ info: 'green',
7
+ data: 'grey',
8
+ help: 'cyan',
9
+ warn: 'yellow',
10
+ debug: 'blue',
11
+ error: 'red'
12
+ };
@@ -0,0 +1,12 @@
1
+ module['exports'] = {
2
+ silly: 'rainbow',
3
+ input: 'grey',
4
+ verbose: 'cyan',
5
+ prompt: 'grey',
6
+ info: 'green',
7
+ data: 'grey',
8
+ help: 'cyan',
9
+ warn: 'yellow',
10
+ debug: 'blue',
11
+ error: 'red'
12
+ };