lib/ddmetrics/table.rb in ddmetrics-1.0.0 vs lib/ddmetrics/table.rb in ddmetrics-1.0.1
- old
+ new
@@ -1,20 +1,26 @@
# frozen_string_literal: true
module DDMetrics
class Table
- def initialize(rows)
+ def initialize(rows, num_headers: 1)
@rows = rows
+ @num_headers = num_headers
end
def to_s
columns = @rows.transpose
column_lengths = columns.map { |c| c.map(&:size).max }
[].tap do |lines|
+ # header
lines << row_to_s(@rows[0], column_lengths)
+
+ # separator
lines << separator(column_lengths)
+
+ # body
rows = sort_rows(@rows.drop(1))
lines.concat(rows.map { |r| row_to_s(r, column_lengths) })
end.join("\n")
end
@@ -24,17 +30,17 @@
rows.sort_by { |r| r.first.downcase }
end
def row_to_s(row, column_lengths)
values = row.zip(column_lengths).map { |text, length| text.rjust(length) }
- values[0] + ' │ ' + values[1..-1].join(' ')
+ values.take(@num_headers).join(' ') + ' │ ' + values.drop(@num_headers).join(' ')
end
def separator(column_lengths)
(+'').tap do |s|
- s << '─' * column_lengths[0]
+ s << column_lengths.take(@num_headers).map { |l| '─' * l }.join('───')
s << '─┼─'
- s << column_lengths[1..-1].map { |l| '─' * l }.join('───')
+ s << column_lengths.drop(@num_headers).map { |l| '─' * l }.join('───')
end
end
end
end