Parent

Files

Mutter::Table

Constants

DefaultTable
DefaultColumn

Attributes

rows[RW]
columns[RW]

Public Class Methods

new(options = {}) click to toggle source
    # File lib/mutter/table.rb, line 14
14:     def initialize options = {}, &blk
15:       @columns, @rows = [], []
16:       @options = DefaultTable.merge options
17: 
18:       instance_eval(&blk) if (@fixed = block_given?)
19:     end

Public Instance Methods

<<(row) click to toggle source
    # File lib/mutter/table.rb, line 27
27:     def << row
28:       if row.size > @columns.size && fixed?
29:         raise ArgumentError,
30:           "row size is #{row.size} but I only have #{@columns.size} columns"
31:       else
32:         @rows << row
33:       end
34:     end
column(options = {}) click to toggle source
    # File lib/mutter/table.rb, line 23
23:     def column options = {}
24:       @columns << DefaultColumn.merge(options)
25:     end
fixed?() click to toggle source
    # File lib/mutter/table.rb, line 21
21:     def fixed?; @fixed end
process(str, length = nil, align = :left, style = []) click to toggle source
    # File lib/mutter/table.rb, line 69
69:     def process str, length = nil, align = :left, style = []
70:       length ||= str.length
71: 
72:       if str.length > length
73:         str[0...(length - @options[:truncater].length)] + @options[:truncater]
74:       else
75:         s = [Mutter.new.clear.process(str, style), ' ' * (length - str.length)]
76:         s.reverse! if align == :right
77:         s.join
78:       end
79:     end
render() click to toggle source
    # File lib/mutter/table.rb, line 36
36:     def render
37:       # Create missing columns as needed
38:       (@rows.map {|r| r.size }.max - @columns.size).times do
39:         self.column
40:       end
41: 
42:       # Compute max column width
43:       @columns.each_with_index do |col, i|
44:         col[:_width] = @rows.map do |r|
45:           r[i].to_s.length
46:         end.max if @rows[i]
47:       end
48: 
49:       # print table
50:       @rows.map do |row|
51:         @columns.zip(row).map do |col, cell|
52:           process(cell.to_s || "",
53:                   col[:width] || col[:_width],
54:                   col[:align],
55:                   col[:style])
56:         end.join @options[:delimiter]
57:       end
58:     end
Also aliased as: to_a
to_a() click to toggle source
Alias for: render
to_s() click to toggle source
    # File lib/mutter/table.rb, line 61
61:     def to_s
62:       render.join("\n")
63:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.