Module: Simple::SQL::Helpers::Printer

Extended by:
Printer
Included in:
Printer
Defined in:
lib/simple/sql/helpers/printer.rb

Overview

private

Constant Summary collapse

ROW_SEPARATOR =
" | "

Class Method Summary collapse

Class Method Details



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

def self.print(records, io: STDOUT, width: :auto)
  # check args

  return if records.empty?
  return if records.first.keys.empty?

  if width == :auto && io.isatty
    width = `tput cols`.to_i
  end
  width = nil if width && width <= 0

  # prepare printing

  rows = materialize_rows(records)
  column_widths = calculate_column_widths(rows)
  column_widths = optimize_column_widths(column_widths, width, rows.first.length) if width

  # print

  print_records(rows, io, column_widths)
end