class TermColor::MyListener

Attributes

result[R]

Public Class Methods

new() click to toggle source
# File lib/termcolor.rb, line 69
def initialize
  @result = ''
  @tag_stack = []
end

Public Instance Methods

tag_end(name) click to toggle source
# File lib/termcolor.rb, line 86
def tag_end(name)
  @tag_stack.pop
  @result << HighLine::CLEAR
  @result << @tag_stack.map{|i| to_esc_seq(i)}.join unless @tag_stack.empty?
end
tag_start(name, attrs) click to toggle source
# File lib/termcolor.rb, line 74
def tag_start(name, attrs)
  esc_seq = to_esc_seq(name)
  if esc_seq
    @result << esc_seq
    @tag_stack.push(name)
  end
end
text(text) click to toggle source
# File lib/termcolor.rb, line 82
def text(text)
  @result << CGI.unescapeHTML(text)
end
to_esc_seq(name) click to toggle source
# File lib/termcolor.rb, line 92
def to_esc_seq(name)
  if (HighLine.const_defined?(name.upcase) rescue false)
    HighLine.const_get(name.upcase)
  else
    case name
    when /^([fb])(\d+)$/
      fb = $1 == 'f' ? 38 : 48
      color = $2.size == 3 ? 16 + $2.to_i(6) : 232 + $2.to_i
      "\e[#{fb};5;#{color}m"
    when /^[^0-9]?(\d+)$/
      "\e[#{$1}m"
    end
  end
end