mirror of
https://github.com/flynx/colors.js.git
synced 2025-11-04 14:00:11 +00:00
[api] First pass at making themes loadable from files
This commit is contained in:
parent
19f23dd070
commit
e33dc81671
25
colors.js
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');
|
||||
|
||||
12
example.js
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);
|
||||
});
|
||||
|
||||
|
||||
12
themes/winston-dark.js
Normal file
12
themes/winston-dark.js
Normal file
@ -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
themes/winston-light.js
Normal file
12
themes/winston-light.js
Normal file
@ -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'
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user