Class: XRay::ConsolePrinter

Inherits:
BasePrinter show all
Defined in:
lib/printer/console_printer.rb

Instance Method Summary collapse

Methods inherited from BasePrinter

#initialize

Constructor Details

This class inherits a constructor from XRay::BasePrinter

Instance Method Details



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/printer/console_printer.rb', line 14

def print
  if @opt[:source]
    print_with_source
  else
    if @results.empty?
      puts "[OK]".white.green_bg << " #{@opt[:file]}"
    else
      prf = @opt[:prefix]
      suf = @opt[:suffix]

      puts ""
      puts "[EE] #{@opt[:file]}".white.magenta_bg

      @results.each do |r|
        puts "#{prf}#{r.to_color_s}#{suf}"
      end
    end
  end
end


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
# File 'lib/printer/console_printer.rb', line 35

def print_with_source
  if @results.empty?
    puts "[OK]".white.green_bg << " #{@opt[:file]}"
  else
    source = @opt[:source]
    lines = source.split(/\r\n|\n|\r/)
    prf = @opt[:prefix]
    suf = @opt[:suffix]

    puts "[EE] #{@opt[:file]}".white.magenta_bg

    @results.each do |r|
      if r.row && r.row > 0
        col = r.column - 1
        row = r.row - 1
        left = col - 50
        right = col + 50
        left = 0 if left < 0

        puts "#{prf}#{lines[row][left..right].gsub(/\t/, ' ')}"
        puts "#{prf}#{' ' * (col - left)}^ #{r.to_color_s}"
        puts "\n"
      else
        puts "#{r.to_color_s}#{suf}\n"
      end
    end
  end
  
end