Class: Progress
- Inherits:
-
Object
- Object
- Progress
- Includes:
- Singleton
- Defined in:
- lib/progress.rb,
lib/progress/eta.rb,
lib/progress/beeper.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: Beeper, Eta, WithProgress
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#note ⇒ Object
Returns the value of attribute note.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
-
.highlight=(value) ⇒ Object
explicitly set highlighting [true/false/nil].
-
.highlight? ⇒ Boolean
highlight output using control characters.
-
.note=(note) ⇒ Object
set note.
-
.running? ⇒ Boolean
check if progress was started.
-
.set(new_current, note = nil, &block) ⇒ Object
set value of current progress.
-
.set_terminal_title=(value) ⇒ Object
explicitly set showing progress in terminal title [true/false/nil].
-
.set_terminal_title? ⇒ Boolean
show progerss in terminal title.
-
.start(total = nil, title = nil) ⇒ Object
start progress indication.
-
.stay_on_line=(value) ⇒ Object
explicitly set staying on one line [true/false/nil].
-
.stay_on_line? ⇒ Boolean
stay on one line.
-
.step(step = nil, note = nil, &block) ⇒ Object
step current progress.
-
.stop ⇒ Object
stop progress.
Instance Method Summary collapse
-
#initialize(total, title) ⇒ Progress
constructor
A new instance of Progress.
- #set(new_current, note) ⇒ Object
- #step(step, note) ⇒ Object
- #to_f(inner) ⇒ Object
Constructor Details
#initialize(total, title) ⇒ Progress
Returns a new instance of Progress.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/progress.rb', line 44 def initialize(total, title) if !total.kind_of?(Numeric) && (title.nil? || title.kind_of?(Numeric)) total, title = title, total end total = total && total != 0 ? Float(total) : 1.0 @total = total @current = 0.0 @title = title end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
41 42 43 |
# File 'lib/progress.rb', line 41 def current @current end |
#note ⇒ Object
Returns the value of attribute note.
43 44 45 |
# File 'lib/progress.rb', line 43 def note @note end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
42 43 44 |
# File 'lib/progress.rb', line 42 def title @title end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
40 41 42 |
# File 'lib/progress.rb', line 40 def total @total end |
Class Method Details
.highlight=(value) ⇒ Object
explicitly set highlighting [true/false/nil]
180 181 182 |
# File 'lib/progress.rb', line 180 def highlight=(value) @highlight = true && value end |
.highlight? ⇒ Boolean
highlight output using control characters
175 176 177 |
# File 'lib/progress.rb', line 175 def highlight? @highlight.nil? ? io_tty? : @highlight end |
.note=(note) ⇒ Object
set note
158 159 160 161 162 |
# File 'lib/progress.rb', line 158 def note=(note) if running? @levels.last.note = note end end |
.running? ⇒ Boolean
check if progress was started
153 154 155 |
# File 'lib/progress.rb', line 153 def running? @levels && !@levels.empty? end |
.set(new_current, note = nil, &block) ⇒ Object
set value of current progress
131 132 133 134 135 136 137 138 139 |
# File 'lib/progress.rb', line 131 def set(new_current, note = nil, &block) if running? ret = @levels.last.set(new_current, note, &block) ret elsif block block.call end end |
.set_terminal_title=(value) ⇒ Object
explicitly set showing progress in terminal title [true/false/nil]
190 191 192 |
# File 'lib/progress.rb', line 190 def set_terminal_title=(value) @set_terminal_title = true && value end |
.set_terminal_title? ⇒ Boolean
show progerss in terminal title
185 186 187 |
# File 'lib/progress.rb', line 185 def set_terminal_title? @set_terminal_title.nil? ? io_tty? : @set_terminal_title end |
.start(total = nil, title = nil) ⇒ Object
start progress indication
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/progress.rb', line 90 def start(total = nil, title = nil) lock do if running? unless @started_in == Thread.current warn 'Can\'t start inner progress in different thread' if block_given? return yield else return end end else @started_in = Thread.current @eta = Eta.new start_beeper end @levels ||= [] @levels.push new(total, title) end :force => true if block_given? begin yield ensure stop end end end |
.stay_on_line=(value) ⇒ Object
explicitly set staying on one line [true/false/nil]
170 171 172 |
# File 'lib/progress.rb', line 170 def stay_on_line=(value) @stay_on_line = true && value end |
.stay_on_line? ⇒ Boolean
stay on one line
165 166 167 |
# File 'lib/progress.rb', line 165 def stay_on_line? @stay_on_line.nil? ? io_tty? : @stay_on_line end |
.step(step = nil, note = nil, &block) ⇒ Object
step current progress
120 121 122 123 124 125 126 127 128 |
# File 'lib/progress.rb', line 120 def step(step = nil, note = nil, &block) if running? ret = @levels.last.step(step, note, &block) ret elsif block block.call end end |
.stop ⇒ Object
stop progress
142 143 144 145 146 147 148 149 150 |
# File 'lib/progress.rb', line 142 def stop if running? if @levels.length == 1 :force => true, :finish => true stop_beeper end @levels.pop end end |
Instance Method Details
#set(new_current, note) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/progress.rb', line 76 def set(new_current, note) @step = new_current - @current @note = note ret = yield if block_given? Thread.exclusive do @current = new_current end ret end |
#step(step, note) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/progress.rb', line 61 def step(step, note) if !step.kind_of?(Numeric) step, note = nil, step end step = 1 if step.nil? @step = step @note = note ret = yield if block_given? Thread.exclusive do @current += step end ret end |
#to_f(inner) ⇒ Object
55 56 57 58 59 |
# File 'lib/progress.rb', line 55 def to_f(inner) inner = 1.0 if inner > 1.0 inner *= @step if @step (current + inner) / total end |