Colorize your Windows Console output

15. October 2009 in General

Update: The code-changes are included in symfony by now. If you use 1.3 or higheryou will not need to patch any symfony files.

If you are one of the developers, that is “forced” to use Windows on your development machine, you probably wish yourself a more comfortable console window. The windows one just missing all features I wish me for developing with Symfony. A good copy&paste, no colored output, no tabs… I know that many developers are using cygwin to get arround of such problem, but cygwin and I will never get the best friends…

Today I came across AnsiCon (http://adoxa.110mb.com/ansicon/index.html). This small utility helps the Windows commandline to understand ANSI-Color-Codes and other escape characters. The installation is pretty easy, just copy the ansi.dll and ansicon.exe in a folder of your machine (i.e. c:\ansicon). After that set the PATH-Environment-Variable in the system settings, add the ansicom path there. Afterwards start ansicon with “ansicon.exe -i”. This installs a registry setting. Thats it!

ANSI-Colors and escape characters are now interpreted correctly. But there is a problem for Symfony-Developers: The Symfony classes with Console output doesn’t output ANSI-Colors on Windows :-(

At the moment the only possibility is to change the Symfony Sourcecode. This is a bad thing. You never should change the framework sourcecode. Therefore I opened a ticket in the Symfony-Wiki (http://trac.symfony-project.org/ticket/7359). If you lin to have this function added to symfony just support this in the ticket.

If you like to test the colorization for yourself open the lib/vendor/lime/lime.php and change line421 from

if (DIRECTORY_SEPARATOR == '\\' ||
   !function_exists('posix_isatty') ||
   !@posix_isatty(STDOUT))

to

if (is_null(getenv('ANSICON')) && (DIRECTORY_SEPARATOR == '\\' ||
   !function_exists('posix_isatty') ||
   !@posix_isatty(STDOUT)))

When you call “symfony test:unit” you will get a result like the one in the screenshot:

console.png

The other file that needs to be changed is the  libn/commands/sfAnsiColorFormatter.class.php. There you need to change line 145 to:

return !is_null(getenv('ANSICON')) ||
  ( DIRECTORY_SEPARATOR != '\\' &&
      function_exists('posix_isatty') &&
     @posix_isatty($stream) );

That’s it! Now the symfony tasks (list, help…) will show their results in color :-)

10 responses to Colorize your Windows Console output

  1. Thanks Timo, i was looking for ANSI support in the windows console. It works beautifully with ansicon :)

    Btw.: in symfony 1.3 you can use the –color parameter to force ANSI color output. If you modify your symfony.bat with that information you even get colored output by default :D

  2. @Kai: One more reason for switching to 1.3 after release :-)

  3. It looks like ansicon doesn’t register properly on Vista with UAC but launching it manually after opening the command line works great.

    Thanks for this little piece of valuable information!

  4. Interesting ! That’s true that the window console really miss colors. It’s always important to see “green” for tests results. :)

  5. Does ansicon work with Windows7 x64? I tried installing it and it still won’t interpret the ansi escape codes.

  6. Does ansicon work on Windows 7?

  7. never tried it so far :-(

  8. LP said on 13. May 2010

    thanx, i was searching for this tool!, now it’ll be more funny testing!

  9. Hi!

    Thank you for the tip!

    It works quite well with Console 2 (http://sourceforge.net/projects/console/) .

    []s
    Paulo

  10. Hello! Is there any solution for do this in symfony2? I’ve triedinstalling cygwin but it doesn`t works.

Leave a reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>