class Terminal::Table
Constants
- VERSION
Attributes
headings[R]
title[R]
Public Class Methods
new(options = {})
click to toggle source
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.headings = options.fetch :headings, [] self.rows = options.fetch :rows, [] self.title = options.fetch :title, nil yield_or_eval(&block) if block end
Public Instance Methods
==(other)
click to toggle source
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 159 def == other if other.respond_to? :render and other.respond_to? :rows self.headings == other.headings and self.rows == other.rows end end
add_row(array)
click to toggle source
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
Also aliased as: <<
add_separator()
click to toggle source
Add a separator.
# File lib/terminal-table/table.rb, line 44 def add_separator self << :separator end
align_column(n, alignment)
click to toggle source
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
cell_padding()
click to toggle source
# File lib/terminal-table/table.rb, line 52 def cell_padding style.padding_left + style.padding_right end
cell_spacing()
click to toggle source
# File lib/terminal-table/table.rb, line 48 def cell_spacing cell_padding + style.border_y.length end
column(n, method = :value, array = rows)
click to toggle source
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
column_width(n)
click to toggle source
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
Also aliased as: length_of_column
column_with_headings(n, method = :value)
click to toggle source
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
columns()
click to toggle source
Return columns.
# File lib/terminal-table/table.rb, line 76 def columns (0...number_of_columns).map { |n| column n } end
headings=(arrays)
click to toggle source
Set the headings
# File lib/terminal-table/table.rb, line 99 def headings= arrays arrays = [arrays] unless arrays.first.is_a?(Array) @headings = arrays.map do |array| row = Row.new(self, array) recalc_column_widths row row end end
number_of_columns()
click to toggle source
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()
click to toggle source
Render the table.
# File lib/terminal-table/table.rb, line 111 def render separator = Separator.new(self) buffer = [separator] unless @title.nil? buffer << Row.new(self, [title_cell_options]) buffer << separator end @headings.each do |row| unless row.cells.empty? buffer << row buffer << separator end end buffer += @rows buffer << separator buffer.map { |r| r.render }.join("\n") end
Also aliased as: to_s
rows()
click to toggle source
Return rows without separator rows.
# File lib/terminal-table/table.rb, line 133 def rows @rows.reject { |row| row.is_a? Separator } end
rows=(array)
click to toggle source
# File lib/terminal-table/table.rb, line 137 def rows= array @rows = [] array.each { |arr| self << arr } end
style()
click to toggle source
# File lib/terminal-table/table.rb, line 146 def style @style ||= Style.new end
style=(options)
click to toggle source
# File lib/terminal-table/table.rb, line 142 def style=(options) style.apply options end
title=(title)
click to toggle source
# File lib/terminal-table/table.rb, line 150 def title=(title) @title = title recalc_column_widths Row.new(self, [title_cell_options]) end
Private Instance Methods
additional_column_widths()
click to toggle source
# File lib/terminal-table/table.rb, line 171 def additional_column_widths return [] if style.width.nil? spacing = style.width - columns_width if spacing < 0 raise "Table width exceeds wanted width of #{style.width} characters." else per_col = spacing / number_of_columns arr = (1...number_of_columns).to_a.map { |i| per_col } other_cols = arr.inject(0) { |s, i| s + i } arr << spacing - other_cols arr end end
columns_width()
click to toggle source
# File lib/terminal-table/table.rb, line 167 def columns_width @column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length end
headings_with_rows()
click to toggle source
Return headings combined with rows.
# File lib/terminal-table/table.rb, line 209 def headings_with_rows @headings + rows end
recalc_column_widths(row)
click to toggle source
# File lib/terminal-table/table.rb, line 185 def recalc_column_widths row return if row.is_a? Separator i = 0 row.cells.each do |cell| colspan = cell.colspan cell_value = cell.value_for_column_width_recalc colspan.downto(1) do |j| cell_length = cell_value.to_s.length if colspan > 1 spacing_length = cell_spacing * (colspan - 1) length_in_columns = (cell_length - spacing_length) cell_length = (length_in_columns.to_f / colspan).ceil end if @column_widths[i].to_i < cell_length @column_widths[i] = cell_length end i = i + 1 end end end
title_cell_options()
click to toggle source
# File lib/terminal-table/table.rb, line 222 def title_cell_options {:value => @title, :alignment => :center, :colspan => number_of_columns} end
yield_or_eval() { |self| ... }
click to toggle source
# File lib/terminal-table/table.rb, line 213 def yield_or_eval &block return unless block if block.arity > 0 yield self else self.instance_eval(&block) end end