Hi, so I made this very barebone unicode progressbar generator but I feel like the way I wrote it kinda sucks, but I don’t know how to improve it.
Does anyone have any tips on how to improve it?
Here is the generator:
local textProgressBar = {}
local BLOCKS = {[0] = "" --[[ Necessary as indices start at 1 ]], "▏","▎","▍","▌","▋","▊","▉","█"} -- to self: smooth right to left bars are not possible as the necessary characters do not exist in unicode
local INTERVAL = 1/8
local INTERVAL_DENOMINATOR = 8
local function quantize(value: number, interval: number): number
local result = value
if result % interval >= interval / 2 then
result = result + (interval - result % interval)
elseif result % interval < interval / 2 then
result = result - result % interval
end
return result
end
function textProgressBar.generateBar(value: number, length: number, minRangeValue: number, maxRangeValue: number): string
local bar = ""
local normalizedValue = (value - minRangeValue) / (maxRangeValue - minRangeValue)
local scaledValue = length * normalizedValue
local scaledValueWhole, scaledValueMantissa = math.modf(scaledValue)
local quantizedScaledValueMantissa = quantize(scaledValueMantissa, INTERVAL)
for index = 1, scaledValueWhole, 1 do
bar ..= BLOCKS[8]
end
bar ..= BLOCKS[quantizedScaledValueMantissa * INTERVAL_DENOMINATOR --[[denominator of 1/8]] ]
return bar
end
return textProgressBar
What particularly bothers me is that I don’t know how I could fix up this part:
local INTERVAL = 1/8
local INTERVAL_DENOMINATOR = 8