Permalink
Browse files

Merge pull request #20 from viking/fix-issue-19

Use colspan in title row when calculating widths
  • Loading branch information...
2 parents bd8c680 + ae55047 commit d4e76365343fab0d45cd78fe64da1718722b6d6f @scottjg scottjg committed Feb 29, 2012
Showing with 16 additions and 13 deletions.
  1. +7 −4 lib/terminal-table/table.rb
  2. +9 −9 spec/table_spec.rb
@@ -11,9 +11,9 @@ class Table
def initialize options = {}, &block
@column_widths = []
self.style = options.fetch :style, {}
- self.title = options.fetch :title, nil
self.headings = options.fetch :headings, []
self.rows = options.fetch :rows, []
+ self.title = options.fetch :title, nil
yield_or_eval(&block) if block
end
@@ -108,8 +108,7 @@ def render
separator = Separator.new(self)
buffer = [separator]
unless @title.nil?
- opts = {:value => @title, :alignment => :center, :colspan => number_of_columns}
- buffer << Row.new(self, [opts])
+ buffer << Row.new(self, [title_cell_options])
buffer << separator
end
unless @headings.cells.empty?
@@ -144,7 +143,7 @@ def style
def title=(title)
@title = title
- recalc_column_widths Row.new(self, [title])
+ recalc_column_widths Row.new(self, [title_cell_options])
end
##
@@ -213,5 +212,9 @@ def yield_or_eval &block
self.instance_eval(&block)
end
end
+
+ def title_cell_options
+ {:value => @title, :alignment => :center, :colspan => number_of_columns}
+ end
end
end
View
@@ -161,15 +161,15 @@ module Terminal
@table << ['b', 2]
@table << ['c', 3]
@table.render.should == <<-EOF.deindent
- +-------+-----+
- | Title |
- +-------+-----+
- | Char | Num |
- +-------+-----+
- | a | 1 |
- | b | 2 |
- | c | 3 |
- +-------+-----+
+ +------+-----+
+ | Title |
+ +------+-----+
+ | Char | Num |
+ +------+-----+
+ | a | 1 |
+ | b | 2 |
+ | c | 3 |
+ +------+-----+
EOF
end

0 comments on commit d4e7636

Please sign in to comment.