我如何可以改变的文字颜色的窗户命令的提示
-
09-06-2019 - |
题
我有一个命令行程序,其输出记录到屏幕上。
我想要的错误行为显示红色的。是有一些特殊字符码我可以输出到开关的文字颜色的红,然后开回到白色的?
我用红宝石但是我想这将是相同在任何其他语言。
是这样的:
red = "\0123" # character code
white = "\0223"
print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"
解决方案
你需要访问的[Win32控制台API](http://msdn.microsoft.com/en-us/library/ms682073(VS。85%29.aspx).不幸的是,我不知道你如何做到这一点从红宝石。在Perl,我会使用的 Win32::控制台 模块。窗台不应对ANSI逃码。
根据 文章在着色红宝石的输出 这artur02提到,需要安装和装载win32console宝石。
其他提示
在windows,你可以很容易地做到这一点在三个方面:
require 'win32console'
puts "\e[31mHello, World!\e[0m"
现在你可以延长串与一个小小的方法称为 red
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
或者,如果您能安排:
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
请参阅的文档期/ansicolor更多的信息和可能的使用。
你可以阅读这里一个很好的和说明的文章:http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
我认为设置控制台文本的颜色是漂亮的语言,特定的。这里是一个例子,在C#从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();
}
控制台。ForegroundColor 是的财产设定文本的颜色。
你可以使用一个ANSI逃生顺序,但不会做你想要什么,在现代版本的Windows。维基百科有一个非常丰富的文章:
http://en.wikipedia.org/wiki/ANSI_escape_code
所以回答你最初的问题几乎肯定是"不"。 但是,你可以改变的前景色不写一个逃生顺序,例如可通过调用Win32API功能。我不知道如何做这样的事情在红宝石掉我的头顶,但是有人似乎已经管理:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925
我想你会想要使用4暗红色或12明亮的红色和7恢复默认的颜色。
希望这可以帮助!
在ANSI逃码:
32位字符模式(子系统:控制台)的Windows的应用程序不写ANSI逃序列控制台
他们必须解释逃代码行动,并呼吁本机控制台API而不是
谢谢微软:-(
color [background][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
例如,要改变的背景下,以色和蓝色前景要灰色,你要类型:
color 18
我已经写了一个小型的跨平台宝石,处理这个无缝地上运行的窗口或POSIX系统,在这两个核磁共振成像和独立.
它没有依存关系,并使用ANSI码POSIX系统,并FFI(独立)或提琴手(MRI)。
使用它,只是:
gem install color-console
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
参观项目的主页 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 介绍了如何可以包控制台输出要达到的效果。
你想要的 ANSI码代码.
标准C/C++说明书,用于输出的命令行未指定任何能力对于改变颜色的控制台窗口。这就是说,有许多功能在Win32做这样的事情。
最简单的方法来改变颜色的Win32控制台是通过系统的命令在法师h。这一功能援引一个DOS命令。改变颜色,我们将用它来调用颜色的命令。例如, 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
只是这点颜色让控制台节目更多的赏心悦目。然而,颜色的命令将会改变颜色的整个控制台。控制单个细胞,我们需要使用功能从窗户。h。
不这样做,你需要用的 SetConsoleAttribute
功能
最终你需要电话 SetConsoleTextAttribute.你可以得到控制台缓冲屏幕处理从 GetStdHandle.
我一直在使用一个免费的windows尾的程序,称为baretail(谷歌)为年龄,让你做一个窗户-appified版本的unix尾的命令。它可以让你上色线依赖于什么关键词进行定义。什么是好的关于它作为一个解决方案是其不依赖于一个具体的语言或安装,等等,你只是定义了你的色彩方案及其在像驴子港。在我的个人的前10免费帮手!