Class: Traquitana::Bar

Inherits:
Object
  • Object
show all
Defined in:
lib/bar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBar

Returns a new instance of Bar.



6
7
8
# File 'lib/bar.rb', line 6

def initialize
  reset
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



3
4
5
# File 'lib/bar.rb', line 3

def current
  @current
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/bar.rb', line 4

def name
  @name
end

#totalObject

Returns the value of attribute total.



4
5
6
# File 'lib/bar.rb', line 4

def total
  @total
end

Instance Method Details

#indicator(current) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/bar.rb', line 18

def indicator(current)
  bar = Array.new(@bar_size, "_")
  return bar.join if current <= 0

  prop = current > 0 ? ((100 / (total / current.to_f)) / @bar_step).to_i : 0
  return bar.join if prop <= 0

  bar[0...prop] = Array.new(prop, "#")
  bar.join
end

#resetObject



10
11
12
13
14
15
16
# File 'lib/bar.rb', line 10

def reset
  @name     = nil
  @total    = 0
  @current  = 0
  @bar_size = 20
  @bar_step = 5
end

#update(current) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/bar.rb', line 29

def update(current)
  @current = current
  file     = File.basename(@name).ljust(25)
  STDOUT.print "#{file} : #{self.indicator(current)}\r"

  if @current >= @total
    STDOUT.puts "\n"
    @current = -1
  end
end