tabularize 0.2.5 → 0.2.7

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.
@@ -1,3 +1,14 @@
1
+ 0.2.7
2
+ -----
3
+ - Customizable border
4
+ ```ruby
5
+ :hborder => '─',
6
+ :vborder => '│',
7
+ :iborder => %w[┌ ┬ ┐ ├ ┼ ┤ └ ┴ ┘],
8
+ ```
9
+ - `:border_color`
10
+ - Predefined border styles. `:border_style => :unicode`
11
+
1
12
  0.2.5
2
13
  -----
3
14
  - Minor bug fixes
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tabularize (0.2.4)
4
+ tabularize (0.2.7)
5
5
  unicode-display_width (~> 0.1.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -46,9 +46,11 @@ puts table
46
46
  * `:pad_left` Size of left padding
47
47
  * `:pad_right` Size of right padding
48
48
  * Border
49
+ * `:border_style` Predefined border styles. `:ascii` (default) and `:unicode`
50
+ * `:border_color` ANSI color code for borders
49
51
  * `:hborder` Character for horizontal border
50
52
  * `:vborder` Character for vertical border
51
- * `:iborder` Character for intersection point
53
+ * `:iborder` Array of 9 characters for intersection points. e.g. `%w[┌ ┬ ┐ ├ ┼ ┤ └ ┴ ┘]`
52
54
  * Alignment
53
55
  * `:align` Horizontal alignment. `:left`, `:center`, `:right`, or Array of the three options
54
56
  * `:valign` Vertical alignment. `:top`, `:middle`, `:bottom`, or Array of the three options
@@ -57,7 +59,11 @@ puts table
57
59
  * `:ellipsis` Ellipsis string when cells are cut off. Default: `>`
58
60
 
59
61
  ```ruby
62
+ require 'ansi'
63
+
60
64
  table = Tabularize.new :pad => '.', :pad_left => 2, :pad_right => 0,
65
+ :border_style => :unicode,
66
+ :border_color => ANSI::Code.red,
61
67
  :hborder => '~', :vborder => 'I', :iborder => '#',
62
68
  :align => [:left, :center, :right],
63
69
  :valign => [:top, :bottom, :middle, :middle],
@@ -71,15 +77,15 @@ puts table
71
77
  ```
72
78
 
73
79
  ```
74
- #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~X
75
- I..Name.......I.....Dept....I.....................LocationI.....PhoneX
76
- #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~X
77
- I..John Doe...I....Finance..I.........Los Angeles CA 90089I..555-1555X
78
- I..Average JoeI..EngineeringI...Somewhere over the rainbowI.......N/AX
79
- I..홍길동.....I.............I..서울역 3번 출구 김씨 옆자리I..........X
80
- I.............I.............I.............................I.......N/AX
81
- I.............I...탁상 3부..I.....................맞습니다I..........X
82
- #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~X
80
+ ┌─────────────┬─────────────┬─────────────────────────────┬──────────~
81
+ │..Name.......│.....Dept....│.....................Location│.....Phone~
82
+ ├─────────────┼─────────────┼─────────────────────────────┼──────────~
83
+ │..John Doe...│....Finance..│.........Los Angeles CA 90089│..555-1555~
84
+ │..Average Joe│..Engineering│...Somewhere over the rainbow│.......N/A~
85
+ │..홍길동.....│.............│..서울역 3번 출구 김씨 옆자리│..........~
86
+ │.............│.............│.............................│.......N/A~
87
+ │.............│...탁상 3부..│.....................맞습니다│..........~
88
+ └─────────────┴─────────────┴─────────────────────────────┴──────────~
83
89
  ```
84
90
 
85
91
  Tabularize.it
@@ -206,6 +212,7 @@ There are a few things, however, [tabularize](https://github.com/junegunn/tabula
206
212
  - It correctly formats cells containing CJK wide characters.
207
213
  - Vertical alignment for multi-line cells
208
214
  - Cutting off trailing cells when the width of the output string exceeds specified screen width
215
+ - Customizable border style
209
216
 
210
217
  Copyright
211
218
  ---------
@@ -1,21 +1,22 @@
1
+ # encoding: utf-8
2
+
1
3
  require "tabularize/version"
2
4
  require 'stringio'
3
5
  require 'unicode/display_width'
4
6
 
5
7
  class Tabularize
6
8
  DEFAULT_OPTIONS = {
7
- :align => :left,
8
- :valign => :top,
9
- :pad => ' ',
10
- :pad_left => 0,
11
- :pad_right => 0,
9
+ :align => :left,
10
+ :valign => :top,
11
+ :pad => ' ',
12
+ :pad_left => 0,
13
+ :pad_right => 0,
12
14
 
13
- :hborder => '-',
14
- :vborder => '|',
15
- :iborder => '+',
15
+ :border_style => :ascii,
16
+ :border_color => nil,
16
17
 
17
- :unicode => true,
18
- :ansi => true,
18
+ :unicode => true,
19
+ :ansi => true,
19
20
 
20
21
  :ellipsis => '>',
21
22
  :screen_width => nil,
@@ -26,6 +27,19 @@ class Tabularize
26
27
  :pad_right => 1,
27
28
  }
28
29
 
30
+ BORDER_STYLE = {
31
+ :ascii => {
32
+ :hborder => '-',
33
+ :vborder => '|',
34
+ :iborder => %w[+ + + + + + + + +],
35
+ },
36
+ :unicode => {
37
+ :hborder => '─',
38
+ :vborder => '│',
39
+ :iborder => %w[┌ ┬ ┐ ├ ┼ ┤ └ ┴ ┘],
40
+ }
41
+ }
42
+
29
43
  # @since 0.2.0
30
44
  def initialize options = {}
31
45
  @rows = []
@@ -33,6 +47,14 @@ class Tabularize
33
47
  @options = DEFAULT_OPTIONS.
34
48
  merge(DEFAULT_OPTIONS_GENERATOR).
35
49
  merge(options)
50
+ if @options[:border_style]
51
+ @options = BORDER_STYLE[@options[:border_style]].merge(@options)
52
+
53
+ # Backward-compatibility
54
+ unless @options[:iborder].is_a?(Array)
55
+ @options[:iborder] = [@options[:iborder]] * 9
56
+ end
57
+ end
36
58
  @cache = {}
37
59
  end
38
60
 
@@ -66,12 +88,12 @@ class Tabularize
66
88
  num_cached_rows = 0
67
89
  else
68
90
  [@seps[@rows.length] - @cache[:last_seps], 0].max.times do
69
- @cache[:string_io].puts @cache[:separator]
91
+ @cache[:string_io].puts @cache[:separators][1]
70
92
  end
71
93
  @cache[:last_seps] = @seps[@rows.length]
72
94
 
73
95
  if num_cached_rows == @rows.length
74
- return @cache[:string_io].string + @cache[:separator]
96
+ return @cache[:string_io].string + @cache[:separators].last
75
97
  end
76
98
  end
77
99
  end
@@ -80,63 +102,73 @@ class Tabularize
80
102
 
81
103
  h = @options[:hborder]
82
104
  v = @options[:vborder]
83
- i = @options[:iborder]
84
105
  vl = @options[:vborder]
85
- il = @options[:iborder]
106
+ i9 = @options[:iborder]
107
+ e = @options[:ellipsis]
108
+ c = @options[:border_color] || ''
109
+ r = c.empty? ? '' : "\e[0m"
86
110
  u = @options[:unicode]
87
111
  a = @options[:ansi]
88
112
  sw = @options[:screen_width]
89
- el = Tabularize.cell_width(@options[:ellipsis], u, a)
113
+ el = Tabularize.cell_width(e, u, a)
90
114
 
91
- separator = @cache[:separator]
115
+ separators = @cache[:separators]
92
116
  col_count = @cache[:col_count]
93
- unless separator
94
- separator = ''
95
- rows[0].each_with_index do |c, idx|
96
- new_sep = separator + i + h * Tabularize.cell_width(c, u, a)
97
-
98
- if sw && Tabularize.cell_width(new_sep, u, a) > sw - el
99
- col_count = idx
100
- break
117
+ separators ||=
118
+ Array.new(3) {''}.zip(i9.each_slice(3).to_a).map { |separator, i3|
119
+ rows[0].each_with_index do |ch, idx|
120
+ new_sep = separator + i3[idx == 0 ? 0 : 1] + h * Tabularize.cell_width(ch, u, a)
121
+
122
+ if sw && Tabularize.cell_width(new_sep, u, a) > sw - el
123
+ col_count = idx
124
+ break
125
+ else
126
+ separator = new_sep
127
+ end
128
+ end
129
+ separator += col_count ? e : i3.last
130
+ if c
131
+ c + separator + r
101
132
  else
102
- separator = new_sep
133
+ separator
103
134
  end
104
- end
105
- separator += il
106
- end
135
+ }
107
136
 
108
- output = @cache[:string_io] || StringIO.new.tap { |io| io.puts separator }
137
+ output = @cache[:string_io] || StringIO.new.tap { |io|
138
+ io.set_encoding 'UTF-8' if io.respond_to? :set_encoding
139
+ io.puts separators.first
140
+ }
109
141
  if col_count
110
142
  rows = rows.map { |line| line[0, col_count] }
111
- vl = il = @options[:ellipsis]
143
+ vl = e
112
144
  end
113
145
  rows.each_with_index do |row, idx|
114
146
  row = row.map { |val| val.lines.to_a.map(&:chomp) }
115
147
  height = row[0] ? row[0].count : 1
116
148
  @seps[idx + num_cached_rows].times do
117
- output.puts separator
149
+ output.puts separators[1]
118
150
  end
119
151
  (0...height).each do |line|
120
- output.print v unless row.empty?
152
+ output.print c + v + r unless row.empty?
121
153
  output.puts row.map { |lines|
122
154
  lines[line] || @options[:pad] * Tabularize.cell_width(lines[0], u, a)
123
- }.join(v) + vl
155
+ }.join(c + v + r) + c + vl + r
124
156
  end
125
157
  end
126
158
 
127
159
  @seps[rows.length + num_cached_rows].times do
128
- output.puts separator
160
+ output.puts separators[1]
129
161
  end
130
162
 
131
163
  @cache = {
132
- :analysis => analysis,
133
- :separator => separator,
134
- :col_count => col_count,
135
- :num_rows => @rows.length,
136
- :string_io => output,
137
- :last_seps => @seps[rows.length]
164
+ :analysis => analysis,
165
+ :separators => separators,
166
+ :col_count => col_count,
167
+ :num_rows => @rows.length,
168
+ :string_io => output,
169
+ :last_seps => @seps[rows.length]
138
170
  }
139
- output.string + separator
171
+ output.string + separators.last
140
172
  rescue Exception
141
173
  @cache = {}
142
174
  raise
@@ -1,3 +1,3 @@
1
1
  class Tabularize
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -9,10 +9,11 @@ require 'tabularize'
9
9
 
10
10
  table = Tabularize.new
11
11
  table = Tabularize.new :pad => '.', :pad_left => 2, :pad_right => 0,
12
- :hborder => '~', :vborder => 'I', :iborder => '#',
12
+ :border_style => :unicode,
13
+ :border_color => ANSI::Code.red,
13
14
  :align => [:left, :center, :right],
14
15
  :valign => [:top, :bottom, :middle, :middle],
15
- :screen_width => 75, :ellipsis => 'X'
16
+ :screen_width => 75, :ellipsis => '~'
16
17
  table << %w[Name Dept Location Phone Description]
17
18
  table.separator!
18
19
  table << ['John Doe', 'Finance', 'Los Angeles CA 90089', '555-1555', 'Just a guy']
@@ -118,6 +118,8 @@ class TestTabularize < Test::Unit::TestCase
118
118
 
119
119
  # TODO: Need assertion
120
120
  def test_tabularize_csv
121
+ return if RUBY_VERSION =~ /^1\.8\./
122
+
121
123
  sio = StringIO.new
122
124
 
123
125
  {
@@ -141,7 +143,7 @@ class TestTabularize < Test::Unit::TestCase
141
143
 
142
144
  def test_invalid_arguments
143
145
  assert_raise(ArgumentError) { Tabularize.it(5) }
144
- assert_raise(ArgumentError) { Tabularize.it("hello") }
146
+ assert_raise(ArgumentError) { Tabularize.it("hello") } unless RUBY_VERSION =~ /^1\.8\./
145
147
  assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :align => :noidea) }
146
148
  assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :valign => :noidea) }
147
149
  assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :align => [:center, :top]) }
@@ -263,7 +265,32 @@ I..This should change everything doh!I.............I............................
263
265
  t << %w[12345]
264
266
  puts t.to_s
265
267
  assert t.to_s.lines.all? { |line| line.chomp.length <= w }
266
- assert t.to_s.lines.all? { |line| %w[+ >].include?(line.chomp.reverse[0, 1]) }
268
+ assert t.to_s.lines.all? { |line| line.chomp.reverse[0, 1] == '>' }
267
269
  end
268
270
  end
271
+
272
+ def test_readme
273
+ table = Tabularize.new
274
+ table = Tabularize.new :pad => '.', :pad_left => 2, :pad_right => 0,
275
+ :border_style => :unicode,
276
+ :align => [:left, :center, :right],
277
+ :valign => [:top, :bottom, :middle, :middle],
278
+ :screen_width => 75, :ellipsis => '~'
279
+ table << %w[Name Dept Location Phone Description]
280
+ table.separator!
281
+ table << ['John Doe', 'Finance', 'Los Angeles CA 90089', '555-1555', 'Just a guy']
282
+ table << ['Average Joe', 'Engineering', 'Somewhere over the rainbow', 'N/A', 'Unknown']
283
+ table << ['홍길동', '탁상 3부', "서울역 3번 출구 김씨 옆자리\n\n맞습니다", 'N/A', 'No description']
284
+ assert_equal "
285
+ ┌─────────────┬─────────────┬─────────────────────────────┬──────────~
286
+ │..Name.......│.....Dept....│.....................Location│.....Phone~
287
+ ├─────────────┼─────────────┼─────────────────────────────┼──────────~
288
+ │..John Doe...│....Finance..│.........Los Angeles CA 90089│..555-1555~
289
+ │..Average Joe│..Engineering│...Somewhere over the rainbow│.......N/A~
290
+ │..홍길동.....│.............│..서울역 3번 출구 김씨 옆자리│..........~
291
+ │.............│.............│.............................│.......N/A~
292
+ │.............│...탁상 3부..│.....................맞습니다│..........~
293
+ └─────────────┴─────────────┴─────────────────────────────┴──────────~
294
+ ".strip, table.to_s
295
+ end
269
296
  end
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.2.5
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-25 00:00:00.000000000 Z
12
+ date: 2012-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print