Class: Charming::Presentation::Components::Progressbar
- Inherits:
-
Charming::Presentation::Component
- Object
- View
- Charming::Presentation::Component
- Charming::Presentation::Components::Progressbar
- 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
-
#bar_format ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
-
#complete ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
-
#current ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
-
#incomplete ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
-
#label ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
-
#total ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
Instance Method Summary collapse
-
#complete! ⇒ Object
Jumps the bar directly to 100% completion.
-
#initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil) ⇒ Progressbar
constructor
total is the maximum unit count.
-
#render ⇒ Object
Renders the bar as ‘[==== ]` (with the label appended when present).
-
#tick(count = 1) ⇒ Object
Advances the current value by count (default 1), clamping to ‘[0, total]`.
-
#update(value) ⇒ Object
Sets the current value, clamping to ‘[0, total]`.
Methods inherited from View
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 = .to_sym @label = label @current = 0 end |
Instance Attribute Details
#bar_format ⇒ Object
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 end |
#complete ⇒ Object
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 |
#current ⇒ Object
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 |
#incomplete ⇒ Object
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 |
#label ⇒ Object
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 |
#total ⇒ Object
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 |
#render ⇒ Object
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? = (@complete * completed) + (@incomplete * incomplete) result = "[" + + "]" 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 |