Module: Decking::Helpers

Extended by:
Helpers
Included in:
Container, Container, Helpers, Image, Image
Defined in:
lib/decking/helpers.rb

Constant Summary collapse

CONSOLE_LENGTH =
80

Instance Method Summary collapse

Instance Method Details

#clear_progresslineObject



78
79
80
81
# File 'lib/decking/helpers.rb', line 78

def clear_progressline
  $stdout.print " " * CONSOLE_LENGTH + "\r"
  #$stdout.print "\n"
end

#run_with_progress(title, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# 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