Sha256: de012c03949c323789265e0795ac25eaf4b1457ba32c66b1ce1c9f93ebaf7e08

Contents?: true

Size: 727 Bytes

Versions: 1

Compression:

Stored size: 727 Bytes

Contents

module Flucti
  module Utilities
    class Table
      def initialize(*headings)
        @headings, @rows = headings, []
      end

      def <<(row)
        @rows << row
        self
      end

      def to_s
        widths = (0...@headings.size).map { |col| ([@headings] + @rows).map { |row| row[col].to_s.length }.max }
        lines = []
        lines << @headings.zip(widths).map { |heading, width| " %-*s " % [width, heading] }.join('|')
        lines << widths.map { |width| '-' * (width + 2) }.join('+')
        @rows.each do |row|
          lines << row.zip(widths).map { |cell, width| " %#{cell.to_s =~ /^\d+\b/ ? '' : '-'}*s " % [width, cell] }.join('|')
        end
        lines * "\n"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
Flucti-flucti-cli-0.1.16 lib/flucti/utilities/table.rb