Class: ColorString

Inherits:
Object
  • Object
show all
Defined in:
lib/helper/color_string.rb

Constant Summary collapse

@@colors =
%w(black red green yellow blue magenta cyan white)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, color = nil, bg = nil) ⇒ ColorString

Returns a new instance of ColorString.



9
10
11
# File 'lib/helper/color_string.rb', line 9

def initialize(str, color=nil, bg=nil)
  @str, @color, @bg = str, color, bg
end

Class Method Details

.colorsObject



5
6
7
# File 'lib/helper/color_string.rb', line 5

def self.colors
  @@colors
end

Instance Method Details

#inspectObject



21
22
23
# File 'lib/helper/color_string.rb', line 21

def inspect
  "#<ColorString \"#{@str}\", color:#{@color}, bg:#{@bg}>"
end

#to_sObject



13
14
15
16
17
18
19
# File 'lib/helper/color_string.rb', line 13

def to_s
  return @str if @color.nil? and @bg.nil?
  s = []
  s << "3#{@color}" unless @color.nil?
  s << "4#{@bg}" unless @bg.nil?
  "\e[#{s.join(';')}m" << @str << "\e[0m"
end