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.



42
43
44
# File 'lib/deep_test/ui/console.rb', line 42

def initialize(label)
  @label = label
end

Instance Method Details

#show(string) ⇒ Object



67
68
69
70
# File 'lib/deep_test/ui/console.rb', line 67

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

#startObject



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

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



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

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