Skip to content

Instantly share code, notes, and snippets.

@nviennot
Last active December 19, 2015 09:09
Show Gist options
  • Save nviennot/5931369 to your computer and use it in GitHub Desktop.
Save nviennot/5931369 to your computer and use it in GitHub Desktop.
#!/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