Module: Ifin24::Helpers::Printer

Included in:
Commands::Base
Defined in:
lib/ifin24/helpers/printer.rb

Instance Method Summary collapse

Instance Method Details



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ifin24/helpers/printer.rb', line 4

def print_list(items, *fields)
  # find max length for each field; start with the field names themselves
  fields = items.first.class.column_names unless fields.any?
  max_len = Hash[*fields.map { |f| [f, f.to_s.length] }.flatten]
  items.each do |item|
    fields.each do |field|
      len = item.send(field).to_s.length
      max_len[field] = len if len > max_len[field]
    end
  end

  border = '+-' + fields.map { |f| '-' * max_len[f] }.join('-+-') + '-+'
  title_row = '| ' + fields.map { |f| sprintf("%-#{max_len[f]}s", f.to_s) }.join(' | ') + ' |'

  puts border
  puts title_row
  puts border

  items.each do |item|
    row = '| ' + fields.map { |f| sprintf("%-#{max_len[f]}s", item.send(f)) }.join(' | ') + ' |'
    puts row
  end

  puts border
  puts "#{items.length} rows in set\n"
end