Posted on Nov 24, 2010

We all love colors

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.

Java, Perl and others all have their implementation.

In the case Ruby, it does too, as port from Perl’s Win32::Console module by Gonzalo Garramuno which then was packaged as gem by Justin Bailey and is currently maintained by me.

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:

https://github.com/adoxa/ansicon/downloads

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

Now that ansicon.exe and it’s support libraries are placed there, you can start it manually:

ansicon

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:

ruby hello.rb

On a normal prompt, we got something like this:

The output generated by the sample script without ansicon installed

But, powered by ansicon, we get this lovely result:

Enjoy the colors provided by ansicon!

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!

12 Comments

  • Hi!

    I’ve tried to use ANSICON as you recommended it on one of my threads in RSpec mailing list if you remember. Anyway, it has been working more or less fine, but i’ve encountered some command preprocessor (cmd.exe) crashes, which are happening intermittently. Unfortunately i don’t have any simple case to reproduce it all the time to give any valuable information or help…

    I’m using Windows 7 64bit and have ANSICON 1.3.0 installed with `ansicon.exe -i` (by the way you could mention it in your posting instead of pointing to README).

    Just wanted to let you know that there might be some problems with ANSICON also…

    I’m pretty sure that these crashes are related with ANSICON since they started to happen right after installing it and went away if i uninstalled it.

  • Luis says:

    Hello,

    Didn’t you report this issue?

    https://github.com/adoxa/ansicon/issues#issue/5

    Please upgrade to ANSICON 1.31, as I mentioned, 1.31 or higher is recommended since it corrected lot of issues related to 64bits.

    I could mention how to install it permanently, but that removes the fun of RTFM, don’t you think? :)

    RSpec and Cucumber are not yet ready to support ANSICON. I just tested a fork of Cucumber with the changes and worked as expected, but haven’t tested RSpec yet.

  • Nope, i didn’t report that issue. I misread from your post that the latest version is 1.3.0 and checked that i had the same. Hehe, my bad. I’ll give 1.3.1 a try and let you know if there’s any problems!

    I noticed that it had a note about fixing issues with multibyte characters. Does it mean that the problems with with UTF-8 characters are gone as written in one of the ANSICON’s RubyInstaller thread? Did someone get the bounty fee? :)

  • Luis says:

    Jason released 1.31 with ANSICON_API overrides:

    https://github.com/adoxa/ansicon/blob/v1.31/readme.txt#L72-89

    Which circumvents the issue of Ruby 1.9.x with UTF (chcp 65001).

    Jason earned the bounty :)

    And I’m working on getting a permanent fix on Ruby trunk.

  • Okay, i can now say that using 1.3.1 will still crash my command preprocessor occasionally. Any ideas or any way i could provide anything helpful to you?

  • Bryan Ash says:

    I know I’m arriving late to the game, but I’m just getting back into Cucumber on Windows and am missing my colored output.

    I’ve tried ANSICON versions 1.50, 1.40 and 1.32 all with the same result in Windows 7 (64 bit).
    If I run ANSICON manually in a CMD window, then “exit”, the window closes.
    If I run Cucumber version 1.1.4 after ANSICON it still tells me that I don’t have it.
    If I install ANSICON with a “-i” I then get no output from any Ruby execution. Echo, dir and others do produce output.

    I’m at a loss as to how to investigate this, any advice you might offer would be much appreciated.

  • Bryan Ash says:

    I found the answer to my woes.

    Github Issue #14 for describes a new option ANSICON_EXC that I set just as described there and I am colorful cuker once again.

    Thank you for you efforts in supporting Ruby on Windows!

  • [...] http://blog.mmediasys.com/2010/11/24/we-all-love-colors/ Share this:TwitterFacebookLike this:LikeBe the first to like this post. [...]

  • rking says:

    Also, it should be worth noting that Cygwin’s terminal is set up for this by default.

    Windows is beyond intolerable without Cygwin, in my opinion.

    -rking

  • Luis says:

    You shouldn’t mix cygwin with native Ruby. If you want cygwin, use cygwin provided Ruby.

  • Andres Riofrio says:

    Please update your link to ANSICON (http://adoxa.110mb.com/ansicon/index.html), which is down. https://github.com/adoxa/ansicon/ seems like the best replacement.

  • Luis says:

    Thanks Andres, links updated.