ProgressBar

ProgressBar is a text-based progressbar library.

  pbar = ProgressBar.new( "Demo", 100 )
  100.times { pbar.inc }
  pbar.finish
Methods
Public Class methods
new(title, total, out = STDERR)
# File lib/facets/progressbar.rb, line 38
  def initialize(title, total, out = STDERR)
    @title = title
    @total = total
    @out = out
    @bar_length = 80
    @bar_mark = "o"
    @total_overflow = true
    @current = 0
    @previous = 0
    @is_finished = false
    @start_time = Time.now
    @format = "%-14s %3d%% %s %s"
    @format_arguments = [:title, :percentage, :bar, :stat]
    show_progress
  end
Public Instance methods
bar_mark=(mark)
# File lib/facets/progressbar.rb, line 189
  def bar_mark=(mark)
    @bar_mark = String(mark)[0..0]
  end
file_transfer_mode()
# File lib/facets/progressbar.rb, line 181
  def file_transfer_mode
    @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
  end
finish()
# File lib/facets/progressbar.rb, line 205
  def finish
    @current = @total
    @is_finished = true
    show_progress
  end
flush()
# File lib/facets/progressbar.rb, line 211
  def flush
    @out.flush
  end
format=(format)
# File lib/facets/progressbar.rb, line 197
  def format=(format)
    @format = format
  end
format_arguments=(arguments)
# File lib/facets/progressbar.rb, line 201
  def format_arguments=(arguments)
    @format_arguments = arguments
  end
halt()
# File lib/facets/progressbar.rb, line 215
  def halt
    @is_finished = true
    show_progress
  end
inc(step = 1)
# File lib/facets/progressbar.rb, line 235
  def inc(step = 1)
    @current += step
    @current = @total if @current > @total
    show_progress
    @previous = @current
  end
inspect()
# File lib/facets/progressbar.rb, line 242
  def inspect
    "(ProgressBar: #{@current}/#{@total})"
  end
set(count)
# File lib/facets/progressbar.rb, line 220
  def set(count)
    if count < 0
      raise "invalid count less than zero: #{count}"
    elsif count > @total
      if @total_overflow
        @total = count + 1
      else
        raise "invalid count greater than total: #{count}"
      end
    end
    @current = count
    show_progress
    @previous = @current
  end
title=(str)
# File lib/facets/progressbar.rb, line 185
  def title=(str)
    @title = str
  end
total_overflow=(boolv)
# File lib/facets/progressbar.rb, line 193
  def total_overflow=(boolv)
    @total_overflow = boolv ? true : false
  end
Private Instance methods
bar()
# File lib/facets/progressbar.rb, line 116
  def bar
    len = percentage * @bar_length / 100
    sprintf("|%s%s|", @bar_mark * len, " " *  (@bar_length - len))
  end
bytes()
# File lib/facets/progressbar.rb, line 72
  def bytes
    convert_bytes(@current)
  end
convert_bytes(bytes)
# File lib/facets/progressbar.rb, line 55
  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
elapsed()
# File lib/facets/progressbar.rb, line 95
  def elapsed
    elapsed = Time.now - @start_time
    sprintf("Time: %s", format_time(elapsed))
  end
eol()
# File lib/facets/progressbar.rb, line 112
  def eol
    if @is_finished then "\n" else "\r" end
  end
eta()

ETA stands for Estimated Time of Arrival.

# File lib/facets/progressbar.rb, line 85
  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
format_time(t)
# File lib/facets/progressbar.rb, line 76
  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()
# File lib/facets/progressbar.rb, line 133
  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
percentage()
# File lib/facets/progressbar.rb, line 121
  def percentage
    if @total.zero?
      100
    else
      @current  * 100 / @total
    end
  end
show()
# File lib/facets/progressbar.rb, line 150
  def show
    arguments = @format_arguments.map{|method| send(method)}
    line = sprintf(@format, *arguments)

    width = get_width
    if line.length == width - 1
      @out.print(line + eol)
    elsif line.length >= width
      @bar_length = [@bar_length - (line.length - width + 1), 0].max
      if @bar_length == 0 then @out.print(line + eol) else show end
    else #line.length < width - 1
      @bar_length += width - line.length + 1
      show
    end
  end
show_progress()
# File lib/facets/progressbar.rb, line 166
  def show_progress
    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

    if cur_percentage > prev_percentage || @is_finished
      show
    end
  end
stat()
# File lib/facets/progressbar.rb, line 100
  def stat
    if @is_finished then elapsed else eta end
  end
stat_for_file_transfer()
# File lib/facets/progressbar.rb, line 104
  def stat_for_file_transfer
    if @is_finished then 
      sprintf("%s %s %s", bytes, transfer_rate, elapsed)
    else 
      sprintf("%s %s %s", bytes, transfer_rate, eta)
    end
  end
title()
# File lib/facets/progressbar.rb, line 129
  def title
    @title[0,13] + ":"
  end
transfer_rate()
# File lib/facets/progressbar.rb, line 67
  def transfer_rate
    bytes_per_second = @current.to_f / (Time.now - @start_time)
    sprintf("%s/s", convert_bytes(bytes_per_second))
  end