Windows命令行彩色输出的小程序,很有用
作者:半瓶墨水 链接:http://www.2maomao.com/blog/win-color-cmd-tool/
最近写ruby的命令行,想做一个文本正则表达式搜索,已经完成了一小半了。(Update@2007 Feb3:放弃了,Ruby的编码解码速度是在是慢的让人惊讶)
搜索的结果想要高亮输出的时候遇到问题了:ruby对windows命令行彩色输出没有内置的库函数。
RubyForge有一些关于Ruby命令行的库,没试过,因为这个简单功能还要背个库,太沉了
今天写了一个小程序colorcmd.exe,可以用来在windows命令行输出彩色文本,以后写脚本(perl/ruby/批处理)想要高亮的话就方便了。
贴个截图:

使用方法:
colorcmd text-color back-color outstring
直接输入: colorcmd就会有帮助提示和上面截图里面的颜色表
比如:
colorcmd red 0 foobar // 红字
colorcmd 13 0 "foo bar" // 红字,只不过这里用的是颜色值,中间有空格的情况
colorcmd red 3 "foo bar" // 红字绿底
写了一个ruby的程序小小测试了一下:
下载: testcmd.rb
#just a small test to colorcmd.exe
#ohmy~ numbers and colors
colors = [
"default",
"black" , "navy" , "green" , "teal" , "maroon",
"purple" , "olive" , "silver", "gray" , "blue" ,
"lime" , "aqua" , "red" , "fuchsia", "yellow",
"white"
]
colors.each_index do |i|
a = format("%2d:%2d", i, 17-i)
str = "colorcmd #{i} #{17-i} \"#{a} what's my color?\""
system(str)
x, y = colors[i], colors[(37 - i) % colors.size]
a = format("%7s:%-7s", x, y)
str = "colorcmd #{x} #{y} \"#{a} what's my color?\""
system(str)
puts
end
puts
#ohmy~ same line
foo = "what's my color indeed, oh, sorry, I just want to make up a long sentence :)"
words = foo.split
words.each_index do |i|
system("colorcmd #{(i + 1) * 11 % colors.size} #{(i + 1) * 7 % colors.size} #{words[i]}")
print " "
end
puts
#ohmy~ numbers and colors
colors = [
"default",
"black" , "navy" , "green" , "teal" , "maroon",
"purple" , "olive" , "silver", "gray" , "blue" ,
"lime" , "aqua" , "red" , "fuchsia", "yellow",
"white"
]
colors.each_index do |i|
a = format("%2d:%2d", i, 17-i)
str = "colorcmd #{i} #{17-i} \"#{a} what's my color?\""
system(str)
x, y = colors[i], colors[(37 - i) % colors.size]
a = format("%7s:%-7s", x, y)
str = "colorcmd #{x} #{y} \"#{a} what's my color?\""
system(str)
puts
end
puts
#ohmy~ same line
foo = "what's my color indeed, oh, sorry, I just want to make up a long sentence :)"
words = foo.split
words.each_index do |i|
system("colorcmd #{(i + 1) * 11 % colors.size} #{(i + 1) * 7 % colors.size} #{words[i]}")
print " "
end
puts
Ruby测试程序测试结果截图(jpeg压缩,有点儿晕)如下:

点击这里下载程序,需要源代码请到blog上留言
Update 2008-03-21T00:29:36+00:00: 刚刚fix了一个小bug,现在支持输出重定向了。
标签:[tag]ruby, windows, 命令行[/tag]
共 11 条评论
发表评论
Additional comments powered by BackType



奇怪,为什么出现“系统无法执行指定的程序”
不知道你用的是虾米系统啊,我这里三台机器都用的好好的
可能是一些必要的动态链接库缺失了…
我刚下载到时可以使用,放着一两天后就没用了。。如直接双击的话会提示“由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。”
有个叫 EchoX.exe 的也是做这事的,体积比你的大点,当然选项也更多
EchoX 2.6 - (C) 2004-2007 by Bill Stewart (bstewart@iname.com)
Usage: echox [-c color | [-f fg] [-b bg]] [-n] [-w width [-r | -e]] message
or: echox -l
-c Specifies a color (01-FE).
-f, -b Specifies the foreground and background colors (0 through F).
-n Do not skip to the next line.
-w Specifies the message should be ‘width’ characters wide.
-r Right-aligns the message with respect to the specified width.
-e Centers the message with respect to the specified width.
message The text to be displayed.
-l Lists available colors in a table.
When specifying colors, the foreground color cannot match the background color.
If you specify -c, you cannot specify -f or -b (and vice versa).
The message can contain the following escape sequences: ~n (return), ~r (line
feed), and ~t (tab). To display a literal ‘~’ if it’s followed by n, r, or t,
use ‘~~’. Escape sequences are ignored if -w is specified.
@路过
多谢提供这些信息
兄弟来份代码谢谢.
TasNat@163.com
想要份colorcmd源码,学习学习~谢谢!
@紫红的泪
代码参见:http://fayaa.com/code/view/35/
用16位汇编写滴!才200字节出头,还附带类似choice的功能.
可以自定义输出的颜色,位置,内容…
可以单独改变原位置的颜色……
不过
在win2003以后的系统上,就不那么兼容16位com程序了555555555
正在寻找替代程序,你这个还不错,就是体积…
QQ:354324773
@SYBN
呵呵2010年了,Windows7都出来了,淘宝网上高清拷片的1G才一毛钱了,还这么在乎体积啊。。。
一个cmd脚本一般还不到10K,如果找大于它的3方工具有点得不偿失了!
最好简化一下语法,利用color的参数那样的颜色信息…
要是能有和SYBC兼容参数的版本就更好了!
http://www.cn-dos.net/forum/viewthread.php?tid=50370&fpage=1
外国友人们把半条命那样的第一人称射击游戏精简到不到96KB,
不知道国内动辄GB级的游戏开发商有没有自杀冲动?