Class: Lintron::TerminalReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/lintron/terminal_reporter.rb

Overview

Outputs lint results on the command line

Instance Method Summary collapse

Instance Method Details

#colorsObject



44
45
46
# File 'lib/lintron/terminal_reporter.rb', line 44

def colors
  @_colors ||= [:magenta, :cyan].cycle.each
end

#do_line(violation, last_file, row_header_width) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lintron/terminal_reporter.rb', line 26

def do_line(violation, last_file, row_header_width)
  rule = ''
  if last_file != violation.path
    colors.rewind
    rule += hr
  end

  file_and_line = violation.file_and_line(row_header_width)
  rule + wrap_pretty(
    "#{file_and_line}#{violation['message']}".colorize(colors.next),
    file_and_line.length,
  )
end

#format_violations(violations) ⇒ Object



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

def format_violations(violations)
  row_header_width = violations.map { |v| v.file_and_line.length }.max
  return no_violations if violations.empty?
  last_file = violations.first.path
  buffer = ''

  violations.each do |violation|
    buffer += do_line(violation, last_file, row_header_width)
    last_file = violation.path
  end
  buffer += "\n\n"
  buffer
end

#hrObject



40
41
42
# File 'lib/lintron/terminal_reporter.rb', line 40

def hr
  '-' * TermInfo.screen_size[1] + "\n\n\n"
end

#no_violationsObject



22
23
24
# File 'lib/lintron/terminal_reporter.rb', line 22

def no_violations
  'No violations found!'.colorize(:green)
end

#wrap_pretty(string, indent_level) ⇒ Object



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
# File 'lib/lintron/terminal_reporter.rb', line 48

def wrap_pretty(string, indent_level)
  width = TermInfo.screen_size[1] # Get the width of the term
  buffer = ''
  words = string.split(/\s/)

  current_word = 0
  line_length = 0
  last_line_word = 0
  while current_word < words.length
    line_length += words[current_word].length
    if line_length > width - 5 - indent_level &&
       current_word > last_line_word

      buffer += "\n"
      buffer += ' ' * indent_level
      line_length = indent_level
      last_line_word = current_word
    else
      buffer += words[current_word]
      buffer += ' '
      current_word += 1
    end
  end
  buffer + "\n"
end