Class: ProgressPrinter

Inherits:
Object
  • Object
show all
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% ~30s
Counting: 250/250 100% 2m30s total

Constant Summary collapse

DEFAULT_EVERY =
100
VERSION =
"0.2.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total: nil, name: nil, every: DEFAULT_EVERY, out: $stdout) ⇒ ProgressPrinter

Returns a new instance of ProgressPrinter.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/progress_printer.rb', line 36

def initialize(total: nil, name: nil, every: DEFAULT_EVERY, out: $stdout)
  @total = total
  @name = name
  @every = every

  if self.class.silent
    @out = StringIO.new
  else
    @out = out
  end
end

Class Attribute Details

.silentObject

Returns the value of attribute silent.



22
23
24
# File 'lib/progress_printer.rb', line 22

def silent
  @silent
end

Instance Attribute Details

#everyObject (readonly)

Returns the value of attribute every.



33
34
35
# File 'lib/progress_printer.rb', line 33

def every
  @every
end

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/progress_printer.rb', line 33

def name
  @name
end

#outObject (readonly)

Returns the value of attribute out.



33
34
35
# File 'lib/progress_printer.rb', line 33

def out
  @out
end

#start_timeObject

Returns the value of attribute start_time.



34
35
36
# File 'lib/progress_printer.rb', line 34

def start_time
  @start_time
end

#totalObject (readonly)

Returns the value of attribute total.



33
34
35
# File 'lib/progress_printer.rb', line 33

def total
  @total
end

Class Method Details

.silenceObject



24
25
26
# File 'lib/progress_printer.rb', line 24

def silence
  self.silent = true
end

.wrap(*args, &block) ⇒ Object



28
29
30
# File 'lib/progress_printer.rb', line 28

def wrap(*args, &block)
  new(*args).wrap(&block)
end

Instance Method Details

#currentObject



106
107
108
# File 'lib/progress_printer.rb', line 106

def current
  enum.peek - 1
end

#estimated_time_remaining(now = Time.now) ⇒ Object



86
87
88
89
90
91
# File 'lib/progress_printer.rb', line 86

def estimated_time_remaining(now = Time.now)
  return unless total
  return "calculating..." if current == 0

  "~" + self.class.format_duration(seconds_remaining(now))
end

#finishObject



66
67
68
# File 'lib/progress_printer.rb', line 66

def finish
  print_progress current, final: true
end

#increment(count = 1) ⇒ Object



60
61
62
63
64
# File 'lib/progress_printer.rb', line 60

def increment(count = 1)
  n = nil
  count.times { n = enum.next }
  print_progress n if at_milestone?
end

#percent_completeObject



70
71
72
73
74
# File 'lib/progress_printer.rb', line 70

def percent_complete
  return unless total
  return 1.0 if current >= total
  current / total.to_f
end

#percent_complete_stringObject



76
77
78
79
# File 'lib/progress_printer.rb', line 76

def percent_complete_string
  return unless total
  "#{(percent_complete * 100).to_i}%"
end

#percent_remainingObject



81
82
83
84
# File 'lib/progress_printer.rb', line 81

def percent_remaining
  return unless total
  1.0 - percent_complete
end

#seconds_remaining(now) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/progress_printer.rb', line 93

def seconds_remaining(now)
  return unless total
  return if percent_remaining == 1.0
  return 0.0 if percent_remaining == 0.0

  time_passed(now) / percent_complete * (1.0 - percent_complete)
end

#startObject



55
56
57
58
# File 'lib/progress_printer.rb', line 55

def start
  self.start_time = Time.now
  print_progress 0
end

#time_passed(now = Time.now) ⇒ Object



101
102
103
104
# File 'lib/progress_printer.rb', line 101

def time_passed(now = Time.now)
  return unless start_time
  now - start_time
end

#wrapObject



48
49
50
51
52
53
# File 'lib/progress_printer.rb', line 48

def wrap
  start
  yield(self)
ensure
  finish
end