Skip to content

Instantly share code, notes, and snippets.

@romellem
Last active March 13, 2024 18:14
Show Gist options
  • Save romellem/26e529387757a4fd1424c59993b3cb99 to your computer and use it in GitHub Desktop.
Save romellem/26e529387757a4fd1424c59993b3cb99 to your computer and use it in GitHub Desktop.
Simple Node.js Color Formatting in console.log()
const util = require('util');
const black = (s) => util.format('\x1b[30m%s\x1b[0m', s);
const red = (s) => util.format('\x1b[31m%s\x1b[0m', s);
const green = (s) => util.format('\x1b[32m%s\x1b[0m', s);
const yellow = (s) => util.format('\x1b[33m%s\x1b[0m', s);
const blue = (s) => util.format('\x1b[34m%s\x1b[0m', s);
const magenta = (s) => util.format('\x1b[35m%s\x1b[0m', s);
const cyan = (s) => util.format('\x1b[36m%s\x1b[0m', s);
const white = (s) => util.format('\x1b[37m%s\x1b[0m', s);
const log = {
black(s) {
console.log(black(s));
},
red(s) {
console.log(red(s));
},
green(s) {
console.log(green(s));
},
yellow(s) {
console.log(yellow(s));
},
blue(s) {
console.log(blue(s));
},
magenta(s) {
console.log(magenta(s));
},
cyan(s) {
console.log(cyan(s));
},
white(s) {
console.log(white(s));
},
};
module.exports = {
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
log,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment