Sha256: dbaff2beb513ae18a7f89bd3443cc695d5d01537b5693f7f3b98f3b34d7c456c

Contents?: true

Size: 966 Bytes

Versions: 12

Compression:

Stored size: 966 Bytes

Contents

# frozen_string_literal: true

module Recog
  class Formatter
    COLORS = {
      red: 31,
      yellow: 33,
      green: 32,
      white: 15
    }.freeze

    attr_reader :options, :output

    def initialize(options, output)
      @options = options
      @output  = output || StringIO.new
    end

    def status_message(text)
      output.puts color(text, :white)
    end

    def success_message(text)
      output.puts color(text, :green)
    end

    def warning_message(text)
      output.puts color(text, :yellow)
    end

    def failure_message(text)
      output.puts color(text, :red)
    end

    private

    def color_enabled?
      options.color
    end

    def color(text, color_code)
      color_enabled? ? colorize(text, color_code) : text
    end

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

    def color_code_for(code)
      COLORS.fetch(code) { COLORS.fetch(:white) }
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
recog-3.1.13 lib/recog/formatter.rb
recog-3.1.12 lib/recog/formatter.rb
recog-3.1.11 lib/recog/formatter.rb
recog-3.1.10 lib/recog/formatter.rb
recog-3.1.9 lib/recog/formatter.rb
recog-3.1.8 lib/recog/formatter.rb
recog-3.1.7 lib/recog/formatter.rb
recog-3.1.6 lib/recog/formatter.rb
recog-3.1.5 lib/recog/formatter.rb
recog-3.1.4 lib/recog/formatter.rb
recog-3.1.3 lib/recog/formatter.rb
recog-3.1.2 lib/recog/formatter.rb