class Ppl::Format::Table
Constants
- SEPARATOR_SPACES
- SEPARATOR_TABS
Attributes
colors[RW]
columns[RW]
rows[RW]
separator[RW]
Public Class Methods
new(columns=[], colors={})
click to toggle source
# File lib/ppl/format/table.rb, line 13 def initialize(columns=[], colors={}) @columns = columns @rows = [] @separator = SEPARATOR_SPACES @colors = colors @color_adapter = Ppl::Adapter::Color::Colored.new @column_widths = {} @columns.each { |c| @column_widths[c] = 0 } end
Public Instance Methods
add_row(row={})
click to toggle source
# File lib/ppl/format/table.rb, line 23 def add_row(row={}) row.each do |column, value| width = sprintf("%s", value).length max_width = @column_widths[column] if width > max_width @column_widths[column] = width end end @rows.push(row) end
disable_colors!()
click to toggle source
# File lib/ppl/format/table.rb, line 40 def disable_colors! @colors = {} end
to_s()
click to toggle source
# File lib/ppl/format/table.rb, line 34 def to_s string = "" @rows.each { |row| string += format_row(row).rstrip + "\n" } string.rstrip end
Private Instance Methods
colorize_string(string, column)
click to toggle source
# File lib/ppl/format/table.rb, line 65 def colorize_string(string, column) @color_adapter.colorize(string, @colors[column.to_s]) end
format_cell(row, column)
click to toggle source
# File lib/ppl/format/table.rb, line 54 def format_cell(row, column) width = @column_widths[column] string = row[column].to_s if @separator == SEPARATOR_SPACES string = sprintf("%-#{width}s ", string) else string = sprintf("%s\t", string) end colorize_string(string, column) end
format_row(row)
click to toggle source
# File lib/ppl/format/table.rb, line 46 def format_row(row) string = "" @columns.each do |column| string += format_cell(row, column).force_encoding("UTF-8") end return string end