Enhance ProgressBar to support customizable text overlay and display percentage value
This commit is contained in:
@@ -12,12 +12,35 @@ UI.ProgressBar.defaults = {
|
|||||||
fillColor = 'gray',
|
fillColor = 'gray',
|
||||||
textColor = 'green',
|
textColor = 'green',
|
||||||
value = 0,
|
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()
|
function UI.ProgressBar:draw()
|
||||||
local width = math.ceil(self.value / 100 * self.width)
|
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(width + 1, 1, self.width - width, self.height, self.fillChar, nil, self.fillColor)
|
||||||
self:fillArea(1, 1, width, self.height, self.progressChar, self.progressColor)
|
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
|
end
|
||||||
|
|
||||||
function UI.ProgressBar.example()
|
function UI.ProgressBar.example()
|
||||||
|
|||||||
Reference in New Issue
Block a user