Class: Chromate::Effect

Inherits:
Object
  • Object
show all
Defined in:
lib/chromate/effect.rb

Overview

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.

Direct Known Subclasses

Color

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Effect

Create a new effect with the specified value.

Parameters:

  • value (Fixnum)


17
18
19
20
21
22
23
# 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

Class Method Details

.[](value) ⇒ Effect

Get the effect with the specified name.

Parameters:

  • value (String, Symbol)

    the name of an effect

Returns:



30
31
32
33
34
35
36
# 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

Instance Method Details

#+(other) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/chromate/effect.rb', line 79

def ==(other)
  to_i == other.to_i
end

#escapeString

Escape as an SGR escape sequence.

Returns:

  • (String)

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



42
43
44
# File 'lib/chromate/effect.rb', line 42

def escape
  "\e[#{@value}m"
end

#inspectString

Returns:



57
58
59
60
61
62
63
# File 'lib/chromate/effect.rb', line 57

def inspect
  if name.nil?
    "#<Effect: #{@value}>"
  else
    "#<Effect: #{name}>"
  end
end

#nameSymbol

Get the name of this effect, if it exists.

Returns:

  • (Symbol)


50
51
52
53
54
# File 'lib/chromate/effect.rb', line 50

def name
  if EFFECTS.value?(self)
    EFFECTS.find { |k, v| v == self }.first
  end
end

#to_iInteger

Yield the internal value.

Returns:

  • (Integer)


69
70
71
# File 'lib/chromate/effect.rb', line 69

def to_i
  @value
end

#to_sString

Returns:



74
75
76
# File 'lib/chromate/effect.rb', line 74

def to_s
  escape
end