Skip to content

Instantly share code, notes, and snippets.

@MoritzKn
Last active Oct 12, 2016
Embed
What would you like to do?
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