termcolor 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/termcolor.rb +18 -5
  2. data/spec/termcolor_spec.rb +16 -0
  3. metadata +1 -1
@@ -7,7 +7,7 @@ require 'rexml/parsers/baseparser'
7
7
  require 'rexml/streamlistener'
8
8
 
9
9
  module TermColor
10
- VERSION = '0.2.3'
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
- @result << HighLine.const_get(name.upcase)
40
- @tag_stack.push(name)
41
- rescue NameError
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 << HighLine.const_get(@tag_stack[-1].upcase) unless @tag_stack.empty?
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
@@ -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&lt;foo&gt;&amp;&lt;/foo&gt;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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: termcolor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo