class Jekyll::LiquidRenderer::Table

Constants

GAUGES

Public Class Methods

new(stats) click to toggle source
# File lib/jekyll/liquid_renderer/table.rb, line 8
def initialize(stats)
  @stats = stats
end

Public Instance Methods

to_s(num_of_rows = 50) click to toggle source
# File lib/jekyll/liquid_renderer/table.rb, line 12
def to_s(num_of_rows = 50)
  tabulate(data_for_table(num_of_rows))
end

Private Instance Methods

data_for_table(num_of_rows) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/jekyll/liquid_renderer/table.rb, line 39
def data_for_table(num_of_rows)
  sorted = @stats.sort_by { |_, file_stats| -file_stats[:time] }
  sorted = sorted.slice(0, num_of_rows)

  table  = [header_labels]
  totals = Hash.new { |hash, key| hash[key] = 0 }

  sorted.each do |filename, file_stats|
    GAUGES.each { |gauge| totals[gauge] += file_stats[gauge] }
    row = []
    row << filename
    row << file_stats[:count].to_s
    row << format_bytes(file_stats[:bytes])
    row << format("%.3f", file_stats[:time])
    table << row
  end

  footer = []
  footer << "TOTAL (for #{sorted.size} files)"
  footer << totals[:count].to_s
  footer << format_bytes(totals[:bytes])
  footer << format("%.3f", totals[:time])
  table  << footer
end
format_bytes(bytes) click to toggle source
# File lib/jekyll/liquid_renderer/table.rb, line 69
def format_bytes(bytes)
  bytes /= 1024.0
  format("%.2fK", bytes)
end
header_labels() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/jekyll/liquid_renderer/table.rb, line 65
def header_labels
  GAUGES.map { |gauge| gauge.to_s.capitalize }.unshift("Filename")
end
tabulate(data) click to toggle source
# File lib/jekyll/liquid_renderer/table.rb, line 18
def tabulate(data)
  require "terminal-table"

  header = data.shift
  footer = data.pop
  output = +"\n"

  table = Terminal::Table.new do |t|
    t << header
    t << :separator
    data.each { |row| t << row }
    t << :separator
    t << footer
    t.style = { :alignment => :right, :border_top => false, :border_bottom => false }
    t.align_column(0, :left)
  end

  output << table.to_s << "\n"
end