Indeed, pretty much everybody will agree with this statement, even developers
like them, and of course, want to use them.
Is quite common use coloring in the output of your tools to indicate an error
or a success, or some sort of information.
To make things easy, there is something called ANSI escape sequences which has great support on Unix systems, but not Windows.
Back in DOS days, there was something called
ANSI.SYS which allowed certain ANSI codes to work.
But DOS has been dead for quite some time, and Windows (32 and 64 bits) had no more this nice ANSI support.
Instead, Microsoft created a lot of functions that make it more easy to use screen buffers, flip between them to avoid flickering and other stuff, but we (developers) loose the ability to use colors across platforms transparently from the code perspective.
Because of that, and because we are eager to use colors (remember, we all love colors), developers created libraries on every language that mimic ANSI sequences on Windows using the Win32 console functions.
With the move of Ruby to version 1.9.x, certain changes were required and still suffer from some compatibility issues, speed been one.
Thanks to the Internet, we found a little gem hidden on the tubes called “ANSICON” created by Jason Hood, which provides — in C — ANSI escaping codes completely independent of your language of choice.
ANSICON works hooking your console process and any program, extracting any ANSI sequence automatically and translating to the proper Win32 console API.
It works under 32 and 64 bits systems.
What all that means to me?
I proposed the deprecation of win32console and replace it with ANSICON, but what are the benefits of it?
- Proven to work under several scenarios
- Its written in C, not Ruby, which make it available to any other language
- Its faster than win32console
- One less gems for me to maintain
Now, how to use this?
Well, projects like RSpec and Cucumber haven’t still been adapted to use ANSICON yet, but developers are working to provide the changes.
First, you will need to visit ANSICON GitHub page and download the binaries:
Grab at least package (1.31) and extract its contents somewhere (newer is better).
Depending on your system architecture, copy the contents of x86 (32bits) or x64 (64bits) to a folder that you have in your PATH. On previous post I’ve recommended have a
Tools\bin folder inside your HOME. See my binfiles README for suggestions
ansicon.exe and it’s support libraries are placed there, you can start it manually:
That will simply create a new
cmd.exe with ANSICON hooks enabled. At any time you can type
exit and return to your previous
cmd.exe process that is clean of ansicon.
I will recommend you read the README file that comes with ANSICON to know about permanently install ANSICON in your system.
Now, let’s get back to an example:
puts "\e[34mHello \e[31mWorld\e[0m"
In this case, I entered manually the escape codes. If you’re serious for colors, take a look to term-ansicolor
Anyhow, now let’s execute our program:
On a normal prompt, we got something like this:
But, powered by ansicon, we get this lovely result:
Now, extra to that, ANSICON advertise certain information into the ANSICON environment variable
C:\Users\Luis>ECHO %ANSICON% 100x3000 (100x28)
In that variable, we have 4 values. The first two are buffer dimensions and the ones between parenthesis are the size of my visible console.
For example, you can use the visible size information to truncate rows of information.
So what, why care?
Unless you suffer from color blind-ness, colors are useful. Tools like RSpec communicate visually errors from other states using colors.
ANSICON offers a less intrusive approach than previous solutions for coloring, less work for me and more fun for you!
Start using it today!