jugyo-termcolor 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/termcolor.rb +18 -5
- data/spec/termcolor_spec.rb +16 -0
- metadata +1 -1
data/lib/termcolor.rb
CHANGED
@@ -7,7 +7,7 @@ require 'rexml/parsers/baseparser'
|
|
7
7
|
require 'rexml/streamlistener'
|
8
8
|
|
9
9
|
module TermColor
|
10
|
-
VERSION = '0.2.
|
10
|
+
VERSION = '0.2.4'
|
11
11
|
include REXML
|
12
12
|
|
13
13
|
class ParseError < StandardError; end
|
@@ -36,9 +36,11 @@ module TermColor
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def tag_start(name, attrs)
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
esc_seq = to_esc_seq(name)
|
40
|
+
if esc_seq
|
41
|
+
@result << esc_seq
|
42
|
+
@tag_stack.push(name)
|
43
|
+
end
|
42
44
|
end
|
43
45
|
|
44
46
|
def text(text)
|
@@ -48,8 +50,19 @@ module TermColor
|
|
48
50
|
def tag_end(name)
|
49
51
|
@tag_stack.pop
|
50
52
|
@result << HighLine::CLEAR
|
51
|
-
@result <<
|
53
|
+
@result << to_esc_seq(@tag_stack[-1].upcase) unless @tag_stack.empty?
|
52
54
|
end
|
53
55
|
|
56
|
+
def to_esc_seq(name)
|
57
|
+
esc_seq = nil
|
58
|
+
begin
|
59
|
+
esc_seq = HighLine.const_get(name.upcase)
|
60
|
+
rescue NameError
|
61
|
+
if name =~ /^\d+$/
|
62
|
+
esc_seq = "\e[#{name}m"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
esc_seq
|
66
|
+
end
|
54
67
|
end
|
55
68
|
end
|
data/spec/termcolor_spec.rb
CHANGED
@@ -24,6 +24,12 @@ module TermColor
|
|
24
24
|
text.should == "aa\e[34maaaaa<aaa\"aaa>aaa&aaaaa\e[0maaa"
|
25
25
|
end
|
26
26
|
|
27
|
+
it 'should parse 3' do
|
28
|
+
text = TermColor.parse('aa<30>bbbbbbb<32>cccc<90>ddd</90>c</32>b</30>aaa')
|
29
|
+
puts text
|
30
|
+
text.should == "aa\e[30mbbbbbbb\e[32mcccc\e[90mddd\e[0m\e[32mc\e[0m\e[30mb\e[0maaa"
|
31
|
+
end
|
32
|
+
|
27
33
|
it 'should raise Error' do
|
28
34
|
lambda{ TermColor.parse('aaaaa<red>aaaaa</blue>aaaaa') }.should raise_error(TermColor::ParseError)
|
29
35
|
lambda{ TermColor.parse('aaaaa<red>aaaaaaaaaa') }.should_not raise_error(TermColor::ParseError)
|
@@ -33,5 +39,15 @@ module TermColor
|
|
33
39
|
text = 'a<foo>&</foo>a'
|
34
40
|
TermColor.escape(text).should == "a<foo>&</foo>a"
|
35
41
|
end
|
42
|
+
|
43
|
+
it 'should convert to escape sequence' do
|
44
|
+
listener = TermColor::MyListener.new
|
45
|
+
listener.to_esc_seq('red').should == "\e[31m"
|
46
|
+
listener.to_esc_seq('on_red').should == "\e[41m"
|
47
|
+
listener.to_esc_seq('foo').should == nil
|
48
|
+
listener.to_esc_seq('0').should == "\e[0m"
|
49
|
+
listener.to_esc_seq('31').should == "\e[31m"
|
50
|
+
listener.to_esc_seq('031').should == "\e[031m"
|
51
|
+
end
|
36
52
|
end
|
37
53
|
end
|