Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed the behaviour of Table when the specified width exceeds column width #30

Open
wants to merge 1 commit into from

6 participants

Andrea Bernardo Ciddio Jonathan Chrisp Abhishek Chanda Darren Cauthon Michal Hantl Carl Myers
Andrea Bernardo Ciddio

This should fix issues/23.

Carl Myers

I ran into this bug too. I'm going to open up a separate issue though because a much bigger problem is how you figure out how to wrap a column without knowing its length, there isn't really a way for the user to find out the evetual length to wrap things properly...

Andrea Bernardo Ciddio

If you don't care about the overall table width, having fields with unknown length should be already handled by the default behaviour of the Table object. On the other hand, if you still want the table to have a fixed width, I guess the only option is to either throw an Error or add a style modifier to truncate the contents artificially:

rows << ['Four', 'A' * 120]
table = Terminal::Table.new
table.headings = ['Word', 'Number']
table.rows = rows
table.style = {:width => 80, :truncate => true}

# > puts table
#
# +--------------------------------------+---------------------------------------+
# | Word                                 | Number                                |
# +--------------------------------------+---------------------------------------+
# | One                                  | 1                                     |
# | Two                                  | 2                                     |
# | Three                                | 3                                     |
# | Four                                 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA... |
# +--------------------------------------+---------------------------------------+
Jonathan Chrisp

Hey all, is this likely to be fixed soon?

Abhishek Chanda

Can I help in merging this? We have a number of bugs depending on this.

Darren Cauthon

I came across this same issue, started to write a PR but found this. Is this to be merged?

Michal Hantl

...year and a half later. whats the status on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 4, 2013
  1. Andrea Bernardo Ciddio
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 1 deletion.
  1. +1 −1  lib/terminal-table/table.rb
  2. +10 −0 spec/table_spec.rb
2  lib/terminal-table/table.rb
View
@@ -166,7 +166,7 @@ def additional_column_widths
return [] if style.width.nil?
spacing = style.width - columns_width
if spacing < 0
- raise "Table width exceeds wanted width of #{wanted} characters."
+ raise "Table width exceeds wanted width of #{style.width} characters."
else
per_col = spacing / number_of_columns
arr = (1...number_of_columns).to_a.map { |i| per_col }
10 spec/table_spec.rb
View
@@ -173,6 +173,16 @@ module Terminal
EOF
end
+ it "should raise an error if the table width exceeds style width" do
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table << ['d', 'x' * 22]
+ @table.style.width = 21
+ expect { @table.render }.to raise_error "Table width exceeds wanted width of 21 characters."
+ end
+
it "should render title properly" do
@table.title = "Title"
@table.headings = ['Char', 'Num']
Something went wrong with that request. Please try again.