Class: TTYString::Parser

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/tty_string/parser.rb

Overview

Reads the text string a

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#screenObject (readonly)

Returns the value of attribute screen.



13
14
15
# File 'lib/tty_string/parser.rb', line 13

def screen
  @screen
end

#style_handlerObject (readonly)

Returns the value of attribute style_handler.



13
14
15
# File 'lib/tty_string/parser.rb', line 13

def style_handler
  @style_handler
end

Instance Method Details

#cursorObject



25
26
27
# File 'lib/tty_string/parser.rb', line 25

def cursor
  screen.cursor
end

#initial_styleObject



43
44
45
46
47
48
# File 'lib/tty_string/parser.rb', line 43

def initial_style
  @initial_style ||= case style_handler
  when RENDER then Style.new(parser: self)
  else NullStyle
  end
end

#render(style:, unknown:) ⇒ Object

rubocop:disable Metrics/MethodLength



15
16
17
18
19
20
21
22
23
# File 'lib/tty_string/parser.rb', line 15

def render(style:, unknown:) # rubocop:disable Metrics/MethodLength
  @style_handler = style
  @unknown_handler = unknown

  reset
  @screen = Screen.new(initial_style: initial_style)
  read until eos?
  screen.to_s
end

#unknownObject

rubocop:disable Metrics/MethodLength



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tty_string/parser.rb', line 29

def unknown # rubocop:disable Metrics/MethodLength
  case unknown_handler
  when RAISE
    raise(
      UnknownCodeError,
      if block_given?
        yield(matched)
      else
        "Unknown code #{matched.inspect}"
      end
    )
  end
end