Module: ColorMeRad::Colorize

Extended by:
Colorize
Included in:
Colorize
Defined in:
lib/color_me_rad/colorize.rb

Constant Summary collapse

CODES =
{
  :black   => 0, :light_black   => 60,
  :red     => 1, :light_red     => 61,
  :green   => 2, :light_green   => 62,
  :yellow  => 3, :light_yellow  => 63,
  :blue    => 4, :light_blue    => 64,
  :magenta => 5, :light_magenta => 65,
  :cyan    => 6, :light_cyan    => 66,
  :white   => 7, :light_white   => 67,
  :default => 9
}

Instance Method Summary collapse

Instance Method Details

#colored?(text) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/color_me_rad/colorize.rb', line 33

def colored?(text)
  return false unless text.is_a?(String)
  !!text.match(/\033\[\d{2,3}m/)
end

#colorize(text, foreground, background) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/color_me_rad/colorize.rb', line 17

def colorize(text, foreground, background)
  return text unless ColorMeRad.enabled?

  text = set_foreground(text, foreground) if foreground
  text = set_background(text, background) if background

  text
end

#show_samplesObject



26
27
28
29
30
31
# File 'lib/color_me_rad/colorize.rb', line 26

def show_samples
  CODES.keys.permutation(2).each do |background, foreground|
    message = sample_message(background, foreground, 13)
    puts colorize(message, foreground, background)
  end
end