Module: Term::ANSIColor
- Extended by:
- ANSIColor
- Defined in:
- lib/term/ansicolor.rb,
lib/term/ansicolor/version.rb,
lib/term/ansicolor/attribute.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/intense_color8.rb
Overview
The ANSIColor module can be used for namespacing and mixed into your own classes.
Defined Under Namespace
Modules: RGBColorMetrics, RGBColorMetricsHelpers Classes: Attribute, HSLTriple, PPMReader, RGBTriple
Constant Summary collapse
- ATTRIBUTE_NAMES =
:stopdoc:
Attribute.named_attributes.map(&:name)
- 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})?m/- VERSION =
Term::ANSIColor version
'1.5.0'- 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
-
.attributes ⇒ Object
Returns an array of all Term::ANSIColor attributes as symbols.
-
.coloring=(val) ⇒ Object
Turns the coloring on or off globally, so you can easily do this for example: Term::ANSIColor::coloring = STDOUT.isatty.
-
.coloring? ⇒ Boolean
Returns true, if the coloring function of this module is switched on, false otherwise.
- .create_color_method(color_name, color_value) ⇒ Object
-
.term_ansicolor_attributes ⇒ Object
Returns an array of all Term::ANSIColor attributes as symbols.
Instance Method Summary collapse
-
#color(name, string = nil, &block) ⇒ Object
Return
stringor the result string of the givenblockcolored with colorname. - #on_color(name, string = nil, &block) ⇒ Object
-
#support?(feature) ⇒ Boolean
Returns true if Term::ANSIColor supports the
feature. -
#term_ansicolor_attributes ⇒ Object
(also: #attributes)
Returns an array of all Term::ANSIColor attributes as symbols.
-
#uncolor(string = nil) ⇒ Object
(also: #uncolored)
Returns an uncolored version of the string, that is all ANSI-Attributes are stripped from the string.
Class Method Details
.attributes ⇒ Object
Returns an array of all Term::ANSIColor attributes as symbols.
112 113 114 |
# File 'lib/term/ansicolor.rb', line 112 def term_ansicolor_attributes ::Term::ANSIColor::ATTRIBUTE_NAMES end |
.coloring=(val) ⇒ Object
Turns the coloring on or off globally, so you can easily do this for example:
Term::ANSIColor::coloring = STDOUT.isatty
43 44 45 |
# File 'lib/term/ansicolor.rb', line 43 def self.coloring=(val) @coloring = val end |
.coloring? ⇒ Boolean
Returns true, if the coloring function of this module is switched on, false otherwise.
36 37 38 |
# File 'lib/term/ansicolor.rb', line 36 def self.coloring? @coloring end |
.create_color_method(color_name, color_value) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/term/ansicolor.rb', line 48 def self.create_color_method(color_name, color_value) module_eval " def \#{color_name}(string = nil, &block)\n color(:\#{color_name}, string, &block)\n end\n EOT\n self\nend\n" |
.term_ansicolor_attributes ⇒ Object
Returns an array of all Term::ANSIColor attributes as symbols.
108 109 110 |
# File 'lib/term/ansicolor.rb', line 108 def term_ansicolor_attributes ::Term::ANSIColor::ATTRIBUTE_NAMES end |
Instance Method Details
#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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/term/ansicolor.rb', line 84 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.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 |
#on_color(name, string = nil, &block) ⇒ Object
101 102 103 104 |
# File 'lib/term/ansicolor.rb', line 101 def on_color(name, string = nil, &block) attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}" color("on_#{attribute.name}", string, &block) end |
#support?(feature) ⇒ Boolean
Returns true if Term::ANSIColor supports the feature.
The feature :clear, that is mixing the clear color attribute into String, is only supported on ruby implementations, that do not already implement the String#clear method. It’s better to use the reset color attribute instead.
28 29 30 31 32 33 |
# File 'lib/term/ansicolor.rb', line 28 def support?(feature) case feature when :clear !String.instance_methods(false).map(&:to_sym).include?(:clear) end end |
#term_ansicolor_attributes ⇒ Object Also known as: attributes
Returns an array of all Term::ANSIColor attributes as symbols.
116 117 118 |
# File 'lib/term/ansicolor.rb', line 116 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.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/term/ansicolor.rb', line 67 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.extend(Term::ANSIColor) end |