Skip to content

Commit

Permalink
Merge pull request #73 from vizv/fix-fullwidth-characters-output
Browse files Browse the repository at this point in the history
Fix fullwidth characters output. Close #68
  • Loading branch information
nateberkopec authored Aug 29, 2016
2 parents 3e389c4 + 6a20ee7 commit 1ac3873
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/terminal-table/cell.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'unicode/display_width'

module Terminal
class Table
class Cell
Expand Down Expand Up @@ -55,7 +57,8 @@ def lines
def render(line = 0)
left = " " * @table.style.padding_left
right = " " * @table.style.padding_right
render_width = lines[line].to_s.size - escape(lines[line]).size + width
display_width = Unicode::DisplayWidth.of(escape(lines[line]))
render_width = lines[line].to_s.size - display_width + width
align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
end
alias :to_s :render
Expand All @@ -65,7 +68,7 @@ def render(line = 0)
# removes all ANSI escape sequences (e.g. color)

def value_for_column_width_recalc
lines.map{ |s| escape(s) }.max_by{ |s| s.size }
lines.map{ |s| escape(s) }.max_by{ |s| Unicode::DisplayWidth.of(s) }
end

##
Expand Down
4 changes: 3 additions & 1 deletion lib/terminal-table/table.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'unicode/display_width'

module Terminal
class Table

Expand Down Expand Up @@ -200,7 +202,7 @@ def recalc_column_widths row
colspan = cell.colspan
cell_value = cell.value_for_column_width_recalc
colspan.downto(1) do |j|
cell_length = cell_value.to_s.length
cell_length = Unicode::DisplayWidth.of(cell_value.to_s)
if colspan > 1
spacing_length = cell_spacing * (colspan - 1)
length_in_columns = (cell_length - spacing_length)
Expand Down
13 changes: 13 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,5 +591,18 @@ module Terminal
EOF
end

it "should render a table with fullwidth characters" do
@table.headings = ['COL 1', 'COL 2', 'COL 3', 'COL4']
@table << ['中文', 'にっぽんご', '한국어', 'ABC']
@table << ['Chinese','Japanese','Korean', '.......']
@table.render.should == <<-EOF.deindent
+---------+------------+--------+----------+
| COL 1 | COL 2 | COL 3 | COL4 |
+---------+------------+--------+----------+
| 中文 | にっぽんご | 한국어 | ABC |
| Chinese | Japanese | Korean | ....... |
+---------+------------+--------+----------+
EOF
end
end
end
2 changes: 2 additions & 0 deletions terminal-table.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", ">= 3.0"
spec.add_development_dependency "term-ansicolor"
spec.add_development_dependency "pry"

spec.add_runtime_dependency "unicode-display_width", "~> 1.1"
end

0 comments on commit 1ac3873

Please sign in to comment.