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

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

def colorize(params)
  return self if self.class.disable_colorization
  require_windows_libs
  scan_for_colors.inject(self.class.new) do |str, match|
    colors_from_params(match, params)
    defaults_colors(match)
    str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
  end
end

#colorized?Boolean

Return true if string is colorized

Returns:

  • (Boolean)

41
42
43
44
45
# File 'lib/colorize/instance_methods.rb', line 41

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

#uncolorizeObject

Return uncolorized string


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

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