lib/tty/command/process_runner.rb in tty-command-0.5.0 vs lib/tty/command/process_runner.rb in tty-command-0.6.0

- old
+ new

@@ -1,6 +1,7 @@ # encoding: utf-8 +# frozen_string_literal: true require 'thread' require_relative 'execute' require_relative 'result' @@ -35,22 +36,22 @@ def run!(&block) @printer.print_command_start(cmd) start = Time.now runtime = 0.0 - pid, stdin, stdout, stderr = spawn(cmd) # do |pid, stdin, stdout, stderr| + pid, stdin, stdout, stderr = spawn(cmd) # write and read streams write_stream(stdin) stdout_data, stderr_data = read_streams(stdout, stderr, &block) status = waitpid(pid) runtime = Time.now - start @printer.print_command_exit(cmd, status, runtime) - Result.new(status, stdout_data, stderr_data) + Result.new(status, stdout_data, stderr_data, runtime) rescue terminate(pid) Result.new(-1, stdout_data, stderr_data) ensure [stdin, stdout, stderr].each { |fd| fd.close if fd && !fd.closed? } @@ -60,11 +61,11 @@ # # @param [Integer] pid # # @api public def terminate(pid) - ::Process.kill(@signal, pid) + ::Process.kill(@signal, pid) rescue nil end private # @api private @@ -109,11 +110,11 @@ # @param [IO] stdout # @param [IO] stderr # # @api private def read_streams(stdout, stderr, &block) - stdout_data = '' + stdout_data = [] stderr_data = Truncator.new print_out = -> (cmd, line) { @printer.print_command_out_data(cmd, line) } print_err = -> (cmd, line) { @printer.print_command_err_data(cmd, line) } @@ -129,10 +130,10 @@ @threads[0].raise @threads[1].raise end end - [stdout_data, stderr_data.read] + [stdout_data.join, stderr_data.read] end def read_stream(stream, data, print_callback, callback) Thread.new do Thread.current[:cmd_start] = Time.now