class Analects::CLI::Progress

Command line progress bar

Attributes

count[RW]
length[RW]

Public Class Methods

new(total, accuracy = 1000, prefix = '') click to toggle source
# File lib/analects/cli/progress.rb, line 7
def initialize(total, accuracy = 1000, prefix = '')
  @total = total
  @current = 0
  @length = 60
  @count = 100
  @accuracy = accuracy
  @prefix = prefix
end

Public Instance Methods

draw() click to toggle source
# File lib/analects/cli/progress.rb, line 21
def draw
  return unless 
  x = pos(@length).floor
  total_count = @count == 100 ? '%' : "/#{@count}"
  print "\e[%dD\e[32m%s[\e[31m%s%s\e[32m]\e[34m %d%s\e[0m" % [@length+10+@prefix.length, @prefix, '='*x, ' '*(@length-x), pos(@count), total_count] 
end
next() click to toggle source
# File lib/analects/cli/progress.rb, line 16
def next
  @current += 1
  draw if (@current % (Float(@total)/@accuracy).ceil) == 0 || @current == @total
end
pos(scale) click to toggle source
# File lib/analects/cli/progress.rb, line 28
def pos(scale)
  if @current == @total
    scale
  else
    Float(@current)/@total * scale 
  end
end