我有一个命令行程序,它会在屏幕上输出日志信息。
我希望错误信息以红色显示。是否有一些特殊的字符代码可以输出来将文本颜色切换为红色,然后再切换回白色?
我正在使用Ruby,但我想这在任何其他语言中都是一样的。
类似于:
red = "\0123" # character code
white = "\0223"
print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"
我有一个命令行程序,它会在屏幕上输出日志信息。
我希望错误信息以红色显示。是否有一些特殊的字符代码可以输出来将文本颜色切换为红色,然后再切换回白色?
我正在使用Ruby,但我想这在任何其他语言中都是一样的。
类似于:
red = "\0123" # character code
white = "\0223"
print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"
在Windows上,你有三种简单的方法可以做到:
require 'win32console'
puts "\e[31mHello, World!\e[0m"
现在你可以使用一个叫做red
的小方法扩展String
require 'win32console'
class String
def red
"\e[31m#{self}\e[0m"
end
end
puts "Hello, World!".red
你也可以像这样扩展字符串,以获得更多的颜色:
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:
gem install term-ansicolor
在你的程序中:
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
请参考 term/ansicolor 文档以获取更多信息和可能的用法。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是用于设置文本颜色的属性。
颜色 [背景][前景]
其中颜色定义如下:
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 位字符模式(子系统:控制台)的 Windows 应用程序不会向控制台写入 ANSI 转义序列
它们必须解释转义码操作并调用本地的控制台 API
感谢微软 :-(
gem install color-console
ColorConsole提供了使用Console.write和Console.puts函数以不同颜色输出文本行的方法。
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以获取更多详细信息。
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描述了如何包装控制台输出以实现效果。
标准的C/C++命令行输出规范并没有指定更改控制台窗口颜色的任何功能。不过,在Win32中有许多函数可以实现这样的操作。
改变Win32控制台颜色最简单的方法是通过iostream.h中的system命令。该函数会调用DOS命令。为了改变颜色,我们将使用它来调用color命令。例如,system("Color F1");
将使控制台呈现深蓝底白字。
DOS颜色
可用于该命令的颜色是十六种DOS颜色,每种颜色都用一个十六进制数字表示。第一个数字代表背景色,第二个数字代表前景色。
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命令会改变整个控制台的颜色。要控制单个单元格,我们需要使用windows.h中的函数。
为了做到这一点,您需要使用SetConsoleAttribute
函数。
require 'win32console'
,因为“colorize”仅检查if RUBY_PLATFORM =~ /win32/
,而在某些情况下,RUBY_PLATFORM
返回i386-mingw32
。 - AZ.end
。 - anon