2019-08-24 09:20:31 +00:00
|
|
|
import logging
|
|
|
|
import urwid
|
2019-08-24 11:13:22 +00:00
|
|
|
import webbrowser
|
2019-08-24 09:20:31 +00:00
|
|
|
|
2019-08-24 10:53:55 +00:00
|
|
|
from toot.utils import format_content
|
|
|
|
|
2019-08-27 12:34:51 +00:00
|
|
|
from .utils import highlight_hashtags, parse_datetime
|
2019-08-24 09:20:31 +00:00
|
|
|
from .widgets import SelectableText, SelectableColumns
|
|
|
|
|
|
|
|
logger = logging.getLogger("toot")
|
|
|
|
|
|
|
|
|
|
|
|
class Timeline(urwid.Columns):
|
|
|
|
"""
|
|
|
|
Displays a list of statuses to the left, and status details on the right.
|
|
|
|
"""
|
2019-08-24 10:53:55 +00:00
|
|
|
signals = [
|
|
|
|
"status_focused",
|
|
|
|
"status_activated",
|
2019-08-24 11:13:22 +00:00
|
|
|
"next",
|
2019-08-24 10:53:55 +00:00
|
|
|
]
|
2019-08-24 09:20:31 +00:00
|
|
|
|
|
|
|
def __init__(self, tui, statuses):
|
|
|
|
self.tui = tui
|
|
|
|
self.statuses = statuses
|
|
|
|
|
|
|
|
self.status_list = self.build_status_list(statuses)
|
2019-08-25 15:58:46 +00:00
|
|
|
self.status_details = StatusDetails(statuses[0])
|
2019-08-24 09:20:31 +00:00
|
|
|
|
|
|
|
super().__init__([
|
2019-08-26 11:28:37 +00:00
|
|
|
("weight", 40, self.status_list),
|
|
|
|
("weight", 60, self.status_details),
|
2019-08-24 09:20:31 +00:00
|
|
|
], dividechars=1)
|
|
|
|
|
|
|
|
def build_status_list(self, statuses):
|
2019-08-24 10:53:55 +00:00
|
|
|
items = [self.build_list_item(status) for status in statuses]
|
2019-08-24 09:20:31 +00:00
|
|
|
walker = urwid.SimpleFocusListWalker(items)
|
|
|
|
urwid.connect_signal(walker, "modified", self.status_focused)
|
|
|
|
return urwid.ListBox(walker)
|
|
|
|
|
2019-08-24 10:53:55 +00:00
|
|
|
def build_list_item(self, status):
|
|
|
|
item = StatusListItem(status)
|
|
|
|
urwid.connect_signal(item, "click", self.status_activated)
|
|
|
|
return urwid.AttrMap(item, None, focus_map={
|
|
|
|
"blue": "green_selected",
|
|
|
|
"green": "green_selected",
|
|
|
|
"yellow": "green_selected",
|
|
|
|
None: "green_selected",
|
|
|
|
})
|
|
|
|
|
2019-08-24 09:20:31 +00:00
|
|
|
def get_focused_status(self):
|
|
|
|
return self.statuses[self.status_list.body.focus]
|
|
|
|
|
|
|
|
def status_activated(self, *args):
|
|
|
|
"""Called when a status is clicked, or Enter is pressed."""
|
2019-08-24 11:43:41 +00:00
|
|
|
status = self.get_focused_status()
|
2019-08-24 10:53:55 +00:00
|
|
|
self._emit("status_activated", [status])
|
2019-08-24 09:20:31 +00:00
|
|
|
|
|
|
|
def status_focused(self):
|
|
|
|
"""Called when the list focus switches to a new status"""
|
|
|
|
status = self.get_focused_status()
|
2019-08-27 08:02:13 +00:00
|
|
|
self.draw_status_details(status)
|
2019-08-24 09:20:31 +00:00
|
|
|
|
2019-08-24 10:53:55 +00:00
|
|
|
index = self.status_list.body.focus
|
|
|
|
count = len(self.statuses)
|
|
|
|
self._emit("status_focused", [status, index, count])
|
2019-08-24 09:20:31 +00:00
|
|
|
|
2019-08-27 08:02:13 +00:00
|
|
|
def draw_status_details(self, status):
|
|
|
|
self.status_details = StatusDetails(status)
|
|
|
|
self.contents[1] = self.status_details, ("weight", 50, False)
|
|
|
|
|
2019-08-24 11:13:22 +00:00
|
|
|
def keypress(self, size, key):
|
|
|
|
# If down is pressed on last status in list emit a signal to load more.
|
|
|
|
# TODO: Consider pre-loading statuses earlier
|
|
|
|
command = self._command_map[key]
|
|
|
|
if command in [urwid.CURSOR_DOWN, urwid.CURSOR_PAGE_DOWN]:
|
|
|
|
index = self.status_list.body.focus + 1
|
|
|
|
count = len(self.statuses)
|
|
|
|
if index >= count:
|
|
|
|
self._emit("next")
|
|
|
|
|
2019-08-26 11:28:37 +00:00
|
|
|
if key in ("b", "B"):
|
|
|
|
status = self.get_focused_status()
|
|
|
|
self.tui.async_toggle_reblog(status)
|
|
|
|
return
|
|
|
|
|
2019-08-27 08:02:13 +00:00
|
|
|
if key in ('c', 'C'):
|
|
|
|
self.tui.show_compose()
|
|
|
|
return
|
|
|
|
|
2019-08-26 11:28:37 +00:00
|
|
|
if key in ("f", "F"):
|
|
|
|
status = self.get_focused_status()
|
|
|
|
self.tui.async_toggle_favourite(status)
|
|
|
|
return
|
|
|
|
|
2019-08-24 11:13:22 +00:00
|
|
|
if key in ("v", "V"):
|
|
|
|
status = self.get_focused_status()
|
2019-08-27 12:43:22 +00:00
|
|
|
if status.data["url"]:
|
|
|
|
webbrowser.open(status.data["url"])
|
2019-08-24 11:13:22 +00:00
|
|
|
|
2019-08-25 12:30:57 +00:00
|
|
|
if key in ("u", "U"):
|
|
|
|
status = self.get_focused_status()
|
|
|
|
self.tui.show_status_source(status)
|
|
|
|
return
|
|
|
|
|
2019-08-24 11:13:22 +00:00
|
|
|
return super().keypress(size, key)
|
|
|
|
|
2019-08-27 10:57:18 +00:00
|
|
|
def append_status(self, status):
|
2019-08-26 11:28:37 +00:00
|
|
|
self.statuses.append(status)
|
|
|
|
self.status_list.body.append(self.build_list_item(status))
|
|
|
|
|
2019-08-27 08:02:13 +00:00
|
|
|
def prepend_status(self, status):
|
|
|
|
self.statuses.insert(0, status)
|
|
|
|
self.status_list.body.insert(0, self.build_list_item(status))
|
|
|
|
|
2019-08-27 10:57:18 +00:00
|
|
|
def append_statuses(self, statuses):
|
2019-08-26 11:28:37 +00:00
|
|
|
for status in statuses:
|
2019-08-27 10:57:18 +00:00
|
|
|
self.append_status(status)
|
|
|
|
|
|
|
|
def get_status_index(self, id):
|
|
|
|
# TODO: This is suboptimal, consider a better way
|
|
|
|
for n, status in enumerate(self.statuses):
|
|
|
|
if status.id == id:
|
|
|
|
return n
|
|
|
|
raise ValueError("Status with ID {} not found".format(id))
|
2019-08-26 11:28:37 +00:00
|
|
|
|
|
|
|
def update_status(self, status):
|
|
|
|
"""Overwrite status in list with the new instance and redraw."""
|
2019-08-27 10:57:18 +00:00
|
|
|
index = self.get_status_index(status.id)
|
|
|
|
assert self.statuses[index].id == status.id # Sanity check
|
2019-08-26 11:28:37 +00:00
|
|
|
|
|
|
|
# Update internal status list
|
|
|
|
self.statuses[index] = status
|
|
|
|
|
|
|
|
# Redraw list item
|
|
|
|
self.status_list.body[index] = self.build_list_item(status)
|
2019-08-24 11:13:22 +00:00
|
|
|
|
2019-08-26 11:28:37 +00:00
|
|
|
# Redraw status details if status is focused
|
|
|
|
if index == self.status_list.body.focus:
|
2019-08-27 08:02:13 +00:00
|
|
|
self.draw_status_details(status)
|
|
|
|
|
2019-08-24 09:20:31 +00:00
|
|
|
|
|
|
|
class StatusDetails(urwid.Pile):
|
2019-08-24 10:53:55 +00:00
|
|
|
def __init__(self, status):
|
|
|
|
widget_list = list(self.content_generator(status))
|
|
|
|
return super().__init__(widget_list)
|
|
|
|
|
|
|
|
def content_generator(self, status):
|
2019-08-24 11:43:41 +00:00
|
|
|
if status.data["reblog"]:
|
2019-08-25 08:00:48 +00:00
|
|
|
boosted_by = status.data["account"]["display_name"]
|
2019-08-25 15:58:46 +00:00
|
|
|
yield ("pack", urwid.Text(("gray", "♺ {} boosted".format(boosted_by))))
|
|
|
|
yield ("pack", urwid.AttrMap(urwid.Divider("-"), "gray"))
|
2019-08-24 11:43:41 +00:00
|
|
|
|
|
|
|
if status.author.display_name:
|
2019-08-25 15:58:46 +00:00
|
|
|
yield ("pack", urwid.Text(("green", status.author.display_name)))
|
2019-08-25 08:00:48 +00:00
|
|
|
|
2019-08-25 15:58:46 +00:00
|
|
|
yield ("pack", urwid.Text(("yellow", status.author.account)))
|
|
|
|
yield ("pack", urwid.Divider())
|
2019-08-24 10:53:55 +00:00
|
|
|
|
|
|
|
for line in format_content(status.data["content"]):
|
2019-08-25 15:58:46 +00:00
|
|
|
yield ("pack", urwid.Text(highlight_hashtags(line)))
|
2019-08-24 09:20:31 +00:00
|
|
|
|
2019-08-27 12:34:51 +00:00
|
|
|
poll = status.data.get("poll")
|
|
|
|
if poll:
|
2019-08-25 15:58:46 +00:00
|
|
|
yield ("pack", urwid.Divider())
|
2019-08-27 12:34:51 +00:00
|
|
|
yield ("pack", self.build_poll(poll))
|
|
|
|
|
|
|
|
card = status.data.get("card")
|
|
|
|
if card:
|
|
|
|
yield ("pack", urwid.Divider())
|
|
|
|
yield ("pack", self.build_card(card))
|
2019-08-25 15:58:46 +00:00
|
|
|
|
|
|
|
# Push things to bottom
|
|
|
|
yield ("weight", 1, urwid.SolidFill(" "))
|
|
|
|
yield ("pack", urwid.Text([
|
|
|
|
("cyan_bold", "B"), ("cyan", "oost"), " | ",
|
|
|
|
("cyan_bold", "F"), ("cyan", "avourite"), " | ",
|
2019-08-27 08:02:13 +00:00
|
|
|
("cyan_bold", "V"), ("cyan", "iew"), " | ",
|
|
|
|
("cyan", "So"), ("cyan_bold", "u"), ("cyan", "rce"), " | ",
|
2019-08-25 15:58:46 +00:00
|
|
|
("cyan_bold", "H"), ("cyan", "elp"), " ",
|
|
|
|
]))
|
2019-08-24 10:53:55 +00:00
|
|
|
|
|
|
|
def card_generator(self, card):
|
|
|
|
yield urwid.Text(("green", card["title"].strip()))
|
|
|
|
if card["author_name"]:
|
|
|
|
yield urwid.Text(["by ", ("yellow", card["author_name"].strip())])
|
2019-08-24 12:14:46 +00:00
|
|
|
yield urwid.Text("")
|
|
|
|
if card["description"]:
|
|
|
|
yield urwid.Text(card["description"].strip())
|
|
|
|
yield urwid.Text("")
|
2019-08-24 10:53:55 +00:00
|
|
|
yield urwid.Text(("link", card["url"]))
|
|
|
|
|
|
|
|
def build_card(self, card):
|
|
|
|
contents = list(self.card_generator(card))
|
2019-08-25 15:58:46 +00:00
|
|
|
card = urwid.Pile(contents)
|
|
|
|
card = urwid.Padding(card, left=1, right=1)
|
|
|
|
return urwid.LineBox(card)
|
2019-08-24 09:20:31 +00:00
|
|
|
|
2019-08-27 12:34:51 +00:00
|
|
|
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)
|
|
|
|
|
2019-08-24 09:20:31 +00:00
|
|
|
|
|
|
|
class StatusListItem(SelectableColumns):
|
2019-08-24 10:53:55 +00:00
|
|
|
def __init__(self, status):
|
2019-08-24 09:20:31 +00:00
|
|
|
created_at = status.created_at.strftime("%Y-%m-%d %H:%M")
|
2019-08-24 10:53:55 +00:00
|
|
|
favourited = ("yellow", "★") if status.favourited else " "
|
|
|
|
reblogged = ("yellow", "⤶") if status.reblogged else " "
|
2019-08-24 09:20:31 +00:00
|
|
|
|
|
|
|
return super().__init__([
|
|
|
|
("pack", SelectableText(("blue", created_at), wrap="clip")),
|
|
|
|
("pack", urwid.Text(" ")),
|
|
|
|
urwid.Text(("green", status.account), wrap="clip"),
|
|
|
|
("pack", urwid.Text(" ")),
|
|
|
|
("pack", urwid.Text(favourited)),
|
2019-08-24 10:53:55 +00:00
|
|
|
("pack", urwid.Text(" ")),
|
2019-08-24 09:20:31 +00:00
|
|
|
("pack", urwid.Text(reblogged)),
|
|
|
|
])
|