Class: OsCtld::ProgressTracker

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

Instance Method Summary collapse

Constructor Details

#initialize(progress: 0, total: 0) ⇒ ProgressTracker

Returns a new instance of ProgressTracker.



3
4
5
6
7
# File 'lib/osctld/progress_tracker.rb', line 3

def initialize(progress: 0, total: 0)
  @progress = progress
  @total = total
  @mutex = Mutex.new
end

Instance Method Details

#add_total(total) ⇒ Object



9
10
11
# File 'lib/osctld/progress_tracker.rb', line 9

def add_total(total)
  @mutex.synchronize { @total += total }
end

#progress_line(message, increment_by: 1) ⇒ Object

Parameters:

  • message (String)
  • increment_by (Integer, nil) (defaults to: 1)


15
16
17
18
19
20
21
# File 'lib/osctld/progress_tracker.rb', line 15

def progress_line(message, increment_by: 1)
  @mutex.synchronize do
    @progress += increment_by if increment_by

    "[#{@progress}/#{@total}] #{message}"
  end
end