Sha256: 4afe840463502c54de84deddb6efe55bb316f5d55e3afb5d9c6348ef0ada02ac

Contents?: true

Size: 996 Bytes

Versions: 5

Compression:

Stored size: 996 Bytes

Contents

module Pronto
  module Formatter
    class TextFormatter
      include Colorizable

      LOCATION_COLOR = :cyan

      LEVEL_COLORS = {
        info: :yellow,
        warning: :magenta,
        error: :red,
        fatal: :red
      }.freeze

      def format(messages, _, _)
        messages.map do |message|
          "#{format_location(message)} #{format_level(message)}: #{message.msg}".strip
        end
      end

      private

      def format_location(message)
        line = message.line
        lineno = line.new_lineno if line
        path = message.path
        commit_sha = message.commit_sha

        if path || lineno
          path = colorize(path, LOCATION_COLOR) if path
          "#{path}:#{lineno}"
        elsif commit_sha
          colorize(commit_sha[0..6], LOCATION_COLOR)
        end
      end

      def format_level(message)
        level = message.level
        color = LEVEL_COLORS.fetch(level)

        colorize(level[0].upcase, color)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pronto-0.8.2 lib/pronto/formatter/text_formatter.rb
pronto-0.8.1 lib/pronto/formatter/text_formatter.rb
pronto-0.8.0 lib/pronto/formatter/text_formatter.rb
pronto-0.7.1 lib/pronto/formatter/text_formatter.rb
pronto-0.7.0 lib/pronto/formatter/text_formatter.rb