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]



奇怪,为什么出现“系统无法执行指定的程序”
不知道你用的是虾米系统啊,我这里三台机器都用的好好的
可能是一些必要的动态链接库缺失了…
我刚下载到时可以使用,放着一两天后就没用了。。如直接双击的话会提示“由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。”
有个叫 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.
@路过
多谢提供这些信息