1 2 3 4 5 6 7 8 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
# frozen-string-literal: true
#
# This _pretty_table extension is only for internal use.
# It adds the Sequel::PrettyTable class without modifying
# Sequel::Dataset.
#
# To load the extension:
#
# Sequel.extension :_pretty_table
#
# Related module: Sequel::PrettyTable
#
module Sequel
module PrettyTable
# Prints nice-looking plain-text tables via puts
#
# +--+-------+
# |id|name |
# |--+-------|
# |1 |fasdfas|
# |2 |test |
# +--+-------+
def self.print(records, columns=nil)
puts string(records, columns)
end
# Return the string that #print will print via puts.
def self.string(records, columns = nil) # records is an array of hashes
columns ||= records.first.keys.sort
sizes = column_sizes(records, columns)
sep_line = separator_line(columns, sizes)
array = [sep_line, header_line(columns, sizes), sep_line]
records.each {|r| array << data_line(columns, sizes, r)}
array << sep_line
array.join("\n")
end
# Hash of the maximum size of the value for each column
def self.column_sizes(records, columns) # :nodoc:
sizes = Hash.new(0)
columns.each do |c|
sizes[c] = c.to_s.size
end
records.each do |r|
columns.each do |c|
s = r[c].to_s.size
sizes[c] = s if s > sizes[c]
end
end
sizes
end
# String for each data line
def self.data_line(columns, sizes, record) # :nodoc:
String.new << '|' << columns.map {|c| format_cell(sizes[c], record[c])}.join('|') << '|'
end
# Format the value so it takes up exactly size characters
def self.format_cell(size, v) # :nodoc:
case v
when Integer
"%#{size}d" % v
when Float, BigDecimal
"%#{size}g" % v
else
"%-#{size}s" % v.to_s
end
end
# String for header line
def self.header_line(columns, sizes) # :nodoc:
String.new << '|' << columns.map {|c| "%-#{sizes[c]}s" % c.to_s}.join('|') << '|'
end
# String for separtor line
def self.separator_line(columns, sizes) # :nodoc:
String.new << '+' << columns.map {|c| '-' * sizes[c]}.join('+') << '+'
end
private_class_method :column_sizes, :data_line, :format_cell, :header_line, :separator_line
end
end
|