Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[api] First pass at making themes loadable from files

  • Loading branch information...
commit e33dc816719d376af1118e4d8866fdd31ff67aac 1 parent 19f23dd
@Marak authored
View
25 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');
View
12 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);
+});
View
12 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'
+};
View
12 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'
+};
Please sign in to comment.
Something went wrong with that request. Please try again.