Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created February 19, 2021 18:33
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 jonathantneal/9b26c630b51b8130d28d0a9661e8e526 to your computer and use it in GitHub Desktop.
Save jonathantneal/9b26c630b51b8130d28d0a9661e8e526 to your computer and use it in GitHub Desktop.
color.js - a color utility useful in a NodeJS terminal, console, command prompt
export default new Proxy(
Object.entries({
reset: 0,
bold: 1,
dim: 2,
underline: 4,
blink: 5,
invert: 7,
hidden: 8,
black: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
white: 37,
bgBlack: 40,
bgRed: 41,
bgGreen: 42,
bgYellow: 43,
bgBlue: 44,
bgMagenta: 45,
bgCyan: 46,
bgWhite: 47,
}).reduce((color, [name, id]) => ({ ...color, [name]: `\x1b[${id}m` }), {}),
{
get: (colors, name) => (string) => colors[name] + string.replace(colors.reset, colors.reset + colors[name]) + colors.reset,
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment