visual_width 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changes +3 -0
- data/lib/visual_width/formatter.rb +7 -1
- data/lib/visual_width/table.rb +14 -16
- data/lib/visual_width/version.rb +1 -1
- data/spec/visual_width/table_spec.rb +28 -28
- data/tool/benchmark-table.rb +4 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 480253e8f93e5a5cc5c3b5f5473697596750adc8
|
4
|
+
data.tar.gz: 2ea8f8132c912004bed62597f41ae0df4b86ba77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd9f84aff900049f75e95f9cca809dd48893ab0dc4fd8b115cce57b51b2251e611da3537206a7010bc68a3b0f1dd04b32850b77e7bc5eba131282cc301cf04c0
|
7
|
+
data.tar.gz: d4df22a84f010d86bdbf05852b6fe1b78bced1808bc9cd0753a27ce14b9e0e5f20f155ab0e547ecdd3e8324eca523791a9fc6d756f75837a21cf76cf71b6adc9
|
data/Changes
CHANGED
@@ -2,6 +2,12 @@ require 'visual_width'
|
|
2
2
|
|
3
3
|
module VisualWidth::Formatter
|
4
4
|
class Align
|
5
|
+
include VisualWidth
|
6
|
+
|
7
|
+
def initialize(east_asian: VisualWidth::EAST_ASIAN)
|
8
|
+
@east_asian = east_asian
|
9
|
+
end
|
10
|
+
|
5
11
|
def left(cell, width)
|
6
12
|
align(cell, width) do |line, fill|
|
7
13
|
line + (' ' * fill)
|
@@ -24,7 +30,7 @@ module VisualWidth::Formatter
|
|
24
30
|
private
|
25
31
|
|
26
32
|
def align(cell, width)
|
27
|
-
w =
|
33
|
+
w = measure(cell, east_asian: @east_asian)
|
28
34
|
if w > width
|
29
35
|
raise ArgumentError, "Invalid width cell: #{cell.inspect}, width: #{width.inspect}"
|
30
36
|
end
|
data/lib/visual_width/table.rb
CHANGED
@@ -6,9 +6,11 @@ class VisualWidth::Table
|
|
6
6
|
|
7
7
|
attr_accessor :header, :style
|
8
8
|
|
9
|
-
def initialize(header: nil, style: [])
|
9
|
+
def initialize(header: nil, style: [], east_asian: VisualWidth::EAST_ASIAN)
|
10
10
|
@header = header
|
11
11
|
@style = style.clone
|
12
|
+
@east_asian = east_asian
|
13
|
+
@padding = ' '
|
12
14
|
|
13
15
|
if header
|
14
16
|
header.length.times do |i|
|
@@ -80,23 +82,19 @@ class VisualWidth::Table
|
|
80
82
|
|
81
83
|
def draw_row(output, max_widths, style, row)
|
82
84
|
output << '|'
|
83
|
-
|
85
|
+
pre = @padding
|
86
|
+
post = @padding + '|'
|
87
|
+
rows = [] # for multi-lined cell
|
84
88
|
max_widths.length.times do |i|
|
85
89
|
cell = "#{row[i]}"
|
86
90
|
st = (style[i] ||= {})
|
87
91
|
align = st[:align] || :left
|
88
92
|
width = st[:width] || max_widths[i]
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
output << '|'
|
95
|
-
if c.length > 0
|
96
|
-
c.each_with_index do |new_cell, row_id|
|
97
|
-
rows[row_id] ||= []
|
98
|
-
rows[row_id][i] = new_cell
|
99
|
-
end
|
93
|
+
first, *rest = cell.split(/\n/)
|
94
|
+
output << pre << aligner.send(align, first.to_s.strip, width) << post
|
95
|
+
rest.each_with_index do |new_cell, row_idx|
|
96
|
+
rows[row_idx] ||= []
|
97
|
+
rows[row_idx][i] = new_cell
|
100
98
|
end
|
101
99
|
end
|
102
100
|
output << "\n"
|
@@ -109,11 +107,11 @@ class VisualWidth::Table
|
|
109
107
|
end
|
110
108
|
|
111
109
|
def line (output, widths)
|
112
|
-
output << '+' << widths.map { |width| '-' * width }.join('+') << "+\n"
|
110
|
+
output << '+' << widths.map { |width| '-' * (width+2) }.join('+') << "+\n"
|
113
111
|
end
|
114
112
|
|
115
113
|
def aligner
|
116
|
-
VisualWidth::Formatter::Align.new
|
114
|
+
VisualWidth::Formatter::Align.new(east_asian: @east_asian)
|
117
115
|
end
|
118
116
|
|
119
117
|
def calc_max_widths(rows) # -> [max_col0_width, max_col1_width, ...]
|
@@ -121,7 +119,7 @@ class VisualWidth::Table
|
|
121
119
|
rows.each_with_index do |row|
|
122
120
|
row.each_with_index do |cell, i|
|
123
121
|
ws = "#{cell}".split(/\n/).map do |line|
|
124
|
-
measure(line)
|
122
|
+
measure(line, east_asian: @east_asian)
|
125
123
|
end
|
126
124
|
style = (@style[i] ||= {})
|
127
125
|
result[i] = (ws << (result[i] || 0) << (style[:width] || 0)).max
|
data/lib/visual_width/version.rb
CHANGED
@@ -18,11 +18,11 @@ describe VisualWidth::Table do
|
|
18
18
|
t = VisualWidth::Table.new
|
19
19
|
|
20
20
|
expect(t.render(rows)).to eql(<<-'TEXT')
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
+------------+------------+
|
22
|
+
| りんご | なし |
|
23
|
+
| べにしぐれ | せいぎょく |
|
24
|
+
| となみ | いなぎ |
|
25
|
+
+------------+------------+
|
26
26
|
TEXT
|
27
27
|
end
|
28
28
|
|
@@ -32,11 +32,11 @@ describe VisualWidth::Table do
|
|
32
32
|
)
|
33
33
|
|
34
34
|
expect(t.render(rows)).to eql(<<-'TEXT')
|
35
|
-
|
36
|
-
|
|
37
|
-
|
38
|
-
|
|
39
|
-
|
35
|
+
+------------+------------+
|
36
|
+
| りんご | なし |
|
37
|
+
| べにしぐれ | せいぎょく |
|
38
|
+
| となみ | いなぎ |
|
39
|
+
+------------+------------+
|
40
40
|
TEXT
|
41
41
|
end
|
42
42
|
|
@@ -45,13 +45,13 @@ describe VisualWidth::Table do
|
|
45
45
|
header: %w(A B),
|
46
46
|
)
|
47
47
|
expect(t.render(rows)).to eql(<<-'TEXT')
|
48
|
-
|
49
|
-
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
48
|
+
+------------+------------+
|
49
|
+
| A | B |
|
50
|
+
+------------+------------+
|
51
|
+
| りんご | なし |
|
52
|
+
| べにしぐれ | せいぎょく |
|
53
|
+
| となみ | いなぎ |
|
54
|
+
+------------+------------+
|
55
55
|
TEXT
|
56
56
|
end
|
57
57
|
|
@@ -64,13 +64,13 @@ describe VisualWidth::Table do
|
|
64
64
|
]
|
65
65
|
|
66
66
|
expect(VisualWidth::Table.new(header: header).render(rows)).to eql(<<-'TEXT')
|
67
|
-
|
68
|
-
|Student|Mid-Terms|Finals|
|
69
|
-
|
70
|
-
|Sam
|
71
|
-
|Jane
|
72
|
-
|Average|93
|
73
|
-
|
67
|
+
+---------+-----------+--------+
|
68
|
+
| Student | Mid-Terms | Finals |
|
69
|
+
+---------+-----------+--------+
|
70
|
+
| Sam | 94 | 93 |
|
71
|
+
| Jane | 92 | 99 |
|
72
|
+
| Average | 93 | 96 |
|
73
|
+
+---------+-----------+--------+
|
74
74
|
TEXT
|
75
75
|
end
|
76
76
|
end
|
@@ -79,10 +79,10 @@ describe VisualWidth::Table do
|
|
79
79
|
it "wraps with 10 width" do
|
80
80
|
t = VisualWidth::Table.new(style: [{width: 10}, {width: 10}])
|
81
81
|
expect(t.render([['テレポーター!', '*いしのなかにいる*']])).to eql(<<-'TEXT')
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
82
|
+
+------------+------------+
|
83
|
+
| テレポータ | *いしのな |
|
84
|
+
| ー! | かにいる* |
|
85
|
+
+------------+------------+
|
86
86
|
TEXT
|
87
87
|
end
|
88
88
|
end
|
data/tool/benchmark-table.rb
CHANGED
@@ -14,9 +14,10 @@ end
|
|
14
14
|
tabler = VisualWidth::Table.new(
|
15
15
|
header: header,
|
16
16
|
)
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
|
18
|
+
a = tabler.render(rows)
|
19
|
+
b = Terminal::Table.new(rows: rows, headings: header)
|
20
|
+
c = Text::Table.new(head: header, rows: rows)
|
20
21
|
|
21
22
|
Benchmark.bmbm do |x|
|
22
23
|
x.report("visual_width/table") do
|