Module: Locomotive::Wagon::SpinnerConcern

Included in:
PullCommand, PushCommand, SyncCommand
Defined in:
lib/locomotive/wagon/commands/concerns/spinner_concern.rb

Instance Method Summary collapse

Instance Method Details

#show_wait_spinner(sentence = nil, fps = 10) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/locomotive/wagon/commands/concerns/spinner_concern.rb', line 6

def show_wait_spinner(sentence = nil, fps = 10)
  chars = %w[| / - \\]
  delay = 1.0/fps
  iter  = 0

  spinner = Thread.new do
    print sentence if sentence

    while iter do  # Keep spinning until told otherwise
      print chars[(iter += 1) % chars.length]
      sleep delay
      print "\b"
    end
  end

  yield.tap {       # After yielding to the block, save the return value
    iter = false   # Tell the thread to exit, cleaning up after itself…
    spinner.join   # …and wait for it to do so.
  }                # Use the block's return value as the method's
end