Module: HeapScope::Tables

Defined in:
lib/heapscope/tables.rb

Overview

ASCII / Markdown tables for diffs and class growth.

Class Method Summary collapse

Class Method Details

.ascii(headers, rows) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/heapscope/tables.rb', line 28

def ascii(headers, rows)
  widths = headers.each_index.map do |i|
    ([headers[i].length] + rows.map { |r| r[i].to_s.length }).max
  end
  sep = "+#{widths.map { |w| '-' * (w + 2) }.join('+')}+"
  line = lambda do |cols|
    cells = cols.each_with_index.map { |c, i| c.to_s.ljust(widths[i]) }.join(" | ")
    "| #{cells} |"
  end
  ([sep, line.call(headers), sep] + rows.map { |r| line.call(r) } + [sep]).join("\n")
end

.bytes(n) ⇒ Object



52
53
54
55
56
# File 'lib/heapscope/tables.rb', line 52

def bytes(n)
  return "n/a" if n.nil?

  format("%+.2fMB", n.to_f / (1024 * 1024))
end

.class_growth(diff, limit: 15, format: :ascii) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/heapscope/tables.rb', line 8

def class_growth(diff, limit: 15, format: :ascii)
  rows = diff.growing_classes(limit).map do |c|
    [
      c[:name].to_s,
      c[:before_count].to_s,
      c[:after_count].to_s,
      signed(c[:delta_count]),
      bytes(c[:delta_bytes])
    ]
  end
  headers = %w[CLASS BEFORE AFTER DELTA BYTES]
  format == :markdown ? markdown(headers, rows) : ascii(headers, rows)
end

.findings(report, format: :ascii) ⇒ Object



22
23
24
25
26
# File 'lib/heapscope/tables.rb', line 22

def findings(report, format: :ascii)
  rows = report.findings.map { |f| [f.code, f.severity.to_s.upcase, f.title.to_s, f.subject.to_s] }
  headers = %w[CODE SEV TITLE SUBJECT]
  format == :markdown ? markdown(headers, rows) : ascii(headers, rows)
end

.markdown(headers, rows) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/heapscope/tables.rb', line 40

def markdown(headers, rows)
  lines = []
  lines << "| #{headers.join(' | ')} |"
  lines << "| #{headers.map { '---' }.join(' | ')} |"
  rows.each { |r| lines << "| #{r.join(' | ')} |" }
  lines.join("\n")
end

.signed(n) ⇒ Object



48
49
50
# File 'lib/heapscope/tables.rb', line 48

def signed(n)
  n.positive? ? "+#{n}" : n.to_s
end