使用PTY.spawn记录脚本化进程的整个IO

时间:2016-08-26 17:10:59

标签: ruby io pty

$watch

有没有办法记录脚本化过程的整个会话?

在此示例中,输出应类似于:

require 'pty'
require 'expect'

reader, writer, _pid = PTY.spawn('irb')

# log = StringIO.new # or maybe something else
# Attach/Tee logger to reader/writer

reader.expect(/.*> /, 1) do |line|
  puts "Line = #{line}"
  writer.puts '1+1'
end

10.times do
  reader.expect(/.*[\r\n]/, 1) { |line| puts "Line = #{line}" }
end

# puts log.string

1 个答案:

答案 0 :(得分:0)

结帐Open3。它已经内置到ruby中,让您可以查看终端中的所有流。您可能需要popen3或capture3