Last active
December 29, 2016 23:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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