Skip to content

Instantly share code, notes, and snippets.

@elisherer
Last active November 22, 2018 11:16
Show Gist options
  • Save elisherer/b76e4ea2671b42492edf74578477aaf0 to your computer and use it in GitHub Desktop.
Save elisherer/b76e4ea2671b42492edf74578477aaf0 to your computer and use it in GitHub Desktop.
Add some color to your node script without adding a library
const c = ((t_mod, t_clr, t_clrm) => (key,txt) => (Array.isArray(key)?key[0]:key).split(' ').reduce((a,k,b,x)=>(x = k.split('.'))&&
(a+"\x1b["+(t_mod.hasOwnProperty(x[0])?t_mod[x[0]]:t_clr.hasOwnProperty(x[0])?''+t_clrm[x[1]]+t_clr[x[0]]:'0')+'m'),'') + (txt ? txt + c`` : ''))(
{ '': 0, reset: 0, bold: 1, dim: 2, italic: 3, underline: 4, blink: 5, inverse: 7, hidden: 8 },
{ black: 0, red: 1, green: 2, yellow: 3, blue: 4, magenta: 5, cyan: 6, white: 7 },
{ [undefined]: 3, bg: 4, light: 9, bglight: 10 }
);
console.log(c("black.light", 'some dim text'));
console.log(c("yellow red.bg", 'yellow and red background'));
console.log(c`red`+ 'template literals supports' + c``);
console.log(`example: ${c`red`}red${c``} ${c`cyan`}cyan${c``}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment