Skip to content

Instantly share code, notes, and snippets.

@mklabs
Created September 26, 2011 14:44
Show Gist options
  • Select an option

  • Save mklabs/1242389 to your computer and use it in GitHub Desktop.

Select an option

Save mklabs/1242389 to your computer and use it in GitHub Desktop.
easily compose log statements with colors and style, a la markdown (kinda)
var colors = require('colors');
// I've got an idea.. (it happens..) What if I could log colors and style,
// like I would do with some markdown content...
//
// so here's the idea, a function wrapper around colors.js to escape some markers
// and easily compose log statement. kinda fun
//
// idea is to automatically escape some custom markers, and apply
// according colors style. Enable easy output, without having to rely,
// on string concat. Ideally, markers should markdown compliant (or close to)
//
// regexp will most likely need some more work, also it may need better semantic
// on colors.
//
// console.log(lolog('lololog should be able to handle **bold**, *italic*, _underline_, ~inverse~, r-red-r, b-blue color-b, and so on'));
//
var lolog = module.exports = function lolog(str) {
return str
.replace(/[^*]\*([^\*]+?)\*[^*]/gm, function(m, s) { return s.italic; })
.replace(/\*\*([^\*]+?)\*\*/gm, function(m, s) { return s.bold; })
.replace(/_([^_]+)_/gm, function(m, s) { return s.underline; })
.replace(/~([^~]+)~/gm, function(m, s) { return s.inverse; })
.replace(/r-([\w\s]+)-r/gm, function(m, s) { return s.red; })
.replace(/y-([\w\s]+)-y/gm, function(m, s) { return s.yellow; })
.replace(/c-([\w\s]+)-c/gm, function(m, s) { return s.cyan; })
.replace(/w-([\w\s]+)-w/gm, function(m, s) { return s.white; })
.replace(/m-([\w\s]+)-m/gm, function(m, s) { return s.magenta; })
.replace(/g-([\w\s]+)-g/gm, function(m, s) { return s.green; })
.replace(/gr-([\w\s]+)-gr/gm, function(m, s) { return s.grey; })
.replace(/b-([\w\s]+)-b/,gm function(m, s) { return s.blue; })
};
console.log(lolog('This is a simple **markdown** like with some *italic* style colors outputed string'));
console.log(lolog('This is a simple **markdown** like with some _underline_ and ~inverse~ **y-style colors-y** b-outputed-b r-string-r'));
console.log(lolog('lololog should be able to handle **r-red and bold-r**, *italic*, _underline_, ~inverse~, r-red-r, b-blue color-b, and so on'));
console.log(lolog('Say _~**y-YAY-y**~_'));
// bold
// italic
// underline
// inverse
// yellow
// cyan
// white
// magenta
// green
// red
// grey
// blue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment