Class: Goodcheck::Reporters::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/goodcheck/reporters/text.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:) ⇒ Text

Returns a new instance of Text.



6
7
8
9
10
# File 'lib/goodcheck/reporters/text.rb', line 6

def initialize(stdout:)
  @stdout = stdout
  @file_count = 0
  @issue_count = 0
end

Instance Attribute Details

#stdoutObject (readonly)

Returns the value of attribute stdout.



4
5
6
# File 'lib/goodcheck/reporters/text.rb', line 4

def stdout
  @stdout
end

Instance Method Details

#analysisObject



12
13
14
# File 'lib/goodcheck/reporters/text.rb', line 12

def analysis
  yield
end

#file(path) ⇒ Object



16
17
18
19
# File 'lib/goodcheck/reporters/text.rb', line 16

def file(path)
  @file_count += 1
  yield
end

#issue(issue) ⇒ Object



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
# File 'lib/goodcheck/reporters/text.rb', line 25

def issue(issue)
  @issue_count += 1

  format_line = lambda do |line:, column:|
    format_args = {
      path: Rainbow(issue.path).cyan,
      location: Rainbow(":#{line}:#{column}:").dimgray,
      message: issue.rule.message.lines.first.chomp,
      rule: Rainbow("(#{issue.rule.id})").dimgray,
      severity: issue.rule.severity ? Rainbow("[#{issue.rule.severity}]").magenta : ""
    }
    format("%<path>s%<location>s %<message>s  %<rule>s  %<severity>s", format_args).strip
  end

  if issue.location
    start_line = issue.location.start_line
    start_column = issue.location.start_column
    start_column_index = start_column - 1
    line = issue.buffer.line(start_line)
    column_size = if issue.location.one_line?
                    issue.location.column_size
                  else
                    line.bytesize - start_column
                  end
    stdout.puts format_line.call(line: start_line, column: start_column)
    stdout.puts line.chomp
    stdout.puts (" " * start_column_index) + Rainbow("^" + "~" * (column_size - 1)).yellow
  else
    stdout.puts format_line.call(line: "-", column: "-")
  end

  justifications = issue.rule.justifications
  unless justifications.empty?
    stdout.puts ""
    stdout.puts "  #{Rainbow('Justifications').dimgray.underline.italic}:"
    justifications.each do |justification|
      stdout.puts "#{Rainbow(justification).dimgray.italic}"
    end
    stdout.puts ""
  end
end

#rule(rule) ⇒ Object



21
22
23
# File 'lib/goodcheck/reporters/text.rb', line 21

def rule(rule)
  yield
end

#summaryObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/goodcheck/reporters/text.rb', line 67

def summary
  files = case @file_count
          when 0
            "no files"
          when 1
            "1 file"
          else
            "#{@file_count} files"
          end
  issues = case @issue_count
           when 0
             Rainbow("no issues").green
           when 1
             Rainbow("1 issue").red
           else
             Rainbow("#{@issue_count} issues").red
           end

  stdout.puts ""
  stdout.puts "#{files} inspected, #{issues} detected"
end