Class: Rgr::ColouredReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/rgr/coloured_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout) ⇒ ColouredReporter

Returns a new instance of ColouredReporter.



7
8
9
# File 'lib/rgr/coloured_reporter.rb', line 7

def initialize(output = $stdout)
  @output = output
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/rgr/coloured_reporter.rb', line 5

def output
  @output
end

Instance Method Details

#highlight_source(source) ⇒ Object



28
29
30
# File 'lib/rgr/coloured_reporter.rb', line 28

def highlight_source(source)
  CodeRay.scan(source, :ruby).terminal
end

#line_number_places(matches) ⇒ Object



23
24
25
26
# File 'lib/rgr/coloured_reporter.rb', line 23

def line_number_places(matches)
  highest_line_number = matches.map { |match| match.expression.line }.max
  highest_line_number.to_s.length
end

#report_file_matches(file, matches) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rgr/coloured_reporter.rb', line 11

def report_file_matches(file, matches)
  output.puts "\e[32;1m#{file}\e[0m"

  line_number_places = line_number_places(matches)

  matches.each do |match|
    expr = match.expression
    highlighted_source = highlight_source(expr.source_line)
    output.printf "\e[33;1m%#{line_number_places}d\e[0m: %s\n", expr.line, highlighted_source
  end
end