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"
Once in the PowerShell settings, go to the 'Appearance' tab. You can now change the text color in Windows Terminal using the first drop-down menu. You can also set the font face, font size, cursor style, and more here. Be sure to hit 'Save' after making the changes.
Right Click the top bar of the Command Prompt Window (i.e. Right Click the words "Command Prompt" on the top left) and select Properties. There you can change Font, Size, Color and other options.
Use Commands Go to Start button, type 'CMD' in the search box to open Command Prompt. Command Prompt window will open, type “color/?” You will get the color codes. To get the color of the background and font changed, use the color command option along with the background color code followed by the font color code.
Select the text that you want to change. On the Home tab, in the Font group, choose the arrow next to Font Color, and then select a color. You can also use the formatting options on the Mini toolbar to quickly format text.
On windows, you can do it easily in three ways:
require 'win32console'
puts "\e[31mHello, World!\e[0m"
Now you could extend String with a small method called red
require 'win32console'
class String
def red
"\e[31m#{self}\e[0m"
end
end
puts "Hello, World!".red
Also you can extend String like this to get more colors:
require 'win32console'
class String
{ :reset => 0,
:bold => 1,
:dark => 2,
:underline => 4,
:blink => 5,
:negative => 7,
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
}.each do |key, value|
define_method key do
"\e[#{value}m" + self + "\e[0m"
end
end
end
puts "Hello, World!".red
Or, if you can install gems:
gem install term-ansicolor
And in your program:
require 'win32console'
require 'term/ansicolor'
class String
include Term::ANSIColor
end
puts "Hello, World!".red
puts "Hello, World!".blue
puts "Annoy me!".blink.yellow.bold
Please see the docs for term/ansicolor for more information and possible usage.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With