From fee8cf4f46d913688572e41bddbcd23ee92bb3df Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 22 Mar 2026 16:51:13 -0400 Subject: [PATCH] Enhance ProgressBar to support customizable text overlay and display percentage value --- .../opus/ui/components/ProgressBar.lua | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/sys/modules/opus/ui/components/ProgressBar.lua b/sys/modules/opus/ui/components/ProgressBar.lua index c769449..1212627 100644 --- a/sys/modules/opus/ui/components/ProgressBar.lua +++ b/sys/modules/opus/ui/components/ProgressBar.lua @@ -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()