mirror of
https://github.com/ihabunek/toot.git
synced 2024-11-03 04:17:21 -05:00
Render polls
This commit is contained in:
parent
9aadec6cfb
commit
871e2bc960
@ -1,6 +1,7 @@
|
|||||||
from datetime import datetime
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from .utils import parse_datetime
|
||||||
|
|
||||||
|
|
||||||
Author = namedtuple("Author", ["account", "display_name"])
|
Author = namedtuple("Author", ["account", "display_name"])
|
||||||
|
|
||||||
@ -13,11 +14,6 @@ def get_author(data, instance):
|
|||||||
return Author(acct, status['account']['display_name'])
|
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:
|
class Status:
|
||||||
"""
|
"""
|
||||||
A wrapper around the Status entity data fetched from Mastodon.
|
A wrapper around the Status entity data fetched from Mastodon.
|
||||||
|
@ -4,7 +4,7 @@ import webbrowser
|
|||||||
|
|
||||||
from toot.utils import format_content
|
from toot.utils import format_content
|
||||||
|
|
||||||
from .utils import highlight_hashtags
|
from .utils import highlight_hashtags, parse_datetime
|
||||||
from .widgets import SelectableText, SelectableColumns
|
from .widgets import SelectableText, SelectableColumns
|
||||||
|
|
||||||
logger = logging.getLogger("toot")
|
logger = logging.getLogger("toot")
|
||||||
@ -160,9 +160,15 @@ class StatusDetails(urwid.Pile):
|
|||||||
for line in format_content(status.data["content"]):
|
for line in format_content(status.data["content"]):
|
||||||
yield ("pack", urwid.Text(highlight_hashtags(line)))
|
yield ("pack", urwid.Text(highlight_hashtags(line)))
|
||||||
|
|
||||||
if status.data["card"]:
|
poll = status.data.get("poll")
|
||||||
|
if poll:
|
||||||
yield ("pack", urwid.Divider())
|
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
|
# Push things to bottom
|
||||||
yield ("weight", 1, urwid.SolidFill(" "))
|
yield ("weight", 1, urwid.SolidFill(" "))
|
||||||
@ -190,6 +196,28 @@ class StatusDetails(urwid.Pile):
|
|||||||
card = urwid.Padding(card, left=1, right=1)
|
card = urwid.Padding(card, left=1, right=1)
|
||||||
return urwid.LineBox(card)
|
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):
|
class StatusListItem(SelectableColumns):
|
||||||
def __init__(self, status):
|
def __init__(self, status):
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
HASHTAG_PATTERN = re.compile(r'(?<!\w)(#\w+)\b')
|
HASHTAG_PATTERN = re.compile(r'(?<!\w)(#\w+)\b')
|
||||||
|
|
||||||
|
|
||||||
@ -8,3 +10,8 @@ def highlight_hashtags(line):
|
|||||||
("hashtag", p) if p.startswith("#") else p
|
("hashtag", p) if p.startswith("#") else p
|
||||||
for p in re.split(HASHTAG_PATTERN, line)
|
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user