Skip to content

Instantly share code, notes, and snippets.

@sahilatahar
Last active June 15, 2024 11:40
Show Gist options
  • Save sahilatahar/a411036386bd68ca0f5c3e8a9631920e to your computer and use it in GitHub Desktop.
Save sahilatahar/a411036386bd68ca0f5c3e8a9631920e to your computer and use it in GitHub Desktop.
This script demonstrates the use of ANSI escape codes to style text output in the console. It includes examples of various text styles such as foreground and background colors, as well as text formatting options like bold, italic, underline, strikethrough, and more.
// ANSI escape codes for styling
const styles = {
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",
bold: "\x1b[1m",
italic: "\x1b[3m",
underline: "\x1b[4m",
strikethrough: "\x1b[9m",
invisible: "\x1b[8m",
}
function printStyledText(text, style) {
console.log(style + text + styles.reset)
}
// Example of each style
Object.keys(styles).forEach((style) => {
printStyledText(`Example of ${style}: ${style}`, styles[style])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment