Class: Forspell::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/forspell/reporter.rb

Constant Summary collapse

SUCCESS_CODE =
0
ERROR_CODE =
1
ERROR_FORMAT =
'%<file>s:%<line>i: %<text>s (suggestions: %<suggestions>s)'
SUMMARY =
"Forspell inspects *.rb, *.c, *.cpp, *.md files\n"\
'%<files>i inspected, %<errors>s detected'

Instance Method Summary collapse

Constructor Details

#initialize(logfile:, verbose:, format:) ⇒ Reporter



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/forspell/reporter.rb', line 16

def initialize(logfile:,
               verbose:,
               format:)

  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 = []
end

Instance Method Details

#error(word, suggestions) ⇒ Object



36
37
38
39
# File 'lib/forspell/reporter.rb', line 36

def error(word, suggestions)
  @errors << [word, suggestions]
  puts readable(word, suggestions) if @format == 'readable'
end

#file(path) ⇒ Object



31
32
33
34
# File 'lib/forspell/reporter.rb', line 31

def file(path)
  @logger.info "Processing #{path}"
  @files << path
end

#finalizeObject



60
61
62
# File 'lib/forspell/reporter.rb', line 60

def finalize
  @errors.empty? ? SUCCESS_CODE : ERROR_CODE
end

#parsing_error(error) ⇒ Object



41
42
43
# File 'lib/forspell/reporter.rb', line 41

def parsing_error(error)
  @logger.error "Parsing error in #{@files.last}: #{error}"
end

#path_load_error(path) ⇒ Object



45
46
47
# File 'lib/forspell/reporter.rb', line 45

def path_load_error path
  @logger.error "Path not found: #{path}"
end

#reportObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/forspell/reporter.rb', line 49

def report
  case @format
  when 'readable'
    print_summary
  when 'dictionary'
    print_dictionary
  when 'json', 'yaml'
    print_formatted
  end
end