lib/doro/progressbar.rb in doro-0.2.0 vs lib/doro/progressbar.rb in doro-0.2.3

- old
+ new

@@ -1,22 +1,31 @@ require 'io/console' require 'time' +require 'notifier' class ProgressBar def initialize(title, max_progress) @progress = 0 @max_progress = max_progress @title = title @start_time = Time.now + @interrupt = false end def start - while (@progress <= @max_progress) + Signal.trap("INT") { @interrupt = true } + + while (@progress <= @max_progress && @interrupt == false ) render_progress - sleep 1 @progress += 1 + yield + sleep 1 end + + print("\r") + + display_notification end private def render_progress @@ -28,11 +37,10 @@ print "\e[32m#{display_time}\e[0m" print "\r" end # Methods for determing time - def display_time Time.at((Time.now - @start_time)).utc.strftime("%M:%S") end # Methods for determing progress bar length @@ -50,11 +58,10 @@ def other_items_length @title.size + 10 end - # Methods for determining terminal length def terminal_length return 80 unless unix? result = dynamic_width @@ -70,6 +77,14 @@ end def unix? RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i end + + # Methods for notifications + def display_notification + unless @interrupt + Notifier.notify(image: "logo.png", title: @title, message: "") + end + end + end