Ruby progress bar

class Progress
  attr_accessor :length, :count
  
  def initialize(total)
    @total = total
    @current = 0
    @length = 30
    @count = 100
  end
  
  def next
    @current += 1
    draw
  end
  
  def draw
    x = pos(@length).floor
    print "\e[60D\e[32m[\e[31m%s%s\e[32m]\e[34m %d/%d\e[0m" % ['='*x, ' '*(@length-x), pos(@count), @count] 
  end
  
  def pos(scale)
    Float(@current)/@total * scale 
  end
end

Trackback URL for this post:

http://www.arnebrasseur.net/trackback/146

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Syndicate content