Skip to content

Instantly share code, notes, and snippets.

@edlea
Last active October 31, 2017 11:04
Show Gist options
  • Save edlea/1583667 to your computer and use it in GitHub Desktop.
Save edlea/1583667 to your computer and use it in GitHub Desktop.
Coloured output in node's console.log()
// From the bottom of https://github.com/bengourley/Node-Deployment/blob/master/Jakefile.js
function stylize(str, style) {
var styles = {
//styles
'bold' : [1, 22], 'italic' : [3, 23],
'underline' : [4, 24], 'inverse' : [7, 27],
//grayscale
'white' : [37, 39], 'grey' : [90, 39],
'black' : [90, 39],
//colors
'blue' : [34, 39], 'cyan' : [36, 39],
'green' : [32, 39], 'magenta' : [35, 39],
'red' : [31, 39],'yellow' : [33, 39]
};
return '\033[' + styles[style][0] + 'm' + str + '\033[' + styles[style][1] + 'm';
}
['bold', 'underline', 'italic',
'inverse', 'grey', 'yellow',
'red', 'green', 'blue',
'white', 'cyan', 'magenta'].forEach(function (style) {
String.prototype.__defineGetter__(style, function () {
return stylize(this, style);
});
});
console.log("Why so blue?".blue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment