Enhance ProgressBar to support customizable text overlay and display percentage value

This commit is contained in:
MayaTheShy
2026-03-22 16:51:13 -04:00
parent f2460aa5e9
commit fee8cf4f46

View File

@@ -12,12 +12,35 @@ UI.ProgressBar.defaults = {
fillColor = 'gray',
textColor = 'green',
value = 0,
showText = false, -- display value text overlay centered on bar
textOverlay = nil, -- custom overlay text (string or function(value) -> string)
textOverlayColor = 'white', -- text overlay foreground color
}
function UI.ProgressBar:draw()
local width = math.ceil(self.value / 100 * self.width)
self:fillArea(width + 1, 1, self.width - width, self.height, self.fillChar, nil, self.fillColor)
self:fillArea(1, 1, width, self.height, self.progressChar, self.progressColor)
if self.showText then
local text
if self.textOverlay then
text = type(self.textOverlay) == 'function'
and self.textOverlay(self.value)
or tostring(self.textOverlay)
else
text = math.floor(self.value) .. '%'
end
local midY = math.ceil(self.height / 2)
local x = math.floor((self.width - #text) / 2) + 1
for i = 1, #text do
local cx = x + i - 1
if cx >= 1 and cx <= self.width then
local bg = cx <= width and self.progressColor or self.fillColor
self:write(cx, midY, text:sub(i, i), bg, self.textOverlayColor)
end
end
end
end
function UI.ProgressBar.example()