class SimpleETA
Usage example:
eta = SimpleETA::ETA.new(100) 100.times { |i| sleep 1; eta.next! }
> [59/100] 59.00% ETA 00m 41s¶ ↑
Constants
- DEFAULT_FMT
- KEYWORDS
Public Class Methods
new(total, opts = {})
click to toggle source
# File lib/simple_eta.rb, line 17 def initialize(total, opts = {}) reset!(opts) @total = total.to_i @format = opts[:format] || DEFAULT_FMT KEYWORDS.each do |keyword, val| @format.gsub!(keyword, val) end end
Public Instance Methods
next!()
click to toggle source
# File lib/simple_eta.rb, line 31 def next! @current += 1 show end
reset!(opts = {})
click to toggle source
# File lib/simple_eta.rb, line 26 def reset!(opts = {}) @start_time = opts[:start_time] || Time.now @current = 0 end
show()
click to toggle source
# File lib/simple_eta.rb, line 36 def show current = @current print(@format % [@total, current, percent(current), duration, eta(current)]) print("\n") if current == @total end
Private Instance Methods
duration()
click to toggle source
# File lib/simple_eta.rb, line 48 def duration dur = (Time.now - @start_time).to_i "%02dm %02ds" % [(dur/60), (dur%60)] end
eta(current)
click to toggle source
# File lib/simple_eta.rb, line 53 def eta(current) if current > 0 && current <= @total dur = (Time.now - @start_time).to_i remaining = dur * (@total - current) / current "%02dm %02ds" % [(remaining/60), (remaining%60)] else "--m --s" end end
percent(current)
click to toggle source
# File lib/simple_eta.rb, line 43 def percent(current) return 100.0 if @total.to_i <= 0 current * 100 / @total.to_f end