Class: Forspell::Reporter

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

Constant Summary collapse

SUCCESS_CODE =
0
ERROR_CODE =
1
DICT_PATH =
File.join(Dir.pwd, 'forspell.dict')
DICT_OVERWRITE =
'Do you want to overwrite forspell.dict? (yN)'
DICT_PROMPT =
<<~PROMPT 
  # Format: one word per line. Empty lines and #-comments are supported too.
  # If you want to add word with its forms, you can write 'word: example' (without quotes) on the line,
  # where 'example' is existing word with the same possible forms (endings) as your word.
  # Example: deduplicate: duplicate
PROMP
SUGGEST_FORMAT =
'(suggestions: %<suggestions>s)'
ERROR_FORMAT =
'%<file>s:%<line>i: %<text>s %<suggest>s'
SUMMARY =
"Forspell inspects *.rb, *.c, *.cpp, *.md files\n"\
'%<files>i file%<files_plural>s inspected, %<errors>s error%<errors_plural>s detected'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Reporter.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#progress_barObject

Returns the value of attribute progress_bar.



27
28
29
# File 'lib/forspell/reporter.rb', line 27

def progress_bar
  @progress_bar
end

Instance Method Details

#error(word, suggestions) ⇒ Object



51
52
53
54
# File 'lib/forspell/reporter.rb', line 51

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

#file(path) ⇒ Object



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

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

#finalizeObject



75
76
77
# File 'lib/forspell/reporter.rb', line 75

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

#parsing_error(error) ⇒ Object



56
57
58
# File 'lib/forspell/reporter.rb', line 56

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

#path_load_error(path) ⇒ Object



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

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

#reportObject



64
65
66
67
68
69
70
71
72
73
# 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