Module: SassProf::Formatter

Extended by:
Formatter
Included in:
Formatter
Defined in:
lib/sass-prof/formatter.rb

Constant Summary collapse

REGEX_ASCII =
/\e\[(\d+)(;\d+)*m/
COLORS =
Hash.new("37").merge({
  :black  => "30",
  :red    => "31",
  :green  => "32",
  :yellow => "33",
  :blue   => "34",
  :purple => "35",
  :cyan   => "36",
  :white  => "37",
})

Instance Method Summary collapse

Instance Method Details

#colorize(string, color) ⇒ Object



16
17
18
19
20
# File 'lib/sass-prof/formatter.rb', line 16

def colorize(string, color)
  return string.to_s unless Config.color

  "\e[0;#{COLORS.fetch(color)}m#{string}\e[0m"
end

#to_table(rows) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sass-prof/formatter.rb', line 22

def to_table(rows)
  t_ms = Timer.t_total

  return if t_ms.nil?

  t_ss, t_ms = t_ms.divmod 1000
  t_mm, t_ss = t_ss.divmod 60

  # Add summary for each action type
  Timer::SUMMARIES.reject { |s| s == :t_total }.each do |summary|
    break unless Config.subtotal

    case summary
    when /^(t_)/
      sum_t_ms = Timer.send summary

      next if sum_t_ms.nil?

      sum_t_ss, sum_t_ms = sum_t_ms.divmod 1000
      sum_t_mm, sum_t_ss = sum_t_ss.divmod 60

      rows << :separator
      rows << [
        "subtotal",
        "%.0fm %.0fs %.0fms" % [sum_t_mm, sum_t_ss, sum_t_ms],
        "#{summary}".gsub(/(^(t_)|(_total)$)/, ""),
        ""
      ]
    when /^(cnt_)/
      count = Timer.send summary

      next if count.nil?

      rows << :separator
      rows << [
        "count",
        "#{count}",
        "#{summary}".gsub(/(^(cnt_)|(_total)$)/, ""),
        ""
      ]
    end
  end

  # Add footer containing total execution time
  rows << :separator
  rows << [
    "total",
    "%.0fm %.0fs %.0fms" % [t_mm, t_ss, t_ms],
    "all",
    ""
  ]

  table = Terminal::Table.new({
    :headings => ["file", "execution time", "action", "signature"],
    :rows     => rows
  })

  table
end

#truncate_row(row) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sass-prof/formatter.rb', line 82

def truncate_row(row)
  max_width = Config.max_width
  tr_row = []

  row.map do |col|
    clean_width = col.gsub(REGEX_ASCII, "").length
    diff        = col.length - clean_width

    if clean_width > max_width
      tr_row << (col[0..max_width + diff] << "\e[0m...")
    else
      tr_row << col
    end
  end

  tr_row
end