在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在使用Ruby向控制台输出文本的时候,很好的Coloring是很有必要的。以下是我找到的如何向OS X的终端中输出着色文本的方法。其实,在Linux下应该也同样适用。 本来我只需要把找到的博文链接贴出来就好,可惜那博文是在blogspot上,正在为一堵伟大的墙挡着。所以方便起见,我只能全文贴过来。附上原文链接: http://craiccomputing.blogspot.com/2010/08/printing-colored-text-in-unix-terminals.html Printing Colored Text in UNIX Terminals from Rubythis Wiki page.
This Ruby script outputs fore and background colors and a couple of the other useful modifiers.
Note that the exact colors will vary between different terminals. So try it and see.The script will show you enough to create your own colored terminal output. But what if you sometimes want your output to go to a terminal (TTY) and other times to a file? In the latter, all the escape codes will give you garbage characters when you view the file. The trick here is to test whether our not your script is writing to a TTY with 'STDOUT.tty?'. If so then add the escape codes, if not then leave them out. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论