Class: TermColor::MyListener

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/termcolor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMyListener

Returns a new instance of MyListener.



65
66
67
68
# File 'lib/termcolor.rb', line 65

def initialize
  @result = ''
  @tag_stack = []
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



64
65
66
# File 'lib/termcolor.rb', line 64

def result
  @result
end

Instance Method Details

#tag_end(name) ⇒ Object



82
83
84
85
86
# File 'lib/termcolor.rb', line 82

def tag_end(name)
  @tag_stack.pop
  @result << HighLine::CLEAR
  @result << to_esc_seq(@tag_stack[-1]) unless @tag_stack.empty?
end

#tag_start(name, attrs) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/termcolor.rb', line 70

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) ⇒ Object



78
79
80
# File 'lib/termcolor.rb', line 78

def text(text)
  @result << CGI.unescapeHTML(text)
end

#to_esc_seq(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/termcolor.rb', line 88

def to_esc_seq(name)
  esc_seq = nil
  begin
    esc_seq = HighLine.const_get(name.upcase)
  rescue NameError
    if name =~ /^[^0-9]?(\d+)$/
      esc_seq = "\e[#{$1}m"
    end
  end
  esc_seq
end