Browse files

Merge pull request #15 from rrrene/master

Add Separator class to represent separating rows
  • Loading branch information...
2 parents 66208ef + 07256b1 commit fd09e19da087cfc86e5e46befa6e1e0264fac6cb @scottjg scottjg committed Oct 19, 2011
View
3 .gitignore
@@ -2,4 +2,5 @@
pkg
tmp
*.cache
-doc
+doc
+Gemfile.lock
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
59 lib/terminal-table/row.rb
@@ -1,7 +1,6 @@
module Terminal
class Table
class Row
- include Enumerable
##
# Row cells
@@ -13,60 +12,36 @@ class Row
##
# Initialize with _width_ and _options_.
- def initialize table, array
+ def initialize table, array = []
+ @cell_index = 0
@table = table
- @cells = if array == :separator
- array
- else
- index = 0
- array.map do |item|
- options = item.is_a?(Hash) ? item : {:value => item, :index => index}
- cell = Cell.new(options.merge(:index => index, :table => @table))
- index += cell.colspan
- cell
- end
- end
+ @cells = []
+ array.each { |item| self << item }
end
- def [] index
- cells[index] unless separator?
- end
-
- def []= index, value
- cells[index] = value
+ def add_cell item
+ options = item.is_a?(Hash) ? item : {:value => item}
+ cell = Cell.new(options.merge(:index => @cell_index, :table => @table))
+ @cell_index += cell.colspan
+ @cells << cell
end
+ alias << add_cell
- def each &block
- cells.each &block unless separator?
+ def [] index
+ cells[index]
end
def height
cells.map { |c| c.lines.count }.max
end
- def method_missing m, *args, &block
- if cells.respond_to?(m)
- cells.__send__(m, *args, &block)
- else
- super
- end
- 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
3 lib/terminal-table/style.rb
@@ -25,7 +25,8 @@ class Table
class Style
@@defaults = {
:border_x => "-", :border_y => "|", :border_i => "+",
- :padding_left => 1, :padding_right => 1
+ :padding_left => 1, :padding_right => 1,
+ :width => nil
}
attr_accessor :border_x
View
38 lib/terminal-table/table.rb
@@ -32,8 +32,9 @@ def align_column n, alignment
# Add a row.
def add_row array
- @rows << Row.new(self, array)
- recalc_column_widths @rows.last
+ row = array == :separator ? Separator.new(self) : Row.new(self, array)
+ @rows << row
+ recalc_column_widths row
end
alias :<< :add_row
@@ -89,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
##
@@ -104,48 +105,35 @@ 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
##
# Return rows without separator rows.
def rows
- @rows.reject { |row| row.separator? }
+ @rows.reject { |row| row.is_a? Separator }
end
def rows= array
@rows = []
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
@@ -190,9 +178,9 @@ def additional_column_widths
end
def recalc_column_widths row
- if row.is_a?(Symbol) then return end
+ return if row.is_a? Separator
i = 0
- row.each do |cell|
+ row.cells.each do |cell|
colspan = cell.colspan
cell_value = cell.value_for_column_width_recalc
colspan.downto(1) do |j|
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

0 comments on commit fd09e19

Please sign in to comment.