class RspecPacmanFormatter::Pacman

Constants

CI_TERMINAL_WIDTH

Attributes

failed[RW]
progress_line[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/rspec_pacman_formatter/pacman.rb, line 13
def initialize(*args)
  super
  @progress_line = ''
  @failed        = 0
  @notification  = 0
  @repetitions   = 0
end

Public Instance Methods

close(_) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 51
def close(_)
  puts 'GAME OVER'.red if @failed > 0
end
cols() click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 21
def cols
  @cols ||= begin
    width = `tput cols`.chomp.to_i
    width.nil? || width.zero? ? CI_TERMINAL_WIDTH : width
  end
end
example_failed(_) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 42
def example_failed(_)
  @failed += 1
  step(Characters.random_ghost)
end
example_passed(_) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 38
def example_passed(_)
  step('.')
end
example_pending(_) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 47
def example_pending(_)
  step('*')
end
example_started(_) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 34
def example_started(_)
  step(Characters::PACMAN)
end
start(notification) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 28
def start(notification)
  puts 'GAME STARTED'
  @notification = notification.count
  update_progress_line
end
update_progress_line() click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 55
def update_progress_line
  return pacdots(@notification) unless @notification > cols
  return pacdots(cols) unless (@notification / cols).eql?(@repetitions)

  pacdots((@notification - (cols * @repetitions)))
end

Private Instance Methods

pacdots(number) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 64
def pacdots(number)
  @progress_line = Characters::PACDOT * number
end
step(character) click to toggle source
# File lib/rspec_pacman_formatter/pacman.rb, line 68
def step(character)
  @progress_line = @progress_line.sub(/#{Regexp.quote(Characters::PACMAN)}|#{Regexp.quote(Characters::PACDOT)}/, character)
  print "#{@progress_line}\r"
  return unless @progress_line[-1] =~ /ᗣ|\./
  @repetitions += 1
  puts
  update_progress_line
end