Skip to content

Commit

Permalink
Merge pull request #9 from Thermatix/master
Browse files Browse the repository at this point in the history
allow start method to accept blocks which it can execute and animate …
  • Loading branch information
piotrmurach committed Jul 13, 2016
2 parents 0f14221 + d0d55f5 commit 770e35a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/tty/spinner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,26 @@ def on(name, &block)
#
#
# @api public
def start
# @param [String] stop_message
# If you provide a block, this is the stop message given when the block finishes
# @yield Provide a block to be executed and have the spinner automatically animate for you
def start(stop_message=nil,&block)
@started_at = Time.now
sleep_time = 1.0 / @interval

@thread = Thread.new do
@thread =
Thread.new do
while @started_at do
spin
sleep(sleep_time)
end
end
if block_given?
@thread = Thread.new(&block)
until(@thread.status == false) do
sleep(sleep_time)
end
stop(stop_message)
end
end

# Duration of the spinning animation
Expand Down
11 changes: 11 additions & 0 deletions spec/unit/start_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@

expect(spinner).to have_received(:spin).at_least(5).times
end

it "accepts block and stop message and will wait and spin until provided block finishes executing" do
spinner = TTY::Spinner.new(output: output, interval: 100)
allow(spinner).to receive(:spin)
spinner.start "done" do
100000000.times do |n|
n * n
end
end
expect(spinner).to have_received(:spin).at_least(5).times
end
end

0 comments on commit 770e35a

Please sign in to comment.