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"
-
As far as I know it is not possible with a command line, it is just one color...
From Mitchel Sellers -
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Console_Test { class Program { static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Hello World"); Console.ReadKey(); } } }
You can change the color using a simple C# program, http://powerof2games.com/node/31 describes how you can wrap console output to achieve the effect.
From betelgeuce -
You want ANSI escape codes.
Mark Tomlin : ANSI escape codes not not available on the Windows Command Prompt, natively.From Adam Rosenfield -
A lot of the old ANSI Color Codes work. The code for a red foreground is something like Escape-[31m. Escape is character 27, so that's "\033[31m" or "\x1B[31m", depending on your escaping scheme.
[39m is the code to return to default color.
It's also possible to specify multiple codes at once to set foreground and background color simultaneously.
You may have to load ANSI.sys, see this page.
From davenpcj -
You can read here a good and illustrated article: http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
I think setting console text color is pretty language-specific. Here is an example in C# from MSDN:
for (int x = 0; x < colorNames.Length; x++) { Console.Write("{0,2}: ", x); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]); Console.Write("This is foreground color {0}.", colorNames[x]); Console.ResetColor(); Console.WriteLine(); }
Console.ForegroundColor is the property for setting text color.
From artur02 -
The standard C/C++ specification for outputting to the command line doesn't specify any capabilities for changing the color of the console window. That said, there are many functions in Win32 for doing such a thing.
The easiest way to change the color of the Win32 console is through the system command in iostream.h. This function invokes a DOS command. To change colors, we will use it to invoke the color command. For example,
system("Color F1");
will make the console darkblue on white.DOS Colors
The colors available for use with the command are the sixteen DOS colors each represented with a hex digit. The first being the background and the second being the foreground.
0 = Black 8 = Gray 1 = Blue 9 = Light Blue 2 = Green A = Light Green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White
Just this little touch of color makes console programs more visually pleasing. However, the Color command will change the color of the entire console. To control individual cells, we need to use functions from windows.h.
Do do that you need to use the
SetConsoleAttribute
functionhttp://msdn.microsoft.com/en-us/library/ms686047.aspx">http://msdn.microsoft.com/en-us/library/ms686047.aspx
From Orion Adrian -
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.
From cjm -
Ultimately you need to call SetConsoleTextAttribute. You can get a console screen buffer handle from GetStdHandle.
From Mike Dimmick -
You could use an ANSI escape sequence, but that won't do what you want under modern versions of Windows. Wikipedia has a very informative article:
http://en.wikipedia.org/wiki/ANSI_escape_code
So the answer to your original question is almost certainly "no." However, you can change the foreground color without writing an escape sequence, for example by invoking a Win32 API function. I don't know how to do this sort of thing in Ruby off the top of my head, but somebody else seems to have managed:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925
I imagine you'd want to use 4 for dark red or 12 for bright red, and 7 to restore the default color.
Hope this helps!
From zaphod -
on ANSI escape codes:
32-bit character-mode (subsystem:console) Windows applications don't write ANSI escape sequences to the console
They must interpret the escape code actions and call the native Console API instead
Thanks microsoft :-(
From Orion Edwards -
color [background][foreground]
Where colors are defined as follows:
0 = Black 8 = Gray 1 = Blue 9 = Light Blue 2 = Green A = Light Green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White
For example, to change the background to blue and the foreground to gray, you would type:
color 18
From Michael Angstadt -
I've been using a freeware windows tail program called baretail (google it) for ages that lets you do a windows-appified version of unix tail command. It lets you colorize lines dependent on whatever keywords you define. What's nice about it as a solution is its not tied to a specific language or setup, etc, you just define your color scheme and its on like donkey kong. In my personal top 10 freeware helpers!
From Mike Duncan -
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 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.
From manveru
0 comments:
Post a Comment