class 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

Private Instance Methods

ansi_formating(cell, col, row) click to toggle source
# File lib/ansi/table.rb, line 158
def ansi_formating(cell, col, row)
  if @format
    case @format.arity
    when 0
      f = @format[]
    when 1
      f = @format[cell]
    when 2 
      f = @format[row, col]
    else
      f = @format[cell, row, col]
    end
  else
    f = nil
  end
  [f].flatten.compact
end
apply_format(str, cell, col, row) click to toggle source
# File lib/ansi/table.rb, line 149
def apply_format(str, cell, col, row)
  if @format
    str.ansi(*ansi_formating(cell, col, row))
  else
    str
  end 
end
cell_template(max) click to toggle source
# File lib/ansi/table.rb, line 123
def cell_template(max)
  case align
  when :right, 'right'
    "#{@pad}%#{max}s"
  else
    "%-#{max}s#{@pad}"
  end
end
column_size() click to toggle source

Number of columns based on the first row of table.

@return [Integer] number of columns

# File lib/ansi/table.rb, line 118
def column_size
  table.first.size
end
dividing_line() click to toggle source

TODO: make more efficient

# File lib/ansi/table.rb, line 133
def dividing_line
  tmp = max_columns(fit).map{ |m| "%#{m}s" }.join(" | ")
  tmp = "| #{tmp} |"
  lin = (tmp % (['-'] * column_size)).gsub(/[^\|]/, '-').gsub('|', '+')
  lin
end
fit_width() click to toggle source

TODO: look at the lines and figure out how many columns will fit

# File lib/ansi/table.rb, line 91
def fit_width
  width = Terminal.terminal_width
  ((width.to_f / column_size) - (padding + 3)).to_i
end
max_columns(fit=false) click to toggle source

Calculate the maximun column sizes.

@return [Array] maximum size for each column

# File lib/ansi/table.rb, line 99
def max_columns(fit=false)
  max = Array.new(column_size, 0)
  table.each do |row|
    row.each_with_index do |col, index|
      col = col.to_s
      col = col.unansi
      if fit
        max[index] = [max[index], col.size, fit_width].max
      else
        max[index] = [max[index], col.size].max
      end
    end
  end
  max
end