Class: String

Inherits:
Object show all
Defined in:
lib/pp-colour/string.rb

Overview

credit goes to [email protected]

Constant Summary collapse

COLORS =
{
  :black     => '0',
  :red       => '1',
  :green     => '2',
  :yellow    => '3',
  :blue      => '4',
  :magenta   => '5',
  :cyan      => '6',
  :white     => '7'
}
FORMAT_CODES =
{
  :bold      => '1',
  :italic    => '3',
  :underline => '4',
  :blink     => '5',
}

Instance Method Summary collapse

Instance Method Details

#ansi_formattedObject



21
22
23
24
25
26
27
28
29
# File 'lib/pp-colour/string.rb', line 21

def ansi_formatted
  ansi_csi = "#{(0x1B).chr}["
  formats = {}
  FORMAT_CODES.each do |name, code|
    formats[name] = code if @properties[name]
  end
  formats[:color] = "3#{COLORS[@properties[:color]]}" if @properties[:color]
  "#{ansi_csi}#{formats.values.join(';')}m#{self}#{ansi_csi}m"
end

#make_colorized(color) ⇒ Object



31
32
33
34
35
# File 'lib/pp-colour/string.rb', line 31

def make_colorized(color)
  @properties ||= {}
  @properties[:color] = color
  ansi_formatted
end