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.



63
64
65
66
# File 'lib/colored2/ascii_decorator.rb', line 63

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.



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

def __background_next
  @__background_next
end

.__colors_disabledObject

Returns the value of attribute __colors_disabled.



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

def __colors_disabled
  @__colors_disabled
end

Instance Attribute Details

#my_classObject

Returns the value of attribute my_class.



61
62
63
# File 'lib/colored2/ascii_decorator.rb', line 61

def my_class
  @my_class
end

#stringObject

Returns the value of attribute string.



61
62
63
# File 'lib/colored2/ascii_decorator.rb', line 61

def string
  @string
end

Class Method Details

.background_next!Object



45
46
47
# File 'lib/colored2/ascii_decorator.rb', line 45

def background_next!
  self.__background_next = true
end

.background_next?Boolean

Returns:

  • (Boolean)


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

def background_next?
  __background_next
end

.disable!Object



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

def disable!
  self.__colors_disabled = true
end

.enable!Object



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

def enable!
  self.__colors_disabled = false
end

.enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  !__colors_disabled
end

.foreground_next!Object



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

def foreground_next!
  self.__background_next = false
end

Instance Method Details

#decorate(options = {}) ⇒ Object

options = :color options = :color | :no_color



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/colored2/ascii_decorator.rb', line 70

def decorate(options = {})
  return string if !self.class.enabled? || string.empty?

  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



89
90
91
# File 'lib/colored2/ascii_decorator.rb', line 89

def un_decorate
  string.gsub(/\e\[\d+(;\d+)*m/, '')
end