Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count a multibyte char width as 2 chars width #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/text-table.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$KCODE = 'u'
%w(table row cell enumerable symbol).each do |lib|
require File.join(File.dirname(__FILE__), 'text-table', lib)
end
end
24 changes: 19 additions & 5 deletions lib/text-table/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Cell
# The object whose <tt>to_s</tt> method is called when rendering the cell.
#
attr_accessor :value

# Text alignment. Acceptable values are <tt>:left</tt> (default),
# <tt>:center</tt> and <tt>:right</tt>
#
Expand All @@ -23,14 +23,28 @@ def initialize(options = {}) #:nodoc:
@colspan = options[:colspan] || 1
end

def text_width
char_count + value.chars.count { |c| c.bytesize > 2 }
end

def char_count
@char_count ||= value.split('').length
end

def to_s #:nodoc:
if RUBY_VERSION < '1.9'
len = value.bytesize + cell_width - text_width
else
len = value.length + cell_width - text_width
end

([' ' * table.horizontal_padding]*2).join case align
when :left
value.ljust cell_width
value.ljust len
when :right
value.rjust cell_width
value.rjust len
when :center
value.center cell_width
value.center len
end
end

Expand All @@ -48,4 +62,4 @@ def cell_width #:nodoc:

end
end
end
end
2 changes: 1 addition & 1 deletion lib/text-table/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def all_text_table_rows #:nodoc:
def column_widths #:nodoc:
@column_widths ||= \
all_text_table_rows.reject {|row| row.cells == :separator}.map do |row|
row.cells.map {|cell| [(cell.value.length/cell.colspan.to_f).ceil] * cell.colspan}.flatten
row.cells.map {|cell| [(cell.text_width/cell.colspan.to_f).ceil] * cell.colspan}.flatten
end.transpose.map(&:max)
end

Expand Down
27 changes: 27 additions & 0 deletions spec/integration/multibyte_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
require 'integration_helper'

describe Text::Table do
let(:table) { Text::Table.new :rows => rows }
let(:rows) do
[
["Hello"],
["こんにちは"],
["مرحبا"],
["안녕하세요"],
]
end

subject { table.to_s }

describe 'rows' do
it { should == deindent(%q{
+------------+
| Hello |
| こんにちは |
| مرحبا |
| 안녕하세요 |
+------------+
}) }
end
end