Class: ConsoleUtil::Color

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

Constant Summary collapse

@@colors =
[:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
@@color_codes =
{
  :reset        =>  0,  # Reset all current console color attributes.
  :bright       =>  1,  # Set intensity of current console foreground color to bright.
  :underline    =>  4,  # Underline console output.
  :blink        =>  5,  # Blink console output.
  :negative     =>  7,  # Invert the current console foreground and background colors.
  :normal       => 22,  # Set intensity of current console foreground color to normal.
  :no_underline => 24,  # Do not underline console output.
  :no_blink     => 25,  # Do not blink console output.
  :positive     => 27   # Do not invert the current console foreground and background colors.
}
@@fg_color_base =
30
@@bg_color_base =
40

Class Method Summary collapse

Class Method Details

.const_missing(const) ⇒ Object

Used to create magic constants for every supported color combination.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/console_util/color.rb', line 31

def const_missing(const)
  # Attempt to parse the method name as a color string.
  color_escape_code = convert_color_string(const.to_s)

  if color_escape_code
    # Define a constant for the ANSI escape code that corresponds to the
    # specified color and return it.
    self.class_eval <<-COLOR_CONST, __FILE__, __LINE__ + 1
      #{const} = "#{color_escape_code}"
    COLOR_CONST
  else
    super
  end
end

.valid_escape_code?(color_escape_code) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/console_util/color.rb', line 46

def valid_escape_code?(color_escape_code)
  !color_escape_code.match(/^\e\[[0-9;]*m$/).nil?
end