Render polls

This commit is contained in:
Ivan Habunek 2019-08-27 14:34:51 +02:00
parent 9aadec6cfb
commit 871e2bc960
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 40 additions and 9 deletions

View File

@ -1,6 +1,7 @@
from datetime import datetime
from collections import namedtuple
from .utils import parse_datetime
Author = namedtuple("Author", ["account", "display_name"])
@ -13,11 +14,6 @@ def get_author(data, instance):
return Author(acct, status['account']['display_name'])
def parse_datetime(value):
"""Returns an aware datetime in local timezone"""
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f%z").astimezone()
class Status:
"""
A wrapper around the Status entity data fetched from Mastodon.

View File

@ -4,7 +4,7 @@ import webbrowser
from toot.utils import format_content
from .utils import highlight_hashtags
from .utils import highlight_hashtags, parse_datetime
from .widgets import SelectableText, SelectableColumns
logger = logging.getLogger("toot")
@ -160,9 +160,15 @@ class StatusDetails(urwid.Pile):
for line in format_content(status.data["content"]):
yield ("pack", urwid.Text(highlight_hashtags(line)))
if status.data["card"]:
poll = status.data.get("poll")
if poll:
yield ("pack", urwid.Divider())
yield ("pack", self.build_card(status.data["card"]))
yield ("pack", self.build_poll(poll))
card = status.data.get("card")
if card:
yield ("pack", urwid.Divider())
yield ("pack", self.build_card(card))
# Push things to bottom
yield ("weight", 1, urwid.SolidFill(" "))
@ -190,6 +196,28 @@ class StatusDetails(urwid.Pile):
card = urwid.Padding(card, left=1, right=1)
return urwid.LineBox(card)
def poll_generator(self, poll):
for option in poll["options"]:
perc = (round(100 * option["votes_count"] / poll["votes_count"])
if poll["votes_count"] else 0)
yield urwid.Text(option["title"])
yield urwid.ProgressBar("", "blue_selected", perc)
if poll["expired"]:
status = "Closed"
else:
expires_at = parse_datetime(poll["expires_at"]).strftime("%Y-%m-%d %H:%M")
status = "Closes on {}".format(expires_at)
status = "Poll · {} votes · {}".format(poll["votes_count"], status)
yield urwid.Text(("gray", status))
def build_poll(self, poll):
contents = list(self.poll_generator(poll))
poll = urwid.Pile(contents)
poll = urwid.Padding(poll, left=1, right=1)
return urwid.LineBox(poll)
class StatusListItem(SelectableColumns):
def __init__(self, status):

View File

@ -1,5 +1,7 @@
import re
from datetime import datetime
HASHTAG_PATTERN = re.compile(r'(?<!\w)(#\w+)\b')
@ -8,3 +10,8 @@ def highlight_hashtags(line):
("hashtag", p) if p.startswith("#") else p
for p in re.split(HASHTAG_PATTERN, line)
]
def parse_datetime(value):
"""Returns an aware datetime in local timezone"""
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f%z").astimezone()