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.
我正在使用ruby,但我想在任何其他语言中这都是一样的。
Something like:
喜欢的东西:
red = "\0123" # character code
white = "\0223"
print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"
14 个解决方案
#1
6
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.
您需要访问[Win32控制台API](http://msdn.microsoft.com/en-us/library/ms682073)。不幸的是,我不知道你如何从Ruby中做到这一点。在Perl中,我将使用Win32::控制台模块。Windows控制台不响应ANSI转义代码。
According to the article on colorizing Ruby output that artur02 mentioned, you need to install & load the win32console gem.
根据artur02所提到的关于着色Ruby输出的文章,您需要安装和加载win32console gem。
#2
24
On windows, you can do it easily in three ways:
在windows上,你可以用三种方式轻松完成:
require 'win32console'
puts "\e[31mHello, World!\e[0m"
Now you could extend String with a small method called red
现在可以用一个名为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:
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.
有关更多信息和可能的用法,请参阅术语/ansicolor文档。
#3
3
You can read here a good and illustrated article: http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
您可以在这里阅读一篇很好的插图文章:http://kpumuk.info/ruby-on-rails/colorizing- consoly -ruby- scrip- output/
I think setting console text color is pretty language-specific. Here is an example in C# from MSDN:
我认为设置控制台文本颜色是非常特定于语言的。下面是MSDN c#中的一个例子:
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
2
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:
您可以使用ANSI转义序列,但是在Windows的现代版本中,这并不能实现您想要的效果。*有一篇非常有用的文章:
http://en.wikipedia.org/wiki/ANSI_escape_code
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:
所以你最初问题的答案几乎肯定是否定的。"但是,您可以在不编写转义序列的情况下更改前景颜色,例如通过调用Win32 API函数。我不知道如何在我的头顶上做这类事情,但其他人似乎已经做到了:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925
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.
我想你可能想用4表示暗红色,12表示亮红色,7表示恢复默认颜色。
Hope this helps!
希望这可以帮助!
#5
2
on ANSI escape codes:
在ANSI转义代码:
32-bit character-mode (subsystem:console) Windows applications don't write ANSI escape sequences to the console
32位字符模式(子系统:控制台)Windows应用程序不会将ANSI转义序列写入控制台。
They must interpret the escape code actions and call the native Console API instead
它们必须解释转义代码动作并调用本机控制台API
Thanks microsoft :-(
由于微软:-(
#6
2
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
颜色18
#7
2
I've authored a small cross-platform gem that handles this seamlessly running on Windows or POSIX-systems, under both MRI and JRuby.
我编写了一个小型的跨平台gem,可以在Windows或posix系统上,在MRI和JRuby下无缝运行。
It has no dependencies, and uses ANSI codes on POSIX systems, and either FFI (JRuby) or Fiddler (MRI) for Windows.
它没有依赖项,在POSIX系统上使用ANSI代码,在Windows上使用FFI (JRuby)或Fiddler (MRI)。
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.
ColorConsole提供使用控制台输出不同颜色的文本行。编写和控制台。将功能。
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.
请访问项目主页https://github.com/agardiner/color console,了解更多细节。
#8
0
As far as I know it is not possible with a command line, it is just one color...
就我所知,用命令行是不可能的,它只是一种颜色……
#9
0
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.
您可以使用一个简单的c#程序来更改颜色,http://powerof2games.com/node/31描述了如何封装控制台输出来实现这种效果。
#11
0
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.
向命令行输出的标准C/ c++规范没有指定任何更改控制台窗口颜色的功能。也就是说,在Win32中有很多功能可以做这样的事情。
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.
更改Win32控制台颜色的最简单方法是通过iostream.h中的系统命令。该函数调用DOS命令。为了改变颜色,我们将使用它来调用颜色命令。例如,系统(“颜色F1”);将使控制台在白色上变为深蓝色。
DOS Colors
DOS的颜色
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.
这小小的色彩让控制台程序更加赏心悦目。但是,颜色命令将更改整个控制台的颜色。为了控制单个的单元,我们需要使用window。h的函数。
Do do that you need to use the SetConsoleAttribute
function
需要使用SetConsoleAttribute函数吗
http://msdn.microsoft.com/en-us/library/ms686047.aspx
http://msdn.microsoft.com/en-us/library/ms686047.aspx
#12
0
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.
很多古老的ANSI颜色代码都可以工作。红色前景的代码类似Escape- 31m。Escape是字符27,所以是“\033[31m]”或“\x1B[31m",这取决于您的转义方案。
[39m is the code to return to default color.
[39m是返回默认颜色的代码。
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.
你可能需要装载ANSI。sys,看到这个页面。
#13
0
Ultimately you need to call SetConsoleTextAttribute. You can get a console screen buffer handle from GetStdHandle.
最终需要调用SetConsoleTextAttribute。您可以从GetStdHandle获得控制台屏幕缓冲区句柄。
#14
0
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!
我一直在使用一个名为baretail(谷歌it)的免费软件windows tail程序,它允许您执行unix tail命令的windows化版本。它使您可以根据定义的关键字对行进行着色。作为一种解决方案,它的好处在于它不依赖于特定的语言或设置,等等,你只需要定义你的配色方案,就像驴子金刚一样。在我个人的十大免费软件助手!
#1
6
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.
您需要访问[Win32控制台API](http://msdn.microsoft.com/en-us/library/ms682073)。不幸的是,我不知道你如何从Ruby中做到这一点。在Perl中,我将使用Win32::控制台模块。Windows控制台不响应ANSI转义代码。
According to the article on colorizing Ruby output that artur02 mentioned, you need to install & load the win32console gem.
根据artur02所提到的关于着色Ruby输出的文章,您需要安装和加载win32console gem。
#2
24
On windows, you can do it easily in three ways:
在windows上,你可以用三种方式轻松完成:
require 'win32console'
puts "\e[31mHello, World!\e[0m"
Now you could extend String with a small method called red
现在可以用一个名为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:
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.
有关更多信息和可能的用法,请参阅术语/ansicolor文档。
#3
3
You can read here a good and illustrated article: http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
您可以在这里阅读一篇很好的插图文章:http://kpumuk.info/ruby-on-rails/colorizing- consoly -ruby- scrip- output/
I think setting console text color is pretty language-specific. Here is an example in C# from MSDN:
我认为设置控制台文本颜色是非常特定于语言的。下面是MSDN c#中的一个例子:
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
2
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:
您可以使用ANSI转义序列,但是在Windows的现代版本中,这并不能实现您想要的效果。*有一篇非常有用的文章:
http://en.wikipedia.org/wiki/ANSI_escape_code
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:
所以你最初问题的答案几乎肯定是否定的。"但是,您可以在不编写转义序列的情况下更改前景颜色,例如通过调用Win32 API函数。我不知道如何在我的头顶上做这类事情,但其他人似乎已经做到了:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925
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.
我想你可能想用4表示暗红色,12表示亮红色,7表示恢复默认颜色。
Hope this helps!
希望这可以帮助!
#5
2
on ANSI escape codes:
在ANSI转义代码:
32-bit character-mode (subsystem:console) Windows applications don't write ANSI escape sequences to the console
32位字符模式(子系统:控制台)Windows应用程序不会将ANSI转义序列写入控制台。
They must interpret the escape code actions and call the native Console API instead
它们必须解释转义代码动作并调用本机控制台API
Thanks microsoft :-(
由于微软:-(
#6
2
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
颜色18
#7
2
I've authored a small cross-platform gem that handles this seamlessly running on Windows or POSIX-systems, under both MRI and JRuby.
我编写了一个小型的跨平台gem,可以在Windows或posix系统上,在MRI和JRuby下无缝运行。
It has no dependencies, and uses ANSI codes on POSIX systems, and either FFI (JRuby) or Fiddler (MRI) for Windows.
它没有依赖项,在POSIX系统上使用ANSI代码,在Windows上使用FFI (JRuby)或Fiddler (MRI)。
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.
ColorConsole提供使用控制台输出不同颜色的文本行。编写和控制台。将功能。
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.
请访问项目主页https://github.com/agardiner/color console,了解更多细节。
#8
0
As far as I know it is not possible with a command line, it is just one color...
就我所知,用命令行是不可能的,它只是一种颜色……
#9
0
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.
您可以使用一个简单的c#程序来更改颜色,http://powerof2games.com/node/31描述了如何封装控制台输出来实现这种效果。
#10
#11
0
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.
向命令行输出的标准C/ c++规范没有指定任何更改控制台窗口颜色的功能。也就是说,在Win32中有很多功能可以做这样的事情。
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.
更改Win32控制台颜色的最简单方法是通过iostream.h中的系统命令。该函数调用DOS命令。为了改变颜色,我们将使用它来调用颜色命令。例如,系统(“颜色F1”);将使控制台在白色上变为深蓝色。
DOS Colors
DOS的颜色
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.
这小小的色彩让控制台程序更加赏心悦目。但是,颜色命令将更改整个控制台的颜色。为了控制单个的单元,我们需要使用window。h的函数。
Do do that you need to use the SetConsoleAttribute
function
需要使用SetConsoleAttribute函数吗
http://msdn.microsoft.com/en-us/library/ms686047.aspx
http://msdn.microsoft.com/en-us/library/ms686047.aspx
#12
0
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.
很多古老的ANSI颜色代码都可以工作。红色前景的代码类似Escape- 31m。Escape是字符27,所以是“\033[31m]”或“\x1B[31m",这取决于您的转义方案。
[39m is the code to return to default color.
[39m是返回默认颜色的代码。
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.
你可能需要装载ANSI。sys,看到这个页面。
#13
0
Ultimately you need to call SetConsoleTextAttribute. You can get a console screen buffer handle from GetStdHandle.
最终需要调用SetConsoleTextAttribute。您可以从GetStdHandle获得控制台屏幕缓冲区句柄。
#14
0
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!
我一直在使用一个名为baretail(谷歌it)的免费软件windows tail程序,它允许您执行unix tail命令的windows化版本。它使您可以根据定义的关键字对行进行着色。作为一种解决方案,它的好处在于它不依赖于特定的语言或设置,等等,你只需要定义你的配色方案,就像驴子金刚一样。在我个人的十大免费软件助手!