From cac570ad1d8805223c0c0d1a388fc091696d0944 Mon Sep 17 00:00:00 2001 From: adorechic Date: Wed, 21 Oct 2015 01:25:44 +0900 Subject: [PATCH 1/4] Count a multibyte char width as 2 chars width --- lib/text-table/cell.rb | 10 +++++++--- lib/text-table/table.rb | 2 +- spec/integration/multibyte_spec.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 spec/integration/multibyte_spec.rb diff --git a/lib/text-table/cell.rb b/lib/text-table/cell.rb index f8298a0..66c509f 100644 --- a/lib/text-table/cell.rb +++ b/lib/text-table/cell.rb @@ -5,7 +5,7 @@ class Cell # The object whose to_s method is called when rendering the cell. # attr_accessor :value - + # Text alignment. Acceptable values are :left (default), # :center and :right # @@ -23,6 +23,10 @@ def initialize(options = {}) #:nodoc: @colspan = options[:colspan] || 1 end + def text_width + value.length + value.chars.count { |c| c.bytesize > 2 } + end + def to_s #:nodoc: ([' ' * table.horizontal_padding]*2).join case align when :left @@ -43,9 +47,9 @@ def column_index #:nodoc: end def cell_width #:nodoc: - (0...colspan).map {|i| table.column_widths[column_index + i]}.inject(&:+) + (colspan - 1)*(2*table.horizontal_padding + table.horizontal_boundary.length) + (0...colspan).map {|i| table.column_widths[column_index + i]}.inject(&:+) + (colspan - 1)*(2*table.horizontal_padding + table.horizontal_boundary.length) - text_width + value.length end end end -end \ No newline at end of file +end diff --git a/lib/text-table/table.rb b/lib/text-table/table.rb index 427e611..d068d84 100644 --- a/lib/text-table/table.rb +++ b/lib/text-table/table.rb @@ -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 diff --git a/spec/integration/multibyte_spec.rb b/spec/integration/multibyte_spec.rb new file mode 100644 index 0000000..94e89cf --- /dev/null +++ b/spec/integration/multibyte_spec.rb @@ -0,0 +1,26 @@ +require 'integration_helper' + +describe Text::Table do + let(:table) { Text::Table.new :rows => rows } + let(:rows) do + [ + %w(Hello), + %w(こんにちは), + %w(مرحبا), + %w(안녕하세요), + ] + end + + subject { table.to_s } + + describe 'rows' do + it { should == deindent(%q{ + +------------+ + | Hello | + | こんにちは | + | مرحبا | + | 안녕하세요 | + +------------+ + }) } + end +end From e7278898eabfcb92326bc9a51e6b2004263d7ebd Mon Sep 17 00:00:00 2001 From: adorechic Date: Thu, 22 Oct 2015 23:05:03 +0900 Subject: [PATCH 2/4] Do not use new syntax --- spec/integration/multibyte_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/integration/multibyte_spec.rb b/spec/integration/multibyte_spec.rb index 94e89cf..c916e17 100644 --- a/spec/integration/multibyte_spec.rb +++ b/spec/integration/multibyte_spec.rb @@ -4,10 +4,10 @@ let(:table) { Text::Table.new :rows => rows } let(:rows) do [ - %w(Hello), - %w(こんにちは), - %w(مرحبا), - %w(안녕하세요), + ["Hello"], + ["こんにちは"], + ["مرحبا"], + ["안녕하세요"], ] end From bb12dec7ec32b36c2b8227212180df23179f96b0 Mon Sep 17 00:00:00 2001 From: adorechic Date: Wed, 28 Oct 2015 01:51:49 +0900 Subject: [PATCH 3/4] Implement for ruby 1.8 --- lib/text-table.rb | 3 ++- lib/text-table/cell.rb | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/text-table.rb b/lib/text-table.rb index 0b71cd4..3820774 100644 --- a/lib/text-table.rb +++ b/lib/text-table.rb @@ -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 \ No newline at end of file +end diff --git a/lib/text-table/cell.rb b/lib/text-table/cell.rb index 66c509f..e29e85f 100644 --- a/lib/text-table/cell.rb +++ b/lib/text-table/cell.rb @@ -24,17 +24,27 @@ def initialize(options = {}) #:nodoc: end def text_width - value.length + value.chars.count { |c| c.bytesize > 2 } + 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 @@ -47,7 +57,7 @@ def column_index #:nodoc: end def cell_width #:nodoc: - (0...colspan).map {|i| table.column_widths[column_index + i]}.inject(&:+) + (colspan - 1)*(2*table.horizontal_padding + table.horizontal_boundary.length) - text_width + value.length + (0...colspan).map {|i| table.column_widths[column_index + i]}.inject(&:+) + (colspan - 1)*(2*table.horizontal_padding + table.horizontal_boundary.length) end end From bedbe84bc37d7d2a3e78152854532ee88be45a3f Mon Sep 17 00:00:00 2001 From: adorechic Date: Wed, 28 Oct 2015 02:15:57 +0900 Subject: [PATCH 4/4] Add magic comment --- spec/integration/multibyte_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/integration/multibyte_spec.rb b/spec/integration/multibyte_spec.rb index c916e17..8b17bf7 100644 --- a/spec/integration/multibyte_spec.rb +++ b/spec/integration/multibyte_spec.rb @@ -1,3 +1,4 @@ +# coding: utf-8 require 'integration_helper' describe Text::Table do