module Term::ANSIColor
def self.coloring=(val)
this for example:
Turns the coloring on or off globally, so you can easily do
def self.coloring=(val) @coloring = val end
def self.coloring?
Returns true, if the coloring function of this module
def self.coloring? @coloring end
def self.create_color_method(color_name, color_value)
def self.create_color_method(color_name, color_value) module_eval <<-EOT def #{color_name}(string = nil, &block) color(:#{color_name}, string, &block) end EOT self end
def color(name, string = nil, &block)
color +name+. If string isn't a string only the escape sequence to switch
Return +string+ or the result string of the given +block+ colored with
def color(name, string = nil, &block) attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}" result = '' result << "\e[#{attribute.code}m" if Term::ANSIColor.coloring? if block_given? result << yield elsif string.respond_to?(:to_str) result << string.to_str elsif respond_to?(:to_str) result << to_str else return result #only switch on end result << "\e[0m" if Term::ANSIColor.coloring? result end
def on_color(name, string = nil, &block)
def on_color(name, string = nil, &block) attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}" color("on_#{attribute.name}", string, &block) end
def support?(feature)
implement the String#clear method. It's better to use the reset color
is only supported on ruby implementations, that do *not* already
The feature :clear, that is mixing the clear color attribute into String,
Returns true if Term::ANSIColor supports the +feature+.
def support?(feature) case feature when :clear !String.instance_methods(false).map(&:to_sym).include?(:clear) end end
def term_ansicolor_attributes
def term_ansicolor_attributes ::Term::ANSIColor::ATTRIBUTE_NAMES end
def term_ansicolor_attributes
def term_ansicolor_attributes ::Term::ANSIColor.term_ansicolor_attributes end
def uncolor(string = nil) # :yields:
ANSI-Attributes are stripped from the string.
Returns an uncolored version of the string, that is all
def uncolor(string = nil) # :yields: if block_given? yield.to_str.gsub(COLORED_REGEXP, '') elsif string.respond_to?(:to_str) string.to_str.gsub(COLORED_REGEXP, '') elsif respond_to?(:to_str) to_str.gsub(COLORED_REGEXP, '') else '' end end