class PassStation::Output::PrettyTable

Pretty table with ASCII borders formatter

Public Class Methods

format(table) click to toggle source

Format the +Array<CSV::Row>+ into a simple table with justified columns @param table [Array<CSV::Row>] an +Array<CSV::Row>+ @return [Array<String>] the formatted table ready to be printed

# File lib/pass_station/output.rb, line 153
def format(table)
  out = []
  colsizes = colsizes_count(table)
  out.push(dividers(colsizes))
  out.push(headers(colsizes))
  out.push(dividers(colsizes))
  table.each do |r|
    out.push(justify_row(r, colsizes))
  end
  out.push(dividers(colsizes))
end

Protected Class Methods

dividers(colsizes) click to toggle source

Generate dividers @param colsizes [Hash] hash containing the column size for each column as returned by {colsizes_count} @return [String] divider line

# File lib/pass_station/output.rb, line 189
def dividers(colsizes)
  "+#{colsizes.map { |_, cs| '-' * (cs + 1) }.join('+')}+"
end
headers(colsizes) click to toggle source

Generate justified headers @param colsizes [Hash] hash containing the column size for each column as returned by {colsizes_count} @return [String] the justified headers

# File lib/pass_station/output.rb, line 196
def headers(colsizes)
  "| #{colsizes.map { |k, v| k.to_s.ljust(v - 1) }.join(' | ')} |"
end
justify(row, column, colsizes) click to toggle source

Left justify an element of the column @param row [CSV::Row] CSV::Row @param column [Symbol] the symbol of the column @param colsizes [Hash] hash containing the column size for each column as returned by {colsizes_count} @return [String] the justified element

# File lib/pass_station/output.rb, line 170
def justify(row, column, colsizes)
  row[column].to_s.ljust(colsizes[column] - 1)
end
justify_row(row, colsizes) click to toggle source

Left justify all elements of the column @param row [CSV::Row] CSV::Row @param colsizes [Hash] hash containing the column size for each column as returned by {colsizes_count} @return [String] the justified row

# File lib/pass_station/output.rb, line 178
def justify_row(row, colsizes)
  out = '| '
  row.to_h.each_key do |col|
    out += "#{justify(row, col, colsizes)} | "
  end
  out
end