class FormatOutput::ColumnBuilder

A class to build columns for display.

Public Class Methods

new(options) click to toggle source

Prepare a blank page.

# File lib/format_output/builders/column_builder.rb, line 10
def initialize(options)
  @body        = ::FormatOutput.body(options)
  @pad         = ::FormatOutput.pad(options)
  @page_data   = []
end

Public Instance Methods

add(raw_item) click to toggle source

Add an item to this page. Returns: The number of items that did not fit in the page.

# File lib/format_output/builders/column_builder.rb, line 18
def add(raw_item)
  item = raw_item.to_s
  fail "Item too large to fit." unless item.length < @body

  if (column = find_next_column)
    @page_data[column] << item
  else
    @page_data << [item]
  end

  adjust_configuration
end
render() click to toggle source

Render the page as an array of strings.

# File lib/format_output/builders/column_builder.rb, line 32
def render
  results, column_widths = [], get_column_widths

  rows.times { |row_index| results << render_row(row_index, column_widths)}

  @page_data.clear
  results
end

Private Instance Methods

add_a_row() click to toggle source

Add a row to the page, moving items as needed.

# File lib/format_output/builders/column_builder.rb, line 64
def add_a_row
  new_rows = rows + 1
  pool, @page_data = @page_data.flatten, []

  until pool.empty?
    @page_data << pool.shift(new_rows)
  end
end
adjust_configuration() click to toggle source

Make sure the page fits within its boundaries.

# File lib/format_output/builders/column_builder.rb, line 57
def adjust_configuration
  while total_width >= @body
    add_a_row
  end
end
empty?() click to toggle source

Is this page empty?

# File lib/format_output/builders/column_builder.rb, line 99
def empty?
  @page_data.empty?
end
find_next_column() click to toggle source

Which column will receive the next item? Returns: The index of the first non-filled column or nil if none found.

# File lib/format_output/builders/column_builder.rb, line 105
def find_next_column
  (1...(@page_data.length)).each do |index|
    if @page_data[index].length < @page_data[index-1].length
      return index
    end
  end

  nil
end
fits?() click to toggle source

Does the data fit on the page?

# File lib/format_output/builders/column_builder.rb, line 89
def fits?
  total_width < @body
end
get_column_widths() click to toggle source

Get the widths of all columns

# File lib/format_output/builders/column_builder.rb, line 44
def get_column_widths
  @page_data.map {|column| column.format_output_greatest_width}
end
render_row(row_index, widths) click to toggle source

Render a single row of data. Returns: A string.

# File lib/format_output/builders/column_builder.rb, line 50
def render_row(row_index, widths)
  @pad + @page_data.each_with_index.map do |column, index|
    column[row_index].to_s.ljust(widths[index])
  end.join(" ")
end
rows() click to toggle source

How many rows are currently in this page?

# File lib/format_output/builders/column_builder.rb, line 94
def rows
  empty? ? 0 : @page_data[0].length
end
total_width() click to toggle source

Compute the total width of all of the columns. Returns: The sum of the widths of the widest items of each column plus

a space between each of those columns.
# File lib/format_output/builders/column_builder.rb, line 76
def total_width
  if empty?
    0
  else
    #The starting point, @page_data.length-1, represents the spaces needed
    #between the columns. So N columns means N-1 spaces.
    @page_data.inject(@page_data.length-1) do |sum, column|
      sum + column.format_output_greatest_width
    end
  end
end