Class: Analects::CLI::Progress
- Inherits:
-
Object
- Object
- Analects::CLI::Progress
- Defined in:
- lib/analects/cli/progress.rb
Overview
Command line progress bar
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#length ⇒ Object
Returns the value of attribute length.
Instance Method Summary collapse
- #draw ⇒ Object
-
#initialize(total, accuracy = 1000, prefix = '') ⇒ Progress
constructor
A new instance of Progress.
- #next ⇒ Object
- #pos(scale) ⇒ Object
Constructor Details
#initialize(total, accuracy = 1000, prefix = '') ⇒ Progress
Returns a new instance of Progress.
7 8 9 10 11 12 13 14 |
# 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 |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
5 6 7 |
# File 'lib/analects/cli/progress.rb', line 5 def count @count end |
#length ⇒ Object
Returns the value of attribute length.
5 6 7 |
# File 'lib/analects/cli/progress.rb', line 5 def length @length end |
Instance Method Details
#draw ⇒ Object
21 22 23 24 25 26 |
# 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 ⇒ Object
16 17 18 19 |
# 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) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/analects/cli/progress.rb', line 28 def pos(scale) if @current == @total scale else Float(@current)/@total * scale end end |