class ProgressBar
Ruby/ProgressBar - a text progress bar library
Copyright © 2001-2005 Satoru Takabayashi <satoru@namazu.org> Copyright © 2010 Chris Gahan <chris@ill-logic.com>
All rights reserved. This is free software with ABSOLUTELY NO WARRANTY.
You can redistribute it and/or modify it under the terms of Ruby’s license.
Constants
- VERSION
Attributes
current[R]
start_time[RW]
title[R]
total[R]
Public Class Methods
new(title, total=nil, out = STDERR)
click to toggle source
# File lib/epitools/progressbar.rb, line 16 def initialize (title, total=nil, out = STDERR) @title = title @total = total @out = out @terminal_width = 80 @bar_mark = "." @current = 0 @previous = 0 @finished_p = false @start_time = Time.now @previous_time = @start_time @title_width = 18 #@format = "%-#{@title_width}s %3d%% %s %s" if @total #@format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer] @format_arguments = [:title, :percentage, :stat_for_file_transfer] else @format_arguments = [:title, :stat_for_file_transfer] end clear show end
Public Instance Methods
clear()
click to toggle source
# File lib/epitools/progressbar.rb, line 202 def clear @out.print "\r" @out.print(" " * (get_width - 1)) @out.print "\r" end
file_transfer_mode()
click to toggle source
# File lib/epitools/progressbar.rb, line 218 def file_transfer_mode @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer] end
finish()
click to toggle source
# File lib/epitools/progressbar.rb, line 208 def finish @current = @total if @total @finished_p = true show end
finished?()
click to toggle source
# File lib/epitools/progressbar.rb, line 214 def finished? @finished_p end
format=(format)
click to toggle source
# File lib/epitools/progressbar.rb, line 222 def format=(format) @format = format end
format_arguments=(arguments)
click to toggle source
# File lib/epitools/progressbar.rb, line 226 def format_arguments=(arguments) @format_arguments = arguments end
halt()
click to toggle source
# File lib/epitools/progressbar.rb, line 230 def halt @finished_p = true show end
inc(step = 1)
click to toggle source
# File lib/epitools/progressbar.rb, line 235 def inc(step = 1) @current += step @current = @total if @total and @current > @total show_if_needed @previous = @current end
inspect()
click to toggle source
# File lib/epitools/progressbar.rb, line 251 def inspect "#<ProgressBar:#{@current}/#{@total}>" end
set(count)
click to toggle source
# File lib/epitools/progressbar.rb, line 242 def set(count) if @total and (count < 0 || count > @total) raise "invalid count: #{count} (total: #{@total})" end @current = count show_if_needed @previous = @current end
Private Instance Methods
bytes()
click to toggle source
# File lib/epitools/progressbar.rb, line 91 def bytes convert_bytes(@current) end
convert_bytes(bytes)
click to toggle source
# File lib/epitools/progressbar.rb, line 74 def convert_bytes(bytes) if bytes < 1024 sprintf("%6dB", bytes) elsif bytes < 1024 * 1000 # 1000kb sprintf("%5.1fKB", bytes.to_f / 1024) elsif bytes < 1024 * 1024 * 1000 # 1000mb sprintf("%5.1fMB", bytes.to_f / 1024 / 1024) else sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024) end end
do_percentage()
click to toggle source
# File lib/epitools/progressbar.rb, line 123 def do_percentage if @total.zero? 100 else @current * 100 / @total end end
elapsed()
click to toggle source
# File lib/epitools/progressbar.rb, line 114 def elapsed elapsed = Time.now - @start_time sprintf("Time: %s", format_time(elapsed)) end
eol()
click to toggle source
# File lib/epitools/progressbar.rb, line 119 def eol if @finished_p then "\n" else "\r" end end
eta()
click to toggle source
ETA stands for Estimated Time
of Arrival.
# File lib/epitools/progressbar.rb, line 104 def eta if @current == 0 "ETA: --:--:--" else elapsed = Time.now - @start_time eta = elapsed * @total / @current - elapsed; sprintf("ETA: %s", format_time(eta)) end end
fmt_bar()
click to toggle source
# File lib/epitools/progressbar.rb, line 46 def fmt_bar bar_width = do_percentage * @terminal_width / 100 sprintf("|%s%s|", @bar_mark * bar_width, " " * (@terminal_width - bar_width)) end
fmt_percentage()
click to toggle source
# File lib/epitools/progressbar.rb, line 53 def fmt_percentage "%3d%%" % do_percentage end
fmt_stat()
click to toggle source
# File lib/epitools/progressbar.rb, line 57 def fmt_stat if @finished_p then elapsed else eta end end
fmt_stat_for_file_transfer()
click to toggle source
# File lib/epitools/progressbar.rb, line 61 def fmt_stat_for_file_transfer if !@total or @finished_p then sprintf("%s %s %s", bytes, transfer_rate, elapsed) else sprintf("%s %s %s", bytes, transfer_rate, eta) end end
fmt_title()
click to toggle source
# File lib/epitools/progressbar.rb, line 69 def fmt_title title = (@title[0,(@title_width - 1)] + ":") @total ? "%-#{@title_width}s" % title : title end
format_time(t)
click to toggle source
# File lib/epitools/progressbar.rb, line 95 def format_time(t) t = t.to_i sec = t % 60 min = (t / 60) % 60 hour = t / 3600 sprintf("%02d:%02d:%02d", hour, min, sec); end
get_width()
click to toggle source
# File lib/epitools/progressbar.rb, line 131 def get_width # FIXME: I don't know how portable it is. default_width = 80 begin tiocgwinsz = 0x5413 data = [0, 0, 0, 0].pack("SSSS") if @out.ioctl(tiocgwinsz, data) >= 0 then rows, cols, xpixels, ypixels = data.unpack("SSSS") if cols >= 0 then cols else default_width end else default_width end rescue Exception default_width end end
show()
click to toggle source
# File lib/epitools/progressbar.rb, line 148 def show arguments = @format_arguments.map {|method| send("fmt_#{method}") } #line = sprintf(@format, *arguments) line = arguments.join(" ") # width = get_width # if line.length == width - 1 # @out.print(line + eol) # @out.flush # elsif line.length >= width # @terminal_width = [@terminal_width - (line.length - width + 1), 0].max # if @terminal_width == 0 then @out.print(line + eol) else show end # else # line.length < width - 1 # @terminal_width += width - line.length + 1 # show # end @out.print(line + eol) @out.flush @previous_time = Time.now end
show_if_needed()
click to toggle source
# File lib/epitools/progressbar.rb, line 173 def show_if_needed if @total if @total.zero? cur_percentage = 100 prev_percentage = 0 else cur_percentage = (@current * 100 / @total).to_i prev_percentage = (@previous * 100 / @total).to_i end # Use "!=" instead of ">" to support negative changes if cur_percentage != prev_percentage || Time.now - @previous_time >= 1 || @finished_p show end else if Time.now - @previous_time >= 1 || @finished_p show end end end
transfer_rate()
click to toggle source
# File lib/epitools/progressbar.rb, line 86 def transfer_rate bytes_per_second = @current.to_f / (Time.now - @start_time) sprintf("%s/s", convert_bytes(bytes_per_second)) end