class Shellout::Table

Public Class Methods

new(table={}) click to toggle source
# File lib/shellout/table.rb, line 6
def initialize(table={})
  @data = []
  @data << table[:headers] if table.has_key? :headers
  @data += table[:rows]    if table.has_key? :rows
  @data << table[:footers] if table.has_key? :footers
  
  @has_headers = table.has_key? :headers
  @has_footers = table.has_key? :footers
end

Public Instance Methods

print(out=$stdout) click to toggle source

Private Instance Methods

build_format() click to toggle source
# File lib/shellout/table.rb, line 47
def build_format
  data = @data.clone
  data.shift if @has_headers
  
  numeric_columns = data.transpose.map do |col|
    col.reduce(true) do |is_numeric, c|
      is_numeric &&= c.is_a?(Numeric) || c.match(/\d+[:.,]?\d*$/)
    end
  end
  
  format = widths.reduce('│') do |f, l|
    justify = numeric_columns.shift ? '' : '-'
    f + " %#{justify}#{l}s │"
  end + "\n"
end
center(row) click to toggle source
# File lib/shellout/table.rb, line 39
def center(row)
  row.zip(widths).map {|s,l| s.center(l)}      
end
widths() click to toggle source
# File lib/shellout/table.rb, line 43
def widths
  @widths ||= @data.transpose.map {|c| c.map(&:to_s).map(&:size).max }
end