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 +4 -4
- data/Changes +3 -0
- data/README.md +2 -2
- data/Rakefile +2 -1
- data/example/table.rb +2 -2
- data/lib/visual_width/table.rb +32 -22
- data/lib/visual_width/version.rb +1 -1
- data/spec/visual_width/table_spec.rb +1 -1
- data/tool/{benchmark.rb → benchmark-measure.rb} +0 -0
- data/tool/benchmark-table.rb +31 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ede4dcf1c5386fcc56ef316d67d8ec06c197e5f5
|
4
|
+
data.tar.gz: 13f86ac56d180d336347749322f8cffc00c2127d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 288435a9f3f043f3b9ca6882b785a2671a31ee0f882a166abc02152d58a04aacf1d891554557050b56099e94ac6ac3f8b800139cf0a7a1e5c6ba9b053988fd1c
|
7
|
+
data.tar.gz: 268d7f59e6844b8d6b7a9a64e4671e96c86bfe83326b39629b5ef3c123b3d50f47ed1e27a20cae8c178aa5cc2fe88f6d3c8df0b70fcacdeb5ad32f0a85a7d14c
|
data/Changes
CHANGED
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
|
54
|
+
puts t.render(rows)
|
55
55
|
# +--------+--------+-----+
|
56
56
|
# | Nick |FullName| Age |
|
57
57
|
# +--------+--------+-----+
|
data/Rakefile
CHANGED
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
|
17
|
+
puts t.render(rows)
|
data/lib/visual_width/table.rb
CHANGED
@@ -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
|
-
|
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,
|
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
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
77
|
-
align =
|
78
|
-
width =
|
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]
|
126
|
+
style = (@style[i] ||= {})
|
117
127
|
result[i] = (ws << (result[i] || 0) << (style[:width] || 0)).max
|
118
128
|
end
|
119
129
|
end
|
data/lib/visual_width/version.rb
CHANGED
@@ -63,7 +63,7 @@ describe VisualWidth::Table do
|
|
63
63
|
['Average', 93, 96],
|
64
64
|
]
|
65
65
|
|
66
|
-
expect(VisualWidth::Table.new
|
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.
|
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
|