Parent

Files

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.title = options.fetch :title, nil
  self.headings = options.fetch :headings, []
  self.rows = options.fetch :rows, []
  yield_or_eval(&block) if block
end

Public Instance Methods

<<(array) click to toggle source
Alias for: add_row
==(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 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_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=(array) click to toggle source

Set the headings

# File lib/terminal-table/table.rb, line 99
def headings= array
  @headings = Row.new(self, array)
  recalc_column_widths @headings
end
length_of_column(n) click to toggle source
Alias for: column_width
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 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
Also aliased as: to_s
rows() click to toggle source

Return rows without separator rows.

# File lib/terminal-table/table.rb, line 128
def rows
  @rows.reject { |row| row.is_a? Separator }
end
rows=(array) click to toggle source
# File lib/terminal-table/table.rb, line 132
def rows= array
  @rows = []
  array.each { |arr| self << arr }
end
style() click to toggle source
# File lib/terminal-table/table.rb, line 141
def style
  @style ||= Style.new
end
style=(options) click to toggle source
# File lib/terminal-table/table.rb, line 137
def style=(options)
  style.apply options
end
title=(title) click to toggle source
# File lib/terminal-table/table.rb, line 145
def title=(title)
  @title = title
  recalc_column_widths Row.new(self, [title])
end
to_s() click to toggle source
Alias for: render

[Validate]

Generated with the Darkfish Rdoc Generator 2.