class ANSI::ProgressBar

Progressbar

Progressbar is a text-based progressbar library.

pbar = Progressbar.new( "Demo", 100 )
100.times { pbar.inc }
pbar.finish

Attributes

format[RW]
format_arguments[RW]
styles[RW]

Public Class Methods

new(title, total, out=STDERR) { |self| ... } click to toggle source
# File lib/ansi/progressbar.rb, line 22
def initialize(title, total, out=STDERR)
  @title = title
  @total = total
  @out   = out

  @bar_length = 80
  @bar_mark = "|"
  @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]
  @styles = {}
  #
  yield self if block_given?
  #
  show_progress
end

Public Instance Methods

bar_mark=(mark) click to toggle source
# File lib/ansi/progressbar.rb, line 54
def bar_mark=(mark)
  @bar_mark = String(mark)[0..0]
end
Also aliased as: barmark=, mark=
barmark=(mark)
Alias for: bar_mark=
clear() click to toggle source
# File lib/ansi/progressbar.rb, line 138
def clear
  @out.print(" " * get_width + eol)
end
file_transfer_mode()

For backward compatability

Alias for: transfer_mode
finish() click to toggle source
# File lib/ansi/progressbar.rb, line 93
def finish
  @current = @total
  @is_finished = true
  show_progress
end
flush() click to toggle source
# File lib/ansi/progressbar.rb, line 99
def flush
  @out.flush
end
halt() click to toggle source
# File lib/ansi/progressbar.rb, line 103
def halt
  @is_finished = true
  show_progress
end
inc(step = 1) click to toggle source
# File lib/ansi/progressbar.rb, line 130
def inc(step = 1)
  @current += step
  @current = @total if @current > @total
  show_progress
  @previous = @current
end
inspect() click to toggle source
# File lib/ansi/progressbar.rb, line 142
def inspect
  "(ProgressBar: #{@current}/#{@total})"
end
mark=(mark)
Alias for: bar_mark=
reset() click to toggle source
# File lib/ansi/progressbar.rb, line 124
def reset
  @current = 0
  @is_finished = false
end
set(count) click to toggle source
# File lib/ansi/progressbar.rb, line 108
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
standard_mode() click to toggle source
# File lib/ansi/progressbar.rb, line 79
def standard_mode
  @format = "%-14s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
end
style(options) click to toggle source

Set ANSI styling options.

# File lib/ansi/progressbar.rb, line 74
def style(options)
  @styles = options
end
title=(str) click to toggle source
# File lib/ansi/progressbar.rb, line 50
def title=(str)
  @title = str
end
total_overflow=(boolv) click to toggle source
# File lib/ansi/progressbar.rb, line 60
def total_overflow=(boolv)
  @total_overflow = boolv ? true : false
end
transfer_mode() click to toggle source
# File lib/ansi/progressbar.rb, line 85
def transfer_mode
  @format = "%-14s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
end
Also aliased as: file_transfer_mode

Private Instance Methods

bar() click to toggle source
# File lib/ansi/progressbar.rb, line 210
def bar
  len = percentage * @bar_length / 100
  sprintf("|%s%s|", @bar_mark * len, " " *  (@bar_length - len))
end
bytes() click to toggle source
# File lib/ansi/progressbar.rb, line 166
def bytes
  convert_bytes(@current)
end
colorize(part, style) click to toggle source
# File lib/ansi/progressbar.rb, line 280
def colorize(part, style)
  return part unless style
  #[style].flatten.inject(part){ |pt, st| ANSI::Code.ansi(pt, *st) }
  ANSI::Code.ansi(part, *style)
end
convert_bytes(bytes) click to toggle source
# File lib/ansi/progressbar.rb, line 149
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/ansi/progressbar.rb, line 189
def elapsed
  elapsed = Time.now - @start_time
  sprintf("Time: %s", format_time(elapsed))
end
eol() click to toggle source
# File lib/ansi/progressbar.rb, line 206
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/ansi/progressbar.rb, line 179
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/ansi/progressbar.rb, line 170
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

TODO: Use ANSI::Terminal#terminal_width instead.

# File lib/ansi/progressbar.rb, line 228
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")
      cols = data.unpack("SSSS")[1]
      if cols >= 0 then cols else default_width end
    else
      default_width
    end
  rescue Exception
    default_width
  end
end
percentage() click to toggle source
# File lib/ansi/progressbar.rb, line 215
def percentage
  if @total.zero?
    100
  else
    @current  * 100 / @total
  end
end
show() click to toggle source
# File lib/ansi/progressbar.rb, line 247
def show
  arguments = @format_arguments.map do |method|
    colorize(send(method), styles[method])
  end
  line      = sprintf(@format, *arguments)
  width     = get_width
  length    = ANSI::Code.uncolor{line}.length
  if length == width - 1
    @out.print(line + eol)
  elsif length >= width
    @bar_length = [@bar_length - (length - width + 1), 0].max
    @bar_length == 0 ?  @out.print(line + eol) : show
  else #line.length < width - 1
    @bar_length += width - length + 1
    show
  end
end
show_progress() click to toggle source
# File lib/ansi/progressbar.rb, line 266
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() click to toggle source
# File lib/ansi/progressbar.rb, line 194
def stat
  if @is_finished then elapsed else eta end
end
stat_for_file_transfer() click to toggle source
# File lib/ansi/progressbar.rb, line 198
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() click to toggle source
# File lib/ansi/progressbar.rb, line 223
def title
  @title[0,13] + ":"
end
transfer_rate() click to toggle source
# File lib/ansi/progressbar.rb, line 161
def transfer_rate
  bytes_per_second = @current.to_f / (Time.now - @start_time)
  sprintf("%s/s", convert_bytes(bytes_per_second))
end