如何在Windows命令提示符中更改文本颜色 [英] How can I change the text color in the windows command prompt

查看:259
本文介绍了如何在Windows命令提示符中更改文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令行程序,它输出记录到屏幕。



我想让错误行显示为红色。有没有一些特殊的字符代码,我可以输出将文本颜色切换到红色,然后切换回白色?



我使用ruby,但我想这将是



如下:

  =\0123#字符代码
white =\0223

print#{red}错误:IT BROKE#{white}
print其他你需要访问[Win32控制台API],然后单击下一步,然后单击下一步。 ]( http://msdn.microsoft.com /en-us/library/ms682073(VS.85%29.aspx)。不幸的是,我不知道你是怎么做的,在Perl中,我会使用 Win32 :: Console 模块。Windows控制台不响应ANSI转义代码。



根据 artur02提到的关于着色Ruby输出的文章,您需要安装&加载win32console gem。


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"

解决方案

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.

这篇关于如何在Windows命令提示符中更改文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆