Skip to content

Instantly share code, notes, and snippets.

@pinksynth
Last active September 18, 2020 16:34
Show Gist options
  • Save pinksynth/209937bd424edb2bd21f7c8bf756befd to your computer and use it in GitHub Desktop.
Save pinksynth/209937bd424edb2bd21f7c8bf756befd to your computer and use it in GitHub Desktop.
ANSI Colors in Node.JS
// Dead-simple usage of ANSI colors to give Node scripts a little more elegance.
// This gist only contains a few of the many useful escape codes:
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
const ANSI = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
}
// Basic usage.
console.log(`${ANSI.Bright}Hey look, this is easy to read!${ANSI.Reset}`)
console.log(`${ANSI.FgRed}This is good for errors.${ANSI.Reset}`)
console.log(`${ANSI.FgGreen}This is good for successful operations.${ANSI.Reset}`)
// Simple wrappers.
const warn = (message) => console.log(`${ANSI.FgYellow}WARNING: ${message}${ANSI.Reset}`)
const success = (message) => console.log(`${ANSI.Bright}${ANSI.FgGreen}${message}${ANSI.Reset}`)
success('This is useful for basic scripting in Node.')
warn('Probably don\'t use it in Production code.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment