-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'allow-configurable-projectors'
- Loading branch information
Showing
16 changed files
with
680 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'ruby-progressbar/projectors/smoothed_average' | ||
|
||
class ProgressBar | ||
class Projector | ||
DEFAULT_PROJECTOR = ProgressBar::Projectors::SmoothedAverage | ||
NAME_TO_PROJECTOR_MAP = { | ||
'smoothed' => ProgressBar::Projectors::SmoothedAverage | ||
}.freeze | ||
|
||
def self.from_type(name) | ||
NAME_TO_PROJECTOR_MAP.fetch(name, DEFAULT_PROJECTOR) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class ProgressBar | ||
module Projectors | ||
class SmoothedAverage | ||
DEFAULT_STRENGTH = 0.1 | ||
DEFAULT_BEGINNING_POSITION = 0 | ||
|
||
attr_accessor :samples, | ||
:strength | ||
attr_reader :projection | ||
|
||
def initialize(options = {}) | ||
self.samples = [] | ||
self.projection = 0.0 | ||
self.strength = options[:strength] || DEFAULT_STRENGTH | ||
|
||
start(:at => DEFAULT_BEGINNING_POSITION) | ||
end | ||
|
||
def start(options = {}) | ||
self.projection = 0.0 | ||
self.progress = samples[0] = (options[:at] || progress) | ||
end | ||
|
||
def decrement | ||
self.progress -= 1 | ||
end | ||
|
||
def increment | ||
self.progress += 1 | ||
end | ||
|
||
def progress | ||
samples[1] | ||
end | ||
|
||
def total=(_new_total); end | ||
|
||
def reset | ||
start(:at => samples[0]) | ||
end | ||
|
||
def progress=(new_progress) | ||
samples[1] = new_progress | ||
self.projection = \ | ||
self.class.calculate( | ||
@projection, | ||
absolute, | ||
strength | ||
) | ||
end | ||
|
||
def none? | ||
projection.zero? | ||
end | ||
|
||
def self.calculate(current_projection, new_value, rate) | ||
(new_value * (1.0 - rate)) + (current_projection * rate) | ||
end | ||
|
||
protected | ||
|
||
attr_writer :projection | ||
|
||
private | ||
|
||
def absolute | ||
samples[1] - samples[0] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
spec/lib/ruby-progressbar/calculators/smoothed_average_spec.rb
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.