Class: TaskProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/task_progress_bar.rb,
lib/task_progress_bar/version.rb

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(total) ⇒ TaskProgressBar

Returns a new instance of TaskProgressBar.



4
5
6
7
8
9
# File 'lib/task_progress_bar.rb', line 4

def initialize(total)
  @total      = total || 1
  @total_bar  = ( total < 50 ? total : 50 )
  @start_time = Time.now
  @i          = 0
end

Instance Method Details

#barObject



16
17
18
# File 'lib/task_progress_bar.rb', line 16

def bar
  "\r[%d/%d][%s][%d%%][%s][%s]" % [@i, @total, green_elapsed_bar, elapsed_percent, elapsed_time_formated, time_left]
end

#bar_quantityObject



32
33
34
# File 'lib/task_progress_bar.rb', line 32

def bar_quantity
  ((@i*1.00/total_progress).to_i)
end

#count(value = 1) ⇒ Object



20
21
22
# File 'lib/task_progress_bar.rb', line 20

def count(value = 1)
  @i += value
end

#elapsed_barObject



40
41
42
# File 'lib/task_progress_bar.rb', line 40

def elapsed_bar
  "%-#{@total_bar}s" % [symbol * bar_quantity]
end

#elapsed_percentObject



44
45
46
# File 'lib/task_progress_bar.rb', line 44

def elapsed_percent
  ((@i*1.00/@total)*100).to_i
end

#elapsed_timeObject



48
49
50
# File 'lib/task_progress_bar.rb', line 48

def elapsed_time
  Time.now - @start_time
end

#elapsed_time_formatedObject



52
53
54
# File 'lib/task_progress_bar.rb', line 52

def elapsed_time_formated
  time_formated( elapsed_time )
end

#green_elapsed_barObject



36
37
38
# File 'lib/task_progress_bar.rb', line 36

def green_elapsed_bar
  "\033[32m#{ elapsed_bar }\033[0m"
end

#stepObject



11
12
13
14
# File 'lib/task_progress_bar.rb', line 11

def step
  self.count
  STDOUT.print self.bar
end

#symbolObject



24
25
26
# File 'lib/task_progress_bar.rb', line 24

def symbol
  '#'
end

#time_formated(value) ⇒ Object



65
66
67
# File 'lib/task_progress_bar.rb', line 65

def time_formated( value )
  Time.at( value ).utc.strftime("%H:%M:%S")
end

#time_leftObject



61
62
63
# File 'lib/task_progress_bar.rb', line 61

def time_left
  time_formated( time_left_in_seconds )
end

#time_left_in_secondsObject



56
57
58
59
# File 'lib/task_progress_bar.rb', line 56

def time_left_in_seconds
  return 0 if @i == 0
  ( (elapsed_time / @i) * (@total.to_f - @i) ).to_i
end

#total_progressObject



28
29
30
# File 'lib/task_progress_bar.rb', line 28

def total_progress
  @total / @total_bar
end