Skip to content

Instantly share code, notes, and snippets.

@nakajima
Created September 26, 2008 01:15
Show Gist options
  • Save nakajima/13001 to your computer and use it in GitHub Desktop.
Save nakajima/13001 to your computer and use it in GitHub Desktop.
undefined
# For making nicer CLI tools that take a while to do things
#
# Example:
#
# with_progress do
# sleep 2 # or some slow task
# end
#
# You can also pass in options to customize messages as well
# as the rate of the progress output.
def with_progress(options={})
options[:before] ||= "" # Message to print before starting
options[:after] ||= "" # Message to print after completion
options[:char] ||= '.' # Character to be printed as progress
options[:rate] ||= 0.2 # Delay between printing :char option
options[:change] ||= 1 # Allows for the rate to accelerate/decelerate
print options[:before]
thread = Thread.new do
printer = proc do |interval|
print(options[:char])
$stdout.flush
sleep interval
printer.call [interval * options[:change], 0.01].max
end
printer.call options[:rate]
end
yield and thread.kill
puts options[:after]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment