Change console forground to Red or Green

L

Laup-Dawg

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...
 
M

Michal Suchanek

2009/8/5 Laup-Dawg said:
Hi All,

I'm looking for a way to change a Window's console foreground color in
Ruby. =C2=A0I have a script that outputs passes and fails and I would lik= e
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...

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
 
T

Thomas Chust

2009/8/5 Laup-Dawg said:
[...]
I'm looking for a way to change a Window's console foreground color in
Ruby. =A0I 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


--=20
When C++ is your hammer, every problem looks like your thumb.
 
G

Gordon Thiesfeld

Hi All,

I'm looking for a way to change a Window's console foreground color in
Ruby. =A0I 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...

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

gem in win32console
 
L

Laup-Dawg

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
 
T

Thomas Chust

2009/8/6 Laup-Dawg said:
[...]
This is what I saw in the window's command window after running the
script.

"The test =E2=86=90[31mFailed=E2=86=90[0m"

The foreground color didn't change to red, it remained the default.
[...]

Hello,

as Gordon Thiesfeld 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


--=20
When C++ is your hammer, every problem looks like your thumb.
 
M

Michal Suchanek

2009/8/6 Thomas Chust said:
2009/8/6 Laup-Dawg said:
[...]
This is what I saw in the window's command window after running the
script.

"The test =E2=86=90[31mFailed=E2=86=90[0m"

The foreground color didn't change to red, it remained the default.
[...]

Hello,

as Gordon Thiesfeld 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.

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
 
L

Laup-Dawg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top