module Colorize::ClassMethods
def add_color_alias(*params)
Add color alias
def add_color_alias(*params) parse_color_alias_params(params).each do |_alias_, _color_| check_if_color_available!(_alias_) check_if_color_exist!(_color_) add_color_code(_alias_, color_codes[_color_]) add_color_method(_alias_) end end
def add_color_code(code, color)
def add_color_code(code, color) @@color_codes[code] = color end
def add_color_method(key)
Generate color and on_color method
def add_color_method(key) define_method key do colorize(:color => key) end define_method "on_#{key}" do colorize(:background => key) end end
def check_if_color_available!(color)
Check if color exists
def check_if_color_available!(color) color_exist?(color) && fail(::Colorize::ColorAlreadyExist, "Colorize: color named :#{color} already exist!") end
def check_if_color_exist!(color)
Check if color is missing
def check_if_color_exist!(color) color_exist?(color) || fail(::Colorize::ColorDontExist, "Colorize: color :#{color} don't exist!") end
def color_codes
Color codes hash
def color_codes @@color_codes end
def color_exist?(color)
def color_exist?(color) !color_codes[color].nil? end
def color_methods
Generate color and on_color methods
def color_methods colors.each do |key| next if key == :default add_color_method(key) end end
def color_samples
Display color samples
def color_samples colors.permutation(2).each do |background, color| sample_text = "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}" puts "#{new(sample_text).colorize(:color => color, :background => background)} #{sample_text}" end end
def colors
Return array of available colors used by colorize
def colors color_codes.keys end
def disable_colorization(value = nil)
Property for disable colorization
def disable_colorization(value = nil) if value.nil? if defined?(@@disable_colorization) @@disable_colorization || false else false end else @@disable_colorization = (value || false) end end
def disable_colorization=(value)
Setter for disable colorization
def disable_colorization=(value) @@disable_colorization = (value || false) end
def enable_readline_support(value = nil)
Property for readline support
def enable_readline_support(value = nil) if value.nil? if defined?(@@readline_support) @@readline_support || false else false end else @@readline_support = (value || false) end end
def enable_readline_support=(value)
Setter for enable readline support
def enable_readline_support=(value) @@readline_support = (value || false) end
def mode_codes
Mode codes hash
def mode_codes @@mode_codes end
def modes
Return array of available modes used by colorize
def modes mode_codes.keys end
def modes_methods
Generate modes methods
def modes_methods modes.each do |key| next if key == :default define_method key do colorize(:mode => key) end end end
def parse_color_alias_params(params)
def parse_color_alias_params(params) return [params] if params.is_a?(Array) && params.length == 2 params.flat_map do |param| next param if param.is_a?(Array) && param.length == 2 next param.to_a if param.is_a?(Hash) end end
def prevent_colors(value = nil)
Property for prevent recolorization
def prevent_colors(value = nil) if value.nil? if defined?(@@prevent_colors) @@prevent_colors || false else false end else @@prevent_colors = (value || false) end end
def prevent_colors=(value)
Setter for prevent recolorization
def prevent_colors=(value) @@prevent_colors = (value || false) end