Top Level Namespace

Defined Under Namespace

Modules: Nakajima Classes: Array, Proc, Symbol

Instance Method Summary collapse

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(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