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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b19580c1adb8f868c39960c5e6368809e361d44
4
- data.tar.gz: f835155f72a5f98763c52c63f76473841955863f
3
+ metadata.gz: 480253e8f93e5a5cc5c3b5f5473697596750adc8
4
+ data.tar.gz: 2ea8f8132c912004bed62597f41ae0df4b86ba77
5
5
  SHA512:
6
- metadata.gz: 5b5039c4c044dda8c265f274980a64ed2c15b2bcefa1ec96c0d9abcaa03f2850b9b71927127b82e98a85afaa075e9597cd3f621a3f6dfe7f4f89809f6d80ef04
7
- data.tar.gz: 6c2bb85713d02ec1cab6a25568d699170a4018060c4a9eed7ec338bbb68fa219c337e39de1530c21c1e791cb77d275336d66325a68807b7828551d3e8555ba0b
6
+ metadata.gz: fd9f84aff900049f75e95f9cca809dd48893ab0dc4fd8b115cce57b51b2251e611da3537206a7010bc68a3b0f1dd04b32850b77e7bc5eba131282cc301cf04c0
7
+ data.tar.gz: d4df22a84f010d86bdbf05852b6fe1b78bced1808bc9cd0753a27ce14b9e0e5f20f155ab0e547ecdd3e8324eca523791a9fc6d756f75837a21cf76cf71b6adc9
data/Changes CHANGED
@@ -1,5 +1,8 @@
1
1
  This is a revision history of visual_width gem.
2
2
 
3
+ 0.0.6 2013-10-26 17:10:50+0900
4
+ - Add paddings
5
+
3
6
  0.0.5 2013-10-26 16:01:33+0900
4
7
  - Re-packaging the gem
5
8
 
@@ -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 = VisualWidth.measure(cell)
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
@@ -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
- rows = []
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
- c = cell.split(/\n/)
90
- if c.length == 0
91
- c << ""
92
- end
93
- output << aligner.send(align, c.shift.strip, width)
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
@@ -1,3 +1,3 @@
1
1
  module VisualWidth
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -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
- | A | B |
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 |94 |93 |
71
- |Jane |92 |99 |
72
- |Average|93 |96 |
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
@@ -14,9 +14,10 @@ end
14
14
  tabler = VisualWidth::Table.new(
15
15
  header: header,
16
16
  )
17
- puts tabler.render(rows)
18
- #puts Terminal::Table.new(rows: rows, headings: header)
19
- #puts Text::Table.new(head: header, rows: rows)
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
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fuji, Goro (gfx)