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 do
Progress.step do
# do something
end
end
Progress.stop
Block example
Progress.start('Test', 1000) do
1000.times do
Progress.step do
# do something
end
end
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: 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.
-
.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.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/progress.rb', line 40 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
151 152 153 |
# File 'lib/progress.rb', line 151 def highlight=(value) @highlight = value end |
.lines=(value) ⇒ Object (writeonly)
output progress as lines (not trying to stay on line)
Progress.lines = true
147 148 149 |
# File 'lib/progress.rb', line 147 def lines=(value) @lines = value end |
Instance Attribute Details
#current ⇒ Object
Returns the value of attribute current.
38 39 40 |
# File 'lib/progress.rb', line 38 def current @current end |
#current_step ⇒ Object (readonly)
Returns the value of attribute current_step.
39 40 41 |
# File 'lib/progress.rb', line 39 def current_step @current_step end |
#note ⇒ Object
Returns the value of attribute note.
38 39 40 |
# File 'lib/progress.rb', line 38 def note @note end |
#title ⇒ Object
Returns the value of attribute title.
38 39 40 |
# File 'lib/progress.rb', line 38 def title @title end |
#total ⇒ Object
Returns the value of attribute total.
38 39 40 |
# File 'lib/progress.rb', line 38 def total @total end |
Class Method Details
.note=(s) ⇒ Object
set note
139 140 141 142 143 |
# File 'lib/progress.rb', line 139 def note=(s) if levels.last levels.last.note = s end end |
.set(value, &block) ⇒ Object
set current progress to ‘value`
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/progress.rb', line 107 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/progress.rb', line 74 def start(title = nil, total = nil) if levels.empty? @started_at = Time.now @eta = nil @semaphore = Mutex.new @beeper = Thread.new do loop do sleep 1 end end 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`
98 99 100 101 102 103 104 |
# File 'lib/progress.rb', line 98 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
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/progress.rb', line 124 def stop if levels.last if levels.last.step_if_blank || levels.length == 1 true set_title nil end levels.pop if levels.empty? @beeper.kill io.puts end end end |
Instance Method Details
#step(steps) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/progress.rb', line 65 def step(steps) @current_step = steps yield ensure @current_step = nil end |
#step_if_blank ⇒ Object
51 52 53 54 55 |
# File 'lib/progress.rb', line 51 def step_if_blank if current == 0.0 && total == 1.0 self.current = 1.0 end end |
#to_f(inner) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/progress.rb', line 57 def to_f(inner) inner = [inner, 1.0].min if current_step inner *= current_step end (current + inner) / total end |