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
# File lib/forspell/reporter.rb, line 131 def print(something) $stdout.tty? ? @progress_bar&.log(something) : puts(something) end
print_dictionary()
click to toggle source
# File lib/forspell/reporter.rb, line 109 def print_dictionary puts DICT_PATH if File.exist?(DICT_PATH) cli = HighLine.new answer = cli.ask(DICT_OVERWRITE) out = answer.downcase == 'y' ? File.new(DICT_PATH, 'w') : exit(1) else out = File.new(DICT_PATH, 'w') end out.puts DICT_PROMPT unless out.tty? @errors.map(&:first) .group_by(&:text) .transform_values { |v| v.map(&:file).uniq } .sort_by { |word, *| word.downcase } .each do |text, files| files.each { |file| out.puts "\# #{file}" } if @print_filepaths out.puts out.tty? ? @pastel.decorate(text, :red) : text end end
print_formatted()
click to toggle source
# File lib/forspell/reporter.rb, line 91 def print_formatted @errors.map { |word, suggestions| word.to_h.merge(suggestions: suggestions) } .public_send("to_#{@format}") .tap { |res| print res } end
print_summary()
click to toggle source
# File lib/forspell/reporter.rb, line 97 def print_summary err_count = @errors.size color = err_count.positive? ? :red : :green total_errors_colorized = @pastel.decorate(err_count.to_s, color) print format(SUMMARY, files: @files.size, errors: total_errors_colorized, files_plural: @files.size == 1 ? '' : 's', errors_plural: err_count == 1 ? '' : 's') end
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