Class: Rainbow

Inherits:
RSpec::Core::Formatters::ProgressFormatter
  • Object
show all
Defined in:
lib/rainbow.rb

Constant Summary collapse

PI_3 =
Math::PI / 3

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Rainbow

Returns a new instance of Rainbow.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rainbow.rb', line 6

def initialize(options)
  # colors calculation stolen from Minitest's Pride Plugin
  # https://github.com/seattlerb/minitest
  @colors = (0...(6 * 7)).map { |n|
    n *= 1.0 / 6
    r  = (3 * Math.sin(n           ) + 3).to_i
    g  = (3 * Math.sin(n + 2 * PI_3) + 3).to_i
    b  = (3 * Math.sin(n + 4 * PI_3) + 3).to_i

    36 * r + 6 * g + b + 16
  }
  @color_index = 0
  super(options)
end

Instance Method Details

#color(text, color_code) ⇒ Object



26
27
28
29
# File 'lib/rainbow.rb', line 26

def color(text, color_code)
  color_code = rainbow if text == '.'
  super
end

#color_code_for(code_or_symbol) ⇒ Object



31
32
33
34
# File 'lib/rainbow.rb', line 31

def color_code_for(code_or_symbol)
  return "38;5;#{code_or_symbol}" if code_or_symbol.is_a?(Integer)
  super
end

#rainbowObject



21
22
23
24
# File 'lib/rainbow.rb', line 21

def rainbow
  @color_index == (@colors.size - 1) ? @color_index = 0 : @color_index += 1
  @colors[@color_index]
end