class Forspell::Reporter

Constants

DICT_OVERWRITE
DICT_PATH
DICT_PROMPT
ERROR_CODE
ERROR_FORMAT
SUCCESS_CODE
SUGGEST_FORMAT
SUMMARY

Attributes

progress_bar[RW]

Public Class Methods

new(logfile:, verbose:, format:, print_filepaths: false) click to toggle source
# File lib/forspell/reporter.rb, line 29
def initialize(logfile:,
               verbose:,
               format:,
               print_filepaths: false)

  FileUtils.touch(logfile) if logfile.is_a?(String)
  @logger = Logger.new(logfile || STDERR)
  @logger.level = verbose ? Logger::INFO : Logger::WARN
  @logger.formatter = proc { |*, msg| "#{msg}\n" }
  @format = format

  @pastel = Pastel.new(enabled: $stdout.tty?)
  @errors = []
  @files = []
  @print_filepaths = print_filepaths
end

Public Instance Methods

error(word, suggestions) click to toggle source
# File lib/forspell/reporter.rb, line 51
def error(word, suggestions)
  @errors << [word, suggestions]
  print(readable(word, suggestions)) if @format == 'readable'
end
file(path) click to toggle source
# File lib/forspell/reporter.rb, line 46
def file(path)
  @logger.info "Processing #{path}"
  @files << path
end
finalize() click to toggle source
# File lib/forspell/reporter.rb, line 75
def finalize
  @errors.empty? ? SUCCESS_CODE : ERROR_CODE
end
parsing_error(error) click to toggle source
# File lib/forspell/reporter.rb, line 56
def parsing_error(error)
  @logger.error "Parsing error in #{@files.last}: #{error}"
end
path_load_error(path) click to toggle source
# File lib/forspell/reporter.rb, line 60
def path_load_error(path)
  @logger.error "Path not found: #{path}"
end
report() click to toggle source
# File lib/forspell/reporter.rb, line 64
def report
  case @format
  when 'readable'
    print_summary
  when 'dictionary'
    print_dictionary
  when 'json', 'yaml'
    print_formatted
  end
end

Private Instance Methods

print(something) click to toggle source
print_dictionary() click to toggle source
print_formatted() click to toggle source
print_summary() click to toggle source
readable(word, suggestions) click to toggle source
# File lib/forspell/reporter.rb, line 81
def readable(word, suggestions)
  suggest = format(SUGGEST_FORMAT, suggestions: suggestions.join(', ')) unless suggestions.empty?

  format(ERROR_FORMAT,
         file: word[:file],
         line: word[:line],
         text: @pastel.red(word[:text]),
         suggest: suggest)
end