Last active
December 8, 2016 15:29
-
-
Save walling/8a7a353fd38140ddb8364ad118cbf0eb to your computer and use it in GitHub Desktop.
Primitive replacement for chalk NPM
This file contains hidden or 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
// ES5 version of chalk replacement with chaining :) | |
var util = require("util"); | |
function C(text) { | |
var self = {}; | |
Object.keys(util.inspect.colors).forEach(function(name) { | |
var color = util.inspect.colors[name]; | |
self[name] = function() { | |
return C(util.format("\x1B[%sm%s\x1B[%sm", color[0], text, color[1])); | |
}; | |
}); | |
self.toString = self.valueOf = self.str = function() { return text; }; | |
return self; | |
} | |
console.log("This is the message: %s", C("Yai!").green().underline()); |
This file contains hidden or 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 util = require("util"); | |
function C(color, text) { | |
var c = util.inspect.colors[color]; | |
return c ? util.format("\x1B[%sm%s\x1B[%sm", c[0], text, c[1]) : text; | |
} | |
console.log(C("green", "Yai!")); | |
console.log("Supported colors: %s", Object.keys(util.inspect.colors)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From my previous tweet https://twitter.com/walling/status/697746936955281408