Sha256: d3104ba92eb3a948d6162232f6f6fe3ccce119a7dc9c2e4074dff55a63f28eca

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

class TrackingExpector
  attr_reader :output

  def initialize(out, debug = false)
    @out = out
    @debug = debug
    @unused = ""
    @output = ""
  end

  def expect(pattern, timeout = 5)
    buffer = ''

    case pattern
    when String
      pattern = Regexp.new(Regexp.quote(pattern))
    when Regexp
    else
      raise TypeError, "unsupported pattern class: #{pattern.class}"
    end

    result = nil
    position = 0
    @unused ||= ""

    while true
      if !@unused.empty?
        c = @unused.slice!(0).chr
      elsif output_ended?(timeout)
        @unused = buffer
        break
      else
        c = @out.getc.chr
      end

      STDOUT.putc c if @debug

      # wear your flip flops
      unless (c == "\e") .. (c == "m")
        if c == "\b"
          if position > 0 && buffer[position - 1] && buffer[position - 1].chr != "\n"
            position -= 1
          end
        else
          if buffer.size > position
            buffer[position] = c
          else
            buffer << c
          end

          position += 1
        end
      end

      if matches = pattern.match(buffer)
        result = [buffer, *matches.to_a[1..-1]]
        break
      end
    end

    @output << buffer

    result
  end

  private

  def output_ended?(timeout)
    (@out.is_a?(IO) && !IO.select([@out], nil, nil, timeout)) || @out.eof?
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
static-1.0.3 vendor/vmc-0.5.0/spec/support/tracking_expector.rb
static-1.0.1 vendor/vmc-0.5.0/spec/support/tracking_expector.rb
vmc-0.5.1.rc1 spec/support/tracking_expector.rb
vmc-0.5.0 spec/support/tracking_expector.rb
vmc-0.5.0.rc4 spec/support/tracking_expector.rb
vmc-0.5.0.rc3 spec/support/tracking_expector.rb
vmc-0.5.0.rc2 spec/support/tracking_expector.rb