3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/coinsync/table_printer.rb', line 3
def print_table(, rows, alignment: nil, separator: ' ')
rows.each do |row|
if row.length != .length
raise "TablePrinter: All rows should have equal number of cells"
end
if !row.all? { |c| c.is_a?(String) }
raise "TablePrinter: All cells should be strings"
end
end
ids = (0....length)
widths = ids.map { |i| (rows + []).map { |r| r[i].length }.max }
puts ids.map { |i| [i].center(widths[i]) }.join(separator)
puts '-' * (widths.inject(&:+) + separator.length * (.length - 1))
rows.each do |row|
cells = ids.map do |i|
row[i].send(alignment && alignment[i] || :ljust, widths[i])
end
puts cells.join(separator)
end
end
|