Last active
September 18, 2020 16:34
-
-
Save pinksynth/209937bd424edb2bd21f7c8bf756befd to your computer and use it in GitHub Desktop.
ANSI Colors in Node.JS
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
// 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