Parent

Methods

ANSI::Table

Attributes

align[RW]
border[RW]
fit[RW]

Fit to scree width.

format[RW]
padding[RW]
table[RW]

Public Class Methods

new(table, options={}, &format) click to toggle source

The Table class can be used to output nicely formatted tables with division lines and alignment.

table - array of array

options - align :left or :right options - space to add to each cell options - fit to screen width options -

The format block must return ANSI codes to apply to each cell.

Other Implementations:

TODO: Support for table headers and footers.

# File lib/ansi/table.rb, line 27
def initialize(table, options={}, &format)
  @table   = table
  @padding = options[:padding] || 0
  @align   = options[:align]
  @fit     = options[:fit]
  @border  = options[:border]
  #@ansi    = [options[:ansi]].flatten
  @format  = format

  @pad = " " * @padding
end

Public Instance Methods

to_s() click to toggle source
# File lib/ansi/table.rb, line 58
def to_s #(fit=false)
  #row_count = table.size
  #col_count = table[0].size

  max = max_columns(fit)

  div = dividing_line
  top = div #.gsub('+', ".")
  bot = div #.gsub('+', "'")

  body = []
  table.each_with_index do |row, r|
     body_row = []
     row.each_with_index do |cell, c|
       t = cell_template(max[c])
       s = t % cell.to_s
       body_row << apply_format(s, cell, c, r)
     end
     body << "| " + body_row.join(' | ') + " |"
  end

  if border
    body = body.join("\n#{div}\n")
  else
    body = body.join("\n")
  end

  "#{top}\n#{body}\n#{bot}\n"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.