How do I get the color from the backticks command in ruby?

社会主义新天地 提交于 2019-12-10 11:24:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5078950/how-do-i-get-the-color-from-the-backticks-command-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!