lib/progress.rb in progress-0.1.0.3 vs lib/progress.rb in progress-0.1.1.0
- old
+ new
@@ -6,22 +6,32 @@
class Progress
include Singleton
module InstanceMethods # :nodoc:
attr_accessor :title, :current, :total
+ attr_reader :current_step
def initialize(title, total)
total = Float(total)
@title, @current, @total = title, 0.0, total == 0.0 ? 1.0 : total
end
def step_if_blank
self.current = 1.0 if current == 0.0 && total == 1.0
end
def to_f(inner)
- (current + (inner < 1.0 ? inner : 1.0)) / total
+ inner = [inner, 1.0].min
+ inner *= current_step if current_step
+ (current + inner) / total
end
+
+ def step(steps)
+ @current_step = steps
+ yield
+ ensure
+ @current_step = nil
+ end
end
include InstanceMethods
class << self
# start progress indication
@@ -54,20 +64,29 @@
# Progress.highlight = true
def start(title, total = 1)
levels << new(title, total)
print_message
if block_given?
- result = yield
- stop
- result
+ begin
+ yield
+ ensure
+ stop
+ end
end
end
def step(steps = 1)
if levels.last
+ if block_given?
+ levels.last.step(steps) do
+ yield
+ end
+ end
levels.last.current += Float(steps)
print_message
+ elsif block_given?
+ yield
end
end
def set(value)
if levels.last
@@ -135,5 +154,13 @@
require 'progress/with_progress'
require 'progress/enumerable'
require 'progress/integer'
+
+# like Pathname
+module Kernel
+ def Progress(title, total = 1, &block)
+ Progress.start(title, total, &block)
+ end
+ private :Progress
+end