Skip to content

Instantly share code, notes, and snippets.

@shock
Forked from jotto/Progress bar
Created April 29, 2009 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shock/103915 to your computer and use it in GitHub Desktop.
Save shock/103915 to your computer and use it in GitHub Desktop.
class Spinner
private
@work
@job
def spinner
return(['\\','|','/','-'][@i%4])
end
public
def initialize max_iterations, job="Working"
$stdout.sync = true
@max_iterations = max_iterations
@job = job
@i=0
@delta_count = 0
@start_time = Time.now
end
def update
@i += 1
@delta_count += 1
fraction = (@i.to_f/@max_iterations)
if @i%10 == 0
now = Time.now
delta_time = now-@start_time
time_per_iteration = delta_time.to_f / @delta_count
x = (@max_iterations-@i) * time_per_iteration
eta_string = "#{(x.to_i/60).to_i} mins #{x.to_i%60} secs "
@delta_count=0
@start_time = Time.now
else
eta_string = "\b"
end
printf("\r#{@job}: %s %3.2f%s ETA: %s",spinner,(fraction)*100,"%",eta_string)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment