Created
December 11, 2012 19:25
-
-
Save ValeriiVasin/4261265 to your computer and use it in GitHub Desktop.
Colorized output in nodejs
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
var color, i; | |
// Notice: octal literals are not allowed in strict mode. | |
function colorize(color, output) { | |
return ['\033[', color, 'm', output, '\033[0m'].join(''); | |
} | |
for (i = 0; i < 100; i += 1) { | |
color = Math.random() > 0.9 ? 91 : 92; // 91 - red, 92 - green | |
process.stdout.write( colorize(color, '●') ); | |
} | |
process.stdout.write('\n'); |
For use in strict mode, instead of using the \033
escape mode, \x1b
can be used:
Escape sequence | Text attributes |
---|---|
\x1b[0m | All attributes off (reset) |
\x1b[1m | Bold on(enable foreground intensity) |
\x1b[4m | Underline on |
\x1b[5m | Blink on (enable background intensity) |
\x1b[21m | Bold off (disable foreground intensity) |
\x1b[24m | Underline off |
\x1b[25m | Blink off (disable background intensity) |
Escape sequence | Foreground colors |
---|---|
\x1b[30m | Black |
\x1b[31m | Red |
\x1b[32m | Green |
\x1b[33m | Yellow |
\x1b[34m | Blue |
\x1b[35m | Magenta |
\x1b[36m | Cyan |
\x1b[37m | White |
\x1b[39m | Default (foreground color at startup) |
\x1b[90m | Light Gray |
\x1b[91m | Light Red |
\x1b[92m | Light Green |
\x1b[93m | Light Yellow |
\x1b[94m | Light Blue |
\x1b[95m | Light Magenta |
\x1b[96m | Light Cyan |
\x1b[97m | Light White |
Escape sequence | Background colors |
---|---|
\x1b[40m | Black |
\x1b[41m | Red |
\x1b[42m | Green |
\x1b[43m | Yellow |
\x1b[44m | Blue |
\x1b[45m | Magenta |
\x1b[46m | Cyan |
\x1b[47m | White |
\x1b[49m | Default (background color at startup) |
\x1b[100m | Light Gray |
\x1b[101m | Light Red |
\x1b[102m | Light Green |
\x1b[103m | Light Yellow |
\x1b[104m | Light Blue |
\x1b[105m | Light Magenta |
\x1b[106m | Light Cyan |
\x1b[107m | Light White |
Source: https://gist.github.com/raghav4/48716264a0f426cf95e4342c21ada8e7
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice !!