# Change console forground to Red or Green

**URL:** <https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258>\
**Category:** Ruby\
**Created:** [August 5, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258 "2009-08-05T20:25:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 5, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/1 "2009-08-05T20:25:35Z")

</div>

Hi All,

I’m looking for a way to change a Window’s console foreground color in  
Ruby. I have a script that outputs passes and fails and I would like  
to know how to change the text to green or red specfically.

I don’t even mind hard coding the console color code values (escape  
sequences), since those are the only two colors I’m planning on using.

Any help would be appreciated…

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 5, 2009, 9:37pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/2 "2009-08-05T21:37:34Z")

</div>

2009/8/5 Laup-Dawg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 

You might be able to do that with the ruby curses extension but on  
windows you cannot change the console colors by simply printing some  
escape sequences.

If you do not insist on using console it should be simple to get this  
functionality in a win32 application, tk, HTML + browser, …

HTH

Michal

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 5, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/3 "2009-08-05T21:44:00Z")

</div>

2009/8/5 Laup-Dawg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> […]  
> I’m looking for a way to change a Window’s console foreground color in  
> Ruby. I have a script that outputs passes and fails and I would like  
> to know how to change the text to green or red specfically.  
> […]

Hello,

if you want complex text user interface functionality, you can use  
Curses. If you just want to color some of the output, the  
term-ansicolor gem may be handy – try something like this:

require ‘term/ansicolor’  
puts 'The test ’ + Term::ANSIColor.red(‘failed’)

cu,  
Thomas

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 5, 2009, 10:16pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/4 "2009-08-05T22:16:47Z")

</div>

On Wed, Aug 5, 2009 at 1:25 PM, Laup-Dawg[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Windows cmd.exe doesn’t support ANSI escape sequences out of the box.  
But the win32console gem will do the trick.

gem in win32console

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 6, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/5 "2009-08-06T19:25:50Z")

</div>

On Aug 5, 12:43Â pm, Thomas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2009/8/5 Laup-Dawg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> […]  
> Â require ‘term/ansicolor’  
> Â puts 'The test ’ + Term::ANSIColor.red(‘failed’)  
> […]  
> Hi Thomas, thanks for your quick reply.

After installing the term-ansicolor gem, I copied and pasted your code  
sample.

This is what I saw in the window’s command window after running the  
script.

“The test â†[31mFailedâ†[0m”

The foreground color didn’t change to red, it remained the default.

Am I missing something obvious? Thanks again for your replies!

Adam

> When C++ is your hammer, every problem looks like your thumb.  
> haha…that’s a great quote

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 6, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/6 "2009-08-06T19:58:03Z")

</div>

2009/8/6 Laup-Dawg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> […]  
> This is what I saw in the window’s command window after running the  
> script.
> 
> “The test â†[31mFailedâ†[0m”
> 
> The foreground color didn’t change to red, it remained the default.  
> […]

Hello,

as Gordon T. already mentioned in this thread, the Windows  
console doesn’t support ANSI escape sequences natively and that the  
win32console gem could be useful to emulate such functionality. The  
output you see includes the uninterpreted escape sequences verbatim –  
so it actually looks “right” in a certain sense 😉

I faintly remember that in the DOS and Windows 95 days you also had  
the option to load a driver that globally taught the console to  
display ANSI escapes, but I have no idea whether something similar is  
still available for modern Windows systems. I haven’t touched a  
Windows box in ages, so I don’t have any hands-on experience in this  
area.

cu,  
Thomas

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 7, 2009, 4:35am UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/7 "2009-08-07T04:35:59Z")

</div>

Thank you everyone for your responses.  
Just for the record, so someone looking for a similar solution can  
find it, I used the following code:

require ‘win32console’  
require ‘term/ansicolor’

include Win32::Console::ANSI  
include Term::ANSIColor  
puts red  
puts “hello, red world”

# or you can modify the string class itself

class String  
include Term::ANSIColor  
end  
puts “hello, blue world”.blue

(Ruby, Windows Vista 64bit, cmd.exe)

Adam

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [August 6, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/change-console-forground-to-red-or-green/173258/8 "2009-08-06T23:53:56Z")

</div>

2009/8/6 Thomas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hello,  
> still available for modern Windows systems. I haven’t touched a  
> Windows box in ages, so I don’t have any hands-on experience in this  
> area.

My hands-on experience suggests that this driver still exists but is  
useless. It somehow interprets the output of echo commands (and not  
ruby scripts) and works in [command.com](http://command.com) (and not in cmd.exe).

That’s why I suggest using a different windows-native method rather  
than escape sequences.

Of course, you can also install mingw with its terminal emulation and  
you could possibly get colours in there. But it’s no longer windows  
console any more than a tk application.

The curses extension for windows should also have colours. IIRC it is  
possible to set colours using some Windows console API which is  
probably what win32console and pdcurses does.

HTH

Michal
