【发布时间】:2016-09-26 14:54:37
【问题描述】:
我有一个 ruby 程序,它为我运行 rake 任务并捕获输出。现在它们正在 PTY.spawn 中运行。我在使用这种方法时遇到了两个问题:
- 我无法在子进程中使用 binding.pry。
- 重写自己的输出(例如进度条 gem)的进程会产生输出,但不能删除以前的输出,所以我会在预期一行的地方得到一堆渐进式输出。
我需要解决撬动问题。如果我能在此过程中解决第二个问题,那就太好了。
【问题讨论】:
我有一个 ruby 程序,它为我运行 rake 任务并捕获输出。现在它们正在 PTY.spawn 中运行。我在使用这种方法时遇到了两个问题:
我需要解决撬动问题。如果我能在此过程中解决第二个问题,那就太好了。
【问题讨论】:
Pry 支持调试远程进程,这应该适用于子进程。搜索“pry-remote”。
至于删除之前的输出,你似乎不明白到底发生了什么;您必须考虑 TTY 是如何工作的。先前的输出不会被删除,而是使用回车符而不是换行符将光标移动到行首。
您可以捕获输出并使用维护行分隔符的lines 将其分解为数组并获取第一行或最后一行。例如:
text = "this is the first line."
text += "\rthis is another line."
text += "\rthis is the last line."
text # => "this is the first line.\rthis is another line.\rthis is the last line."
text.lines("\r").first # => "this is the first line.\r"
text.lines("\r").last # => "this is the last line."
您也可以使用split,它不返回行分隔符:
text.split("\r").first # => "this is the first line."
text.split("\r").last # => "this is the last line."
【讨论】: