Class: Console::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/console/progress.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, subject, total = 0, minimum_output_duration: 1.0) ⇒ Progress



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/console/progress.rb', line 30

def initialize(output, subject, total = 0, minimum_output_duration: 1.0)
  @output = output
  @subject = subject
  
  @start_time = Progress.now
  
  @last_output_time = nil
  @minimum_output_duration = 0.1
  
  @current = 0
  @total = total
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



44
45
46
# File 'lib/console/progress.rb', line 44

def current
  @current
end

#subjectObject (readonly)

Returns the value of attribute subject.



43
44
45
# File 'lib/console/progress.rb', line 43

def subject
  @subject
end

#totalObject (readonly)

Returns the value of attribute total.



45
46
47
# File 'lib/console/progress.rb', line 45

def total
  @total
end

Class Method Details

.nowObject



26
27
28
# File 'lib/console/progress.rb', line 26

def self.now
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Method Details

#average_durationObject



59
60
61
62
63
# File 'lib/console/progress.rb', line 59

def average_duration
  if @current > 0
    duration / @current
  end
end

#durationObject



47
48
49
# File 'lib/console/progress.rb', line 47

def duration
  Progress.now - @start_time
end

#estimated_remaining_timeObject



65
66
67
68
69
# File 'lib/console/progress.rb', line 65

def estimated_remaining_time
  if average_duration = self.average_duration
    average_duration * remaining
  end
end

#increment(amount = 1) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/console/progress.rb', line 71

def increment(amount = 1)
  @current += amount
  
  if output?
    @output.info(@subject, self) {Event::Progress.new(@current, @total)}
    @last_output_time = Progress.now
  end
  
  return self
end

#mark(*arguments) ⇒ Object



91
92
93
# File 'lib/console/progress.rb', line 91

def mark(*arguments)
  @output.info(@subject, *arguments)
end

#progressObject



51
52
53
# File 'lib/console/progress.rb', line 51

def progress
  @current.to_f / @total.to_f
end

#remainingObject



55
56
57
# File 'lib/console/progress.rb', line 55

def remaining
  @total - @current
end

#resize(total) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/console/progress.rb', line 82

def resize(total)
  @total = total
  
  @output.info(@subject, self) {Event::Progress.new(@current, @total)}
  @last_output_time = Progress.now
  
  return self
end

#to_sObject



95
96
97
98
99
100
101
# File 'lib/console/progress.rb', line 95

def to_s
  if estimated_remaining_time = self.estimated_remaining_time
    "#{@current}/#{@total} completed in #{Clock.formatted_duration(self.duration)}, #{Clock.formatted_duration(estimated_remaining_time)} remaining."
  else
    "#{@current}/#{@total} completed, waiting for estimate..."
  end
end