Class: TermColor::MyListener
- Inherits:
-
Object
- Object
- TermColor::MyListener
- Includes:
- REXML::StreamListener
- Defined in:
- lib/termcolor.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#initialize ⇒ MyListener
constructor
A new instance of MyListener.
- #tag_end(name) ⇒ Object
- #tag_start(name, attrs) ⇒ Object
- #text(text) ⇒ Object
- #to_esc_seq(name) ⇒ Object
Constructor Details
#initialize ⇒ MyListener
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
#result ⇒ Object (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 |