Sha256: eebef2cd7dac3496c9d93ba24b67e7717f31901d18ff20ea720f13ca742d7a0a

Contents?: true

Size: 869 Bytes

Versions: 136

Compression:

Stored size: 869 Bytes

Contents

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

  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

136 entries across 136 versions & 2 rubygems

Version Path
recog-3.1.1 lib/recog/formatter.rb
recog-3.1.0 lib/recog/formatter.rb
recog-3.0.3 lib/recog/formatter.rb
recog-3.0.2 lib/recog/formatter.rb
recog-3.0.1 lib/recog/formatter.rb
recog-2.3.23 lib/recog/formatter.rb
recog-2.3.22 lib/recog/formatter.rb
recog-2.3.21 lib/recog/formatter.rb
recog-2.3.20 lib/recog/formatter.rb
recog-2.3.19 lib/recog/formatter.rb
recog-2.3.18 lib/recog/formatter.rb
recog-2.3.17 lib/recog/formatter.rb
recog-2.3.16 lib/recog/formatter.rb
recog-2.3.15 lib/recog/formatter.rb
recog-intrigue-2.3.14 lib/recog/formatter.rb
recog-2.3.14 lib/recog/formatter.rb
recog-2.3.13 lib/recog/formatter.rb
recog-2.3.12 lib/recog/formatter.rb
recog-2.3.11 lib/recog/formatter.rb
recog-2.3.10 lib/recog/formatter.rb