/usr/lib/ruby/vendor_ruby/sequel/extensions/pretty_table.rb is in ruby-sequel 3.33.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # The pretty_table extension adds Sequel::Dataset#print and the
# Sequel::PrettyTable class for creating nice-looking plain-text
# tables.
module Sequel
class Dataset
# Pretty prints the records in the dataset as plain-text table.
def print(*cols)
Sequel::PrettyTable.print(naked.all, cols.empty? ? columns : cols)
end
end
module PrettyTable
# Prints nice-looking plain-text tables via puts
#
# +--+-------+
# |id|name |
# |--+-------|
# |1 |fasdfas|
# |2 |test |
# +--+-------+
def self.print(records, columns = nil) # records is an array of hashes
columns ||= records.first.keys.sort_by{|x|x.to_s}
sizes = column_sizes(records, columns)
sep_line = separator_line(columns, sizes)
puts sep_line
puts header_line(columns, sizes)
puts sep_line
records.each {|r| puts data_line(columns, sizes, r)}
puts sep_line
end
### Private Module Methods ###
# 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|
s = c.to_s.size
sizes[c.to_sym] = s if s > sizes[c.to_sym]
end
records.each do |r|
columns.each do |c|
s = r[c].to_s.size
sizes[c.to_sym] = s if s > sizes[c.to_sym]
end
end
sizes
end
# String for each data line
def self.data_line(columns, sizes, record) # :nodoc:
'|' << 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 Bignum, Fixnum
"%#{size}d" % v
when Float
"%#{size}g" % v
else
"%-#{size}s" % v.to_s
end
end
# String for header line
def self.header_line(columns, sizes) # :nodoc:
'|' << columns.map {|c| "%-#{sizes[c]}s" % c.to_s}.join('|') << '|'
end
# String for separtor line
def self.separator_line(columns, sizes) # :nodoc:
'+' << columns.map {|c| '-' * sizes[c]}.join('+') << '+'
end
private_class_method :column_sizes, :data_line, :format_cell, :header_line, :separator_line
end
end
|