lib/tty/command/execute.rb in tty-command-0.4.0 vs lib/tty/command/execute.rb in tty-command-0.5.0
- old
+ new
@@ -4,40 +4,42 @@
require 'securerandom'
module TTY
class Command
module Execute
- # Execute command in a child process
+ # Execute command in a child process with all IO streams piped
+ # in and out. The interface is similar to Process.spawn
#
# The caller should ensure that all IO objects are closed
# when the child process is finished. However, when block
# is provided this will be taken care of automatically.
#
# @param [Cmd] cmd
- # the command to execute
+ # the command to spawn
#
# @return [pid, stdin, stdout, stderr]
#
# @api public
def spawn(cmd)
- @process_options = normalize_redirect_options(cmd.options)
+ process_opts = normalize_redirect_options(cmd.options)
# Create pipes
in_rd, in_wr = IO.pipe # reading
out_rd, out_wr = IO.pipe # writing
err_rd, err_wr = IO.pipe # error
+ in_wr.sync = true
# redirect fds
opts = ({
- :in => in_rd, # in_wr => :close,
- :out => out_wr,# out_rd => :close,
- :err => err_wr,# err_rd => :close
- }).merge(@process_options)
+ :in => in_rd, in_wr => :close,
+ :out => out_wr, out_rd => :close,
+ :err => err_wr, err_rd => :close
+ }).merge(process_opts)
pid = Process.spawn(cmd.to_command, opts)
# close in parent process
- [out_wr, err_wr].each { |fd| fd.close if fd }
+ [in_rd, out_wr, err_wr].each { |fd| fd.close if fd }
tuple = [pid, in_wr, out_rd, err_rd]
if block_given?
begin