mirror of
https://github.com/flynx/colors.js.git
synced 2026-01-05 18:21:16 +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) {
|
exports.addSequencer = function (name, map) {
|
||||||
addProperty(name, sequencer(map));
|
addProperty(name, sequencer(map));
|
||||||
}
|
}
|
||||||
@ -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.
|
||||||
@ -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');
|
||||||
|
|||||||
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
|
// Custom themes
|
||||||
//
|
//
|
||||||
|
|
||||||
|
// Load theme with JSON literal
|
||||||
colors.setTheme({
|
colors.setTheme({
|
||||||
silly: 'rainbow',
|
silly: 'rainbow',
|
||||||
input: 'grey',
|
input: 'grey',
|
||||||
@ -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
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