tabularize 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ 0.1.1
2
+ -----
3
+ - :unicode_display option added for CJK wide characters.
4
+
5
+ 0.1.0
6
+ -----
7
+ - Initial release
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tabularize (0.1.1)
5
+ unicode-display_width (~> 0.1.1)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ awesome_print (1.0.2)
11
+ unicode-display_width (0.1.1)
12
+
13
+ PLATFORMS
14
+ java
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ awesome_print
19
+ tabularize!
data/README.md CHANGED
@@ -2,6 +2,7 @@ tabularize
2
2
  ==========
3
3
 
4
4
  Formatting tabular data with paddings.
5
+
5
6
  Inspired by tabular.vim (https://github.com/godlygeek/tabular)
6
7
 
7
8
  Installation
@@ -107,6 +108,20 @@ Average Joe_ | Engineering | Somewhere over the rainbow | N/A_____
107
108
  Hong Gildong | HR_________ | Nowhere___________________ | 555-5555
108
109
  ```
109
110
 
111
+ #### CJK wide characters
112
+
113
+ ```ruby
114
+ puts Tabularize.it(data, :unicode_display => true).map { |row| row.join ' | ' }
115
+ ```
116
+
117
+ ```
118
+ Name | Dept | Location | Phone
119
+ John Doe | Finance | Los Angeles, CA 90089 | 555-1555
120
+ Average Joe | Engineering | Somewhere over the rainbow | N/A
121
+ Hong Gildong | HR | Nowhere | 555-5555
122
+ 홍길동 | 탁상 3부 | 서울역 3번 출구 김씨 옆자리 | N/A
123
+ ```
124
+
110
125
  Copyright
111
126
  ---------
112
127
 
@@ -3,7 +3,8 @@ require "tabularize/version"
3
3
  module Tabularize
4
4
  DEFAULT_OPTIONS = {
5
5
  :pad => ' ',
6
- :align => :left
6
+ :align => :left,
7
+ :unicode_display => false
7
8
  }
8
9
 
9
10
  # Formats two-dimensional tabular data.
@@ -19,17 +20,26 @@ module Tabularize
19
20
  options = DEFAULT_OPTIONS.merge(options)
20
21
  pad = options[:pad].to_s
21
22
  align = options[:align]
22
- raise ArgumentError.new("Invalid alignment") unless pad.length == 1
23
+ unicode = options[:unicode_display]
24
+ raise ArgumentError.new("Invalid padding") unless pad.length == 1
23
25
  raise ArgumentError.new("Invalid alignment") unless
24
26
  [:left, :right, :center].include?(align)
25
27
 
28
+ l =
29
+ if unicode
30
+ require 'unicode/display_width'
31
+ :display_width
32
+ else
33
+ :length
34
+ end
35
+
26
36
  rows = []
27
37
  max_widths = []
28
38
  table_data.each do |row|
29
39
  rows << row = [*row].map(&:to_s).map(&:chomp)
30
40
 
31
41
  row.each_with_index do |cell, idx|
32
- max_widths[idx] = [ cell.length, max_widths[idx] || 0 ].max
42
+ max_widths[idx] = [ cell.send(l), max_widths[idx] || 0 ].max
33
43
  end
34
44
  end
35
45
 
@@ -38,6 +48,9 @@ module Tabularize
38
48
  row.map { |str|
39
49
  idx += 1
40
50
  w = max_widths[idx]
51
+ if unicode
52
+ w += str.length - str.display_width
53
+ end
41
54
  case align
42
55
  when :left
43
56
  str.ljust(w, pad)
@@ -1,3 +1,3 @@
1
1
  module Tabularize
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # specify any dependencies here; for example:
22
22
  s.add_development_dependency "awesome_print"
23
+ s.add_runtime_dependency 'unicode-display_width', '~> 0.1.1'
23
24
  end
@@ -101,19 +101,26 @@ class TestTabularize < Test::Unit::TestCase
101
101
  end
102
102
  end
103
103
 
104
+ # TODO: Need assertion
104
105
  def test_tabularize_csv
105
- data = CSV.read(File.join(File.dirname(__FILE__), 'test.csv'), :col_sep => '|')
106
- ap data
107
- output = Tabularize.it(data).map { |row| row.join ' | ' }
108
- ap output
109
- puts
110
- puts output
106
+ {
107
+ 'test.csv' => false,
108
+ 'test_unicode.csv' => true
109
+ }.each do |file, unicode|
110
+ data = CSV.read(File.join(File.dirname(__FILE__), file), :col_sep => '|')
111
+ ap data
112
+ output = Tabularize.it(data, :unicode_display => unicode).map { |row| row.join ' | ' }
113
+ ap output
114
+ puts
115
+ puts output
111
116
 
112
- puts Tabularize.it(data, :align => :right).map { |row| row.join ' | ' }
113
- puts
114
- puts Tabularize.it(data, :align => :center).map { |row| row.join ' | ' }
115
- puts
116
- puts Tabularize.it(data, :pad => '_').map { |row| row.join ' | ' }
117
+ puts Tabularize.it(data, :align => :right, :unicode_display => unicode).map { |row| row.join ' | ' }
118
+ puts
119
+ puts Tabularize.it(data, :align => :center, :unicode_display => unicode).map { |row| row.join ' | ' }
120
+ puts
121
+ puts Tabularize.it(data, :pad => '_', :unicode_display => unicode).map { |row| row.join ' | ' }
122
+
123
+ end
117
124
  end
118
125
 
119
126
  def test_invalid_arguments
@@ -0,0 +1,5 @@
1
+ Name|Dept|Location|Phone
2
+ John Doe|Finance|Los Angeles, CA 90089|555-1555
3
+ Average Joe|Engineering|Somewhere over the rainbow|N/A
4
+ Hong Gildong|HR|Nowhere|555-5555
5
+ 홍길동|탁상 3부|서울역 3번 출구 김씨 옆자리|N/A
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabularize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-10 00:00:00.000000000 Z
12
+ date: 2012-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print
16
- requirement: &2152636520 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,28 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2152636520
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: unicode-display_width
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.1.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.1.1
25
46
  description: Formatting tabular data with paddings and alignments
26
47
  email:
27
48
  - junegunn.c@gmail.com
@@ -30,7 +51,9 @@ extensions: []
30
51
  extra_rdoc_files: []
31
52
  files:
32
53
  - .gitignore
54
+ - CHANGELOG.md
33
55
  - Gemfile
56
+ - Gemfile.lock
34
57
  - LICENSE.txt
35
58
  - README.md
36
59
  - Rakefile
@@ -39,6 +62,7 @@ files:
39
62
  - tabularize.gemspec
40
63
  - test/test.csv
41
64
  - test/test_tabularize.rb
65
+ - test/test_unicode.csv
42
66
  homepage: ''
43
67
  licenses: []
44
68
  post_install_message:
@@ -59,11 +83,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
83
  version: '0'
60
84
  requirements: []
61
85
  rubyforge_project: tabularize
62
- rubygems_version: 1.8.15
86
+ rubygems_version: 1.8.18
63
87
  signing_key:
64
88
  specification_version: 3
65
89
  summary: Formatting tabular data
66
90
  test_files:
67
91
  - test/test.csv
68
92
  - test/test_tabularize.rb
93
+ - test/test_unicode.csv
69
94
  has_rdoc: