Display polls in command line status output

Also display your own votes in toot tui poll displays.
This commit is contained in:
Daniel Schwarz 2022-11-27 21:27:02 -05:00 committed by Ivan Habunek
parent 7b194880a0
commit f15310cc75
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
2 changed files with 34 additions and 2 deletions

View File

@ -168,6 +168,7 @@ def print_status(status, width):
content = reblog['content'] if reblog else status['content']
media_attachments = reblog['media_attachments'] if reblog else status['media_attachments']
in_reply_to = status['in_reply_to_id']
poll = reblog['poll'] if reblog else status['poll']
time = parse_datetime(status['created_at'])
time = time.strftime('%Y-%m-%d %H:%M %Z')
@ -199,6 +200,31 @@ def print_status(status, width):
for line in wc_wrap(url, width):
print_out(line)
if poll:
print_out("")
for idx, option in enumerate(poll["options"]):
perc = (round(100 * option["votes_count"] / poll["votes_count"])
if poll["votes_count"] else 0)
if poll["voted"] and poll["own_votes"] and idx in poll["own_votes"]:
voted_for = " <yellow>✓<yellow>"
else:
voted_for = ""
print_out(option["title"] + " - {}%".format(perc) + voted_for)
poll_footer = "Poll · {} votes".format(poll["votes_count"])
if poll["expired"]:
poll_footer += " · Closed"
if poll["expires_at"]:
expires_at = parse_datetime(poll["expires_at"]).strftime("%Y-%m-%d %H:%M")
poll_footer += " · Closes on {}".format(expires_at)
print_out("\n{}".format(poll_footer))
print_out("\n{}{}{}".format(
"ID <yellow>{}</yellow> ".format(status['id']),
"↲ In reply to <yellow>{}</yellow> ".format(in_reply_to) if in_reply_to else "",

View File

@ -328,10 +328,16 @@ class StatusDetails(urwid.Pile):
yield urwid.Text(("link", card["url"]))
def poll_generator(self, poll):
for option in poll["options"]:
for idx, option in enumerate(poll["options"]):
perc = (round(100 * option["votes_count"] / poll["votes_count"])
if poll["votes_count"] else 0)
yield urwid.Text(option["title"])
if poll["voted"] and poll["own_votes"] and idx in poll["own_votes"]:
voted_for = ""
else:
voted_for = ""
yield urwid.Text(option["title"] + voted_for)
yield urwid.ProgressBar("", "poll_bar", perc)
status = "Poll · {} votes".format(poll["votes_count"])