Skip to content

Commit

Permalink
[api] First pass at making themes loadable from files
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Dec 10, 2011
1 parent 19f23dd commit e33dc81
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
25 changes: 24 additions & 1 deletion colors.js
Expand Up @@ -89,6 +89,8 @@ var rainbowMap = (function () {
} }
})(); })();


exports.themes = {};

exports.addSequencer = function (name, map) { exports.addSequencer = function (name, map) {
addProperty(name, sequencer(map)); addProperty(name, sequencer(map));
} }
Expand All @@ -98,7 +100,27 @@ exports.addSequencer('zebra', function (letter, i, exploded) {
return i % 2 === 0 ? letter : letter.inverse; return i % 2 === 0 ? letter : letter.inverse;
}); });


exports.setTheme = function (theme) { exports.setTheme = function (theme, cb) {
if(typeof cb !== 'function') {
cb = function (err, result) {
console.log(err);
};
}
if (typeof theme === 'string') {
try {
exports.themes[theme] = require(theme);
applyTheme(exports.themes[theme]);
cb(null, exports.themes[theme]);
} catch (err) {
return cb(err);
}
} else {
applyTheme(theme);
}
}

function applyTheme (theme) {

// //
// Remark: This is a list of methods that exist // Remark: This is a list of methods that exist
// on String that you should not overwrite. // on String that you should not overwrite.
Expand All @@ -109,6 +131,7 @@ exports.setTheme = function (theme) {
'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring', 'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring',
'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight' 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
]; ];

Object.keys(theme).forEach(function(prop){ Object.keys(theme).forEach(function(prop){
if (stringPrototypeBlacklist.indexOf(prop) !== -1) { 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'); console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name');
Expand Down
12 changes: 12 additions & 0 deletions example.js
Expand Up @@ -43,6 +43,7 @@ console.log("So apparently I've been to Mars, with all the little green men. But
// Custom themes // Custom themes
// //


// Load theme with JSON literal
colors.setTheme({ colors.setTheme({
silly: 'rainbow', silly: 'rainbow',
input: 'grey', input: 'grey',
Expand All @@ -62,4 +63,15 @@ console.log("this is an error".error);
// outputs yellow text // outputs yellow text
console.log("this is a warning".warn); 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', function(err){
if (err) {
return console.log('error loading theme '.error, err)
}
// outputs black text
console.log("this is an input".input);
});


12 changes: 12 additions & 0 deletions 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'
};
12 changes: 12 additions & 0 deletions 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'
};

0 comments on commit e33dc81

Please sign in to comment.