Module: Jekyll::DatabaseTables::Formatter::Helpers

Included in:
PsqlFormatter
Defined in:
lib/jekyll-database-tables/formatters.rb

Overview

Optional mixin providing geometry utilities for formatter subclasses.

Include this modules to gain access to #column_widths, which is useful for any formatter that needs to align output by column.

Instance Method Summary collapse

Instance Method Details

#column_widths(table) ⇒ Array<Integer>

Computes the minimum column widths needed to fit all cell values.

Parameters:

  • table (Table)

    the table to measure

Returns:

  • (Array<Integer>)

    the maximum cell width for each column, in order



19
20
21
22
23
24
25
26
# File 'lib/jekyll-database-tables/formatters.rb', line 19

def column_widths(table)
  col_count = table.headers.length
  all_rows  = [table.headers] + table.rows.map(&:cells)

  (0...col_count).map do |index|
    all_rows.map { |row| row[index].to_s.length }.max
  end
end