Class: Chutney::PieFormatter

Inherits:
Formatter show all
Defined in:
lib/chutney/formatter/pie_formatter.rb

Overview

format results as pie charts

Instance Attribute Summary

Attributes inherited from Formatter

#results

Instance Method Summary collapse

Methods inherited from Formatter

#files, #files_with_issues, #initialize

Constructor Details

This class inherits a constructor from Chutney::Formatter

Instance Method Details

#char_loopObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chutney/formatter/pie_formatter.rb', line 38

def char_loop
  @char_looper ||= Fiber.new do
    chars = %w[ x + @ * / -]
    current = 0
    loop do
      current = 0 if current >= chars.count
      Fiber.yield chars[current]
      current += 1
    end
  end
  @char_looper.resume
end

#colour_loopObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chutney/formatter/pie_formatter.rb', line 51

def colour_loop
  @colour_looper ||= Fiber.new do
    colours = %i[bright_cyan bright_magenta bright_yellow bright_green]
    current = 0
    loop do
      current = 0 if current >= colours.count
      Fiber.yield colours[current]
      current += 1
    end
  end
  @colour_looper.resume
end

#formatObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/chutney/formatter/pie_formatter.rb', line 9

def format
  data = top_offences.map do |offence|
    {
      name: offence.first,
      value: offence.last,
      color: colour_loop,
      fill: char_loop
    }
  end
  print_report(data)
end


21
22
23
24
25
26
# File 'lib/chutney/formatter/pie_formatter.rb', line 21

def print_report(data)
  return if data.empty?

  print TTY::Pie.new(data: data, radius: 8, legend: { format: '%<label>s %<name>s %<value>i' })
  puts
end

#put_summaryObject



64
65
66
67
68
69
70
71
72
# File 'lib/chutney/formatter/pie_formatter.rb', line 64

def put_summary
  pastel = Pastel.new
  print "#{files.count} features inspected, "
  if files_with_issues.count.zero?
    puts pastel.green('all taste delicious')
  else
    puts pastel.red("#{files_with_issues.count} taste nasty")
  end
end

#top_offencesObject



28
29
30
31
32
33
34
35
36
# File 'lib/chutney/formatter/pie_formatter.rb', line 28

def top_offences
  offence = Hash.new(0)
  files_with_issues.each do |_file, linter|
    linter.each do |lint|
      offence[lint[:linter]] += lint[:issues].count
    end
  end
  offence.reject { |_k, v| v.zero? }.sort_by { |_linter, count| -count }
end