<commit_before>require "pty"

module RSpecLive
  class ConcurrentProcess
    def initialize(command)
      @command = command
      @output = ""
    end

    def start
      @stdout = PTY.spawn(@command).first
    end

    def each_line
      begin
        loop do
          sleep 0.001
          read_characters
        end
      rescue Errno::EIO
      end
      @output.each_line { |line| yield line }
    end

    private

    def read_characters
      while char = @stdout.read_nonblock(1)
        @output << char
      end
    rescue IO::EAGAINWaitReadable
    end
  end
end<commit_msg>Extract lines from possibly partial output in ConcurrentProcess<commit_after>require "pty"

module RSpecLive
  class ConcurrentProcess
    def initialize(command)
      @command = command
      @output = ""
    end

    def start
      @stdout = PTY.spawn(@command).first
    end

    def each_line
      begin
        loop do
          sleep 0.001
          read_characters
        end
      rescue Errno::EIO
      end
      shift_completed_lines.each_line { |line| yield line }
    end

    private

    def shift_completed_lines
      lines = ""
      while index = @output.index("\n")
        lines << @output.slice(0, index + 1)
        @output = @output.slice(index + 1, @output.length - index - 1)
      end
      lines
    end

    def read_characters
      while char = @stdout.read_nonblock(1)
        @output << char
      end
    rescue IO::EAGAINWaitReadable
    end
  end
end
{'fraction_non_alphanumeric': 0.0728744939271255, 'fraction_numerical': 0.009446693657219974, 'mean_word_length': 1.9308300395256917, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5883156', 'n_tokens_mistral': 459, 'n_tokens_neox': 445, 'n_words': 121}