Ruby popen3 和 ANSI 颜色

发布于 2024-10-29 18:31:52 字数 385 浏览 2 评论 0原文

我试图让 watchr 在文件更改时自动运行测试,并得到了我需要的大部分工作,除了 RSpec 中的所有 ANSI 颜色都被忽略这一事实。有问题的代码如下:

stdin, stdout, stderr = Open3.popen3(cmd)
stdout.each_line do |line|
  last_output = line
  puts line
end

当 cmd 等于 rspec spec/**/*.rb 之类的内容时,上面的代码可以正常运行 RSpec,只是所有输出都是单色的。我已经考虑使用 Kernel.system 来代替,但是系统不会返回我需要确定测试是否失败/成功的输出。如何获取从 Ruby 内执行的脚本的输出(包括 ANSI 颜色)并将其输出到控制台?

I am attempting to get watchr running tests automatically as files change, and got most of what I need working except for the fact that all ANSI colours from RSpec are being disregarded. The offending code is as follows:

stdin, stdout, stderr = Open3.popen3(cmd)
stdout.each_line do |line|
  last_output = line
  puts line
end

When cmd is equal to something like rspec spec/**/*.rb then the above code runs RSpec fine except that all output is in monochrome. I've looked at using Kernel.system instead, however system does not return the output which I need to determine if a test failed / succeeded. How can I get the output form a script that is executed from within Ruby including the ANSI color and output this to the console?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羅雙樹 2024-11-05 18:31:52

我猜想 rspec 正在检查它正在写入输出的流,看看它是否是 tty(即控制台),如果不是,则禁用颜色。许多命令都可以执行此操作 - 例如 GNU ls 和 grep。由于从子进程到脚本的流不是 tty,因此颜色将被禁用。

希望 rspec 有一个标志,可以强制使用颜色,无论流类型如何。如果没有,你将不得不采取一些真正奇怪的诡计。

I would guess that rspec is examining the stream to which it is writing output to see if it is a tty (ie the console) or not, and if it's not, disabling colour. Many commands do this - GNU ls and grep, for example. Since the stream from the child process to your script is not a tty, colour will be disabled.

Hopefully, rspec has a flag which will force colour to be used, regardless of the stream type. If it doesn't, you will have to resort to some truly weird stty shenanigans.

长不大的小祸害 2024-11-05 18:31:52

在决定是否使用颜色输出之前,rspec 工具很可能会检查它是否在终端上交互式运行或从脚本自动运行。

文档说您可以使用--color命令强制颜色行选项。

尝试:rspec --color spec/**/*.rb。

Chances are good that the rspec tool is checking to see if it is running interactively on a terminal or being run automatically from a script before deciding to use color output or not.

The documentation says you can force color with --color command line option.

Try: rspec --color spec/**/*.rb.

青柠芒果 2024-11-05 18:31:52

可以通过 PTY 模块在伪终端中运行命令 为了保留用户面对类似终端的行为。功劳归于 tty-command gem 的创建者 (查看此问题)谁在他的 gem 中实现了此行为:

require 'tty-command'
cmd = TTY::Command.new(pty: true)
cmd.run('rspec', 'spec/**/*.rb')

请记住,使用伪终端可能会有不需要的一面效果,例如某些 git 命令使用寻呼机 这本质上会导致命令挂起。因此引入该功能可能是一个突破性的改变。

It's possible to run commands in a pseudo terminal via the PTY module in order to preserve a user facing terminal-like behaviour. Credits go to the creator of the tty-command gem (see this issue) who implemented this behaviour in his gem:

require 'tty-command'
cmd = TTY::Command.new(pty: true)
cmd.run('rspec', 'spec/**/*.rb')

Keep in mind that using a pseudo terminal may have unwanted side effects, such as certain git commands using a pager which will essentially cause commands to hang. So introducing the functionality might be a breaking change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文