Skip to content

Instantly share code, notes, and snippets.

@MoritzKn
Last active October 12, 2016 09:15
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 MoritzKn/ee40ed9019699d5d005d72d7a3ebfbd4 to your computer and use it in GitHub Desktop.
Save MoritzKn/ee40ed9019699d5d005d72d7a3ebfbd4 to your computer and use it in GitHub Desktop.
Terminal colors in Nodejs without extra dependencies
const util = require('util');
const colorize = (str, colorCodes) => `\x1b[${colorCodes[0]}m${str}\x1b[${colorCodes[1]}m`;
const bold = str => colorize(str, util.inspect.colors.bold);
const italic = str => colorize(str, util.inspect.colors.italic);
const underline = str => colorize(str, util.inspect.colors.underline);
const inverse = str => colorize(str, util.inspect.colors.inverse);
const white = str => colorize(str, util.inspect.colors.white);
const grey = str => colorize(str, util.inspect.colors.grey);
const black = str => colorize(str, util.inspect.colors.black);
const blue = str => colorize(str, util.inspect.colors.blue);
const cyan = str => colorize(str, util.inspect.colors.cyan);
const green = str => colorize(str, util.inspect.colors.green);
const magenta = str => colorize(str, util.inspect.colors.magenta);
const red = str => colorize(str, util.inspect.colors.red);
const yellow = str => colorize(str, util.inspect.colors.yellow);
console.log(grey('Woow,'), cyan('This'), blue('is'), green('a'), magenta('colorful'), red('text'), yellow('!'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment