colors 0.5.0 → 0.5.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.
Files changed (3) hide show
  1. package/colors.js +63 -24
  2. package/example.js +18 -5
  3. package/package.json +4 -4
package/colors.js CHANGED
@@ -23,14 +23,24 @@ THE SOFTWARE.
23
23
 
24
24
  */
25
25
 
26
+ exports.mode = "console";
27
+
26
28
  // prototypes the string object to have additional method calls that add terminal colors
29
+
30
+ var addProperty = function (color, func) {
31
+ exports[color] = function(str) {
32
+ return func.apply(str);
33
+ };
34
+ String.prototype.__defineGetter__(color, func);
35
+ }
36
+
27
37
  var isHeadless = (typeof module !== 'undefined');
28
- ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
38
+ ['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
29
39
 
30
40
  // __defineGetter__ at the least works in more browsers
31
41
  // http://robertnyman.com/javascript/javascript-getters-setters.html
32
42
  // Object.defineProperty only works in Chrome
33
- String.prototype.__defineGetter__(style, function () {
43
+ addProperty(style, function () {
34
44
  return isHeadless ?
35
45
  stylize(this, style) : // for those running in node (headless environments)
36
46
  this.replace(/( )/, '$1'); // and for those running in browsers:
@@ -40,7 +50,7 @@ var isHeadless = (typeof module !== 'undefined');
40
50
 
41
51
  // prototypes string with method "rainbow"
42
52
  // rainbow will apply a the color spectrum to a string, changing colors every letter
43
- String.prototype.__defineGetter__('rainbow', function () {
53
+ addProperty('rainbow', function () {
44
54
  if (!isHeadless) {
45
55
  return this.replace(/( )/, '$1');
46
56
  }
@@ -59,30 +69,55 @@ String.prototype.__defineGetter__('rainbow', function () {
59
69
  });
60
70
 
61
71
  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';
72
+ if (exports.mode == 'console') {
73
+ var styles = {
74
+ //styles
75
+ 'bold' : ['\033[1m', '\033[22m'],
76
+ 'italic' : ['\033[3m', '\033[23m'],
77
+ 'underline' : ['\033[4m', '\033[24m'],
78
+ 'inverse' : ['\033[7m', '\033[27m'],
79
+ //grayscale
80
+ 'white' : ['\033[37m', '\033[39m'],
81
+ 'grey' : ['\033[90m', '\033[39m'],
82
+ 'black' : ['\033[30m', '\033[39m'],
83
+ //colors
84
+ 'blue' : ['\033[34m', '\033[39m'],
85
+ 'cyan' : ['\033[36m', '\033[39m'],
86
+ 'green' : ['\033[32m', '\033[39m'],
87
+ 'magenta' : ['\033[35m', '\033[39m'],
88
+ 'red' : ['\033[31m', '\033[39m'],
89
+ 'yellow' : ['\033[33m', '\033[39m']
90
+ };
91
+ } else if (exports.mode == 'browser') {
92
+ var styles = {
93
+ //styles
94
+ 'bold' : ['<b>', '</b>'],
95
+ 'italic' : ['<i>', '</i>'],
96
+ 'underline' : ['<u>', '</u>'],
97
+ 'inverse' : ['<span style="background-color:black;color:white;">', '</span>'],
98
+ //grayscale
99
+ 'white' : ['<span style="color:white;">', '</span>'],
100
+ 'grey' : ['<span style="color:grey;">', '</span>'],
101
+ 'black' : ['<span style="color:black;">', '</span>'],
102
+ //colors
103
+ 'blue' : ['<span style="color:blue;">', '</span>'],
104
+ 'cyan' : ['<span style="color:cyan;">', '</span>'],
105
+ 'green' : ['<span style="color:green;">', '</span>'],
106
+ 'magenta' : ['<span style="color:magenta;">', '</span>'],
107
+ 'red' : ['<span style="color:red;">', '</span>'],
108
+ 'yellow' : ['<span style="color:yellow;">', '</span>']
109
+ };
110
+ } else if (exports.mode == 'none') {
111
+ return str;
112
+ } else {
113
+ console.log('unsupported mode, try "browser", "console" or "none"');
114
+ }
115
+
116
+ return styles[style][0] + str + styles[style][1];
82
117
  };
83
118
 
84
119
  // don't summon zalgo
85
- String.prototype.__defineGetter__('zalgo', function () {
120
+ addProperty('zalgo', function () {
86
121
  return zalgo(this);
87
122
  });
88
123
 
@@ -189,3 +224,7 @@ function zalgo(text, options) {
189
224
  };
190
225
  return heComes(text);
191
226
  }
227
+
228
+ addProperty('stripColors', function() {
229
+ return ("" + this).replace(/\u001b\[\d+m/g,'');
230
+ });
package/example.js CHANGED
@@ -1,7 +1,20 @@
1
- var sys = require('sys');
1
+ var util = require('util');
2
2
  var colors = require('./colors');
3
3
 
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);
4
+ //colors.mode = "browser";
5
+
6
+ var test = colors.red("hopefully colorless output");
7
+ util.puts('Rainbows are fun!'.rainbow);
8
+ util.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
9
+ util.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
10
+ //util.puts('zalgo time!'.zalgo);
11
+ util.puts(test.stripColors);
12
+ util.puts("a".grey + " b".black);
13
+
14
+ util.puts(colors.rainbow('Rainbows are fun!'));
15
+ util.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
16
+ util.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
17
+ //util.puts(colors.zalgo('zalgo time!'));
18
+ util.puts(colors.stripColors(test));
19
+ util.puts(colors.grey("a") + colors.black(" b"));
20
+
package/package.json CHANGED
@@ -1,14 +1,14 @@
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.5.1",
5
5
  "author": "Marak Squires",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "http://github.com/Marak/colors.js.git"
9
9
  },
10
- "engine": [
11
- "node >=0.1.90"
12
- ],
10
+ "engines": {
11
+ "node": ">=0.1.90"
12
+ },
13
13
  "main": "colors"
14
14
  }