From e33dc816719d376af1118e4d8866fdd31ff67aac Mon Sep 17 00:00:00 2001 From: Marak Squires Date: Fri, 9 Dec 2011 22:50:23 -0800 Subject: [PATCH] [api] First pass at making themes loadable from files --- colors.js | 25 ++++++++++++++++++++++++- example.js | 12 ++++++++++++ themes/winston-dark.js | 12 ++++++++++++ themes/winston-light.js | 12 ++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 themes/winston-dark.js create mode 100644 themes/winston-light.js diff --git a/colors.js b/colors.js index f023cfbd..c3fc81f2 100644 --- a/colors.js +++ b/colors.js @@ -89,6 +89,8 @@ var rainbowMap = (function () { } })(); +exports.themes = {}; + exports.addSequencer = function (name, map) { addProperty(name, sequencer(map)); } @@ -98,7 +100,27 @@ exports.addSequencer('zebra', function (letter, i, exploded) { 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 // on String that you should not overwrite. @@ -109,6 +131,7 @@ exports.setTheme = function (theme) { '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'); diff --git a/example.js b/example.js index 3da29864..a0d686c4 100644 --- a/example.js +++ b/example.js @@ -43,6 +43,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 +63,15 @@ 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', function(err){ + if (err) { + return console.log('error loading theme '.error, err) + } + // outputs black text + console.log("this is an input".input); +}); diff --git a/themes/winston-dark.js b/themes/winston-dark.js new file mode 100644 index 00000000..49a905ba --- /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 00000000..571972c1 --- /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