Class: Shellout::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/shellout/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(table = {}) ⇒ Table

Returns a new instance of Table.



6
7
8
9
10
11
12
13
14
# File 'lib/shellout/table.rb', line 6

def initialize(table={})
  @data = []
  @data << table[:headers] if table.has_key? :headers
  @data += table[:rows]    if table.has_key? :rows
  @data << table[:footers] if table.has_key? :footers
  
  @has_headers = table.has_key? :headers
  @has_footers = table.has_key? :footers
end

Instance Method Details



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/shellout/table.rb', line 16

def print(out=$stdout)
  return if @data.empty?
  
  separators = widths.map {|w| ''*(w+2)}
  format = build_format
  
  out.print '' << separators.join('') << "┐\n"
  @data.each_with_index do |r, i|
    if @has_headers and i == 0
      out.print '' << center(r).join('') << " │\n"
      out.print '' << separators.join('') << "┤\n"
    elsif @has_footers and i == @data.size-1
      out.print '' << separators.join('') << "┤\n"
      out.printf(format, *r)          
    else
      out.printf(format, *r)          
    end
  end
  out.print '' << separators.join('') << "┘\n"
end