Created
July 22, 2011 02:30
-
-
Save cwoodall/1098766 to your computer and use it in GitHub Desktop.
A nice little (very rough and being matured) wrapper for reading output asynchronously in ruby using PTY (built-in PseudoTerminal Library). I plan on expanding on this.
This file contains 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
# Author :: Christopher Woodall <chris.j.woodall@gmail.com> | |
# Copyright :: 2011 Christopher Woodall | |
# License :: MIT License | |
require 'pty' | |
# run_proc(proc) runs a psuedoterminal version of a command returning output to a block | |
# asynchronously as it is produced. | |
def run_proc(proc) | |
# Spawn the PTY process | |
PTY.spawn(proc) do |r, w, pid| | |
# Loop through the process until the end of file is reached | |
# and yeild to the block whenever a line is read | |
loop do | |
begin | |
yield r.readline | |
rescue EOFError | |
break | |
end | |
end | |
end | |
end | |
# Example | |
run_proc("ruby meow.rb") do |a| | |
puts a | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment