Class: Colored2::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/colored2/codes.rb

Direct Known Subclasses

Effect, TextColor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Code

Returns a new instance of Code.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
# File 'lib/colored2/codes.rb', line 29

def initialize(name)
  @name = name
  return if name.nil?

  @escape = codes[name.to_sym]
  raise ArgumentError, "No color or effect named #{name} exists for #{self.class}." if @escape.nil?
end

Instance Attribute Details

#escapeObject

Returns the value of attribute escape.



27
28
29
# File 'lib/colored2/codes.rb', line 27

def escape
  @escape
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/colored2/codes.rb', line 27

def name
  @name
end

Instance Method Details

#to_sObject



43
44
45
# File 'lib/colored2/codes.rb', line 43

def to_s
  value
end

#value(shift = nil) ⇒ Object



37
38
39
40
41
# File 'lib/colored2/codes.rb', line 37

def value(shift = nil)
  escape_code = escape
  escape_code += shift if shift && escape_code
  name && escape ? "\e[#{escape_code}m" : ''
end