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/ppm_reader.rb,
lib/term/ansicolor/rgb_triple.rb,
lib/term/ansicolor/rgb_color_metrics.rb
Overview
The ANSIColor module can be used for namespacing and mixed into your own classes.
Defined Under Namespace
Modules: RGBColorMetrics, RGBColorMetricsHelpers Classes: Attribute, 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.2.1'
- 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
string
or the result string of the givenblock
colored 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.
216 217 218 |
# File 'lib/term/ansicolor.rb', line 216 def term_ansicolor_attributes ::Term::ANSIColor::ATTRIBUTE_NAMES end |
.coloring=(val) ⇒ Object
147 148 149 |
# File 'lib/term/ansicolor.rb', line 147 def self.coloring=(val) @coloring = val end |
.coloring? ⇒ Boolean
Returns true, if the coloring function of this module is switched on, false otherwise.
140 141 142 |
# File 'lib/term/ansicolor.rb', line 140 def self.coloring? @coloring end |
.create_color_method(color_name, color_value) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/term/ansicolor.rb', line 152 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 |
.term_ansicolor_attributes ⇒ Object
Returns an array of all Term::ANSIColor attributes as symbols.
212 213 214 |
# File 'lib/term/ansicolor.rb', line 212 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.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/term/ansicolor.rb', line 188 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 |
#on_color(name, string = nil, &block) ⇒ Object
205 206 207 208 |
# File 'lib/term/ansicolor.rb', line 205 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.
132 133 134 135 136 137 |
# File 'lib/term/ansicolor.rb', line 132 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.
220 221 222 |
# File 'lib/term/ansicolor.rb', line 220 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.
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/term/ansicolor.rb', line 171 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 |