module Colorize::InstanceMethods
Public Instance Methods
colorize(params)
click to toggle source
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
# File lib/colorize/instance_methods.rb, line 20 def colorize(params) return self if self.class.disable_colorization require_windows_libs scan_for_colors.inject('') do |str, match| defaults_colors(match) colors_from_params(match, params) str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m" end end
colorized?()
click to toggle source
Return true if string is colorized
# File lib/colorize/instance_methods.rb, line 42 def colorized? scan_for_colors.inject([]) do |colors, match| colors << match.tap(&:pop) end.flatten.compact.any? end
uncolorize()
click to toggle source
Return uncolorized string
# File lib/colorize/instance_methods.rb, line 33 def uncolorize scan_for_colors.inject('') do |str, match| str << match[3] end end
Private Instance Methods
background_color(color)
click to toggle source
Color for background (offset 40)
# File lib/colorize/instance_methods.rb, line 95 def background_color(color) self.class.color_codes[color] + 40 if self.class.color_codes[color] end
color(color)
click to toggle source
Color for foreground (offset 30)
# File lib/colorize/instance_methods.rb, line 88 def color(color) self.class.color_codes[color] + 30 if self.class.color_codes[color] end
color_from_symbol(match, symbol)
click to toggle source
Set color from params symbol
# File lib/colorize/instance_methods.rb, line 81 def color_from_symbol(match, symbol) match[1] = color(symbol) if color(symbol) end
colors_from_hash(match, hash)
click to toggle source
Set colors from params hash
# File lib/colorize/instance_methods.rb, line 72 def colors_from_hash(match, hash) match[0] = mode(hash[:mode]) if mode(hash[:mode]) match[1] = color(hash[:color]) if color(hash[:color]) match[2] = background_color(hash[:background]) if background_color(hash[:background]) end
colors_from_params(match, params)
click to toggle source
Set color from params
# File lib/colorize/instance_methods.rb, line 62 def colors_from_params(match, params) case params when Hash then colors_from_hash(match, params) when Symbol then color_from_symbol(match, params) end end
defaults_colors(match)
click to toggle source
Set default colors
# File lib/colorize/instance_methods.rb, line 53 def defaults_colors(match) match[0] ||= mode(:default) match[1] ||= color(:default) match[2] ||= background_color(:default) end
mode(mode)
click to toggle source
Mode
# File lib/colorize/instance_methods.rb, line 102 def mode(mode) self.class.mode_codes[mode] end
require_windows_libs()
click to toggle source
Require windows libs
# File lib/colorize/instance_methods.rb, line 127 def require_windows_libs begin require 'Win32/Console/ANSI' if RUBY_VERSION < "2.0.0" && RUBY_PLATFORM =~ /win32/ rescue LoadError raise 'You must gem install win32console to use colorize on Windows' end end
scan_for_colors()
click to toggle source
Scan for colorized string
# File lib/colorize/instance_methods.rb, line 109 def scan_for_colors scan(/\033\[([0-9;]+)m(.+?)\033\[0m|([^\033]+)/m).map do |match| split_colors(match) end end
split_colors(match)
click to toggle source
# File lib/colorize/instance_methods.rb, line 115 def split_colors(match) colors = (match[0] || "").split(';') Array.new(4).tap do |array| array[0], array[1], array[2] = colors if colors.length == 3 array[1] = colors if colors.length == 1 array[3] = match[1] || match[2] end end