module Bar
Helper module for rendering horizontal progress bars using Unicode block characters.
Definitions
def self.format(value, width)
Format a fractional value as a horizontal bar.
Signature
-
parameter
valueFloat A value between 0.0 and 1.0 representing the fill level.
-
parameter
widthInteger The width of the bar in characters.
-
returns
String A string of Unicode block characters representing the filled bar.
Implementation
def self.format(value, width)
blocks = width * value
full_blocks = blocks.floor
partial_block = ((blocks - full_blocks) * BLOCK.size).floor
if partial_block.zero?
BLOCK.last * full_blocks
else
"#{BLOCK.last * full_blocks}#{BLOCK[partial_block]}"
end.ljust(width)
end