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