Module: Simple::SQL::TablePrint

Extended by:
TablePrint
Included in:
TablePrint
Defined in:
lib/simple/sql/table_print.rb

Overview

private

Constant Summary collapse

ROW_SEPARATOR =
" | "

Instance Method Summary collapse

Instance Method Details

#table_print(records, io: STDOUT, width: :auto) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simple/sql/table_print.rb', line 9

def table_print(records, io: STDOUT, width: :auto)
  # check args

  return if records.empty?

  column_count = records.first.length
  return if column_count == 0

  # -- determine/adjust total width for output. -----------------------------

  width = terminal_width(io) if width == :auto
  width = nil if width && width <= 0

  # -- prepare printing -----------------------------------------------------

  rows = materialize_rows(records)
  column_widths = column_max_lengths(rows)

  if width
    column_widths = distribute_column_widths(column_widths, width, column_count, rows.first)
  end

  # -- print ----------------------------------------------------------------

  print_records(rows, io, column_widths)
end