Class: Progress
- Inherits:
-
Object
- Object
- Progress
- Includes:
- Singleton
- Defined in:
- lib/progress.rb,
lib/progress/with_progress.rb
Overview
Procedural example
Progress.start('Test', 1000)
1000.times{ Progress.step }
Progress.stop
Block example
Progress.start('Test', 1000) do
1000.times{ Progress.step }
end
Step must not always be one
symbols = []
Progress.start('Input 100 symbols', 100) do
while symbols.length < 100
input = gets.scan(/\S/)
symbols += input
Progress.step input.length
end
end
Enclosed block example
[1, 2, 3].each_with_progress('1 2 3') do |one_of_1_2_3|
10.times_with_progress('10') do |one_of_10|
sleep(0.001)
end
end
Defined Under Namespace
Classes: Enhancer, WithProgress
Class Attribute Summary collapse
-
.highlight ⇒ Object
writeonly
force highlight Progress.highlight = true.
-
.lines ⇒ Object
writeonly
output progress as lines (not trying to stay on line) Progress.lines = true.
Instance Attribute Summary collapse
-
#current ⇒ Object
Returns the value of attribute current.
-
#current_step ⇒ Object
readonly
Returns the value of attribute current_step.
-
#note ⇒ Object
Returns the value of attribute note.
-
#title ⇒ Object
Returns the value of attribute title.
-
#total ⇒ Object
Returns the value of attribute total.
Class Method Summary collapse
-
.note=(s) ⇒ Object
set note (will be shown after progress message).
-
.set(value, &block) ⇒ Object
set current progress to ‘value`.
-
.start(title = nil, total = nil) ⇒ Object
start progress indication.
-
.step(num = 1, den = 1, &block) ⇒ Object
step current progress by ‘num / den`.
-
.stop ⇒ Object
stop progress.
Instance Method Summary collapse
-
#initialize(title, total) ⇒ Progress
constructor
A new instance of Progress.
- #step(steps) ⇒ Object
- #step_if_blank ⇒ Object
- #to_f(inner) ⇒ Object
Constructor Details
#initialize(title, total) ⇒ Progress
Returns a new instance of Progress.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/progress.rb', line 31 def initialize(title, total) if title.is_a?(Numeric) && total.nil? title, total = nil, title elsif total.nil? total = 1 end @title = title @current = 0.0 @total = total == 0.0 ? 1.0 : Float(total) end |
Class Attribute Details
.highlight=(value) ⇒ Object (writeonly)
force highlight
Progress.highlight = true
134 135 136 |
# File 'lib/progress.rb', line 134 def highlight=(value) @highlight = value end |
.lines=(value) ⇒ Object (writeonly)
output progress as lines (not trying to stay on line)
Progress.lines = true
130 131 132 |
# File 'lib/progress.rb', line 130 def lines=(value) @lines = value end |
Instance Attribute Details
#current ⇒ Object
Returns the value of attribute current.
29 30 31 |
# File 'lib/progress.rb', line 29 def current @current end |
#current_step ⇒ Object (readonly)
Returns the value of attribute current_step.
30 31 32 |
# File 'lib/progress.rb', line 30 def current_step @current_step end |
#note ⇒ Object
Returns the value of attribute note.
29 30 31 |
# File 'lib/progress.rb', line 29 def note @note end |
#title ⇒ Object
Returns the value of attribute title.
29 30 31 |
# File 'lib/progress.rb', line 29 def title @title end |
#total ⇒ Object
Returns the value of attribute total.
29 30 31 |
# File 'lib/progress.rb', line 29 def total @total end |
Class Method Details
.note=(s) ⇒ Object
set note (will be shown after progress message)
122 123 124 125 126 |
# File 'lib/progress.rb', line 122 def note=(s) if levels.last levels.last.note = s end end |
.set(value, &block) ⇒ Object
set current progress to ‘value`
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/progress.rb', line 91 def set(value, &block) if levels.last ret = if block levels.last.step(value - levels.last.current, &block) end if levels.last levels.last.current = Float(value) end self.note = nil ret elsif block block.call end end |
.start(title = nil, total = nil) ⇒ Object
start progress indication
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/progress.rb', line 65 def start(title = nil, total = nil) if levels.empty? @started_at = Time.now @eta = nil end levels << new(title, total) true if block_given? begin yield ensure stop end end end |
.step(num = 1, den = 1, &block) ⇒ Object
step current progress by ‘num / den`
82 83 84 85 86 87 88 |
# File 'lib/progress.rb', line 82 def step(num = 1, den = 1, &block) if levels.last set(levels.last.current + Float(num) / den, &block) elsif block block.call end end |
.stop ⇒ Object
stop progress
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/progress.rb', line 108 def stop if levels.last if levels.last.step_if_blank || levels.length == 1 true set_title nil end levels.pop if levels.empty? io.puts end end end |
Instance Method Details
#step(steps) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/progress.rb', line 56 def step(steps) @current_step = steps yield ensure @current_step = nil end |
#step_if_blank ⇒ Object
42 43 44 45 46 |
# File 'lib/progress.rb', line 42 def step_if_blank if current == 0.0 && total == 1.0 self.current = 1.0 end end |
#to_f(inner) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/progress.rb', line 48 def to_f(inner) inner = [inner, 1.0].min if current_step inner *= current_step end (current + inner) / total end |