Class: MGit::Output::TableOutputter

Inherits:
Object
  • Object
show all
Defined in:
lib/mgit/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, options) ⇒ TableOutputter

Returns a new instance of TableOutputter.



24
25
26
27
28
# File 'lib/mgit/output.rb', line 24

def initialize(table, options)
  @table = table
  @options = options
  raise ImplementationError.new('ptable called with invalid table') unless valid_table?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/mgit/output.rb', line 22

def options
  @options
end

#tableObject (readonly)

Returns the value of attribute table.



22
23
24
# File 'lib/mgit/output.rb', line 22

def table
  @table
end

Instance Method Details

#to_sObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mgit/output.rb', line 30

def to_s
  cw = column_widths
  
  if options[:columns]
    options[:columns].each_with_index do |c, i|
      cw[i] = c if c
    end
  end

  table.map do |row|
    row.map.with_index do |cell, i| 
      # Only justify the column if it is not the last one.
      # This avoids too many line breaks.
      i < (columns - 1) ? justify(cell, cw[i]) : cell
    end.join(' | ')
  end.join("\n")
end