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

Fix fullwidth characters output. Close #68 #73

Merged
merged 1 commit into from
Aug 29, 2016
Merged
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
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 @@ -192,7 +194,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