jugyo-termcolor 0.4.6 → 1.0.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/README.rdoc CHANGED
@@ -4,24 +4,32 @@ http://wiki.github.com/jugyo/termcolor
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Termcolor is a library for ANSII color formatting like HTML for output in terminal.
7
+ Termcolor is a library for ANSI color formatting like HTML for output in terminal.
8
8
 
9
9
  == FEATURES/PROBLEMS:
10
10
 
11
-
12
11
  == SYNOPSIS:
13
12
 
13
+ TermColor.parse('<blue>Hello</blue> <red>World!</red>')
14
+ #=> "\e[34mHello\e[0m \e[31mWorld!\e[0m"
15
+
14
16
  == REQUIREMENTS:
15
17
 
16
18
  * highline
17
19
 
18
20
  == INSTALL:
19
21
 
22
+ sudo gem install termcolor
23
+
24
+ or
25
+
20
26
  gem source -a http://gems.github.com
21
27
  sudo gem install jugyo-termcolor
22
28
 
23
29
  == TODO:
24
30
 
31
+ for windows...
32
+
25
33
  == LICENSE:
26
34
 
27
35
  (The MIT License)
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  $:.unshift File.dirname(__FILE__) + '/lib/'
2
3
  require 'termcolor'
3
4
  require 'spec/rake/spectask'
@@ -12,11 +13,12 @@ desc 'Generate gemspec'
12
13
  task :gemspec do |t|
13
14
  open('termcolor.gemspec', "wb" ) do |file|
14
15
  file << <<-EOS
16
+ # -*- coding: utf-8 -*-
15
17
  Gem::Specification.new do |s|
16
18
  s.name = 'termcolor'
17
19
  s.version = '#{TermColor::VERSION}'
18
- s.summary = "Termcolor is a library for ANSII color formatting like HTML for output in terminal."
19
- s.description = "Termcolor is a library for ANSII color formatting like HTML for output in terminal."
20
+ s.summary = "Termcolor is a library for ANSI color formatting like HTML for output in terminal."
21
+ s.description = "Termcolor is a library for ANSI color formatting like HTML for output in terminal."
20
22
  s.files = %w( #{Dir['lib/**/*.rb'].join(' ')}
21
23
  #{Dir['spec/**/*.rb'].join(' ')}
22
24
  #{Dir['examples/**/*.rb'].join(' ')}
@@ -0,0 +1,14 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'termcolor'
4
+
5
+ puts TermColor.parse <<EOS
6
+
7
+ <on_blue><white><bold>TermColor Example</bold></white></on_blue>
8
+
9
+ <on_green><white>Termcolor</white></on_green> is a library for <red><bold>ANSI</bold></red> <blue>c</blue><yellow>o</yellow><green>l</green>o<red>r</red> formatting like <on_magenta>HTML</on_magenta>
10
+ for output in terminal.
11
+
12
+ EOS
13
+
14
+ ["REVERSE", "ON_RED", "DARK", "MAGENTA", "ColorScheme", "RESET", "RED", "ON_BLUE", "BLINK", "ON_BLACK", "BOLD", "BLUE", "ON_WHITE", "QuestionError", "CLEAR", "BLACK", "ON_YELLOW", "SampleColorScheme", "UNDERSCORE", "WHITE", "SystemExtensions", "ERASE_CHAR", "YELLOW", "ON_CYAN", "CONCEALED", "ON_GREEN", "CHARACTER_MODE", "UNDERLINE", "CYAN", "Question", "Menu", "VERSION", "ERASE_LINE", "GREEN", "ON_MAGENTA"]
data/lib/termcolor.rb CHANGED
@@ -1,15 +1,96 @@
1
- require "rexml/document"
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'highline'
4
+ require 'cgi'
5
+ require 'rexml/parsers/streamparser'
6
+ require 'rexml/parsers/baseparser'
7
+ require 'rexml/streamlistener'
2
8
 
3
- class TermColor
4
- VERSION = '0.0.1'
9
+ module TermColor
10
+ VERSION = '1.0.0'
11
+ include REXML
5
12
 
6
13
  class << self
7
- @highline = HighLine.new
8
- def print(text)
9
- print parse(text)
10
- end
11
14
  def parse(text)
12
- # TODO
15
+ listener = MyListener.new
16
+ REXML::Parsers::StreamParser.new(prepare_parse(text), listener).parse
17
+ listener.result
18
+ end
19
+
20
+ def escape(text)
21
+ text.gsub(/[&<>'"]/) do | match |
22
+ case match
23
+ when '&' then '&amp;'
24
+ when '<' then '&lt;'
25
+ when '>' then '&gt;'
26
+ when "'" then '&apos;'
27
+ when '"' then '&quot;'
28
+ end
29
+ end
30
+ end
31
+
32
+ def unescape(text)
33
+ text.gsub(/&(lt|gt|amp|quot|apos);/) do | match |
34
+ case match
35
+ when '&amp;' then '&'
36
+ when '&lt;' then '<'
37
+ when '&gt;' then '>'
38
+ when '&apos;' then "'"
39
+ when '&quot;' then '"'
40
+ end
41
+ end
42
+ end
43
+
44
+ def prepare_parse(text)
45
+ text.gsub(/<(\/?)(\d+)>/, '<\1_\2>')
46
+ end
47
+
48
+ def test(*args)
49
+ args = (0..109).to_a if args.empty?
50
+ args.each_with_index do |color, index|
51
+ print parse("<#{color}> #{color} </#{color}>") + "\t"
52
+ puts if (index + 1) % 10 == 0
53
+ end
54
+ end
55
+ end
56
+
57
+ class MyListener
58
+ include REXML::StreamListener
59
+
60
+ attr_reader :result
61
+ def initialize
62
+ @result = ''
63
+ @tag_stack = []
64
+ end
65
+
66
+ def tag_start(name, attrs)
67
+ esc_seq = to_esc_seq(name)
68
+ if esc_seq
69
+ @result << esc_seq
70
+ @tag_stack.push(name)
71
+ end
72
+ end
73
+
74
+ def text(text)
75
+ @result << CGI.unescapeHTML(text)
76
+ end
77
+
78
+ def tag_end(name)
79
+ @tag_stack.pop
80
+ @result << HighLine::CLEAR
81
+ @result << to_esc_seq(@tag_stack[-1]) unless @tag_stack.empty?
82
+ end
83
+
84
+ def to_esc_seq(name)
85
+ esc_seq = nil
86
+ begin
87
+ esc_seq = HighLine.const_get(name.upcase)
88
+ rescue NameError
89
+ if name =~ /^[^0-9]?(\d+)$/
90
+ esc_seq = "\e[#{$1}m"
91
+ end
92
+ end
93
+ esc_seq
13
94
  end
14
95
  end
15
96
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,3 @@
1
1
  # -*- coding: utf-8 -*-
2
-
3
2
  $:.unshift(File.dirname(__FILE__) + '/../lib')
4
3
  require 'termcolor'
@@ -1,11 +1,63 @@
1
1
  # -*- coding: utf-8 -*-
2
-
3
2
  require File.dirname(__FILE__) + '/spec_helper'
4
3
 
5
- class TermColor
4
+ module TermColor
6
5
  describe TermColor do
7
6
  before do
8
- @termcolor = TermColor.new
7
+ end
8
+
9
+ it 'should parse 1' do
10
+ text = TermColor.parse('aaa<red>aaaa<bold>foo</bold>bb<blue>bbbb</blue>bbb</red>ccc<on_yellow>ccccc</on_yellow>ccc')
11
+ puts text
12
+ text.should == "aaa\e[31maaaa\e[1mfoo\e[0m\e[31mbb\e[34mbbbb\e[0m\e[31mbbb\e[0mccc\e[43mccccc\e[0mccc"
13
+ end
14
+
15
+ it 'should parse 2' do
16
+ text = TermColor.parse('aa<blue>a<foo>aaa<red>aa</red>aaaa</foo>a</blue>aaa')
17
+ puts text
18
+ text.should == "aa\e[34maaaa\e[31maa\e[0m\e[34maaaa\e[0ma\e[0maaa"
19
+ end
20
+
21
+ it 'should parse 3' do
22
+ text = TermColor.parse('aa<blue>aaaaa&lt;aaa&quot;aaa&gt;aaa&amp;aaaaa</blue>aaa')
23
+ puts text
24
+ text.should == "aa\e[34maaaaa<aaa\"aaa>aaa&aaaaa\e[0maaa"
25
+ end
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
+
33
+ it 'should raise Error' do
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
+ end
37
+
38
+ it 'should escape text' do
39
+ TermColor.escape('<>&"\'').should == "&lt;&gt;&amp;&quot;&apos;"
40
+ end
41
+
42
+ it 'should unescape text' do
43
+ TermColor.unescape("&lt;&gt;&amp;&quot;&apos;").should == '<>&"\''
44
+ end
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
+
51
+ it 'should convert to escape sequence' do
52
+ listener = TermColor::MyListener.new
53
+ listener.to_esc_seq('red').should == "\e[31m"
54
+ listener.to_esc_seq('on_red').should == "\e[41m"
55
+ listener.to_esc_seq('foo').should == nil
56
+ listener.to_esc_seq('0').should == "\e[0m"
57
+ listener.to_esc_seq('31').should == "\e[31m"
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"
9
61
  end
10
62
  end
11
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jugyo-termcolor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,20 +9,20 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-13 00:00:00 -08:00
12
+ date: 2009-04-08 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: json_pure
16
+ name: highline
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.1.3
23
+ version: 1.5.0
24
24
  version:
25
- description: TermColor is a simple twitter client.
25
+ description: Termcolor is a library for ANSI color formatting like HTML for output in terminal.
26
26
  email: jugyo.org@gmail.com
27
27
  executables: []
28
28
 
@@ -32,19 +32,10 @@ extra_rdoc_files:
32
32
  - README.rdoc
33
33
  - History.txt
34
34
  files:
35
- - lib/termcolor/connection.rb
36
35
  - lib/termcolor.rb
37
- - spec/termcolor_spec.rb
38
36
  - spec/spec_helper.rb
39
- - examples/direct_message.rb
40
- - examples/favorite.rb
41
- - examples/follow.rb
42
- - examples/friends_timeline.rb
43
- - examples/limit.rb
44
- - examples/replies.rb
45
- - examples/search.rb
46
- - examples/update_status.rb
47
- - examples/user.rb
37
+ - spec/termcolor_spec.rb
38
+ - examples/example.rb
48
39
  - README.rdoc
49
40
  - History.txt
50
41
  - Rakefile
@@ -76,6 +67,6 @@ rubyforge_project: termcolor
76
67
  rubygems_version: 1.2.0
77
68
  signing_key:
78
69
  specification_version: 2
79
- summary: Simple twitter client.
70
+ summary: Termcolor is a library for ANSI color formatting like HTML for output in terminal.
80
71
  test_files: []
81
72