jugyo-termcolor 0.2.6 → 0.3.0
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 +7 -7
- data/spec/termcolor_spec.rb +9 -2
- metadata +1 -1
data/lib/termcolor.rb
CHANGED
@@ -7,18 +7,14 @@ require 'rexml/parsers/baseparser'
|
|
7
7
|
require 'rexml/streamlistener'
|
8
8
|
|
9
9
|
module TermColor
|
10
|
-
VERSION = '0.
|
10
|
+
VERSION = '0.3.0'
|
11
11
|
include REXML
|
12
12
|
|
13
|
-
class ParseError < StandardError; end
|
14
|
-
|
15
13
|
class << self
|
16
14
|
def parse(text)
|
17
15
|
listener = MyListener.new
|
18
16
|
REXML::Parsers::StreamParser.new(text, listener).parse
|
19
17
|
listener.result
|
20
|
-
rescue REXML::ParseException => e
|
21
|
-
raise ParseError, e
|
22
18
|
end
|
23
19
|
|
24
20
|
def escape(text)
|
@@ -44,6 +40,10 @@ module TermColor
|
|
44
40
|
end
|
45
41
|
end
|
46
42
|
end
|
43
|
+
|
44
|
+
def prepare_parse(text)
|
45
|
+
text.gsub(/<(\/?)(\d+)>/, '<\1_\2>')
|
46
|
+
end
|
47
47
|
end
|
48
48
|
|
49
49
|
class MyListener
|
@@ -78,8 +78,8 @@ module TermColor
|
|
78
78
|
begin
|
79
79
|
esc_seq = HighLine.const_get(name.upcase)
|
80
80
|
rescue NameError
|
81
|
-
if name =~
|
82
|
-
esc_seq = "\e[#{
|
81
|
+
if name =~ /^[^0-9]?(\d+)$/
|
82
|
+
esc_seq = "\e[#{$1}m"
|
83
83
|
end
|
84
84
|
end
|
85
85
|
esc_seq
|
data/spec/termcolor_spec.rb
CHANGED
@@ -31,8 +31,8 @@ module TermColor
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'should raise Error' do
|
34
|
-
lambda{ TermColor.parse('aaaaa<red>aaaaa</blue>aaaaa') }.should raise_error(
|
35
|
-
lambda{ TermColor.parse('aaaaa<red>aaaaaaaaaa') }.should_not raise_error(
|
34
|
+
lambda{ TermColor.parse('aaaaa<red>aaaaa</blue>aaaaa') }.should raise_error(REXML::ParseException)
|
35
|
+
lambda{ TermColor.parse('aaaaa<red>aaaaaaaaaa') }.should_not raise_error(REXML::ParseException)
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'should escape text' do
|
@@ -43,6 +43,11 @@ module TermColor
|
|
43
43
|
TermColor.unescape("<>&"e;'").should == '<>&"\''
|
44
44
|
end
|
45
45
|
|
46
|
+
it 'should prepare parse' do
|
47
|
+
TermColor.prepare_parse("<10>10</10>").should == '<_10>10</_10>'
|
48
|
+
TermColor.prepare_parse("<32>10</32>").should == '<_32>10</_32>'
|
49
|
+
end
|
50
|
+
|
46
51
|
it 'should convert to escape sequence' do
|
47
52
|
listener = TermColor::MyListener.new
|
48
53
|
listener.to_esc_seq('red').should == "\e[31m"
|
@@ -51,6 +56,8 @@ module TermColor
|
|
51
56
|
listener.to_esc_seq('0').should == "\e[0m"
|
52
57
|
listener.to_esc_seq('31').should == "\e[31m"
|
53
58
|
listener.to_esc_seq('031').should == "\e[031m"
|
59
|
+
listener.to_esc_seq('_0').should == "\e[0m"
|
60
|
+
listener.to_esc_seq('_31').should == "\e[31m"
|
54
61
|
end
|
55
62
|
end
|
56
63
|
end
|