module Term::ANSIColor

The ANSIColor module can be used for namespacing and mixed into your own classes.

Constants

COLORED_REGEXP

Regular expression that is used to scan for ANSI-Attributes while uncoloring strings.

VERSION

Term::ANSIColor version

Public Class Methods

attributes()
coloring=(val) click to toggle source

Turns the coloring on or off globally, so you can easily do this for example:

Term::ANSIColor::coloring = STDOUT.isatty
# File lib/term/ansicolor.rb, line 33
def self.coloring=(val)
  @coloring = !!val
end
coloring?() click to toggle source

Returns true, if the coloring function of this module is switched on, false otherwise.

# File lib/term/ansicolor.rb, line 26
def self.coloring?
  @coloring
end
term_ansicolor_attributes() click to toggle source

Returns an array of all Term::ANSIColor attributes as symbols.

# File lib/term/ansicolor.rb, line 114
def term_ansicolor_attributes
  ::Term::ANSIColor::Attribute.attributes.map(&:name)
end
Also aliased as: attributes
true_coloring=(val) click to toggle source

Turns the true coloring mode on or off globally, that will display 24-bit colors if your terminal supports it:

Term::ANSIColor::true_coloring = ENV['COLORTERM'] =~ /\A(truecolor|24bit)\z/
# File lib/term/ansicolor.rb, line 47
def self.true_coloring=(val)
  @true_coloring = !!val
end
true_coloring?() click to toggle source

Returns true, if the tue coloring mode of this module is switched on, false otherwise.

# File lib/term/ansicolor.rb, line 40
def self.true_coloring?
  @true_coloring
end

Public Instance Methods

apply_attribute(name, string = nil, &block) click to toggle source
# File lib/term/ansicolor.rb, line 88
def apply_attribute(name, string = nil, &block)
  attribute = Attribute[name] or
    raise ArgumentError, "unknown attribute #{name.inspect}"
  apply_code(attribute.code, string, &block)
end
apply_code(code, string = nil) { || ... } click to toggle source
# File lib/term/ansicolor.rb, line 72
def apply_code(code, string = nil, &block)
  result = ''.dup
  result << "\e[#{code}m" if Term::ANSIColor.coloring?
  if block_given?
    result << yield.to_s
  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.extend(Term::ANSIColor)
end
attributes()
color(name, string = nil, &block) click to toggle source

Return string or the result string of the given block colored with color name. If string isn’t a string only the escape sequence to switch on the color name is returned.

# File lib/term/ansicolor.rb, line 97
def color(name, string = nil, &block)
  apply_attribute(name, string, &block)
end
on_color(name, string = nil, &block) click to toggle source

Return string or the result string of the given block with a background colored with color name. If string isn’t a string only the escape sequence to switch on the color name is returned.

# File lib/term/ansicolor.rb, line 104
def on_color(name, string = nil, &block)
  attribute = Attribute[name] or
    raise ArgumentError, "unknown attribute #{name.inspect}"
  attribute = attribute.dup
  attribute.background = true
  apply_attribute(attribute, string, &block)
end
term_ansicolor_attributes() click to toggle source

Returns an array of all Term::ANSIColor attributes as symbols.

# File lib/term/ansicolor.rb, line 122
def  term_ansicolor_attributes
  ::Term::ANSIColor.term_ansicolor_attributes
end
Also aliased as: attributes
uncolor(string = nil) { || ... } click to toggle source

Returns an uncolored version of the string, that is all ANSI-Attributes are stripped from the string.

# File lib/term/ansicolor.rb, line 58
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
    ''.dup
  end.extend(Term::ANSIColor)
end
Also aliased as: uncolored
uncolored(string = nil)
Alias for: uncolor