clean up get_poll()

This commit is contained in:
scuti 2024-11-28 15:45:09 -08:00
parent 940f549821
commit 7113d9e28a

View File

@ -135,24 +135,30 @@ function write_comments(pleroma_posts, instance, show_avatars)
return "" return ""
end end
local bar_chart = {"<div class=\"chart\">"} local bar_chart = {"<div class=\"chart\">"}
local bar_template = "<div class=\"bar\" style=\"width: $pct$%;\">$label$</div>" local bar_template = [[
<div class="bar" style="width: $pct$%;">
$label$
</div>
]]
local total_votes = math.floor(poll["votes_count"]) local total_votes = math.floor(poll["votes_count"])
local total_voters = math.floor(poll["voters_count"])
for _, v in pairs(poll["options"]) do for _, v in pairs(poll["options"]) do
local width = math.floor( local percentage = (v["votes_count"]/total_votes) * 100
0.5 + (v["votes_count"]/total_votes) * 100) local rounded = math.floor(0.5 + percentage)
vars = { local vars = {
label = width .. "% " .. v["title"], label = rounded .. "% " .. v["title"],
pct = width pct = rounded
} }
local bar = bar_template:gsub("%$(%w+)%$", vars) local bar = bar_template:gsub("%$(%w+)%$", vars)
print(bar)
table.insert(bar_chart, bar) table.insert(bar_chart, bar)
end end
-- close chart div
table.insert(bar_chart, "</div>") table.insert(bar_chart, "</div>")
local summary = "<p>$x$ people have cast $y$ votes</p>" local summary = "<p>$x$ people have cast $y$ votes</p>"
local foo = summary:gsub( local foo = summary:gsub(
"%$(%w+)%$", "%$(%w+)%$",
{x=math.floor(poll["voters_count"]), y=total_votes} {x=total_voters, y=total_votes}
) )
table.insert(bar_chart, foo) table.insert(bar_chart, foo)
return table.concat(bar_chart,"\n") return table.concat(bar_chart,"\n")