Sha256: 8d8f95025795ed89c094912b1d78d80b2ac9edc6235c07f69efd404e3ad3670a

Contents?: true

Size: 1.87 KB

Versions: 39

Compression:

Stored size: 1.87 KB

Contents

module Sequel
  # Prints nice-looking plain-text tables
  # +--+-------+
  # |id|name   |
  # |--+-------|
  # |1 |fasdfas|
  # |2 |test   |
  # +--+-------+
  module PrettyTable
    def self.records_columns(records)
      columns = []
      records.each do |r|
        if Array === r && (k = r.keys)
          return k
        elsif Hash === r
          r.keys.each {|k| columns << k unless columns.include?(k)}
        end
      end
      columns
    end
    
    def self.column_sizes(records, columns)
      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
    
    def self.separator_line(columns, sizes)
      l = ''
      '+' + columns.map {|c| '-' * sizes[c]}.join('+') + '+'
    end
    
    def self.format_cell(size, v)
      case v
      when Bignum, Fixnum
        "%#{size}d" % v
      when Float
        "%#{size}g" % v
      else
        "%-#{size}s" % v.to_s
      end
    end
    
    def self.data_line(columns, sizes, record)
      '|' + columns.map {|c| format_cell(sizes[c], record[c])}.join('|') + '|'
    end
    
    def self.header_line(columns, sizes)
      '|' + columns.map {|c| "%-#{sizes[c]}s" % c.to_s}.join('|') + '|'
    end
    
    def self.print(records, columns = nil) # records is an array of hashes
      columns ||= records_columns(records)
      sizes = column_sizes(records, columns)
      
      puts separator_line(columns, sizes)
      puts header_line(columns, sizes)
      puts separator_line(columns, sizes)
      records.each {|r| puts data_line(columns, sizes, r)}
      puts separator_line(columns, sizes)
    end
  end
end

Version data entries

39 entries across 39 versions & 3 rubygems

Version Path
sequel-0.4.4.2 lib/sequel/pretty_table.rb
sequel-0.5.0.2 lib/sequel/pretty_table.rb
sequel-0.4.5 lib/sequel/pretty_table.rb
sequel-0.5.0.1 lib/sequel/pretty_table.rb
sequel-0.5 lib/sequel/pretty_table.rb
sequel-1.4.0 lib/sequel_model/pretty_table.rb
sequel_core-1.0.1 lib/sequel_core/pretty_table.rb
sequel_core-1.0.3 lib/sequel_core/pretty_table.rb
sequel_core-1.0.0.1 lib/sequel_core/pretty_table.rb
sequel_core-1.0.10 lib/sequel_core/pretty_table.rb
sequel_core-1.0.2 lib/sequel_core/pretty_table.rb
sequel_core-1.0.6 lib/sequel_core/pretty_table.rb
sequel_core-1.0.4.1 lib/sequel_core/pretty_table.rb
sequel_core-1.0.8.1 lib/sequel_core/pretty_table.rb
sequel_core-1.0.5 lib/sequel_core/pretty_table.rb
sequel_core-1.0.7 lib/sequel_core/pretty_table.rb
sequel_core-1.0.8.2 lib/sequel_core/pretty_table.rb
sequel_core-1.0.4 lib/sequel_core/pretty_table.rb
sequel_core-1.0.9.1 lib/sequel_core/pretty_table.rb
sequel_core-1.0.8 lib/sequel_core/pretty_table.rb