Module: Termin::ANSIColor

Extended by:
ANSIColor
Included in:
ANSIColor, PPMReader
Defined in:
lib/termin/ansicolor.rb,
lib/termin/ansicolor/version.rb,
lib/termin/ansicolor/attribute.rb,
lib/termin/ansicolor/ppm_reader.rb,
lib/termin/ansicolor/rgb_triple.rb,
lib/termin/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 =

Termin::ANSIColor version

'1.3.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

Instance Method Summary collapse

Class Method Details

.attributesObject

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



235
236
237
# File 'lib/termin/ansicolor.rb', line 235

def term_ansicolor_attributes
  ::Termin::ANSIColor::ATTRIBUTE_NAMES
end

.coloring=(val) ⇒ Object

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

Termin::ANSIColor::coloring = STDOUT.isatty


166
167
168
# File 'lib/termin/ansicolor.rb', line 166

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)


159
160
161
# File 'lib/termin/ansicolor.rb', line 159

def self.coloring?
  @coloring
end

.create_color_method(color_name, color_value) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/termin/ansicolor.rb', line 171

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_attributesObject

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



231
232
233
# File 'lib/termin/ansicolor.rb', line 231

def term_ansicolor_attributes
  ::Termin::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.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/termin/ansicolor.rb', line 207

def color(name, string = nil, &block)
  attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}"
  result = ''
  result << "\e[#{attribute.code}m" if Termin::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 Termin::ANSIColor.coloring?
  result
end

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



224
225
226
227
# File 'lib/termin/ansicolor.rb', line 224

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 Termin::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.

Returns:

  • (Boolean)


151
152
153
154
155
156
# File 'lib/termin/ansicolor.rb', line 151

def support?(feature)
  case feature
  when :clear
    !String.instance_methods(false).map(&:to_sym).include?(:clear)
  end
end

#term_ansicolor_attributesObject Also known as: attributes

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



239
240
241
# File 'lib/termin/ansicolor.rb', line 239

def  term_ansicolor_attributes
  ::Termin::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.



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/termin/ansicolor.rb', line 190

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