如何从ruby中的backticks命令获取颜色? [英] How do I get the color from the backticks command in ruby?

查看:133
本文介绍了如何从ruby中的backticks命令获取颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ruby文件中:



当我执行 system(rspec file_spec.rb)



当我这样做:

  result =`rspec file_spec.rb` 
puts result


解决方案

从阅读代码看起来像RSpec在输出流上调用 IO#isatty 以决定是否以对输出进行着色。



编辑 p>

如果您向rspec命令添加选项 - tty ,则可以使用此方法:


$ `rspec --color --tty file_spec.rb`



此SO问题


In a ruby file:

When I do system("rspec file_spec.rb") I get a nice colored output.

When I do this:

result = `rspec file_spec.rb`
puts result

I get no color at all.

Is there any way to preserve the color?

btw, I'm on OSX with Terminal.app if that matters.

解决方案

From reading the code it looks like RSpec calls IO#isatty on the output stream to decide whether or not to colorize the output. The backquote method must work differently from system in this respect.

EDIT

This works if you add the option --tty to the rspec command:

`rspec --color --tty file_spec.rb`

as mentioned in this SO question.

这篇关于如何从ruby中的backticks命令获取颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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