Module: Colored

Extended by:
Colored
Included in:
Colored
Defined in:
lib/opcolor.rb

Overview

cute.

>> "this is red".red

>> "this is red with a blue background (read: ugly)".red_on_blue

>> "this is red with an underline".red.underline

>> "this is really bold and really blue".bold.blue

>> Colored.red "This is red" # but this part is mostly untested

modded to opal standards

Constant Summary collapse

COLORS =
{ 
  'black'   => 30,
  'red'     => 31, 
  'green'   => 32, 
  'yellow'  => 33,
  'blue'    => 34,
  'magenta' => 35,
  'cyan'    => 36,
  'white'   => 37
}
EXTRAS =
{
  'clear'     => 0, 
  'bold'      => 1,
  'underline' => 4,
  'reversed'  => 7
}

Instance Method Summary collapse

Instance Method Details

#color(color_name) ⇒ Object



86
87
88
89
90
91
# File 'lib/opcolor.rb', line 86

def color(color_name)
  background = color_name.to_s =~ /on_/
  color_name = color_name.to_s.sub('on_', '')
  return unless color_name && COLORS[color_name]
  "\e[#{COLORS[color_name] + (background ? 10 : 0)}m" 
end

#colorize(string, options = {}) ⇒ Object



70
71
72
73
74
75
# File 'lib/opcolor.rb', line 70

def colorize(string, options = {})
  colored = ""
  colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * '' unless $nocolor
  colored << string
  colored << extra(:clear)
end

#colorsObject



77
78
79
# File 'lib/opcolor.rb', line 77

def colors
  @@colors ||= COLORS.keys.sort
end

#extra(extra_name) ⇒ Object



81
82
83
84
# File 'lib/opcolor.rb', line 81

def extra(extra_name)
  extra_name = extra_name.to_s
  "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name]
end