class Mutter::Table
Constants
- DefaultColumn
- DefaultTable
Attributes
Public Class Methods
Source
# File lib/mutter/table.rb, line 14 def initialize options = {}, &blk @columns, @rows = [], [] @options = DefaultTable.merge options instance_eval(&blk) if (@fixed = block_given?) end
Public Instance Methods
Source
# File lib/mutter/table.rb, line 27 def << row if row.size > @columns.size && fixed? raise ArgumentError, "row size is #{row.size} but I only have #{@columns.size} columns" else @rows << row end end
Source
# File lib/mutter/table.rb, line 23 def column options = {} @columns << DefaultColumn.merge(options) end
Source
# File lib/mutter/table.rb, line 69 def process str, length = nil, align = :left, style = [] length ||= str.length if str.length > length str[0...(length - @options[:truncater].length)] + @options[:truncater] else s = [Mutter.new.clear.process(str, style), ' ' * (length - str.length)] s.reverse! if align == :right s.join end end
Source
# File lib/mutter/table.rb, line 36 def render # Create missing columns as needed (@rows.map {|r| r.size }.max - @columns.size).times do self.column end # Compute max column width @columns.each_with_index do |col, i| col[:_width] = @rows.map do |r| r[i].to_s.length end.max if @rows[i] end # print table @rows.map do |row| @columns.zip(row).map do |col, cell| process(cell.to_s || "", col[:width] || col[:_width], col[:align], col[:style]) end.join @options[:delimiter] end end
Also aliased as: to_a