Generates a ASCII table with the given options.
# File lib/terminal-table/table.rb, line 11 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, [] yield_or_eval(&block) if block end
Check if other is equal to self. other is considered equal if it contains the same headings and rows.
# File lib/terminal-table/table.rb, line 154 def == other if other.respond_to? :render and other.respond_to? :rows self.headings == other.headings and self.rows == other.rows end end
Add a row.
# File lib/terminal-table/table.rb, line 34 def add_row array row = array == :separator ? Separator.new(self) : Row.new(self, array) @rows << row recalc_column_widths row end
Add a separator.
# File lib/terminal-table/table.rb, line 44 def add_separator self << :separator end
Align column n to the given alignment of :center, :left, or :right.
# File lib/terminal-table/table.rb, line 23 def align_column n, alignment r = rows column(n).each_with_index do |col, i| cell = r[i][n] cell.alignment = alignment unless cell.alignment? end end
# File lib/terminal-table/table.rb, line 52 def cell_padding style.padding_left + style.padding_right end
# File lib/terminal-table/table.rb, line 48 def cell_spacing cell_padding + style.border_y.length end
Return column n.
# File lib/terminal-table/table.rb, line 59 def column n, method = :value, array = rows array.map { |row| cell = row[n] cell && method ? cell.__send__(method) : cell }.compact end
Return length of column n.
# File lib/terminal-table/table.rb, line 83 def column_width n width = @column_widths[n] || 0 width + additional_column_widths[n].to_i end
Return n column including headings.
# File lib/terminal-table/table.rb, line 69 def column_with_headings n, method = :value column n, method, headings_with_rows end
Return columns.
# File lib/terminal-table/table.rb, line 76 def columns (0...number_of_columns).map { |n| column n } end
Set the headings
# File lib/terminal-table/table.rb, line 99 def headings= array @headings = Row.new(self, array) recalc_column_widths @headings end
Return total number of columns available.
# File lib/terminal-table/table.rb, line 92 def number_of_columns headings_with_rows.map { |r| r.cells.size }.max end
Render the table.
# File lib/terminal-table/table.rb, line 107 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 << separator end unless @headings.cells.empty? buffer << @headings buffer << separator end buffer += @rows buffer << separator buffer.map { |r| r.render }.join("\n") end
Return rows without separator rows.
# File lib/terminal-table/table.rb, line 128 def rows @rows.reject { |row| row.is_a? Separator } end
# File lib/terminal-table/table.rb, line 132 def rows= array @rows = [] array.each { |arr| self << arr } end
# File lib/terminal-table/table.rb, line 141 def style @style ||= Style.new end
# File lib/terminal-table/table.rb, line 137 def style=(options) style.apply options end
Generated with the Darkfish Rdoc Generator 2.