lib/tty/command/process_runner.rb in tty-command-0.8.0 vs lib/tty/command/process_runner.rb in tty-command-0.8.1

- old
+ new

@@ -42,22 +42,12 @@ @printer.print_command_start(cmd) start = Time.now pid, stdin, stdout, stderr = ChildProcess.spawn(cmd) - # no input to write, close child's stdin pipe - stdin.close if (@input.nil? || @input.empty?) && !stdin.nil? + write_stream(stdin, @input) - writers = [@input && stdin].compact - - while writers.any? - ready = IO.select(nil, writers, writers, @timeout) - raise TimeoutExceeded if ready.nil? - - write_stream(ready[1], writers) - end - stdout_data, stderr_data = read_streams(stdout, stderr) status = waitpid(pid) runtime = Time.now - start @@ -82,11 +72,11 @@ end private # The buffer size for reading stdout and stderr - BUFSIZE = 3 * 1024 + BUFSIZE = 16 * 1024 # @api private def handle_timeout(runtime) return unless @timeout @@ -95,31 +85,38 @@ end # Write the input to the process stdin # # @api private - def write_stream(ready_writers, writers) + def write_stream(stream, input) start = Time.now - ready_writers.each do |fd| - begin - err = nil - size = fd.write(@input) - @input = @input.byteslice(size..-1) - rescue IO::WaitWritable - rescue Errno::EPIPE => err - # The pipe closed before all input written - # Probably process exited prematurely - fd.close - writers.delete(fd) - end - if err || @input.bytesize == 0 - fd.close - writers.delete(fd) - end + writers = [input && stream].compact - # control total time spent writing - runtime = Time.now - start - handle_timeout(runtime) + while writers.any? + ready = IO.select(nil, writers, writers, @timeout) + raise TimeoutExceeded if ready.nil? + + ready[1].each do |writer| + begin + err = nil + size = writer.write(@input) + input = input.byteslice(size..-1) + rescue IO::WaitWritable + rescue Errno::EPIPE => err + # The pipe closed before all input written + # Probably process exited prematurely + writer.close + writers.delete(writer) + end + if err || input.bytesize == 0 + writer.close + writers.delete(writer) + end + + # control total time spent writing + runtime = Time.now - start + handle_timeout(runtime) + end end end # Read stdout & stderr streams in the background #