diff --git a/formatador.gemspec b/formatador.gemspec index 9cfa1da..11cc0e6 100644 --- a/formatador.gemspec +++ b/formatador.gemspec @@ -50,6 +50,7 @@ Gem::Specification.new do |s| ## List your runtime dependencies here. Runtime dependencies are those ## that are needed for an end user to actually USE your code. # s.add_dependency('DEPNAME', [">= 1.1.0", "< 2.0.0"]) + s.add_dependency('unicode') ## List your development dependencies here. Development dependencies are ## those that are only needed during development diff --git a/lib/formatador.rb b/lib/formatador.rb index 93cd7a4..9bca780 100644 --- a/lib/formatador.rb +++ b/lib/formatador.rb @@ -1,5 +1,6 @@ require File.join(File.dirname(__FILE__), 'formatador', 'table') require File.join(File.dirname(__FILE__), 'formatador', 'progressbar') +require 'unicode' class Formatador diff --git a/lib/formatador/table.rb b/lib/formatador/table.rb index 49c190e..d89a529 100644 --- a/lib/formatador/table.rb +++ b/lib/formatador/table.rb @@ -80,7 +80,7 @@ def display_compact_table(hashes, keys = nil, &block) private def length(value) - value.to_s.gsub(PARSE_REGEX, '').length + Unicode.width(value.to_s.gsub(PARSE_REGEX, '')) end def calculate_datum(header, hash) diff --git a/tests/table_tests.rb b/tests/table_tests.rb index e387dc9..5e3056d 100644 --- a/tests/table_tests.rb +++ b/tests/table_tests.rb @@ -1,3 +1,4 @@ +# coding: utf-8 Shindo.tests("Formatador: tables") do output = <<-OUTPUT @@ -93,4 +94,22 @@ end end -end \ No newline at end of file + +output = <<-OUTPUT + +------+ + | [bold]a[/] | + +------+ + | 1 | + +------+ + | 震度 | + +------+ +OUTPUT +output = Formatador.parse(output) + + tests("#display_table([{:a => 1}, {:a => 2}])").returns(output) do + capture_stdout do + Formatador.display_table([{:a => 1}, {:a => "震度"}]) + end + end + +end