我有一个命令行程序,它将日志记录输出到屏幕上。
我希望错误行显示为红色。 我可以输出一些特殊的字符代码来将文本颜色切换为红色,然后再切换回白色吗?
我正在使用ruby,但我想这在任何其他语言中都一样。
就像是:
1 2 3 4 5
| red ="\\0123" # character code
white ="\\0223"
print"#{red} ERROR: IT BROKE #{white}"
print"other stuff" |
在Windows上,您可以通过以下三种方式轻松完成此操作:
1 2
| require 'win32console'
puts"\\e[31mHello, World!\\e[0m" |
现在您可以使用名为red的小方法扩展String
1 2 3 4 5 6 7 8
| require 'win32console'
class String
def red
"\\e[31m#{self}\\e[0m"
end
end
puts"Hello, World!".red |
您也可以像这样扩展String以获取更多颜色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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 |
或者,如果您可以安装gems:
1
| gem install term-ansicolor |
在您的程序中:
1 2 3 4 5 6 7 8 9 10
| 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 |
有关更多信息和可能的用法,请参阅文档以了解术语/杂色。
您需要访问[Win32控制台API](http://msdn.microsoft.com/zh-cn/library/ms682073(VS.85%29.aspx)。不幸的是,我不知道您会怎么做在Ruby中,我将使用Win32 :: Console模块。Windows控制台不响应ANSI转义码。
根据artur02提到的有关为Ruby输出着色的文章,您需要安装并加载win32console gem。
您可以在此处阅读精美的插图文章:
http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
我认为设置控制台文本颜色是特定于语言的。这是来自MSDN的C#示例:
1 2 3 4 5 6 7 8 9
| 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是用于设置文本颜色的属性。
我编写了一个跨平台的小程序,可以处理在MRI和JRuby下在Windows或POSIX系统上无缝运行的问题。
它没有依赖性,并且在POSIX系统上使用ANSI代码,在Windows上使用FFI(JRuby)或Fiddler(MRI)。
要使用它,只需:
1
| gem install color-console |
ColorConsole提供了使用Console.write和Console.puts函数以不同颜色输出文本行的方法。
1 2 3 4 5 6 7 8 9 10
| 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 |
请访问项目主页,网址为https://github.com/agardiner/color-console以获取更多详细信息。
color [background][foreground]
颜色定义如下:
1 2 3 4 5 6 7 8
| 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 |
例如,要将背景更改为蓝色,将前景更改为灰色,请输入:
color 18
在ANSI转义码上:
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
谢谢微软:-(
您可以使用ANSI转义序列,但是在现代Windows版本下,这将无法实现您想要的功能。维基百科上有一篇非常翔实的文章:
http://en.wikipedia.org/wiki/ANSI_escape_code
因此,您最初的问题的答案几乎肯定是"否"。但是,您可以更改前景色而无需编写转义序列,例如,通过调用Win32 API函数。我不知道该如何在Ruby中执行此类操作,但似乎有人设法做到了:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925
我想您想将4用于深红色,将12用于明亮的红色,将7用于恢复默认颜色。
希望这可以帮助!
多年来,我一直在使用一种称为裸尾(google it)的免费软件Windows tail程序,该程序可以让您执行Windows应用版本的unix tail命令。它使您可以根据定义的任何关键字为行着色。作为解决方案的好处是,它不受特定语言或设置等的限制,您只需定义配色方案及其类似驴金刚。在我个人的十大免费软件助手中!
最终,您需要调用SetConsoleTextAttribute。您可以从GetStdHandle获取控制台屏幕缓冲区句柄。
用于输出到命令行的标准C / C ++规范没有指定任何更改控制台窗口颜色的功能。就是说,Win32中有许多功能可以执行此操作。
更改Win32控制台颜色的最简单方法是通过iostream.h中的系统命令。此函数调用DOS命令。要更改颜色,我们将使用它来调用color命令。例如,system("Color F1");将使控制台变成白色的深蓝色。
DOS颜色
该命令可用的颜色是16种DOS颜色,每种颜色都用十六进制数字表示。第一个是背景,第二个是前景。
1 2 3 4 5 6 7 8
| 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 |
只需一点点颜色,即可使控制台程序在视觉上更令人愉悦。但是,"颜色"命令将更改整个控制台的颜色。要控制单个单元,我们需要使用windows.h中的函数。
为此,您需要使用SetConsoleAttribute函数
http://msdn.microsoft.com/en-us/library/ms686047.aspx
许多旧的ANSI颜色代码都可以使用。红色前景的代码类似于Escape- [31m。转义符是字符27,根据您的转义方案,它是" \ 033 [31m"或" \ x1B [31m""。
[39m是返回默认颜色的代码。
也可以一次指定多个代码以同时设置前景色和背景色。
您可能必须加载ANSI.sys,请参阅此页面。
您需要ANSI转义码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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();
}
}
} |
您可以使用简单的C#程序来更改颜色,http://powerof2games.com/node/31介绍了如何包装控制台输出以实现效果。
据我所知,使用命令行是不可能的,它只是一种颜色...