Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Separator class to represent separating rows

  • Loading branch information...
commit 41654274d7e86d65570a0e2a2a05123ec7e0a0e6 1 parent f6dd4bb
@rrrene rrrene authored
View
2  lib/terminal-table.rb
@@ -22,6 +22,6 @@
#++
$:.unshift File.dirname(__FILE__)
-%w(version core_ext table cell row style table_helper).each do |file|
+%w(version core_ext table cell row separator style table_helper).each do |file|
require "terminal-table/#{file}"
end
View
38 lib/terminal-table/row.rb
@@ -12,15 +12,11 @@ class Row
##
# Initialize with _width_ and _options_.
- def initialize table, array
+ def initialize table, array = []
@cell_index = 0
@table = table
- if array == :separator
- @cells = array
- else
- @cells = []
- array.each { |item| self << item }
- end
+ @cells = []
+ array.each { |item| self << item }
end
def add_cell item
@@ -32,36 +28,20 @@ def add_cell item
alias << add_cell
def [] index
- cells[index] unless separator?
+ cells[index]
end
def height
cells.map { |c| c.lines.count }.max
end
- def empty?
- cells.empty?
- end
-
- def size
- cells.size
- end
-
def render
y = @table.style.border_y
- if separator?
- @table.separator
- else
- (0...height).to_a.map do |line|
- y + cells.map do |cell|
- cell.render(line)
- end.join(y) + y
- end.join("\n")
- end
- end
-
- def separator?
- cells == :separator
+ (0...height).to_a.map do |line|
+ y + cells.map do |cell|
+ cell.render(line)
+ end.join(y) + y
+ end.join("\n")
end
end
end
View
14 lib/terminal-table/separator.rb
@@ -0,0 +1,14 @@
+module Terminal
+ class Table
+ class Separator < Row
+
+ def render
+ arr_x = (0...@table.number_of_columns).to_a.map do |i|
+ @table.style.border_x * (@table.column_width(i) + @table.cell_padding)
+ end
+ border_i = @table.style.border_i
+ border_i + arr_x.join(border_i) + border_i
+ end
+ end
+ end
+end
View
33 lib/terminal-table/table.rb
@@ -32,7 +32,7 @@ def align_column n, alignment
# Add a row.
def add_row array
- row = array == :separator ? Row.new(self, :separator) : Row.new(self, array)
+ row = array == :separator ? Separator.new(self) : Row.new(self, array)
@rows << row
recalc_column_widths row
end
@@ -90,7 +90,7 @@ def column_width n
# Return total number of columns available.
def number_of_columns
- headings_with_rows.map { |r| r.size }.max
+ headings_with_rows.map { |r| r.cells.size }.max
end
##
@@ -105,22 +105,20 @@ def headings= array
# Render the table.
def render
- @seperator = nil
+ separator = Separator.new(self)
buffer = [separator]
unless @title.nil?
opts = {:value => @title, :alignment => :center, :colspan => number_of_columns}
- buffer << Row.new(self, [opts]).render
+ buffer << Row.new(self, [opts])
buffer << separator
end
- unless @headings.empty?
- buffer << @headings.render
+ unless @headings.cells.empty?
+ buffer << @headings
buffer << separator
end
- buffer += @rows.map do |row|
- row.render
- end
+ buffer += @rows
buffer << separator
- buffer.join("\n")
+ buffer.map { |r| r.render }.join("\n")
end
alias :to_s :render
@@ -128,7 +126,7 @@ def render
# Return rows without separator rows.
def rows
- @rows.reject { |row| row.separator? }
+ @rows.reject { |row| row.is_a? Separator }
end
def rows= array
@@ -136,17 +134,6 @@ def rows= array
array.each { |arr| self << arr }
end
- ##
- # Create a separator based on colum lengths.
-
- def separator
- @separator ||= begin
- style.border_i + (0...number_of_columns).to_a.map do |i|
- style.border_x * (column_width(i) + cell_padding)
- end.join(style.border_i) + style.border_i
- end
- end
-
def style=(options)
style.apply options
end
@@ -191,7 +178,7 @@ def additional_column_widths
end
def recalc_column_widths row
- return if row.separator?
+ return if row.is_a? Separator
i = 0
row.cells.each do |cell|
colspan = cell.colspan
View
3  spec/table_spec.rb
@@ -85,7 +85,8 @@ module Terminal
it "should render separators" do
@table.headings = ['Char', 'Num']
@table << ['a', 1]
- @table.separator.should == '+------+-----+'
+ separator = Terminal::Table::Separator.new(@table)
+ separator.render.should == '+------+-----+'
end
it "should add separator" do
Please sign in to comment.
Something went wrong with that request. Please try again.