Last active
November 12, 2023 09:50
-
-
Save himalay/8d7a7ba8cbd8b9906590f64429d82059 to your computer and use it in GitHub Desktop.
Colorful console.log in 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
// browser | |
console.log('%c Fucking hell! ', 'background: #222; font-size: 42px; color: #ff0000'); | |
// nodejs | |
console.log('\x1b[36m%s\x1b[0m', info); //cyan | |
console.log('\x1b[33m%s\x1b[0m: ', path); //yellow | |
//Here is reference of colors and other characters: | |
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" | |
var colors={ | |
Reset: "\x1b[0m", | |
Red: "\x1b[31m", | |
Green: "\x1b[32m", | |
Yellow: "\x1b[33m" | |
}; | |
var infoLog = console.info; | |
var logLog = console.log; | |
var errorLog = console.error; | |
var warnLog = console.warn; | |
console.info= function(args) | |
{ | |
var copyArgs = Array.prototype.slice.call(arguments); | |
copyArgs.unshift(colors.Green); | |
copyArgs.push(colors.Reset); | |
infoLog.apply(null,copyArgs); | |
}; | |
console.warn= function(args) | |
{ | |
var copyArgs = Array.prototype.slice.call(arguments); | |
copyArgs.unshift(colors.Yellow); | |
copyArgs.push(colors.Reset); | |
warnLog.apply(null,copyArgs); | |
}; | |
console.error= function(args) | |
{ | |
var copyArgs = Array.prototype.slice.call(arguments); | |
copyArgs.unshift(colors.Red); | |
copyArgs.push(colors.Reset); | |
errorLog.apply(null,copyArgs); | |
}; | |
// examples | |
console.info("Numeros",1,2,3); | |
console.warn("pares",2,4,6); | |
console.error("reiniciandooo"); | |
const colors = { | |
Reset: "\x1b[0m", | |
Bright: "\x1b[1m", | |
Dim: "\x1b[2m", | |
Underscore: "\x1b[4m", | |
Blink: "\x1b[5m", | |
Reverse: "\x1b[7m", | |
Hidden: "\x1b[8m", | |
fg: { | |
Black: "\x1b[30m", | |
Red: "\x1b[31m", | |
Green: "\x1b[32m", | |
Yellow: "\x1b[33m", | |
Blue: "\x1b[34m", | |
Magenta: "\x1b[35m", | |
Cyan: "\x1b[36m", | |
White: "\x1b[37m", | |
Crimson: "\x1b[38m" //القرمزي | |
}, | |
bg: { | |
Black: "\x1b[40m", | |
Red: "\x1b[41m", | |
Green: "\x1b[42m", | |
Yellow: "\x1b[43m", | |
Blue: "\x1b[44m", | |
Magenta: "\x1b[45m", | |
Cyan: "\x1b[46m", | |
White: "\x1b[47m", | |
Crimson: "\x1b[48m" | |
} | |
}; | |
console.log(colors.bg.Blue, colors.fg.White , "I am white message with blue background", colors.Reset) ; | |
//don't forget "colors.Reset" to stop this color and return back to the default color | |
// ----- | |
// colors.js | |
const util = require('util') | |
function colorize (color, text) { | |
const codes = util.inspect.colors[color] | |
return `\x1b[${codes[0]}m${text}\x1b[${codes[1]}m` | |
} | |
function colors () { | |
let returnValue = {} | |
Object.keys(util.inspect.colors).forEach((color) => { | |
returnValue[color] = (text) => colorize(color, text) | |
}) | |
return returnValue | |
} | |
module.exports = colors() | |
// ----- | |
const colors = require('./colors') | |
console.log(colors.green("I'm green!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment