class Archive::Tar::Minitar::Command::ProgressBar

Constants

VERSION

Attributes

title[RW]
total[RW]

Public Class Methods

new(title, total, out = STDERR) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 35
def initialize (title, total, out = STDERR)
  @title = title
  @total = total
  @out = out
  @bar_width = 80
  @bar_mark = "o"
  @current = 0
  @previous = 0
  @is_finished = false
  @start_time = Time.now
  @previous_time = @start_time
  @title_width = 14
  @format = "%-#{@title_width}s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
  show
end

Public Instance Methods

file_transfer_mode() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 182
  def file_transfer_mode
  @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]  
end
finish() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 194
def finish
  @current = @total
  @is_finished = true
  show_progress
end
format=(format) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 186
def format= (format)
  @format = format
end
format_arguments=(arguments) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 190
def format_arguments= (arguments)
  @format_arguments = arguments
end
halt() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 200
def halt
  @is_finished = true
  show_progress
end
inc(step = 1) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 214
def inc (step = 1)
  @current += step
  @current = @total if @current > @total
  show_progress
  @previous = @current
end
inspect() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 221
def inspect
  "(ProgressBar: #{@current}/#{@total})"
end
set(count) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 205
def set (count)
  if count < 0 || count > @total
    raise "invalid count: #{count} (total: #{@total})"
  end
  @current = count
  show_progress
  @previous = @current
end

Private Instance Methods

bar() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 114
def bar
  len = percentage * @bar_width / 100
  sprintf("|%s%s|", @bar_mark * len, " " *  (@bar_width - len))
end
bytes() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 70
def bytes
  convert_bytes(@current)
end
convert_bytes(bytes) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 53
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() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 93
def elapsed
  elapsed = Time.now - @start_time
  sprintf("Time: %s", format_time(elapsed))
end
eol() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 110
def eol
  if @is_finished then "\n" else "\r" end
end
eta() click to toggle source

ETA stands for Estimated Time of Arrival.

# File lib/archive/tar/minitar/command.rb, line 83
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) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 74
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/archive/tar/minitar/command.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
percentage(value = nil) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 119
def percentage(value = nil)
  if @total.zero?
    100
  else
    (value || @current) * 100 / @total
  end
end
show() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 148
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_width = [@bar_width - (line.length - width + 1), 0].max
    if @bar_width == 0 then @out.print(line + eol) else show end
  else # line.length < width - 1
    @bar_width += width - line.length + 1
    show
  end
  @previous_time = Time.now
end
show_progress() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 165
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 || 
    Time.now - @previous_time >= 1 ||
    @is_finished
    show
  end
end
stat() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 98
def stat
  if @is_finished then elapsed else eta end
end
stat_for_file_transfer() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 102
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
transfer_rate() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 65
def transfer_rate
  bytes_per_second = @current.to_f / (Time.now - @start_time)
  sprintf("%s/s", convert_bytes(bytes_per_second))
end