Class: ET::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/et/formatter.rb

Class Method Summary collapse

Class Method Details



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/et/formatter.rb', line 3

def self.print_table(data, *headers)
  return puts "No challenges assigned" if data.empty?
  column_widths = headers.map do |header|
    data.map { |row| row[header].length }.max
  end

  total_width = column_widths.reduce(0) do |width, sum|
    sum + width + 3
  end

  header = build_row(headers, column_widths, YELLOW)

  puts header
  puts "\e[34m" + ("-" * total_width) + "\e[0m"

  data.each do |row|
    values = headers.map { |header| row[header] }
    puts build_row(values, column_widths, WHITE)
  end
end