Last active
December 19, 2015 09:09
-
-
Save nviennot/5931369 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
pid = fork do | |
Process.setsid # get immune to our parent's TTY signals | |
Signal.trap("CLD") { exit } # mplayer died | |
IO.popen(['mplayer', *ARGV], :err => [:child, :out]) do |io| | |
begin | |
loop do | |
begin | |
IO.copy_stream(io, STDOUT) | |
rescue Errno::EIO # STDOUT is gone, but we still need to pump the input | |
end | |
end | |
rescue EOFError # mplayer died | |
exit | |
rescue Interrupt # parent wants us to go away | |
exit # this kills mplayer with us with a SIGPIPE | |
end | |
end | |
end | |
loop do | |
begin | |
Process.wait(pid) | |
rescue Interrupt | |
Process.kill('INT', pid) | |
# we still need to wait for mplayer to die | |
# otherwise, the shell prints its prompt before mplayer has died | |
rescue Errno::ECHILD | |
exit | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment