module Decking::Helpers

Constants

CONSOLE_LENGTH

Public Instance Methods

clear_progressline() click to toggle source
# File lib/decking/helpers.rb, line 78
def clear_progressline
  $stdout.print " " * CONSOLE_LENGTH + "\r"
  #$stdout.print "\n"
end
run_with_progress(title, &block) click to toggle source
# File lib/decking/helpers.rb, line 7
def run_with_progress(title, &block)
  command  = Thread.new(&block).tap{ |t| t.abort_on_exception = true}

  progress = Thread.new do
    opts = { title: title, 
             total: nil, 
             length: CONSOLE_LENGTH, 
             format: '%t%B', 
             progress_mark: ' ', 
             unknown_progress_animation_steps: ['..  .', '...  ', ' ... ', '  ...', '.  ..'] }
    progressbar = ProgressBar.create opts

    begin
      loop do
        progressbar.increment
        sleep 0.5
      end
    rescue RuntimeError => e
      if e.message == 'Shutdown'
        progressbar.total = 100
        progressbar.format '%t ' + "\u2713".green
        progressbar.finish
      else
        raise RuntimeError e
      end
    end
  end.tap {|t| t.abort_on_exception = true }

  command.join
  progress.raise 'Shutdown'
  progress.join
  finished = true
rescue Interrupt
  clear_progressline
  puts "I know you did't mean to do that... try again if you really do".yellow
rescue Exception => e
  clear_progressline
  puts e.class
  puts e.message
  puts e.backtrace.inspect
  exit
ensure
  begin
    unless finished
      command.join
      progress.raise 'Shutdown'
      progress.join
    end
  rescue Interrupt
    puts "Caught second interrupt, exiting...".red
    exit
  rescue SystemExit
    puts "Caught SystemExit. Exiting...".red
    exit
  end
end
run_with_threads_multiplexed(method, containers, *args) click to toggle source
# File lib/decking/helpers.rb, line 64
def run_with_threads_multiplexed method, containers, *args
  clear_progressline
  threads = Array.new 
  containers.map do |name, container|
    threads << Thread.new do
      container.method(method).call(*args)
      sleep 0.1
    end
  end
  threads.map { |thread| thread.join }
rescue Interrupt
  threads.map { |thread| thread.kill }
end