<<(bytes)
click to toggle source
def <<(bytes)
inc bytes.size
end
count()
click to toggle source
def count
human(@count)
end
duration(seconds)
click to toggle source
def duration(seconds)
'%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60]
end
elapsed()
click to toggle source
def elapsed
'Time: %s' % duration(Time.now - @start)
end
eta()
click to toggle source
def eta
return 'ETA: --:--:--' if @count == 0
elapsed = Time.now - @start
eta = elapsed * @total / @count - elapsed
'ETA: %s' % duration(eta.ceil)
end
finish()
click to toggle source
def finish
unless @finished
@finished = true
render
end
end
human(bytes)
click to toggle source
def human(bytes)
magnitude = (0..3).find { |i| bytes < (1024 << i * 10) } || 3
return '%dB' % bytes if magnitude == 0
return '%.1f%s' % [ bytes.to_f / (1 << magnitude * 10), [nil, 'KB', 'MB', 'GB'][magnitude] ]
end
inc(count)
click to toggle source
def inc(count)
set @count + count
end
percentage()
click to toggle source
def percentage
'%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total))
end
progress(width)
click to toggle source
def progress(width)
width -= 2
marks = @total == 0 ? width : (@count * width / @total)
"|%-#{width}s|" % (@mark * marks)
end
rate()
click to toggle source
def rate
'%s/s' % human(@count / (Time.now - @start))
end
set(count)
click to toggle source
def set(count)
@count = [count, 0].max
@count = [count, @total].min unless @total == 0
render if changed?
end
start() { |self| ... }
click to toggle source
def start
@start = @last_time = Time.now
@count = 0
@finished = false
render
if block_given?
result = yield(self) if block_given?
finish
result
else
self
end
end
time()
click to toggle source
def time
@finished ? elapsed : eta
end
title()
click to toggle source
def title
return @title if ProgressBar.width <= 10
@title.size > ProgressBar.width / 5 ? (@title[0, ProgressBar.width / 5 - 2] + '..') : @title
end
total()
click to toggle source
def total
human(@total)
end