Win32console changes Windows cmd.exe colors

A problem has occurred with Windows Vista64 rspec since I upgraded to
rspec 2.0.1 and others. Before the upgrade, even with win32console
installed ‘C> rspec spec’ showed escape codes, but ‘rspec spec | cat’
(using cygwin/bin/cat/) showed colors fine–so I was happy. Now rspec
shows colors without having to pipe to cat, but the text output is
red/green/grey-on-black, regardless of my cmd.exe color properties, and
cmd.exe colors get changed to grey-on-black. Besides having to change
the colors back after rspec, red-on-black rspec text is not easy to
read.

Why do I blame win32console(1.3.0)? My thanks to Gordon T. in
post http://www.ruby-forum.com/topic/205569 for the following test case:

  1. Open a Windows cmd console, presumably not a black background,
    otherwise change by (say) ‘color f0’ (black on white). Execute
    .
    C> ruby -e “require ‘rubygems’; require ‘win32console’;
    %Q{\e[31mred\e[0m\e[32mgreen\e[0m}.each_byte{|i| putc i }”
    .
    Note the red-green output on a black background, and subsequent typing
    is grey-on-black. Reset the colors by executing ‘color f0’.
    .
  2. Now output the escape codes without ‘require win32console’:
    .
    C> ruby -e “%Q{\e[31mred\e[0m\e[32mgreen\e[0m }.each_byte{|i| putc i}”
    .
    Escape-codes are shown instead of colors, but cmd-window colors remain
    black-on-white. Some may deem this inferior to the previous result,
    but note that (using something like cygwin/bin/cat)…
    .
    C> ruby -e “%Q{\e[31mred\e[0m\e[32mgreen\e[0m hello}.each_byte{|i| putc
    i }” | cat
    .
    Yields a perfect red-green output and does not affect cmd-window colors.
    Far superior since rspec is usually executed from the command-history
    buffer. However, would it be possible to fix this issue so that a)
    cmd-window properties are not affected and b) cat is not needed, like
    now?

On Mon, Oct 25, 2010 at 3:10 AM, John G. [email protected] wrote:

buffer. However, would it be possible to fix this issue so that a)
cmd-window properties are not affected and b) cat is not needed, like
now?

I believe so. Currently, win32console makes assumptions about the
default
foreground and background colors[1]. This should be easy enough to
change.
The only part I’m not sure of is how to determine the defaults. It may
be
as simple as reading the registry for the current values. I’ll play
with
this a bit, and submit a patch if I come up with something.

[1]
http://github.com/luislavena/win32console/blob/master/lib/Win32/Console/ANSI.rb#L162

Gordon