Class: Colored2::AsciiDecorator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/colored2/ascii_decorator.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_string) ⇒ AsciiDecorator

Returns a new instance of AsciiDecorator.



51
52
53
54
# File 'lib/colored2/ascii_decorator.rb', line 51

def initialize(a_string)
  self.string = a_string.instance_of?(Object) ? '' : a_string.to_s
  self.my_class = self.class
end

Class Attribute Details

.__background_nextObject

Returns the value of attribute __background_next.



25
26
27
# File 'lib/colored2/ascii_decorator.rb', line 25

def __background_next
  @__background_next
end

.__colors_disabledObject

Returns the value of attribute __colors_disabled.



25
26
27
# File 'lib/colored2/ascii_decorator.rb', line 25

def __colors_disabled
  @__colors_disabled
end

Instance Attribute Details

#my_classObject

Returns the value of attribute my_class.



49
50
51
# File 'lib/colored2/ascii_decorator.rb', line 49

def my_class
  @my_class
end

#stringObject

Returns the value of attribute string.



49
50
51
# File 'lib/colored2/ascii_decorator.rb', line 49

def string
  @string
end

Class Method Details

.background_next!Object



35
36
37
# File 'lib/colored2/ascii_decorator.rb', line 35

def background_next!
  self.__background_next = true
end

.background_next?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/colored2/ascii_decorator.rb', line 41

def background_next?
  self.__background_next
end

.disable!Object



32
33
34
# File 'lib/colored2/ascii_decorator.rb', line 32

def disable!
  self.__colors_disabled = true
end

.enable!Object



26
27
28
# File 'lib/colored2/ascii_decorator.rb', line 26

def enable!
  self.__colors_disabled = false
end

.enabled?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/colored2/ascii_decorator.rb', line 29

def enabled?
  !self.__colors_disabled
end

.foreground_next!Object



38
39
40
# File 'lib/colored2/ascii_decorator.rb', line 38

def foreground_next!
  self.__background_next = false
end

Instance Method Details

#decorate(options = {}) ⇒ Object

options = :color options = :color | :no_color



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/colored2/ascii_decorator.rb', line 58

def decorate(options = {})
  return string if !self.class.enabled? || string.length == 0
  escape_sequence = [
    Colored2::TextColor.new(options[:foreground]),
    Colored2::BackgroundColor.new(options[:background]),
    Colored2::Effect.new(options[:effect])
  ].compact.join

  colored = ''
  colored << escape_sequence if options[:beginning] == :on
  colored << string
  if options[:end]
    colored << no_color if options[:end] == :off && !colored.end_with?(no_color)
    colored << escape_sequence if options[:end] == :on
  end
  colored
end

#un_decorateObject



76
77
78
# File 'lib/colored2/ascii_decorator.rb', line 76

def un_decorate
  string.gsub(%r{\e\[\d+(;\d+)*m}, '')
end