Module: Term::ANSIColor

Extended by:
ANSIColor
Includes:
Attribute::Underline, Hyperlink, Movement
Included in:
ANSIColor, PPMReader
Defined in:
lib/term/ansicolor.rb,
lib/term/ansicolor/version.rb,
lib/term/ansicolor/movement.rb,
lib/term/ansicolor/attribute.rb,
lib/term/ansicolor/hyperlink.rb,
lib/term/ansicolor/hsl_triple.rb,
lib/term/ansicolor/ppm_reader.rb,
lib/term/ansicolor/rgb_triple.rb,
lib/term/ansicolor/attribute/text.rb,
lib/term/ansicolor/attribute/color8.rb,
lib/term/ansicolor/rgb_color_metrics.rb,
lib/term/ansicolor/attribute/color256.rb,
lib/term/ansicolor/attribute/underline.rb,
lib/term/ansicolor/attribute/intense_color8.rb

Overview

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

Defined Under Namespace

Modules: Hyperlink, Movement, RGBColorMetrics, RGBColorMetricsHelpers Classes: Attribute, HSLTriple, PPMReader, RGBTriple

Constant Summary collapse

COLORED_REGEXP =

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

/\e\[(?:(?:[349]|10)[0-7]|[0-9]|[34]8;(5;\d{1,3}|2;\d{1,3}(;\d{1,3}){2})|4:\d|53)?m/
VERSION =

Term::ANSIColor version

'1.11.2'
VERSION_ARRAY =

:nodoc:

VERSION.split('.').map(&:to_i)
VERSION_MAJOR =

:nodoc:

VERSION_ARRAY[0]
VERSION_MINOR =

:nodoc:

VERSION_ARRAY[1]
VERSION_BUILD =

:nodoc:

VERSION_ARRAY[2]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attribute::Underline

#underline

Methods included from Hyperlink

#hyperlink

Methods included from Movement

#clear_screen, #erase_in_display, #erase_in_line, #hide_cursor, #move_backward, #move_down, #move_forward, #move_home, #move_to, #move_to_column, #move_to_line, #move_to_next_line, #move_to_previous_line, #move_up, #restore_position, #return_to_position, #save_position, #scroll_down, #scroll_up, #show_cursor, #terminal_columns, #terminal_lines

Class Method Details

.attributesObject

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



118
119
120
# File 'lib/term/ansicolor.rb', line 118

def term_ansicolor_attributes
  ::Term::ANSIColor::Attribute.attributes.map(&:name)
end

.coloring=(val) ⇒ Object

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

Term::ANSIColor::coloring = STDOUT.isatty


33
34
35
# File 'lib/term/ansicolor.rb', line 33

def self.coloring=(val)
  @coloring = !!val
end

.coloring?Boolean

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

Returns:

  • (Boolean)


26
27
28
# File 'lib/term/ansicolor.rb', line 26

def self.coloring?
  @coloring
end

.term_ansicolor_attributesObject

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



114
115
116
# File 'lib/term/ansicolor.rb', line 114

def term_ansicolor_attributes
  ::Term::ANSIColor::Attribute.attributes.map(&:name)
end

.true_coloring=(val) ⇒ Object

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/


47
48
49
# File 'lib/term/ansicolor.rb', line 47

def self.true_coloring=(val)
  @true_coloring = !!val
end

.true_coloring?Boolean

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

Returns:

  • (Boolean)


40
41
42
# File 'lib/term/ansicolor.rb', line 40

def self.true_coloring?
  @true_coloring
end

Instance Method Details

#apply_attribute(name, string = nil, &block) ⇒ Object



88
89
90
91
92
# 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, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

#color(name, string = nil, &block) ⇒ Object

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.



97
98
99
# 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) ⇒ Object

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.



104
105
106
107
108
109
110
# 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_attributesObject Also known as: attributes

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



122
123
124
# File 'lib/term/ansicolor.rb', line 122

def  term_ansicolor_attributes
  ::Term::ANSIColor.term_ansicolor_attributes
end

#uncolor(string = nil) ⇒ Object Also known as: uncolored

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



58
59
60
61
62
63
64
65
66
67
68
# 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