Top Level Namespace
Defined Under Namespace
Modules: Nakajima Classes: Array, Proc, Symbol
Instance Method Summary collapse
-
#with_progress(options = {}) ⇒ Object
For making nicer CLI tools that take a while to do things.
Instance Method Details
#with_progress(options = {}) ⇒ Object
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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/nakajima/ui/progress.rb', line 11 def with_progress(={}) [:before] ||= "" # Message to print before starting [:after] ||= "" # Message to print after completion [:char] ||= '.' # Character to be printed as progress [:rate] ||= 0.2 # Delay between printing :char option [:change] ||= 1 # Allows for the rate to accelerate/decelerate print [:before] thread = Thread.new do printer = proc do |interval| print([:char]) $stdout.flush sleep interval printer.call [interval * [:change], 0.01].max end printer.call [:rate] end yield and thread.kill puts [:after] end |