Skip to content
This repository

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

Open
wants to merge 1 commit into from

4 participants

Andrea Bernardo Ciddio Jonathan Chrisp Abhishek Chanda 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 04, 2013
Andrea Bernardo Ciddio bcandrea Fixed the behaviour of Table when the specified width exceeds the tot…
…al column width
fc44bc8
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 11 additions and 1 deletion. Show diff stats Hide diff stats

  1. +1 1  lib/terminal-table/table.rb
  2. +10 0 spec/table_spec.rb
2  lib/terminal-table/table.rb
@@ -166,7 +166,7 @@ def additional_column_widths
166 166 return [] if style.width.nil?
167 167 spacing = style.width - columns_width
168 168 if spacing < 0
169   - raise "Table width exceeds wanted width of #{wanted} characters."
  169 + raise "Table width exceeds wanted width of #{style.width} characters."
170 170 else
171 171 per_col = spacing / number_of_columns
172 172 arr = (1...number_of_columns).to_a.map { |i| per_col }
10 spec/table_spec.rb
@@ -173,6 +173,16 @@ module Terminal
173 173 EOF
174 174 end
175 175
  176 + it "should raise an error if the table width exceeds style width" do
  177 + @table.headings = ['Char', 'Num']
  178 + @table << ['a', 1]
  179 + @table << ['b', 2]
  180 + @table << ['c', 3]
  181 + @table << ['d', 'x' * 22]
  182 + @table.style.width = 21
  183 + expect { @table.render }.to raise_error "Table width exceeds wanted width of 21 characters."
  184 + end
  185 +
176 186 it "should render title properly" do
177 187 @table.title = "Title"
178 188 @table.headings = ['Char', 'Num']

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.