Class: Analects::CLI::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/analects/cli/progress.rb

Overview

Command line progress bar

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#countObject

Returns the value of attribute count.



5
6
7
# File 'lib/analects/cli/progress.rb', line 5

def count
  @count
end

#lengthObject

Returns the value of attribute length.



5
6
7
# File 'lib/analects/cli/progress.rb', line 5

def length
  @length
end

Instance Method Details

#drawObject



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

#nextObject



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