class Tablr

Attributes

add_column[RW]
column[RW]
columns[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/tablr/tablr.rb, line 4
def initialize(options = {})
  @columns            ||= TablrSpace::OrderedHash.auto
  @row_data           ||= TablrSpace::OrderedHash.new
  options[:separate]  ||= "false"
  options[:headers]   ||= "true"

  @options = options
end

Public Instance Methods

add_row(column, value) click to toggle source

Add rows

# File lib/tablr/tablr.rb, line 21
def add_row(column, value)
  add_column(column) unless column_exists?(column) or !column.is_a? String

  Array(value).each do |v|
    @columns[column][:rows].push v
    adjust(column, v.length)
  end
end
adjust(column, length) click to toggle source

Adjust max width for padding

# File lib/tablr/tablr.rb, line 31
def adjust(column, length)
  return nil unless column_exists?(column)
  @columns[column][:length] = length if length > @columns[column][:length]
end
display()
Alias for: print
exec()
Alias for: print
print() click to toggle source
Also aliased as: exec, display

Protected Instance Methods

column_exists?(column) click to toggle source
# File lib/tablr/tablr.rb, line 49
def column_exists?(column)
  @columns.has_key? column
end
fill_rows() click to toggle source
# File lib/tablr/tablr.rb, line 85
def fill_rows
  max_count = 0
  @columns.each_with_index do |v, k|
    max_count = v[1][:rows].length if v[1][:rows].length > max_count
  end
  @columns.each_with_index do |v, k|
    (0..(max_count-v[1][:rows].length)).each do |i|
      add_row v[0], ""
    end
  end
end
get_rows() click to toggle source
# File lib/tablr/tablr.rb, line 97
def get_rows
  count, array = @columns.length-1, Array.new
  fill_rows
  @columns.each_with_index do |value, key|
    value[1][:rows].each_with_index do |v, k|
      array[k] = [] if array[k].nil?
      array[k].insert -1, v
    end
  end
  array
end
headers() click to toggle source
# File lib/tablr/tablr.rb, line 60
def headers
  return '' if @options[:headers] == "false"
  str = lines << "\n"
  @columns.each { |k,v| str << "| #{k}" << " " * str_length(k, v[:length], -1) }
  str << "|"
end
lines() click to toggle source
# File lib/tablr/tablr.rb, line 53
def lines
  str = '+'
  # Get number of dashes within columns
  @columns.each { |k,v| str << '-' * v[:length] << '--+' }
  str
end
print_rows() click to toggle source
str_length(key, length, padding=1) click to toggle source
# File lib/tablr/tablr.rb, line 109
def str_length(key, length, padding=1)
  (length-(key.gsub(/\e\[(\d+)m/, '').length)-padding).abs
end