class ProgressBar

Public Class Methods

new(args = {}) click to toggle source
# File lib/buildr/core/progressbar.rb, line 31
def initialize(args = {})
  @title = args[:title] || ''
  @total = args[:total] || 0
  @mark = args[:mark] || '.'
  @format = args[:format] || default_format
  @output = args[:output] || $stderr unless args[:hidden] || !$stdout.isatty
  clear
end
start(args, &block) click to toggle source
# File lib/buildr/core/progressbar.rb, line 21
def start(args, &block)
  new(args).start &block
end
width() click to toggle source
# File lib/buildr/core/progressbar.rb, line 25
def width
  @width ||= Buildr::Console.output_cols || 0
end

Public Instance Methods

<<(bytes) click to toggle source
# File lib/buildr/core/progressbar.rb, line 58
def <<(bytes)
  inc bytes.size
end
count() click to toggle source
# File lib/buildr/core/progressbar.rb, line 73
def count
  human(@count)
end
duration(seconds) click to toggle source
# File lib/buildr/core/progressbar.rb, line 116
def duration(seconds)
  '%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60]
end
elapsed() click to toggle source
# File lib/buildr/core/progressbar.rb, line 96
def elapsed
  'Time: %s' % duration(Time.now - @start)
end
eta() click to toggle source
# File lib/buildr/core/progressbar.rb, line 89
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
# File lib/buildr/core/progressbar.rb, line 120
def finish
  unless @finished
    @finished = true
    render
  end
end
human(bytes) click to toggle source
# File lib/buildr/core/progressbar.rb, line 110
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
# File lib/buildr/core/progressbar.rb, line 54
def inc(count)
  set @count + count
end
percentage() click to toggle source
# File lib/buildr/core/progressbar.rb, line 81
def percentage
  '%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total))
end
progress(width) click to toggle source
# File lib/buildr/core/progressbar.rb, line 104
def progress(width)
  width -= 2
  marks = @total == 0 ? width : (@count * width / @total)
  "|%-#{width}s|" % (@mark * marks)
end
rate() click to toggle source
# File lib/buildr/core/progressbar.rb, line 100
def rate
  '%s/s' % human(@count / (Time.now - @start))
end
set(count) click to toggle source
# File lib/buildr/core/progressbar.rb, line 62
def set(count)
  @count = [count, 0].max
  @count = [count, @total].min unless @total == 0
  render if changed?
end
start() { |self| ... } click to toggle source
# File lib/buildr/core/progressbar.rb, line 40
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
# File lib/buildr/core/progressbar.rb, line 85
def time
  @finished ? elapsed : eta
end
title() click to toggle source
# File lib/buildr/core/progressbar.rb, line 68
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
# File lib/buildr/core/progressbar.rb, line 77
def total
  human(@total)
end

Protected Instance Methods

changed?() click to toggle source
# File lib/buildr/core/progressbar.rb, line 150
def changed?
  return false unless @output && Time.now - @last_time > 0.1
  return human(@count) != human(@previous) if @total == 0
  return true if (@count - @previous) >= @total / 100
  return Time.now - @last_time > 1
end
clear() click to toggle source
# File lib/buildr/core/progressbar.rb, line 129
def clear
  return if @output == nil || ProgressBar.width <= 0
  @output.print "\r", " " * (ProgressBar.width - 1), "\r"
  @output.flush
end
default_format() click to toggle source
# File lib/buildr/core/progressbar.rb, line 157
def default_format
  @total == 0 ? ['%s %8s %s', :title, :count, :elapsed] : ['%s: %s |--| %8s/%s %s', :title, :percentage, :count, :total, :time]
end
render() click to toggle source
# File lib/buildr/core/progressbar.rb, line 135
def render
  return unless @output
  format, *args = @format
  line = format % args.map { |arg| send(arg) }
  if ProgressBar.width >= line.size
    @output.print line.sub('|--|') { progress(ProgressBar.width - line.size + 3) }
  else
    @output.print line.sub('|--|', '')
  end
  @output.print @finished ? "\n" : "\r"
  @output.flush
  @previous = @count
  @last_time = Time.now
end