Class: AnsiChameleon::TextRendering

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi_chameleon/text_rendering.rb

Constant Summary collapse

DEFAULT_STYLE =
{
  :effect_name           => :none,
  :foreground_color_name => :white,
  :background_color_name => :black
}

Instance Method Summary collapse

Constructor Details

#initialize(style_sheet_handler) ⇒ TextRendering

Returns a new instance of TextRendering.



10
11
12
13
14
15
16
17
# File 'lib/ansi_chameleon/text_rendering.rb', line 10

def initialize(style_sheet_handler)
  @style_sheet_handler = style_sheet_handler
  @stack = []
  @current_style = DEFAULT_STYLE.merge(@style_sheet_handler.default_values)

  @rendered_text = ''
  @rendered_text << sequence_for(@current_style)
end

Instance Method Details

#push_closing_tag(tag_name) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ansi_chameleon/text_rendering.rb', line 26

def push_closing_tag(tag_name)
  unless tag_names_chain.last == tag_name
    raise SyntaxError.new("Encountered </#{tag_name}> tag that had not been opened yet")
  end

  @current_style = @stack.pop[:outer_style]
  @rendered_text << sequence_for(@current_style)
end

#push_opening_tag(tag_name) ⇒ Object



19
20
21
22
23
24
# File 'lib/ansi_chameleon/text_rendering.rb', line 19

def push_opening_tag(tag_name)
  @stack.push({ :tag_name => tag_name, :outer_style => @current_style })

  @current_style = deduce_current_style
  @rendered_text << sequence_for(@current_style)
end

#push_text(text) ⇒ Object



35
36
37
# File 'lib/ansi_chameleon/text_rendering.rb', line 35

def push_text(text)
  @rendered_text << text
end

#to_sObject



39
40
41
42
43
44
45
46
47
# File 'lib/ansi_chameleon/text_rendering.rb', line 39

def to_s
  if @stack.any?
    tag_names = @stack.map { |data| "<#{data[:tag_name]}>" }.join(', ')
    msg_prefix = @stack.size == 1 ? "Tag #{tag_names} has" : "Tags #{tag_names} have"
    raise SyntaxError.new(msg_prefix + " been opened but not closed yet")
  end

  @rendered_text + AnsiChameleon::SequenceGenerator.generate(:reset)
end