Class: EscapeCode::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/escape_code/scanner.rb

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Scanner

Returns a new instance of Scanner.



4
5
6
# File 'lib/escape_code/scanner.rb', line 4

def initialize(string)
  @string = string
end

Instance Method Details

#scan(&block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/escape_code/scanner.rb', line 8

def scan(&block)
  enumerator = Enumerator.new do |y|
    scanner = StringScanner.new(@string)

    until scanner.eos?
      if scanner.scan EscapeCode::Code::REGEX
        y << EscapeCode::Code.parse(scanner.matched)
      else
        # TODO: don't split up strings with \e but not an actual escape sequence
        y << scanner.scan(/[^\e]*/)
      end
    end
  end

  if block
    enumerator.each(&block)
    nil
  else
    enumerator
  end
end