Created
March 16, 2017 19:18
-
-
Save jbodah/1d3d396dd6b0a51ed15ea92d33bd43ed to your computer and use it in GitHub Desktop.
io multiplexing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TermTerm | |
def self.start!(command) | |
master, slave = PTY.open | |
comand = "script --return -c '#{command}' /dev/null" | |
i,_,e,t = Open3.popen3(command, out: slave) | |
multi = TermTerm::Multiplexer.new | |
multi.forward($stdin, i) | |
multi.forward(master, $stdout) | |
multi.forward(e, $stderr) | |
multi.run! | |
end | |
class Multiplexer | |
def initialize | |
@route_map = {} | |
end | |
def forward(from_pipe, to_pipe) | |
@route_map[from_pipe] = to_pipe | |
end | |
def run! | |
loop do | |
begin | |
readers, _, _ = select(@route_map.keys) | |
readers.each do |reader| | |
writer = @route_map[reader] | |
lines = reader.read_nonblock(1000) | |
writer.write lines | |
end | |
rescue IO::WaitReadable | |
retry | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment