<commit_before>require "open3"

class Command
  attr_reader :out, :code

  def initialize(*args)
    @args = args
  end

  def to_s
    @args.join(' ')
  end

  def success?
    code == 0
  end

  def stream(&block)
    _in, @out, wait_thread = Open3.popen2e(*@args)
    _in.close
    @out.each_line(&block) if block
    @code = wait_thread.value
    self
  end

end<commit_msg>Use readpartial instead of each_line for non blocking read of commands output<commit_after>require "open3"

class Command
  MAX_READ = 2 ** 16
  attr_reader :out, :code

  def initialize(*args)
    @args = args
  end

  def to_s
    @args.join(' ')
  end

  def success?
    code == 0
  end

  def stream(&block)
    _in, @out, wait_thread = Open3.popen2e(*@args)
    _in.close
    read_stream(@out, &block)
    @code = wait_thread.value
    self
  end

  def read_stream(io, &block)
    loop do
      yield io.readpartial(MAX_READ)
    end
  rescue EOFError
  end

end
{'fraction_non_alphanumeric': 0.10137275607180571, 'fraction_numerical': 0.011615628299894404, 'mean_word_length': 2.761904761904762, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2679364', 'n_tokens_mistral': 358, 'n_tokens_neox': 344, 'n_words': 95}