lib/doro/progressbar.rb in doro-0.2.3 vs lib/doro/progressbar.rb in doro-0.2.4
- old
+ new
@@ -1,18 +1,29 @@
require 'io/console'
require 'time'
require 'notifier'
+##
+# This class handles the incrementing and display of the progress bar.
class ProgressBar
+ ##
+ # Title can be any string, max_progress is in seconds.
def initialize(title, max_progress)
@progress = 0
@max_progress = max_progress
@title = title
@start_time = Time.now
@interrupt = false
end
+ ##
+ # This starts the timer. It accepts a block which allows you plug in other
+ # behaviors (such as incrementing another timer outside of the class).
+ # You'd do something like:
+ # ProgressBar.new("Example", 60).start do |t|
+ # p 'this fires every second'
+ # end
def start
Signal.trap("INT") { @interrupt = true }
while (@progress <= @max_progress && @interrupt == false )
render_progress
@@ -28,14 +39,14 @@
private
def render_progress
print @title
- print " ["
- print "=" * progress_bar_ticks
- print " " * remaining_bar_ticks rescue 0
- print "] "
+ print ' ['
+ print '=' * progress_bar_ticks
+ print ' ' * remaining_bar_ticks rescue 0
+ print '] '
print "\e[32m#{display_time}\e[0m"
print "\r"
end
# Methods for determing time
@@ -63,11 +74,11 @@
# Methods for determining terminal length
def terminal_length
return 80 unless unix?
result = dynamic_width
- result < 20? 80 : result
+ result < 20 ? 80 : result
rescue
80
end
@@ -86,5 +97,6 @@
Notifier.notify(image: "logo.png", title: @title, message: "")
end
end
end
+