Sha256: 8053af6d3eaad52efe0453c5f18bc9a5015e1177188536ed68b39b4b591832cf

Contents?: true

Size: 939 Bytes

Versions: 6

Compression:

Stored size: 939 Bytes

Contents

module Analects
  module CLI
    # Command line progress bar
    class Progress
      attr_accessor :length, :count
      
      def initialize(total, accuracy = 1000, prefix = '')
        @total = total
        @current = 0
        @length = 60
        @count = 100
        @accuracy = accuracy
        @prefix = prefix
      end
      
      def next
        @current += 1
        draw if (@current % (Float(@total)/@accuracy).ceil) == 0 || @current == @total
      end
      
      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
      
      def pos(scale)
        if @current == @total
          scale
        else
          Float(@current)/@total * scale 
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
analects-0.4.2 lib/analects/cli/progress.rb
analects-0.4.1 lib/analects/cli/progress.rb
analects-0.4.0 lib/analects/cli/progress.rb
analects-0.3.1 lib/analects/cli/progress.rb
analects-0.2.1 lib/analects/cli/progress.rb
analects-0.2.0 lib/analects/cli/progress.rb