# Win32console changes Windows cmd.exe colors

**URL:** <https://rubytalk.org/t/win32console-changes-windows-cmd-exe-colors/60592>\
**Category:** ruby-talk\
**Created:** [25 October 2010 08:10 UTC](https://rubytalk.org/t/win32console-changes-windows-cmd-exe-colors/60592 "2010-10-25T08:10:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_G](https://avatars.discourse-cdn.com/v4/letter/j/c5a1d2/32.png) [@John\_G](https://rubytalk.org/u/John_G)\
**Post date:** [25 October 2010 08:10 UTC](https://rubytalk.org/t/win32console-changes-windows-cmd-exe-colors/60592/1 "2010-10-25T08:10:56Z")

</div>

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 Thiesfeld in  
> post [Win32console 1.3.0.beta2 Released - Ruby - Ruby-Forum](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  
> .
> 
> > 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':  
> .
> 
> > 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)...  
> .
> 
> > 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?
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gordon\_Thiesfeld](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Gordon\_Thiesfeld](https://rubytalk.org/u/Gordon_Thiesfeld)\
**Post date:** [25 October 2010 15:14 UTC](https://rubytalk.org/t/win32console-changes-windows-cmd-exe-colors/60592/2 "2010-10-25T15:14:48Z")

</div>

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]

> <https://github.com/luislavena/win32console/blob/master/lib/Win32/Console/ANSI.rb#L162>

Gordon

> **···**
>
> On Mon, Oct 25, 2010 at 3:10 AM, John G. \<redspotcab@gmail.com\> 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?
