Class: Mago::Cli::Formatter

Inherits:
Object
  • Object
show all
Includes:
Colorize
Defined in:
lib/mago/cli/formatter.rb

Overview

Formats report to be printed

Direct Known Subclasses

SourceFormatter

Instance Method Summary collapse

Methods included from Colorize

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

Constructor Details

#initialize(opts = {}) ⇒ Formatter

Returns a new instance of Formatter.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :color (Boolean)

    whether colorize output or no



10
11
12
# File 'lib/mago/cli/formatter.rb', line 10

def initialize(opts = {})
  @color = opts[:color]
end

Instance Method Details

#format(report) ⇒ String

Format report.

Parameters:

Returns:

  • (String)

    formated report



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mago/cli/formatter.rb', line 19

def format(report)
  out = ''

  report.files.each do |file|
    out << format_file(file, out)
  end

  report.errors.each do |error|
    out << format_error(error)
  end

  out
end

#format_error(error) ⇒ void

This method returns an undefined value.

Format error.

Parameters:

  • error (String)

    error message



64
65
66
67
# File 'lib/mago/cli/formatter.rb', line 64

def format_error(error)
  out = "ERROR: #{error}"
  @color ? red(out) : out
end

#format_file(file) ⇒ void

This method returns an undefined value.

Format file with magic numbers.

Parameters:

  • file (Mago::File)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mago/cli/formatter.rb', line 39

def format_file(file)
  out = ''

  file.magic_numbers.each do |num|
    if @color
      val  = red(num.value)
      line = yellow(num.line)
      path = pink(file.path)
    else
      val  = num.value
      line = num.line
      path = file.path
    end

    out << "#{path}:#{line} detected magic number #{val}\n"
  end

  out
end