Class: CLIProgressBar::ItemProgressBar

Inherits:
ProgressBar show all
Defined in:
lib/cli_progress_bar/item_progress_bar.rb

Instance Attribute Summary collapse

Attributes inherited from ProgressBar

#progress

Instance Method Summary collapse

Constructor Details

#initialize(max_items, of: "", log_at: LOG_AT_ALL_PERCENTS, bar_length: BAR_LENGTH, line_char: LINE_CHAR, prefix: "", suffix: "", stream: STD_OUT) ⇒ ItemProgressBar

Returns a new instance of ItemProgressBar.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cli_progress_bar/item_progress_bar.rb', line 9

def initialize(
  max_items,
  of: "", log_at: LOG_AT_ALL_PERCENTS, bar_length: BAR_LENGTH,
  line_char: LINE_CHAR, prefix: "", suffix: "", stream: STD_OUT
)
  raise "max_items must be positive" unless max_items.positive?

  of = "#{max_items} #{of.strip}" unless of.empty?

  super(of:, log_at:, bar_length:, line_char:, prefix:, suffix:, stream:)

  @max_items = max_items
  @current_items = 0
end

Instance Attribute Details

#max_itemsObject (readonly)

Returns the value of attribute max_items.



7
8
9
# File 'lib/cli_progress_bar/item_progress_bar.rb', line 7

def max_items
  @max_items
end

Instance Method Details

#increment(by: 1, prefix: nil, suffix: nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/cli_progress_bar/item_progress_bar.rb', line 24

def increment(by: 1, prefix: nil, suffix: nil)
  raise "Invalid by: value" unless (@current_items + by).between?(1, @max_items)

  @current_items += by
  update_progress(prefix:, suffix:)
end

#update(num_items, prefix: nil, suffix: nil) ⇒ Object



31
32
33
34
35
36
# File 'lib/cli_progress_bar/item_progress_bar.rb', line 31

def update(num_items, prefix: nil, suffix: nil)
  raise "Invalid num_items value" unless num_items.between?(1, @max_items)

  @current_items = num_items
  update_progress(prefix:, suffix:)
end