Class: Charming::Components::Progressbar
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::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, gradient: nil) ⇒ Progressbar
constructor
total is the maximum unit count.
-
#percent ⇒ Object
The current completion as an integer percentage (0-100).
-
#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 Charming::Component
Methods inherited from View
Constructor Details
#initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil, gradient: 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. gradient is an optional ["#rrggbb", "#rrggbb"] pair that colors the filled region with a sweep across the full bar width.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/charming/presentation/components/progressbar.rb', line 18 def initialize(total:, complete: "=", incomplete: " ", bar_format: :classic, label: nil, gradient: nil) super() @total = [total.to_i, 0].max @complete = complete.to_s @incomplete = incomplete.to_s @bar_format = .to_sym @label = label @gradient = gradient @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.
11 12 13 |
# File 'lib/charming/presentation/components/progressbar.rb', line 11 def @bar_format end |
#complete ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
11 12 13 |
# File 'lib/charming/presentation/components/progressbar.rb', line 11 def complete @complete end |
#current ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
11 12 13 |
# File 'lib/charming/presentation/components/progressbar.rb', line 11 def current @current end |
#incomplete ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
11 12 13 |
# File 'lib/charming/presentation/components/progressbar.rb', line 11 def incomplete @incomplete end |
#label ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
11 12 13 |
# File 'lib/charming/presentation/components/progressbar.rb', line 11 def label @label end |
#total ⇒ Object
Public accessors: total units, current value, label text, completed and remaining characters, and the bar format symbol.
11 12 13 |
# File 'lib/charming/presentation/components/progressbar.rb', line 11 def total @total end |
Instance Method Details
#complete! ⇒ Object
Jumps the bar directly to 100% completion. Returns self.
42 43 44 45 |
# File 'lib/charming/presentation/components/progressbar.rb', line 42 def complete! @current = @total self end |
#percent ⇒ Object
The current completion as an integer percentage (0-100).
60 61 62 63 64 |
# File 'lib/charming/presentation/components/progressbar.rb', line 60 def percent return 0 unless @total.positive? ((@current * 100) / @total.to_f).round end |
#render ⇒ Object
Renders the bar as [==== ] (with the label appended when present).
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/charming/presentation/components/progressbar.rb', line 48 def render width = [@total, 1].max completed = completed_width(width) = filled_cells(completed, width) + (@incomplete * (width - completed)) 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.
30 31 32 33 |
# File 'lib/charming/presentation/components/progressbar.rb', line 30 def tick(count = 1) update(@current + count) self end |
#update(value) ⇒ Object
Sets the current value, clamping to [0, total]. Returns self.
36 37 38 39 |
# File 'lib/charming/presentation/components/progressbar.rb', line 36 def update(value) @current = value.to_i.clamp(0, @total) self end |