Class: Chromate::Effect
- Inherits:
-
Object
- Object
- Chromate::Effect
- 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
Class Method Summary collapse
-
.[](value) ⇒ Effect
Get the effect with the specified name.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Boolean
-
#escape ⇒ String
Escape as an SGR escape sequence.
-
#initialize(value) ⇒ Effect
constructor
Create a new effect with the specified value.
- #inspect ⇒ String
-
#name ⇒ Symbol
Get the name of this effect, if it exists.
-
#to_i ⇒ Integer
Yield the internal value.
- #to_s ⇒ String
Constructor Details
#initialize(value) ⇒ Effect
Create a new effect with the specified value.
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.
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
79 80 81 |
# File 'lib/chromate/effect.rb', line 79 def ==(other) to_i == other.to_i end |
#escape ⇒ String
Escape as an SGR escape sequence.
42 43 44 |
# File 'lib/chromate/effect.rb', line 42 def escape "\e[#{@value}m" end |
#inspect ⇒ String
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 |
#name ⇒ Symbol
Get the name of this effect, if it exists.
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_i ⇒ Integer
Yield the internal value.
69 70 71 |
# File 'lib/chromate/effect.rb', line 69 def to_i @value end |