class TerraformLandscape::Printer

def process_stream(io, options = {}) # rubocop:disable Metrics/MethodLength

rubocop:disable Metrics/MethodLength
def process_stream(io, options = {}) # rubocop:disable Metrics/MethodLength
  apply = nil
  buffer = StringIO.new
  original_tf_output = StringIO.new
  begin
    block_size = 1024
    done = false
    until done
      readable_fds, = IO.select([io])
      next unless readable_fds
      readable_fds.each do |f|
        begin
          new_output = f.read_nonblock(block_size)
          original_tf_output << new_output
          buffer << strip_ansi(new_output)
        rescue IO::WaitReadable # rubocop:disable Lint/HandleExceptions
          # Ignore; we'll call IO.select again
        rescue EOFError
          done = true
        end
      end
      apply = apply_prompt(buffer.string.encode('UTF-8',
                                                invalid: :replace,
                                                replace: ''))
      done = true if apply
    end
    begin
      process_string(buffer.string)
      @output.print apply if apply
    rescue ParseError, TerraformPlan::ParseError => e
      raise e if options[:trace]
      @output.warning FALLBACK_MESSAGE
      @output.print original_tf_output.string
    end
    @output.write_from(io)
  ensure
    io.close
  end
end