Class: Dry::Logger::Formatters::Colors Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/logger/formatters/colors.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Shell colorizer

This was ported from hanami-utils

Since:

  • 1.0.0

Defined Under Namespace

Classes: UnknownColorCodeError

Constant Summary collapse

COLORS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Escapes codes for terminals to output strings in colors

Since:

  • 1.2.0

{black: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
gray: 37}.freeze

Class Method Summary collapse

Class Method Details

.[](code) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method to translate between color names and terminal escape codes

Raises:

  • (UnknownColorError)

    if the color code is unknown

Since:

  • 1.0.0



78
79
80
# File 'lib/dry/logger/formatters/colors.rb', line 78

def self.[](code)
  COLORS.fetch(code) { raise UnknownColorCodeError, code }
end

.call(color, input) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Colorizes output 8 colors available: black, red, green, yellow, blue, magenta, cyan, and gray

Parameters:

  • input (#to_s)

    the string to colorize

  • color (Symbol)

    the color

Returns:

  • (String)

    the colorized string

Raises:

  • (UnknownColorError)

    if the color code is unknown

Since:

  • 1.0.0



56
57
58
# File 'lib/dry/logger/formatters/colors.rb', line 56

def self.call(color, input)
  "#{start(color)}#{input}#{stop}"
end

.evaluate(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



36
37
38
39
40
41
42
# File 'lib/dry/logger/formatters/colors.rb', line 36

def self.evaluate(input)
  COLORS.keys.reduce(input.dup) { |output, color|
    output.gsub!("<#{color}>", start(color))
    output.gsub!("</#{color}>", stop)
    output
  }
end

.start(color) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



62
63
64
# File 'lib/dry/logger/formatters/colors.rb', line 62

def self.start(color)
  "\e[#{self[color]}m"
end

.stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



68
69
70
# File 'lib/dry/logger/formatters/colors.rb', line 68

def self.stop
  "\e[0m"
end