Class: Mago::Cli::SourceFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/mago/cli/source_formatter.rb

Overview

Formats report showing lines of source code where magic number was detected.

Instance Method Summary collapse

Methods inherited from Formatter

#format, #format_error, #initialize

Methods included from Colorize

#colorize, #green, #pink, #red, #yellow

Constructor Details

This class inherits a constructor from Mago::Cli::Formatter

Instance Method Details

#format_file(file) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mago/cli/source_formatter.rb', line 6

def format_file(file)
  out = ''
  source_lines = File.readlines(file.path)

  file.magic_numbers.each do |num|
    if @color
      line = yellow(num.line)
      path = pink(file.path)
      source_line = make_red(source_lines[num.line-1], num.value.to_s)
    else
      line = num.line
      path = file.path
      source_line = source_lines[num.line-1]
    end

    out << "#{path}:#{line}| #{source_line}"
  end

  out
end

#make_red(str, substr) ⇒ String

Find a substing in a string and make it red.

Parameters:

  • str (String)
  • substr (String)

Returns:

  • (String)

    string with red substring



33
34
35
36
# File 'lib/mago/cli/source_formatter.rb', line 33

def make_red(str, substr)
  chunks = str.split(substr)
  chunks.join(red(substr))
end