Class: PBAndJ::ProgressBar

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

Instance Method Summary collapse

Constructor Details

#initialize(desc, count, pad: 0, width: 80, show: true, stream: STDOUT) ⇒ ProgressBar

Returns a new instance of ProgressBar.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pb_and_j/progress_bar.rb', line 13

def initialize(desc, count, pad: 0, width: 80, show: true, stream: STDOUT)
  @description = desc    || ''
  @count       = count   || 0
  @padding     = pad     || 0
  @width       = width   || 80
  @stream      = stream  || STDOUT
  @show        = !!show
  @index       = 0
  @message     = ''

  raise "Count must be greater than 0" if @count < 1
end

Instance Method Details

#messageObject



51
52
53
# File 'lib/pb_and_j/progress_bar.rb', line 51

def message
  @message ||= label + marks + suffix
end

#show?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/pb_and_j/progress_bar.rb', line 47

def show?
  @show
end

#start(now = Time.now) ⇒ Object



26
27
28
29
30
31
# File 'lib/pb_and_j/progress_bar.rb', line 26

def start(now = Time.now)
  @start_at = now
  @index = 0
  reset now
  print
end

#stop(now = Time.now) ⇒ Object



41
42
43
44
45
# File 'lib/pb_and_j/progress_bar.rb', line 41

def stop(now = Time.now)
  reset now
  print
  stream.puts if show?
end

#tick(index = nil, finish = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/pb_and_j/progress_bar.rb', line 33

def tick(index = nil, finish = nil)
  start unless defined?(@start_at) && @start_at
  reset
  @index  = index || @index + 1
  @finish = finish if finish
  print
end