Method: Collins::CLI::Formatter#display_as_table

Defined in:
lib/collins/cli/formatter.rb

#display_as_table(assets, columns, separator, show_header = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/collins/cli/formatter.rb', line 76

def display_as_table(assets, columns, separator, show_header = false)
  # lets figure out how wide each column is, including header
  column_width_pairs = columns.map do |column|
    # grab all attributes == column and figure out max width
    width = assets.map{|a| (column == :state) ?  a.send(column).label.to_s.length : a.send(column).to_s.length}.max
    width = [width, column.to_s.length].max if show_header
    [column,width]
  end
  column_width_map = Hash[column_width_pairs]
  if show_header
    $stderr.puts column_width_map.map{|c,w| "%-#{w}s" % c}.join(separator)
  end
  assets.each do |a|
    puts column_width_map.map {|c,w| v = (c == :state) ?  a.send(c).label : a.send(c) ; "%-#{w}s" % v }.join(separator)
  end
end