Module: Colorize::InstanceMethods

Included in:
ColorizedString, String
Defined in:
lib/colorize/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#colorize(params) ⇒ Object

Change color of string

Examples:

puts "This is blue".colorize(:blue)
puts "This is light blue".colorize(:light_blue)
puts "This is also blue".colorize(:color => :blue)
puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red)
puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red)
puts "This is blue text on red".blue.on_red
puts "This is red on blue".colorize(:red).on_blue
puts "This is red on blue and underline".colorize(:red).on_blue.underline
puts "This is blue text on red".blue.on_red.blink
puts "This is uncolorized".blue.on_red.uncolorize


21
22
23
24
25
26
27
28
29
# File 'lib/colorize/instance_methods.rb', line 21

def colorize(params)
  return self if self.class.disable_colorization

  scan_for_colors.inject(self.class.new) do |str, match|
    colors_from_params(match, params)
    defaults_colors(match)
    str << colorized_string(match[0], match[1], match[2], match[3])
  end
end

#colorized?Boolean

Return true if string is colorized

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/colorize/instance_methods.rb', line 43

def colorized?
  scan_for_colors.inject([]) do |colors, match|
    colors << match.tap(&:pop)
  end.flatten.compact.any?
end

#uncolorizeObject

Return uncolorized string



34
35
36
37
38
# File 'lib/colorize/instance_methods.rb', line 34

def uncolorize
  scan_for_colors.inject(self.class.new) do |str, match|
    str << match[3]
  end
end