class Chromate::Effect

The `Effect` class is used to represent a generic escape sequences, providing a standardized escape mechanism. You may convert an effect to either a string, in which case it will be escaped as an SGR escape sequence, or an integer, in which case it will yield its internal value. You may also explicitly escape an effect by calling {#escape} on it.

Public Class Methods

[](value) click to toggle source

Get the effect with the specified name. @param value [String,Symbol] the name of an effect

@return [Effect]

# File lib/chromate/effect.rb, line 30
def self.[](value)
  if value.is_a?(Symbol) or value.is_a?(String)
    EFFECTS[value.intern]
  else
    raise TypeError, "don't know what to do with a #{value.class}"
  end
end
new(value) click to toggle source

Create a new effect with the specified value. @param value [Fixnum]

@return [Effect]

# File lib/chromate/effect.rb, line 17
def initialize(value)
  unless value.is_a?(Numeric)
    raise TypeError, "cannot create #{self.class} from #{value.class}"
  end

  @value = value.to_i
end

Public Instance Methods

+(other) click to toggle source
# File lib/chromate/effect.rb, line 83
def +(other)
  case other
  when EffectSet
    other.add(self)
  when Effect
    EffectSet.new([self, other])
  when String
    escape + other
  else
    raise TypeError, "cannot add #{other.class} to #{self.class}"
  end
end
==(other) click to toggle source

@return [Boolean]

# File lib/chromate/effect.rb, line 79
def ==(other)
  to_i == other.to_i
end
escape() click to toggle source

Escape as an SGR escape sequence.

@return [String] an escape sequence of the form “e[1;2;3;…m”

# File lib/chromate/effect.rb, line 42
def escape
  "\e[#{@value}m"
end
inspect() click to toggle source

@return [String]

# File lib/chromate/effect.rb, line 57
def inspect
  if name.nil?
    "#<Effect: #{@value}>"
  else
    "#<Effect: #{name}>"
  end
end
name() click to toggle source

Get the name of this effect, if it exists.

@return [Symbol]

# File lib/chromate/effect.rb, line 50
def name
  if EFFECTS.value?(self)
    EFFECTS.find { |k, v| v == self }.first
  end
end
to_i() click to toggle source

Yield the internal value.

@return [Integer]

# File lib/chromate/effect.rb, line 69
def to_i
  @value
end
to_s() click to toggle source

@return [String]

# File lib/chromate/effect.rb, line 74
def to_s
  escape
end