Sha256: 8d2d785d4f58e2f3eaf64a3233df191c637c14a791571a3a09c0b52715e9eebb
Contents?: true
Size: 1.07 KB
Versions: 34
Compression:
Stored size: 1.07 KB
Contents
require 'thread' class Shell PROMPT = "_-=READY=-_" def initialize(shell = "/bin/bash", delay = 1, &block) @cb = block @shell = shell @delay = delay @parent_to_child_read, @parent_to_child_write = IO.pipe @child_to_parent_read, @child_to_parent_write = IO.pipe @child_pid = fork do @parent_to_child_write.close @child_to_parent_read.close $stdin.reopen(@parent_to_child_read) $stdout.reopen(@child_to_parent_write) $stderr.reopen(@child_to_parent_write) exec("/bin/bash") end buffer = "" semaphore = Mutex.new Thread.new do while true c = @child_to_parent_read.read(1) semaphore.synchronize { buffer += c } end end Thread.new do ch = "" while true do sleep @delay semaphore.synchronize { if buffer == ch and ch != "" @cb.call buffer buffer = "" end ch = buffer } end end end def puts(str) @parent_to_child_write.puts(str) end def kill @child_pid.kill end end
Version data entries
34 entries across 34 versions & 13 rubygems