Change console forground to Red or Green

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…

2009/8/5 Laup-Dawg [email protected]:

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

2009/8/5 Laup-Dawg [email protected]:

[…]
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

On Wed, Aug 5, 2009 at 1:25 PM, Laup-Dawg[email protected] 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

On Aug 5, 12:43Â pm, Thomas C. [email protected] wrote:

2009/8/5 Laup-Dawg [email protected]:

[…]
 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

2009/8/6 Laup-Dawg [email protected]:

[…]
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 :wink:

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

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

2009/8/6 Thomas C. [email protected]:

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 (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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs