Class: Charming::Presentation::Components::Progressbar

Inherits:
Charming::Presentation::Component show all
Defined in:
lib/charming/presentation/components/progressbar.rb

Overview

Progressbar renders a fixed-width ASCII progress bar. The bar is sized to the configured total (in arbitrary units) and fills proportionally to the current value. Optionally appends a label after the bar.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil) ⇒ Progressbar

total is the maximum unit count. complete and incomplete are the characters used for filled and unfilled positions (default “=” and “ ”). bar_format is reserved for future format variants. label is an optional suffix shown after the bar.



17
18
19
20
21
22
23
24
25
# File 'lib/charming/presentation/components/progressbar.rb', line 17

def initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil)
  super()
  @total = [total.to_i, 0].max
  @complete = complete.to_s
  @incomplete = incomplete.to_s
  @bar_format = bar_format.to_sym
  @label = label
  @current = 0
end

Instance Attribute Details

#bar_formatObject

Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.



12
13
14
# File 'lib/charming/presentation/components/progressbar.rb', line 12

def bar_format
  @bar_format
end

#completeObject

Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.



12
13
14
# File 'lib/charming/presentation/components/progressbar.rb', line 12

def complete
  @complete
end

#currentObject

Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.



12
13
14
# File 'lib/charming/presentation/components/progressbar.rb', line 12

def current
  @current
end

#incompleteObject

Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.



12
13
14
# File 'lib/charming/presentation/components/progressbar.rb', line 12

def incomplete
  @incomplete
end

#labelObject

Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.



12
13
14
# File 'lib/charming/presentation/components/progressbar.rb', line 12

def label
  @label
end

#totalObject

Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.



12
13
14
# File 'lib/charming/presentation/components/progressbar.rb', line 12

def total
  @total
end

Instance Method Details

#complete!Object

Jumps the bar directly to 100% completion. Returns self.



40
41
42
43
# File 'lib/charming/presentation/components/progressbar.rb', line 40

def complete!
  @current = @total
  self
end

#renderObject

Renders the bar as ‘[==== ]` (with the label appended when present).



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/charming/presentation/components/progressbar.rb', line 46

def render
  width = [@total, 1].max
  completed = completed_width(width)
  incomplete = width - completed
  incomplete -= 1 if @current.zero?
  bar = (@complete * completed) + (@incomplete * incomplete)
  result = "[" + bar + "]"

  return result unless @label

  "#{result} #{@label}"
end

#tick(count = 1) ⇒ Object

Advances the current value by count (default 1), clamping to ‘[0, total]`. Returns self.



28
29
30
31
# File 'lib/charming/presentation/components/progressbar.rb', line 28

def tick(count = 1)
  update(@current + count)
  self
end

#update(value) ⇒ Object

Sets the current value, clamping to ‘[0, total]`. Returns self.



34
35
36
37
# File 'lib/charming/presentation/components/progressbar.rb', line 34

def update(value)
  @current = value.to_i.clamp(0, @total)
  self
end