Class: String

Inherits:
Object show all
Defined in:
lib/frontends/html_frontend.rb,
lib/frontends/ansi_color_frontend.rb

Constant Summary collapse

COLOR_LOOKUP =
{'black' => 30,
  'red' => 31,
  'green' => 32,
  'yellow' => 33,
  'blue' => 34,
  'magenta' => 35,
  'cyan' => 36,
  'white' => 37,
'default' => 38 }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lookup_color_code(color, foreground = true) ⇒ Object



20
21
22
23
# File 'lib/frontends/ansi_color_frontend.rb', line 20

def self.lookup_color_code(color, foreground = true)
  return color if color.is_a? Fixnum
  COLOR_LOOKUP[color.downcase]
end

Instance Method Details

#colorize(color_code) ⇒ Object



12
13
14
# File 'lib/frontends/ansi_color_frontend.rb', line 12

def colorize(color_code)
  "\e[#{String.lookup_color_code(color_code)}m#{self}\e[0m"
end

#colorize_range(range, color) ⇒ Object



16
17
18
# File 'lib/frontends/ansi_color_frontend.rb', line 16

def colorize_range(range, color)
  "#{self[0 ... range.begin]}#{self[range].colorize(color)}#{self[range.end .. -1]}"
end

#html_colorize(color) ⇒ Object



2
3
4
# File 'lib/frontends/html_frontend.rb', line 2

def html_colorize(color)
  "<span style='color: #{color};'>#{self}</span>"
end

#html_colorize_range(range, color) ⇒ Object



6
7
8
# File 'lib/frontends/html_frontend.rb', line 6

def html_colorize_range(range, color)
  "#{self[0 ... range.begin]}#{self[range].html_colorize(color)}#{self[range.end .. -1]}"
end