Hello everyone!

Would you like to print out a fancy terminal log for your Node.js applications? Check this out…

Colourful Terminal

In this post I am going to tell you how to do it. This can be useful especially if you need to show WARNINGS, ERRORS or PASSING TESTS. In my case, I have a Queue Engine feature to test. The system under test must download files from the servers with or without the Queue Engine up and running, so I decided to print out WARNINGS in yellow should the Engine be not enabled and GREEN logs should everything be setup as expected. For this purpose, we would like to use ANSI escape sequences:

const TEXT = "\[\033[32m\] Your_text_here \[\033[m\]";
console.log(TEXT);

Okay, but what the hell are those numbers and characters all together? Well, this is BASH Scripting. Non printing escape sequences in BASH must be enclosed by:

\[\033[ <AND> \]

Although, when changing the color  of the output, the escape sequence must contain the character “m” before closing:

\[\033[ <AND> m\]

As you might have guessed, the number 32 on the first example represents the ANSI color code. So if you run:

const TEXT = "\[\033[32m\] Your_text_here";
console.log(TEXT);

You are going to print out in GREEN, but the problem is, it never returns to the regular color because there is no escaping sequence to tell the computer to do so! Okay, so lets try to place another escape sequence at the end to tell the machine to return to standard color output, then:

const TEXT = "\[\033[32m\] Your_text_here \[\033[m\]";
console.log(TEXT);

Looks pretty neat, but not so much. Why? Because this way you are telling the computer to only print this message in GREEN and then return to standard again. A nice way to deal with this, if you want to declare CONSTANTS with escaping sequences to print colorful texts in the terminal, you can use a PLACEHOLDER – represented by %s to print a STRING. Now, you would like to have this in your JavaScript code:

//Constants
const MESSAGES = ["One message", "Another message"];
const GREEN = "\[\033[32m\] %s \[\033[m\]";

//Prints "One Message" in green
console.log(GREEN, MESSAGES[0]);

Okay, that is nice. But could get even better! You can send to the escaping sequence formatting, foreground color and background color, separated by semi-colons. For example:

//Constants
const MESSAGES = ["One message", "Another message"];
const BOLD_RED_YELLOW = "\[\033[1;31;43m\] %s \[\033[m\]";

//Prints "Another Message" in bold (1) red (31) over yellow (43) background
console.log(BOLD_RED_YELLOW, MESSAGES[1]);

Now you’ve got cool colours and styles for your terminal debugging! Now take a look at the following link to find any colour you like…

ANSI escape sequences

Text attributes
0 - All attributes off
1 - Bold on
4 - Underscore (on monochrome display adapter only)
5 - Blink on
7 - Reverse video on
8 - Concealed on
Foreground colors
30 - Black
31 - Red
32 - Green
33 - Yellow
34 - Blue
35 - Magenta
36 - Cyan
37 - White
Background colors
40 - Black
41 - Red
42 - Green
43 - Yellow
44 - Blue
45 - Magenta
46 - Cyan
47 - White

Hope you like that, leave your comment!

Best, Raf.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s