Class: Loupe::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/loupe/color.rb

Overview

Color

This class is responsible for coloring strings.

Constant Summary collapse

COLORS =

Returns:

  • (Hash<Symbol, String>)
{
  red: "31",
  green: "32",
  yellow: "33"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(enabled) ⇒ Color

Returns a new instance of Color.

Parameters:

  • enabled (Boolean)


17
18
19
# File 'lib/loupe/color.rb', line 17

def initialize(enabled)
  @enabled = enabled
end

Instance Method Details

#p(string, color) ⇒ String

Parameters:

  • string (String, Symbol)
  • color (Symbol)

Returns:

  • (String)


24
25
26
27
28
29
# File 'lib/loupe/color.rb', line 24

def p(string, color)
  return string unless @enabled

  color_code = COLORS[color]
  "\033[1;#{color_code}m#{string}\033[0m"
end