The Table class can be used to output nicely formatted tables with division lines and alignment.
table - array of array
options - align :left or :right options - space to add to each cell options - fit to screen width options -
The format block must return ANSI codes to apply to each cell.
Other Implementations:
TODO: Support for table headers and footers.
# File lib/ansi/table.rb, line 27 def initialize(table, options={}, &format) @table = table @padding = options[:padding] || 0 @align = options[:align] @fit = options[:fit] @border = options[:border] #@ansi = [options[:ansi]].flatten @format = format @pad = " " * @padding end
# File lib/ansi/table.rb, line 58 def to_s #(fit=false) #row_count = table.size #col_count = table[0].size max = max_columns(fit) div = dividing_line top = div #.gsub('+', ".") bot = div #.gsub('+', "'") body = [] table.each_with_index do |row, r| body_row = [] row.each_with_index do |cell, c| t = cell_template(max[c]) s = t % cell.to_s body_row << apply_format(s, cell, c, r) end body << "| " + body_row.join(' | ') + " |" end if border body = body.join("\n#{div}\n") else body = body.join("\n") end "#{top}\n#{body}\n#{bot}\n" end
Generated with the Darkfish Rdoc Generator 2.