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 ""
end
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_voters = math.floor(poll["voters_count"])
for _, v in pairs(poll["options"]) do
local width = math.floor(
0.5 + (v["votes_count"]/total_votes) * 100)
vars = {
label = width .. "% " .. v["title"],
pct = width
local percentage = (v["votes_count"]/total_votes) * 100
local rounded = math.floor(0.5 + percentage)
local vars = {
label = rounded .. "% " .. v["title"],
pct = rounded
}
local bar = bar_template:gsub("%$(%w+)%$", vars)
print(bar)
table.insert(bar_chart, bar)
end
-- close chart div
table.insert(bar_chart, "</div>")
local summary = "<p>$x$ people have cast $y$ votes</p>"
local foo = summary:gsub(
"%$(%w+)%$",
{x=math.floor(poll["voters_count"]), y=total_votes}
{x=total_voters, y=total_votes}
)
table.insert(bar_chart, foo)
return table.concat(bar_chart,"\n")