Class: Legion::CLI::Chat::ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/cli/chat/progress_bar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total:, label: '', width: 40, output: $stdout) ⇒ ProgressBar

Returns a new instance of ProgressBar.



9
10
11
12
13
14
15
16
# File 'lib/legion/cli/chat/progress_bar.rb', line 9

def initialize(total:, label: '', width: 40, output: $stdout)
  @total = [total, 1].max
  @current = 0
  @label = label
  @width = width
  @output = output
  @start_time = Time.now
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



7
8
9
# File 'lib/legion/cli/chat/progress_bar.rb', line 7

def current
  @current
end

#totalObject (readonly)

Returns the value of attribute total.



7
8
9
# File 'lib/legion/cli/chat/progress_bar.rb', line 7

def total
  @total
end

Instance Method Details

#advance(amount = 1) ⇒ Object



18
19
20
21
22
# File 'lib/legion/cli/chat/progress_bar.rb', line 18

def advance(amount = 1)
  @current = [@current + amount, @total].min
  render
  self
end

#elapsedObject



35
36
37
# File 'lib/legion/cli/chat/progress_bar.rb', line 35

def elapsed
  Time.now - @start_time
end

#etaObject



39
40
41
42
43
# File 'lib/legion/cli/chat/progress_bar.rb', line 39

def eta
  return 0 if @current.zero? || @current >= @total

  (elapsed / @current * (@total - @current)).round
end

#finishObject



24
25
26
27
28
29
# File 'lib/legion/cli/chat/progress_bar.rb', line 24

def finish
  @current = @total
  render
  @output.puts
  self
end

#percentageObject



31
32
33
# File 'lib/legion/cli/chat/progress_bar.rb', line 31

def percentage
  (@current.to_f / @total * 100).round(1)
end