Class: Potty::Widgets::ProgressBar
- Inherits:
-
Object
- Object
- Potty::Widgets::ProgressBar
- Defined in:
- lib/potty/widgets/progress_bar.rb
Overview
High-fidelity progress bar using Unicode block elements Provides 8x more granularity than simple filled/empty characters Pure string rendering - usable on a curses window or plain stdout
Constant Summary collapse
- BLOCKS =
[ ' ', "\u258F", # 1/8 "\u258E", # 2/8 "\u258D", # 3/8 "\u258C", # 4/8 "\u258B", # 5/8 "\u258A", # 6/8 "\u2589", # 7/8 "\u2588" # Full ].freeze
Instance Attribute Summary collapse
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(width: 20) ⇒ ProgressBar
constructor
A new instance of ProgressBar.
-
#render(progress) ⇒ String
Generate progress bar string.
-
#render_with_brackets(progress) ⇒ Object
Render with brackets.
Constructor Details
#initialize(width: 20) ⇒ ProgressBar
Returns a new instance of ProgressBar.
23 24 25 |
# File 'lib/potty/widgets/progress_bar.rb', line 23 def initialize(width: 20) @width = width end |
Instance Attribute Details
#width ⇒ Object (readonly)
Returns the value of attribute width.
21 22 23 |
# File 'lib/potty/widgets/progress_bar.rb', line 21 def width @width end |
Instance Method Details
#render(progress) ⇒ String
Generate progress bar string
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/potty/widgets/progress_bar.rb', line 30 def render(progress) progress = [[progress, 0.0].max, 1.0].min total_units = @width * 8 filled_units = (progress * total_units).round full_blocks = filled_units / 8 partial_eighths = filled_units % 8 = BLOCKS[8] * full_blocks if full_blocks < @width += BLOCKS[partial_eighths] += ' ' * (@width - full_blocks - 1) end end |
#render_with_brackets(progress) ⇒ Object
Render with brackets
50 51 52 |
# File 'lib/potty/widgets/progress_bar.rb', line 50 def render_with_brackets(progress) "[#{render(progress)}]" end |