Skip to content

Instantly share code, notes, and snippets.

@brettz9
Last active December 29, 2016 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettz9/8d6561f56cb91965525659f8b9a85c40 to your computer and use it in GitHub Desktop.
Save brettz9/8d6561f56cb91965525659f8b9a85c40 to your computer and use it in GitHub Desktop.
A close substitute for [colors.js](https://github.com/Marak/colors.js) in the browser. See https://jsfiddle.net/fbe4s9ry/ . Plan to move to https://github.com/brettz9/styled-log
const colors = new Proxy({
setTheme: function (theme) {
this._theme = theme;
},
log: function (msgColor) {
console.log(...msgColor);
}
}, {
get: function (target, name) {
if (['setTheme', 'log', '_theme'].includes(name)) {
return target[name];
}
const log = (/log/).test(name);
name = log ? name.slice(3).toLowerCase() : name;
let color = name;
if (name in target._theme) {
color = target._theme[name];
}
const ret = function (msg) {
return ['%c' + msg, 'color: ' + color];
};
if (log) {
return function (msgColor) {
target.log(ret(msgColor));
};
}
return ret;
}
});
if (typeof module !== 'undefined') {
module.exports = colors;
}
var theme = {
pass: 'green',
fail: 'red',
timeout: 'red',
notrun: 'red'
};
colors.setTheme(theme);
// Log "Good!" in green
colors.log(colors.pass('Good!'));
console.log(...colors.pass('Good!'));
colors.logPass('Good!');
colors.logGreen('Good!');
// Log "Bad!" in red
colors.log(colors.fail('Bad!'));
console.log(...colors.fail('Bad!'));
colors.logFail('Bad!');
colors.logRed('Bad!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment