Class: ProgressPrinter
- Inherits:
-
Object
- Object
- ProgressPrinter
- Defined in:
- lib/progress_printer.rb,
lib/progress_printer/version.rb
Overview
A progress printer which simplifies logging the progress of loops. To use, create a new ProgressPrinter, and then increment inside of a loop.
Example:
printer = ProgressPrinter.new(name: "Counting", total: 250)
printer.start
250.times { printer.increment }
printer.finish
Output:
Counting: 0/250 0% calculating...
Counting: 100/250 40% ~1m30s
Counting: 200/250 80% ~45s
Counting: 250/250 100% ~0s
Constant Summary collapse
- DEFAULT_EVERY =
100- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#every ⇒ Object
readonly
Returns the value of attribute every.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#out ⇒ Object
readonly
Returns the value of attribute out.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #current ⇒ Object
- #estimated_time_remaining(now = Time.now) ⇒ Object
- #finish ⇒ Object
- #increment(count = 1) ⇒ Object
-
#initialize(total: nil, name: nil, every: DEFAULT_EVERY, out: $stdout) ⇒ ProgressPrinter
constructor
A new instance of ProgressPrinter.
- #percent_complete ⇒ Object
- #percent_complete_string ⇒ Object
- #percent_remaining ⇒ Object
- #seconds_remaining(now) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(total: nil, name: nil, every: DEFAULT_EVERY, out: $stdout) ⇒ ProgressPrinter
Returns a new instance of ProgressPrinter.
24 25 26 27 28 29 |
# File 'lib/progress_printer.rb', line 24 def initialize(total: nil, name: nil, every: DEFAULT_EVERY, out: $stdout) @total = total @name = name @every = every @out = out end |
Instance Attribute Details
#every ⇒ Object (readonly)
Returns the value of attribute every.
21 22 23 |
# File 'lib/progress_printer.rb', line 21 def every @every end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/progress_printer.rb', line 21 def name @name end |
#out ⇒ Object (readonly)
Returns the value of attribute out.
21 22 23 |
# File 'lib/progress_printer.rb', line 21 def out @out end |
#start_time ⇒ Object
Returns the value of attribute start_time.
22 23 24 |
# File 'lib/progress_printer.rb', line 22 def start_time @start_time end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
21 22 23 |
# File 'lib/progress_printer.rb', line 21 def total @total end |
Instance Method Details
#current ⇒ Object
82 83 84 |
# File 'lib/progress_printer.rb', line 82 def current enum.peek - 1 end |
#estimated_time_remaining(now = Time.now) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/progress_printer.rb', line 66 def estimated_time_remaining(now = Time.now) return unless total return "calculating..." if current == 0 "~" + self.class.format_duration(seconds_remaining(now)) end |
#finish ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/progress_printer.rb', line 42 def finish if total print_progress total else print_progress current unless at_milestone? end end |
#increment(count = 1) ⇒ Object
36 37 38 39 40 |
# File 'lib/progress_printer.rb', line 36 def increment(count = 1) n = nil count.times { n = enum.next } print_progress n if at_milestone? end |
#percent_complete ⇒ Object
50 51 52 53 54 |
# File 'lib/progress_printer.rb', line 50 def percent_complete return unless total return 1.0 if current >= total current / total.to_f end |
#percent_complete_string ⇒ Object
56 57 58 59 |
# File 'lib/progress_printer.rb', line 56 def percent_complete_string return unless total "#{(percent_complete * 100).to_i}%" end |
#percent_remaining ⇒ Object
61 62 63 64 |
# File 'lib/progress_printer.rb', line 61 def percent_remaining return unless total 1.0 - percent_complete end |
#seconds_remaining(now) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/progress_printer.rb', line 73 def seconds_remaining(now) return unless total return if percent_remaining == 1.0 return 0.0 if percent_remaining == 0.0 time_passed = now - start_time time_passed / percent_complete * (1.0 - percent_complete) end |
#start ⇒ Object
31 32 33 34 |
# File 'lib/progress_printer.rb', line 31 def start self.start_time = Time.now print_progress 0 end |