可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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"
回答1:
You need to access the [Win32 Console API](http://msdn.microsoft.com/en-us/library/ms682073(VS.85%29.aspx). 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.
回答2:
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.
回答3:
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.
回答4:
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!
回答5:
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 :-(
回答6:
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
回答7:
I've authored a small cross-platform gem that handles this seamlessly running on Windows or POSIX-systems, under both MRI and JRuby.
It has no dependencies, and uses ANSI codes on POSIX systems, and either FFI (JRuby) or Fiddler (MRI) for Windows.
To use it, simply:
gem install color-console
ColorConsole provides methods for outputting lines of text in different colors, using the Console.write and Console.puts functions.
require 'color-console'
Console.puts "Some text" # Outputs text using the current console colours
Console.puts "Some other text", :red # Outputs red text with the current background
Console.puts "Yet more text", nil, :blue # Outputs text using the current foreground and a blue background
# The following lines output BlueRedGreen on a single line, each word in the appropriate color
Console.write "Blue ", :blue
Console.write "Red ", :red
Console.write "Green", :green
Visit the project home page at https://github.com/agardiner/color-console for more details.
回答8:
As far as I know it is not possible with a command line, it is just one color...
回答9:
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.
回答10:
You want ANSI escape codes.
回答11:
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
function
http://msdn.microsoft.com/en-us/library/ms686047.aspx
回答12:
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.
回答13:
Ultimately you need to call SetConsoleTextAttribute. You can get a console screen buffer handle from GetStdHandle.
回答14:
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!