[Pkg-javascript-commits] [node-colors] 01/03: Imported Upstream version 0.6.2
Marcelo Jorge Vieira
metal at moszumanska.debian.org
Thu Nov 21 02:16:47 UTC 2013
This is an automated email from the git hooks/post-receive script.
metal pushed a commit to branch master
in repository node-colors.
commit 21a91f3fb80a2be060b7232fc3b5ffe555ecd4d3
Author: Marcelo Jorge Vieira <metal at alucinados.com>
Date: Wed Nov 20 23:15:37 2013 -0200
Imported Upstream version 0.6.2
---
ReadMe.md | 2 +-
colors.js | 365 ++++++++++++++++++++++++++++-------------------
example.html | 2 +
example.js | 14 +-
package.json | 5 +-
test.js | 31 ++--
themes/winston-dark.js | 12 ++
themes/winston-light.js | 12 ++
8 files changed, 281 insertions(+), 162 deletions(-)
diff --git a/ReadMe.md b/ReadMe.md
index 1c6b0d0..0eda52d 100644
--- a/ReadMe.md
+++ b/ReadMe.md
@@ -40,7 +40,7 @@ console.log('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
```js
-var require('colors');
+var colors = require('colors');
colors.setTheme({
silly: 'rainbow',
diff --git a/colors.js b/colors.js
index a7198f1..7a537d8 100644
--- a/colors.js
+++ b/colors.js
@@ -45,18 +45,131 @@ if (!isHeadless) {
// Prototypes the string object to have additional method calls that add terminal colors
//
var addProperty = function (color, func) {
- var allowOverride = ['bold'];
- exports[color] = function(str) {
+ exports[color] = function (str) {
return func.apply(str);
};
String.prototype.__defineGetter__(color, func);
+};
+
+function stylize(str, style) {
+
+ var styles;
+
+ if (exports.mode === 'console') {
+ styles = {
+ //styles
+ 'bold' : ['\x1B[1m', '\x1B[22m'],
+ 'italic' : ['\x1B[3m', '\x1B[23m'],
+ 'underline' : ['\x1B[4m', '\x1B[24m'],
+ 'inverse' : ['\x1B[7m', '\x1B[27m'],
+ 'strikethrough' : ['\x1B[9m', '\x1B[29m'],
+ //text colors
+ //grayscale
+ 'white' : ['\x1B[37m', '\x1B[39m'],
+ 'grey' : ['\x1B[90m', '\x1B[39m'],
+ 'black' : ['\x1B[30m', '\x1B[39m'],
+ //colors
+ 'blue' : ['\x1B[34m', '\x1B[39m'],
+ 'cyan' : ['\x1B[36m', '\x1B[39m'],
+ 'green' : ['\x1B[32m', '\x1B[39m'],
+ 'magenta' : ['\x1B[35m', '\x1B[39m'],
+ 'red' : ['\x1B[31m', '\x1B[39m'],
+ 'yellow' : ['\x1B[33m', '\x1B[39m'],
+ //background colors
+ //grayscale
+ 'whiteBG' : ['\x1B[47m', '\x1B[49m'],
+ 'greyBG' : ['\x1B[49;5;8m', '\x1B[49m'],
+ 'blackBG' : ['\x1B[40m', '\x1B[49m'],
+ //colors
+ 'blueBG' : ['\x1B[44m', '\x1B[49m'],
+ 'cyanBG' : ['\x1B[46m', '\x1B[49m'],
+ 'greenBG' : ['\x1B[42m', '\x1B[49m'],
+ 'magentaBG' : ['\x1B[45m', '\x1B[49m'],
+ 'redBG' : ['\x1B[41m', '\x1B[49m'],
+ 'yellowBG' : ['\x1B[43m', '\x1B[49m']
+ };
+ } else if (exports.mode === 'browser') {
+ styles = {
+ //styles
+ 'bold' : ['<b>', '</b>'],
+ 'italic' : ['<i>', '</i>'],
+ 'underline' : ['<u>', '</u>'],
+ 'inverse' : ['<span style="background-color:black;color:white;">', '</span>'],
+ 'strikethrough' : ['<del>', '</del>'],
+ //text colors
+ //grayscale
+ 'white' : ['<span style="color:white;">', '</span>'],
+ 'grey' : ['<span style="color:gray;">', '</span>'],
+ 'black' : ['<span style="color:black;">', '</span>'],
+ //colors
+ 'blue' : ['<span style="color:blue;">', '</span>'],
+ 'cyan' : ['<span style="color:cyan;">', '</span>'],
+ 'green' : ['<span style="color:green;">', '</span>'],
+ 'magenta' : ['<span style="color:magenta;">', '</span>'],
+ 'red' : ['<span style="color:red;">', '</span>'],
+ 'yellow' : ['<span style="color:yellow;">', '</span>'],
+ //background colors
+ //grayscale
+ 'whiteBG' : ['<span style="background-color:white;">', '</span>'],
+ 'greyBG' : ['<span style="background-color:gray;">', '</span>'],
+ 'blackBG' : ['<span style="background-color:black;">', '</span>'],
+ //colors
+ 'blueBG' : ['<span style="background-color:blue;">', '</span>'],
+ 'cyanBG' : ['<span style="background-color:cyan;">', '</span>'],
+ 'greenBG' : ['<span style="background-color:green;">', '</span>'],
+ 'magentaBG' : ['<span style="background-color:magenta;">', '</span>'],
+ 'redBG' : ['<span style="background-color:red;">', '</span>'],
+ 'yellowBG' : ['<span style="background-color:yellow;">', '</span>']
+ };
+ } else if (exports.mode === 'none') {
+ return str + '';
+ } else {
+ console.log('unsupported mode, try "browser", "console" or "none"');
+ }
+ return styles[style][0] + str + styles[style][1];
}
+function applyTheme(theme) {
+
+ //
+ // Remark: This is a list of methods that exist
+ // on String that you should not overwrite.
+ //
+ var stringPrototypeBlacklist = [
+ '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor',
+ 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt',
+ 'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring',
+ 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
+ ];
+
+ Object.keys(theme).forEach(function (prop) {
+ if (stringPrototypeBlacklist.indexOf(prop) !== -1) {
+ console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name');
+ }
+ else {
+ if (typeof(theme[prop]) === 'string') {
+ addProperty(prop, function () {
+ return exports[theme[prop]](this);
+ });
+ }
+ else {
+ addProperty(prop, function () {
+ var ret = this;
+ for (var t = 0; t < theme[prop].length; t++) {
+ ret = exports[theme[prop][t]](ret);
+ }
+ return ret;
+ });
+ }
+ }
+ });
+}
+
+
//
// Iterate through all default styles and colors
//
-
-var x = ['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
+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'];
x.forEach(function (style) {
// __defineGetter__ at the least works in more browsers
@@ -72,27 +185,28 @@ function sequencer(map) {
if (!isHeadless) {
return this.replace(/( )/, '$1');
}
- var exploded = this.split("");
- var i = 0;
+ var exploded = this.split(""), i = 0;
exploded = exploded.map(map);
return exploded.join("");
- }
+ };
}
var rainbowMap = (function () {
- var rainbowColors = ['red','yellow','green','blue','magenta']; //RoY G BiV
+ var rainbowColors = ['red', 'yellow', 'green', 'blue', 'magenta']; //RoY G BiV
return function (letter, i, exploded) {
- if (letter == " ") {
+ if (letter === " ") {
return letter;
} else {
return stylize(letter, rainbowColors[i++ % rainbowColors.length]);
}
- }
+ };
})();
+exports.themes = {};
+
exports.addSequencer = function (name, map) {
addProperty(name, sequencer(map));
-}
+};
exports.addSequencer('rainbow', rainbowMap);
exports.addSequencer('zebra', function (letter, i, exploded) {
@@ -100,170 +214,129 @@ exports.addSequencer('zebra', function (letter, i, exploded) {
});
exports.setTheme = function (theme) {
- Object.keys(theme).forEach(function(prop){
- addProperty(prop, function(){
- return exports[theme[prop]](this);
- });
- });
-}
-
-function stylize(str, style) {
-
- if (exports.mode == 'console') {
- var styles = {
- //styles
- 'bold' : ['\033[1m', '\033[22m'],
- 'italic' : ['\033[3m', '\033[23m'],
- 'underline' : ['\033[4m', '\033[24m'],
- 'inverse' : ['\033[7m', '\033[27m'],
- //grayscale
- 'white' : ['\033[37m', '\033[39m'],
- 'grey' : ['\033[90m', '\033[39m'],
- 'black' : ['\033[30m', '\033[39m'],
- //colors
- 'blue' : ['\033[34m', '\033[39m'],
- 'cyan' : ['\033[36m', '\033[39m'],
- 'green' : ['\033[32m', '\033[39m'],
- 'magenta' : ['\033[35m', '\033[39m'],
- 'red' : ['\033[31m', '\033[39m'],
- 'yellow' : ['\033[33m', '\033[39m']
- };
- } else if (exports.mode == 'browser') {
- var styles = {
- //styles
- 'bold' : ['<b>', '</b>'],
- 'italic' : ['<i>', '</i>'],
- 'underline' : ['<u>', '</u>'],
- 'inverse' : ['<span style="background-color:black;color:white;">', '</span>'],
- //grayscale
- 'white' : ['<span style="color:white;">', '</span>'],
- 'grey' : ['<span style="color:grey;">', '</span>'],
- 'black' : ['<span style="color:black;">', '</span>'],
- //colors
- 'blue' : ['<span style="color:blue;">', '</span>'],
- 'cyan' : ['<span style="color:cyan;">', '</span>'],
- 'green' : ['<span style="color:green;">', '</span>'],
- 'magenta' : ['<span style="color:magenta;">', '</span>'],
- 'red' : ['<span style="color:red;">', '</span>'],
- 'yellow' : ['<span style="color:yellow;">', '</span>']
- };
- } else if (exports.mode == 'none') {
- return str;
+ if (typeof theme === 'string') {
+ try {
+ exports.themes[theme] = require(theme);
+ applyTheme(exports.themes[theme]);
+ return exports.themes[theme];
+ } catch (err) {
+ console.log(err);
+ return err;
+ }
} else {
- console.log('unsupported mode, try "browser", "console" or "none"');
+ applyTheme(theme);
}
- return styles[style][0] + str + styles[style][1];
};
-// don't summon zalgo
-addProperty('zalgo', function () {
- return zalgo(this);
+
+addProperty('stripColors', function () {
+ return ("" + this).replace(/\x1B\[\d+m/g, '');
});
// please no
function zalgo(text, options) {
var soul = {
"up" : [
- '̍','̎','̄','̅',
- '̿','̑','̆','̐',
- '͒','͗','͑','̇',
- '̈','̊','͂','̓',
- '̈','͊','͋','͌',
- '̃','̂','̌','͐',
- '̀','́','̋','̏',
- '̒','̓','̔','̽',
- '̉','ͣ','ͤ','ͥ',
- 'ͦ','ͧ','ͨ','ͩ',
- 'ͪ','ͫ','ͬ','ͭ',
- 'ͮ','ͯ','̾','͛',
- '͆','̚'
- ],
+ '̍', '̎', '̄', '̅',
+ '̿', '̑', '̆', '̐',
+ '͒', '͗', '͑', '̇',
+ '̈', '̊', '͂', '̓',
+ '̈', '͊', '͋', '͌',
+ '̃', '̂', '̌', '͐',
+ '̀', '́', '̋', '̏',
+ '̒', '̓', '̔', '̽',
+ '̉', 'ͣ', 'ͤ', 'ͥ',
+ 'ͦ', 'ͧ', 'ͨ', 'ͩ',
+ 'ͪ', 'ͫ', 'ͬ', 'ͭ',
+ 'ͮ', 'ͯ', '̾', '͛',
+ '͆', '̚'
+ ],
"down" : [
- '̖','̗','̘','̙',
- '̜','̝','̞','̟',
- '̠','̤','̥','̦',
- '̩','̪','̫','̬',
- '̭','̮','̯','̰',
- '̱','̲','̳','̹',
- '̺','̻','̼','ͅ',
- '͇','͈','͉','͍',
- '͎','͓','͔','͕',
- '͖','͙','͚','̣'
- ],
+ '̖', '̗', '̘', '̙',
+ '̜', '̝', '̞', '̟',
+ '̠', '̤', '̥', '̦',
+ '̩', '̪', '̫', '̬',
+ '̭', '̮', '̯', '̰',
+ '̱', '̲', '̳', '̹',
+ '̺', '̻', '̼', 'ͅ',
+ '͇', '͈', '͉', '͍',
+ '͎', '͓', '͔', '͕',
+ '͖', '͙', '͚', '̣'
+ ],
"mid" : [
- '̕','̛','̀','́',
- '͘','̡','̢','̧',
- '̨','̴','̵','̶',
- '͜','͝','͞',
- '͟','͠','͢','̸',
- '̷','͡',' ҉'
- ]
+ '̕', '̛', '̀', '́',
+ '͘', '̡', '̢', '̧',
+ '̨', '̴', '̵', '̶',
+ '͜', '͝', '͞',
+ '͟', '͠', '͢', '̸',
+ '̷', '͡', ' ҉'
+ ]
},
all = [].concat(soul.up, soul.down, soul.mid),
zalgo = {};
function randomNumber(range) {
- r = Math.floor(Math.random()*range);
+ var r = Math.floor(Math.random() * range);
return r;
- };
+ }
function is_char(character) {
var bool = false;
- all.filter(function(i){
- bool = (i == character);
+ all.filter(function (i) {
+ bool = (i === character);
});
return bool;
}
- function heComes(text, options){
- result = '';
- options = options || {};
- options["up"] = options["up"] || true;
- options["mid"] = options["mid"] || true;
- options["down"] = options["down"] || true;
- options["size"] = options["size"] || "maxi";
- var counts;
- text = text.split('');
- for(var l in text){
- if(is_char(l)) { continue; }
- result = result + text[l];
-
- counts = {"up" : 0, "down" : 0, "mid" : 0};
-
- switch(options.size) {
- case 'mini':
- counts.up = randomNumber(8);
- counts.min= randomNumber(2);
- counts.down = randomNumber(8);
- break;
- case 'maxi':
- counts.up = randomNumber(16) + 3;
- counts.min = randomNumber(4) + 1;
- counts.down = randomNumber(64) + 3;
- break;
- default:
- counts.up = randomNumber(8) + 1;
- counts.mid = randomNumber(6) / 2;
- counts.down= randomNumber(8) + 1;
- break;
- }
+ function heComes(text, options) {
+ var result = '', counts, l;
+ options = options || {};
+ options["up"] = options["up"] || true;
+ options["mid"] = options["mid"] || true;
+ options["down"] = options["down"] || true;
+ options["size"] = options["size"] || "maxi";
+ text = text.split('');
+ for (l in text) {
+ if (is_char(l)) {
+ continue;
+ }
+ result = result + text[l];
+ counts = {"up" : 0, "down" : 0, "mid" : 0};
+ switch (options.size) {
+ case 'mini':
+ counts.up = randomNumber(8);
+ counts.min = randomNumber(2);
+ counts.down = randomNumber(8);
+ break;
+ case 'maxi':
+ counts.up = randomNumber(16) + 3;
+ counts.min = randomNumber(4) + 1;
+ counts.down = randomNumber(64) + 3;
+ break;
+ default:
+ counts.up = randomNumber(8) + 1;
+ counts.mid = randomNumber(6) / 2;
+ counts.down = randomNumber(8) + 1;
+ break;
+ }
- var arr = ["up", "mid", "down"];
- for(var d in arr){
- var index = arr[d];
- for (var i = 0 ; i <= counts[index]; i++)
- {
- if(options[index]) {
- result = result + soul[index][randomNumber(soul[index].length)];
- }
- }
+ var arr = ["up", "mid", "down"];
+ for (var d in arr) {
+ var index = arr[d];
+ for (var i = 0 ; i <= counts[index]; i++) {
+ if (options[index]) {
+ result = result + soul[index][randomNumber(soul[index].length)];
}
}
- return result;
- };
+ }
+ }
+ return result;
+ }
return heComes(text);
}
-addProperty('stripColors', function() {
- return ("" + this).replace(/\u001b\[\d+m/g,'');
+
+// don't summon zalgo
+addProperty('zalgo', function () {
+ return zalgo(this);
});
diff --git a/example.html b/example.html
index ab95649..7a2ae60 100644
--- a/example.html
+++ b/example.html
@@ -20,6 +20,8 @@
document.write("Zebras are so fun!".zebra);
document.write(colors.rainbow('Rainbows are fun!'));
+ document.write("This is " + "not".strikethrough + " fun.");
+
document.write(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
document.write(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
//document.write(colors.zalgo('zalgo time!'));
diff --git a/example.js b/example.js
index 3da2986..b1e03a4 100644
--- a/example.js
+++ b/example.js
@@ -9,9 +9,13 @@ console.log('Chains are also cool.'.bold.italic.underline.red); // styles not wi
//console.log('zalgo time!'.zalgo);
console.log(test.stripColors);
console.log("a".grey + " b".black);
-
console.log("Zebras are so fun!".zebra);
+console.log('background color attack!'.black.whiteBG)
+//
+// Remark: .strikethrough may not work with Mac OS Terminal App
+//
+console.log("This is " + "not".strikethrough + " fun.");
console.log(colors.rainbow('Rainbows are fun!'));
console.log(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
console.log(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
@@ -43,6 +47,7 @@ console.log("So apparently I've been to Mars, with all the little green men. But
// Custom themes
//
+// Load theme with JSON literal
colors.setTheme({
silly: 'rainbow',
input: 'grey',
@@ -62,4 +67,11 @@ console.log("this is an error".error);
// outputs yellow text
console.log("this is a warning".warn);
+// outputs grey text
+console.log("this is an input".input);
+
+// Load a theme from file
+colors.setTheme('./themes/winston-dark.js');
+
+console.log("this is an input".input);
diff --git a/package.json b/package.json
index 3a53d62..7af24a1 100644
--- a/package.json
+++ b/package.json
@@ -1,8 +1,11 @@
{
"name": "colors",
"description": "get colors in your node.js console like what",
- "version": "0.6.0-1",
+ "version": "0.6.2",
"author": "Marak Squires",
+ "homepage": "https://github.com/Marak/colors.js",
+ "bugs": "https://github.com/Marak/colors.js/issues",
+ "keywords": [ "ansi", "terminal", "colors" ],
"repository": {
"type": "git",
"url": "http://github.com/Marak/colors.js.git"
diff --git a/test.js b/test.js
index 1c03d65..c32417d 100644
--- a/test.js
+++ b/test.js
@@ -1,16 +1,10 @@
var assert = require('assert'),
colors = require('./colors');
-//
-// This is a pretty nice example on how tests shouldn't be written. However,
-// it's more about API stability than about really testing it (although it's
-// a pretty complete test suite).
-//
-
var s = 'string';
function a(s, code) {
- return '\033[' + code.toString() + 'm' + s + '\033[39m';
+ return '\x1B[' + code.toString() + 'm' + s + '\x1B[39m';
}
function aE(s, color, code) {
@@ -23,17 +17,17 @@ function aE(s, color, code) {
function h(s, color) {
return '<span style="color:' + color + ';">' + s + '</span>';
- // that's pretty dumb approach to testing it
}
-var stylesColors = ['white', 'grey', 'black', 'blue', 'cyan', 'green', 'magenta', 'red', 'yellow'];
+var stylesColors = ['white', 'black', 'blue', 'cyan', 'green', 'magenta', 'red', 'yellow'];
var stylesAll = stylesColors.concat(['bold', 'italic', 'underline', 'inverse', 'rainbow']);
colors.mode = 'console';
-assert.equal(s.bold, '\033[1m' + s + '\033[22m');
-assert.equal(s.italic, '\033[3m' + s + '\033[23m');
-assert.equal(s.underline, '\033[4m' + s + '\033[24m');
-assert.equal(s.inverse, '\033[7m' + s + '\033[27m');
+assert.equal(s.bold, '\x1B[1m' + s + '\x1B[22m');
+assert.equal(s.italic, '\x1B[3m' + s + '\x1B[23m');
+assert.equal(s.underline, '\x1B[4m' + s + '\x1B[24m');
+assert.equal(s.strikethrough, '\x1B[9m' + s + '\x1B[29m');
+assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m');
assert.ok(s.rainbow);
aE(s, 'white', 37);
aE(s, 'grey', 90);
@@ -46,10 +40,16 @@ aE(s, 'red', 31);
aE(s, 'yellow', 33);
assert.equal(s, 'string');
+colors.setTheme({error:'red'});
+
+assert.equal(typeof("astring".red),'string');
+assert.equal(typeof("astring".error),'string');
+
colors.mode = 'browser';
assert.equal(s.bold, '<b>' + s + '</b>');
assert.equal(s.italic, '<i>' + s + '</i>');
assert.equal(s.underline, '<u>' + s + '</u>');
+assert.equal(s.strikethrough, '<del>' + s + '</del>');
assert.equal(s.inverse, '<span style="background-color:black;color:white;">' + s + '</span>');
assert.ok(s.rainbow);
stylesColors.forEach(function (color) {
@@ -57,9 +57,14 @@ stylesColors.forEach(function (color) {
assert.equal(colors[color](s), h(s, color));
});
+assert.equal(typeof("astring".red),'string');
+assert.equal(typeof("astring".error),'string');
+
colors.mode = 'none';
stylesAll.forEach(function (style) {
assert.equal(s[style], s);
assert.equal(colors[style](s), s);
});
+assert.equal(typeof("astring".red),'string');
+assert.equal(typeof("astring".error),'string');
diff --git a/themes/winston-dark.js b/themes/winston-dark.js
new file mode 100644
index 0000000..49a905b
--- /dev/null
+++ b/themes/winston-dark.js
@@ -0,0 +1,12 @@
+module['exports'] = {
+ silly: 'rainbow',
+ input: 'black',
+ verbose: 'cyan',
+ prompt: 'grey',
+ info: 'green',
+ data: 'grey',
+ help: 'cyan',
+ warn: 'yellow',
+ debug: 'blue',
+ error: 'red'
+};
\ No newline at end of file
diff --git a/themes/winston-light.js b/themes/winston-light.js
new file mode 100644
index 0000000..571972c
--- /dev/null
+++ b/themes/winston-light.js
@@ -0,0 +1,12 @@
+module['exports'] = {
+ silly: 'rainbow',
+ input: 'grey',
+ verbose: 'cyan',
+ prompt: 'grey',
+ info: 'green',
+ data: 'grey',
+ help: 'cyan',
+ warn: 'yellow',
+ debug: 'blue',
+ error: 'red'
+};
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-colors.git
More information about the Pkg-javascript-commits
mailing list