visual_width 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 970d02ea37970eae029eb5078fd36eb85e662d9e
4
- data.tar.gz: 619d3e14f123e4c9858b9328731209d8fe2f5efd
3
+ metadata.gz: ede4dcf1c5386fcc56ef316d67d8ec06c197e5f5
4
+ data.tar.gz: 13f86ac56d180d336347749322f8cffc00c2127d
5
5
  SHA512:
6
- metadata.gz: 1c531f47569a9c8c4b616be2cf20d6f5bbc8235f61f07ae1e04f02f83863858e47efc73c44d73c5357c51711159c7d4158e2d3e09651ce95d9036e58402ca864
7
- data.tar.gz: b2b573865cb4e4ec041f2bc12e5344ac01636dec1a3198273cd1dbf2ba5eb6c723216f436d7d5b72f0ff4e2b6c39677ca69699527a3949bb589343bdf1133cfb
6
+ metadata.gz: 288435a9f3f043f3b9ca6882b785a2671a31ee0f882a166abc02152d58a04aacf1d891554557050b56099e94ac6ac3f8b800139cf0a7a1e5c6ba9b053988fd1c
7
+ data.tar.gz: 268d7f59e6844b8d6b7a9a64e4671e96c86bfe83326b39629b5ef3c123b3d50f47ed1e27a20cae8c178aa5cc2fe88f6d3c8df0b70fcacdeb5ad32f0a85a7d14c
data/Changes CHANGED
@@ -1,5 +1,8 @@
1
1
  This is a revision history of visual_width gem.
2
2
 
3
+ 0.0.3 2013-10-26 15:58:51+0900
4
+ - Revise interface of VisualWidth::Table
5
+
3
6
  0.0.2 2013-10-26 08:51:02+0900
4
7
  - Added visual_width/table.rb to draw text tables
5
8
 
data/README.md CHANGED
@@ -43,15 +43,15 @@ See [Ambiguous Characters](http://www.unicode.org/reports/tr11/#Ambiguous) in th
43
43
  { align: :center, width: 8 },
44
44
  { align: :right, width: 5 },
45
45
  ],
46
+ header: ['Nick', 'FullName', 'Age'],
46
47
  )
47
48
 
48
- header = ['Nick', 'FullName', 'Age']
49
49
  rows = [
50
50
  ['カネダ', '金田 正太郎', 17],
51
51
  ['テツオ', '島 鉄雄', 16],
52
52
  ['ケイ', '?', 18],
53
53
  ]
54
- puts t.render(rows, header: header)
54
+ puts t.render(rows)
55
55
  # +--------+--------+-----+
56
56
  # | Nick |FullName| Age |
57
57
  # +--------+--------+-----+
data/Rakefile CHANGED
@@ -10,5 +10,6 @@ task :data do
10
10
  end
11
11
 
12
12
  task :bench do
13
- sh "ruby -Ilib tool/benchmark.rb"
13
+ sh "ruby -Ilib tool/benchmark-measure.rb"
14
+ sh "ruby -Ilib tool/benchmark-table.rb"
14
15
  end
data/example/table.rb CHANGED
@@ -6,12 +6,12 @@ t = VisualWidth::Table.new(
6
6
  { align: :center, width: 8 },
7
7
  { align: :right, width: 5 },
8
8
  ],
9
+ header: ['Nick', 'FullName', 'Age'],
9
10
  )
10
11
 
11
- header = ['Nick', 'FullName', 'Age']
12
12
  rows = [
13
13
  ['カネダ', '金田 正太郎', 17],
14
14
  ['テツオ', '島 鉄雄', 16],
15
15
  ['ケイ', '?', 18],
16
16
  ]
17
- puts t.render(rows, header: header)
17
+ puts t.render(rows)
@@ -8,19 +8,29 @@ class VisualWidth::Table
8
8
 
9
9
  def initialize(header: nil, style: [])
10
10
  @header = header
11
- @style = style
11
+ @style = style.clone
12
12
 
13
- @needs_wrap = @style.any? { |style| style[:width] != nil }
13
+ if header
14
+ header.length.times do |i|
15
+ @style[i] ||= {}
16
+ end
17
+ end
18
+
19
+ widths = @style.map { |s| s[:width] }
20
+ @needs_wrap = widths.any?
21
+ if header
22
+ @header_widths = calc_max_widths([header])
23
+ end
14
24
  end
15
25
 
16
- def render(rows, header: nil, output: "")
26
+ def render(rows, output: "")
17
27
  if @needs_wrap
18
- default_style = {}
19
28
  rows = rows.map do |row|
20
29
  i = 0
21
30
  row.map do |cell|
22
31
  cell = "#{cell}"
23
- width = (@style[i] || default_style)[:width]
32
+ style = (@style[i] ||= {})
33
+ width = style[:width]
24
34
  i += 1
25
35
  if width
26
36
  wrap(cell, width)
@@ -31,19 +41,20 @@ class VisualWidth::Table
31
41
  end
32
42
  end
33
43
  max_widths = calc_max_widths(rows)
34
- h = header || @header
35
- style_header = []
36
- if h
37
- max_widths = calc_max_widths([h])
44
+ if @header
45
+ header_style = []
46
+ max_widths = @header_widths
38
47
  .zip(max_widths)
39
48
  .map { |values| values.max }
40
- h.length.times do |i|
41
- style = @style[i] || {}
42
-
43
- style_header << style.merge(align: :center)
49
+ @header.length.times do |i|
50
+ style = (@style[i] ||= {})
51
+ header_style << style.merge(align: :center)
44
52
  end
53
+ draw_header(output, max_widths, header_style, @header)
54
+ else
55
+ line(output, max_widths)
45
56
  end
46
- draw_header(output, max_widths, style_header, h)
57
+
47
58
  rows.each do |row|
48
59
  draw_row(output, max_widths, @style, row)
49
60
  end
@@ -69,18 +80,18 @@ class VisualWidth::Table
69
80
 
70
81
  def draw_row(output, max_widths, style, row)
71
82
  output << '|'
72
-
73
83
  rows = []
74
84
  max_widths.length.times do |i|
75
85
  cell = "#{row[i]}"
76
- s = style[i] || {}
77
- align = s[:align] || :left
78
- width = s[:width] || max_widths[i]
86
+ st = (style[i] ||= {})
87
+ align = st[:align] || :left
88
+ width = st[:width] || max_widths[i]
79
89
  c = cell.split(/\n/)
80
90
  if c.length == 0
81
91
  c << ""
82
92
  end
83
- output << aligner.send(align, c.shift.strip, width) << '|'
93
+ output << aligner.send(align, c.shift.strip, width)
94
+ output << '|'
84
95
  if c.length > 0
85
96
  c.each_with_index do |new_cell, row_id|
86
97
  rows[row_id] ||= []
@@ -106,14 +117,13 @@ class VisualWidth::Table
106
117
  end
107
118
 
108
119
  def calc_max_widths(rows) # -> [max_col0_width, max_col1_width, ...]
109
- result = []
110
- default_style = {}
120
+ result = Array.new((@header || rows[0] || []).length, 0)
111
121
  rows.each_with_index do |row|
112
122
  row.each_with_index do |cell, i|
113
123
  ws = "#{cell}".split(/\n/).map do |line|
114
124
  measure(line)
115
125
  end
116
- style = @style[i] || default_style
126
+ style = (@style[i] ||= {})
117
127
  result[i] = (ws << (result[i] || 0) << (style[:width] || 0)).max
118
128
  end
119
129
  end
@@ -1,3 +1,3 @@
1
1
  module VisualWidth
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -63,7 +63,7 @@ describe VisualWidth::Table do
63
63
  ['Average', 93, 96],
64
64
  ]
65
65
 
66
- expect(VisualWidth::Table.new.render(rows, header: header)).to eql(<<-'TEXT')
66
+ expect(VisualWidth::Table.new(header: header).render(rows)).to eql(<<-'TEXT')
67
67
  +-------+---------+------+
68
68
  |Student|Mid-Terms|Finals|
69
69
  +-------+---------+------+
File without changes
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'benchmark'
4
+ require 'visual_width/table'
5
+ require 'terminal-table'
6
+ require 'text-table'
7
+
8
+ header = [:Name, :Arity?, :DefinedInTheClass?]
9
+
10
+ rows = String.instance_methods.map do |sym|
11
+ [sym, "".method(sym).arity, "".class.superclass.instance_methods.include?(sym)]
12
+ end
13
+
14
+ tabler = VisualWidth::Table.new(
15
+ header: header,
16
+ )
17
+ puts tabler.render(rows)
18
+ #puts Terminal::Table.new(rows: rows, headings: header)
19
+ #puts Text::Table.new(head: header, rows: rows)
20
+
21
+ Benchmark.bmbm do |x|
22
+ x.report("visual_width/table") do
23
+ 100.times { tabler.render(rows) }
24
+ end
25
+ x.report("terminal-table") do
26
+ 100.times { Terminal::Table.new(rows: rows, headings: header).to_s }
27
+ end
28
+ x.report("text-table") do
29
+ 100.times { Text::Table.new(rows: rows, head: header).to_s }
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visual_width
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fuji, Goro (gfx)
@@ -64,7 +64,8 @@ files:
64
64
  - spec/visual_width/string_refine_spec.rb
65
65
  - spec/visual_width/table_spec.rb
66
66
  - spec/visual_width_spec.rb
67
- - tool/benchmark.rb
67
+ - tool/benchmark-measure.rb
68
+ - tool/benchmark-table.rb
68
69
  - tool/east-asian-width.pl
69
70
  - tool/tables.rb
70
71
  - visual_width.gemspec