Class: DeepTest::UI::Console::Spinner

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_test/ui/console.rb

Constant Summary collapse

FRAMES =
['|', '/', '-', '\\']
BACKSPACE =
"\x08"
SECONDS_PER_FRAME =
0.5 / 4

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ Spinner

Returns a new instance of Spinner.



44
45
46
# File 'lib/deep_test/ui/console.rb', line 44

def initialize(label)
  @label = label
end

Instance Method Details

#show(string) ⇒ Object



69
70
71
72
# File 'lib/deep_test/ui/console.rb', line 69

def show(string)
  $stdout.print string
  $stdout.flush
end

#startObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/deep_test/ui/console.rb', line 48

def start
  @start_time = Time.now
  show "#{@label}: "
  @thread = Thread.new do
    index = 0
    loop do
      show FRAMES[index]
      sleep SECONDS_PER_FRAME
      show BACKSPACE
      index = (index + 1) % FRAMES.length
    end
  end 
end

#stopObject



62
63
64
65
66
67
# File 'lib/deep_test/ui/console.rb', line 62

def stop
  @stop_time = Time.now
  @thread.kill if @thread
  show BACKSPACE
  show("finished in %.2f seconds\n" % (@stop_time.to_f - @start_time.to_f))
end