Skip to main content
small code fixes
Source Link
acdcjunior
  • 136.2k
  • 37
  • 341
  • 312

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", FgGray: "\x1b[90m", BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m", BgGray: "\x1b[100m", } function colorString(color, stringmsg) { return `${color}${stringmsg}${Color.Reset}`; } function colorLog(color, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, stringit) : it )); } 

Use it like this:

colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", FgGray: "\x1b[90m", BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m" BgGray: "\x1b[100m", } function colorString(color, string) { return `${color}${string}${Color.Reset}`; } function colorLog(color, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, string) : it )); } 

Use it like this:

colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", FgGray: "\x1b[90m", BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m", BgGray: "\x1b[100m", } function colorString(color, msg) { return `${color}${msg}${Color.Reset}`; } function colorLog(color, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, it) : it )); } 

Use it like this:

colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 
Add gray
Source Link
Raine Revere
  • 34.3k
  • 6
  • 48
  • 65

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", FgGray: "\x1b[90m",  BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m" BgGray: "\x1b[100m", } function colorString(color, string) { return `${color}${string}${Color.Reset}`; } function colorLog(color, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, string) : it )); } 

Use it like this:

colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m" } function colorString(color, string) { return `${color}${string}${Color.Reset}`; } function colorLog(color, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, string) : it )); } 

Use it like this:

colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", FgGray: "\x1b[90m",  BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m" BgGray: "\x1b[100m", } function colorString(color, string) { return `${color}${string}${Color.Reset}`; } function colorLog(color, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, string) : it )); } 

Use it like this:

colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 
Add ...args to pass additional arguments to the console.log call
Source Link
Seph Reed
  • 11.5k
  • 15
  • 89
  • 159

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m" } function colorString(color, string) { return `${color}${string}${Color.Reset}`; } function colorStringLogcolorLog(color, string, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, string), ...args: it )); } 

Use it like this:

colorStringLogcolorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m" } function colorString(color, string) { return `${color}${string}${Color.Reset}`; } function colorStringLog(color, string, ...args) { console.log(colorString(color, string), ...args); } 

Use it like this:

colorStringLog(Color.FgYellow, "Some Yellow text to console log"); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 

I found this answer above (https://stackoverflow.com/a/41407246/4808079) very useful, but incomplete. If you only ever wanted to color something once, I guess it'd be fine, but I think sharing it in a runnable functional form is much more applicable to real life use cases.

const Color = { Reset: "\x1b[0m", Bright: "\x1b[1m", Dim: "\x1b[2m", Underscore: "\x1b[4m", Blink: "\x1b[5m", Reverse: "\x1b[7m", Hidden: "\x1b[8m", FgBlack: "\x1b[30m", FgRed: "\x1b[31m", FgGreen: "\x1b[32m", FgYellow: "\x1b[33m", FgBlue: "\x1b[34m", FgMagenta: "\x1b[35m", FgCyan: "\x1b[36m", FgWhite: "\x1b[37m", BgBlack: "\x1b[40m", BgRed: "\x1b[41m", BgGreen: "\x1b[42m", BgYellow: "\x1b[43m", BgBlue: "\x1b[44m", BgMagenta: "\x1b[45m", BgCyan: "\x1b[46m", BgWhite: "\x1b[47m" } function colorString(color, string) { return `${color}${string}${Color.Reset}`; } function colorLog(color, ...args) { console.log(...args.map( (it) => typeof it === "string" ? colorString(color, string) : it )); } 

Use it like this:

colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true }); console.log([ colorString(Color.FgRed, "red"), colorString(Color.FgGreen, "green"), colorString(Color.FgBlue, "blue"), ].join(", ")); 
Add ...args to pass additional arguments to the console.log call
Source Link
Loading
Source Link
Seph Reed
  • 11.5k
  • 15
  • 89
  • 159
Loading