class Codeqa::RunnerDecorator

Public Class Methods

new(runner, options={}) click to toggle source
# File lib/codeqa/runner_decorator.rb, line 3
def initialize(runner, options={})
  @options = { :colors => true }.merge(options)
  @runner = runner
  @msg = ''
end

Public Instance Methods

details_to_s() click to toggle source
# File lib/codeqa/runner_decorator.rb, line 21
def details_to_s
  error_details
end
sourcefile_to_s() click to toggle source
# File lib/codeqa/runner_decorator.rb, line 9
def sourcefile_to_s
  info("Codeqa on :'#{@runner.sourcefile.filename}'\n")
end
success_to_s() click to toggle source
# File lib/codeqa/runner_decorator.rb, line 13
def success_to_s
  if @runner.success?
    success("Passed tests: #{@runner.results.map(&:name).join(', ')}\n")
  else
    error("Failed tests: #{@runner.failures.map(&:name).join(', ')}\n")
  end
end
to_s() click to toggle source
# File lib/codeqa/runner_decorator.rb, line 25
def to_s
  @msg << sourcefile_to_s
  @msg << success_to_s
  @msg << details_to_s unless @runner.success?

  @msg
end

Private Instance Methods

colorize(color_code, txt) click to toggle source
# File lib/codeqa/runner_decorator.rb, line 73
def colorize(color_code, txt)
  if @options[:colors]
    "\e[#{color_code}m#{txt}\e[0m"
  else
    txt
  end
end
error(txt) click to toggle source
# File lib/codeqa/runner_decorator.rb, line 65
def error(txt)
  red(txt)
end
error_details() click to toggle source

TODO: move this error formating into check error class

# File lib/codeqa/runner_decorator.rb, line 36
def error_details
  msg = ''
  @runner.failures.each do |checker|
    # msg << error("------- #{checker.name} -------") << "\n"
    # msg << error("#{checker.hint}") << "\n"
    checker.errors.details.each do |type, content|
      case type
      when :source
        content.each_line.with_index do |l, i|
          msg << yellow((i + 1).to_s.rjust(3)) << '|' << l
        end
      when Integer
        msg << info('Line: ') << yellow(type) << '|' << info(content)
      when Array
        msg << info('Pos: ') << yellow(type.join(',')) << '|' << info(content)
      when nil
        msg << info(content)
      end
      msg << "\n"
    end
  end
  msg
end
green(txt) click to toggle source
# File lib/codeqa/runner_decorator.rb, line 85
def green(txt)
  colorize(32, txt)
end
info(txt) click to toggle source

stackoverflow.com/questions/1489183/colorized-ruby-output

# File lib/codeqa/runner_decorator.rb, line 61
def info(txt)
  txt
end
red(txt) click to toggle source
# File lib/codeqa/runner_decorator.rb, line 81
def red(txt)
  colorize(31, txt)
end
success(txt) click to toggle source
# File lib/codeqa/runner_decorator.rb, line 69
def success(txt)
  green(txt)
end
yellow(txt) click to toggle source
# File lib/codeqa/runner_decorator.rb, line 89
def yellow(txt)
  colorize(33, txt)
end