Sha256: 0a3b88041cd0af6b4927ff86d632b033b3252cd4606aacc6010af13501e83666

Contents?: true

Size: 1.84 KB

Versions: 22

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module ThemeCheck
  class Printer
    def initialize(out_stream = STDOUT)
      @out = out_stream
    end

    def print(theme, offenses, auto_correct)
      offenses.each do |offense|
        print_offense(offense, auto_correct)
        @out.puts
      end

      correctable = offenses.select(&:correctable?)
      @out.puts "#{theme.all.size} files inspected, #{red(offenses.size.to_s + ' offenses')} detected, \
#{yellow(correctable.size.to_s + ' offenses')} #{auto_correct ? 'corrected' : 'auto-correctable'}"
    end

    def print_offense(offense, auto_correct)
      location = if offense.location
        blue(offense.location) + ": "
      else
        ""
      end

      corrected = if auto_correct && offense.correctable?
        green("[Corrected] ")
      else
        ""
      end

      @out.puts location +
        colorized_severity(offense.severity) + ": " +
        yellow(offense.check_name) + ": " +
        corrected +
        offense.message + "."
      if offense.source_excerpt
        @out.puts "\t#{offense.source_excerpt}"
        if offense.markup_start_in_excerpt
          @out.puts "\t" + (" " * offense.markup_start_in_excerpt) + ("^" * offense.markup.size)
        end
      end
    end

    private

    def colorize(str, color_code)
      "\e[#{color_code}m#{str}\e[0m"
    end

    def colorized_severity(severity)
      case severity
      when :error
        red(severity)
      when :suggestion
        pink(severity)
      when :style
        light_blue(severity)
      end
    end

    def red(str)
      colorize(str, 31)
    end

    def green(str)
      colorize(str, 32)
    end

    def yellow(str)
      colorize(str, 33)
    end

    def blue(str)
      colorize(str, 34)
    end

    def pink(str)
      colorize(str, 35)
    end

    def light_blue(str)
      colorize(str, 36)
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/printer.rb
theme-check-1.14.0 lib/theme_check/printer.rb
theme-check-1.13.0 lib/theme_check/printer.rb
theme-check-1.12.1 lib/theme_check/printer.rb
theme-check-1.12.0 lib/theme_check/printer.rb
theme-check-1.11.0 lib/theme_check/printer.rb
theme-check-1.10.3 lib/theme_check/printer.rb
theme-check-1.10.2 lib/theme_check/printer.rb
theme-check-1.10.1 lib/theme_check/printer.rb
theme-check-1.10.0 lib/theme_check/printer.rb
theme-check-1.9.2 lib/theme_check/printer.rb
theme-check-1.9.1 lib/theme_check/printer.rb
theme-check-1.9.0 lib/theme_check/printer.rb
theme-check-1.8.0 lib/theme_check/printer.rb
theme-check-1.7.2 lib/theme_check/printer.rb
theme-check-1.7.1 lib/theme_check/printer.rb
theme-check-1.7.0 lib/theme_check/printer.rb
theme-check-1.6.2 lib/theme_check/printer.rb
theme-check-1.6.1 lib/theme_check/printer.rb
theme-check-1.6.0 lib/theme_check/printer.rb