Search code examples
windowsrubycommand-linecolors

How can I change the text color in the windows command prompt


I have a command line program, which outputs logging to the screen.

I want error lines to show up in red. Is there some special character codes I can output to switch the text color to red, then switch it back to white?

I'm using ruby but I imagine this would be the same in any other language.

Something like:

red = "\0123" # character code
white = "\0223"

print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"

Solution

  • You need to access the Win32 Console API. Unfortunately, I don't know how you'd do that from Ruby. In Perl, I'd use the Win32::Console module. The Windows console does not respond to ANSI escape codes.

    According to the article on colorizing Ruby output that artur02 mentioned, you need to install & load the win32console gem.