2019-08-24 11:20:31 +02:00
|
|
|
import logging
|
|
|
|
import urwid
|
2019-08-24 13:13:22 +02:00
|
|
|
import webbrowser
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2023-03-15 09:55:50 +01:00
|
|
|
from typing import List, Optional
|
2019-08-24 12:53:55 +02:00
|
|
|
|
2023-03-15 09:43:54 +01:00
|
|
|
from toot.tui import app
|
2023-11-16 11:46:54 +01:00
|
|
|
from toot.tui.richtext import html_to_widgets, url_to_widget
|
2023-06-30 11:06:17 +02:00
|
|
|
from toot.utils.datetime import parse_datetime, time_ago
|
2023-01-19 09:53:38 +01:00
|
|
|
from toot.utils.language import language_name
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2023-09-23 03:32:19 +02:00
|
|
|
from toot.entities import Status
|
|
|
|
from toot.tui.scroll import Scrollable, ScrollBar
|
|
|
|
from toot.tui.utils import highlight_keys
|
|
|
|
from toot.tui.widgets import SelectableText, SelectableColumns
|
2023-06-30 11:06:17 +02:00
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
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 12:53:55 +02:00
|
|
|
signals = [
|
2023-03-15 10:17:35 +01:00
|
|
|
"close", # Close thread
|
|
|
|
"focus", # Focus changed
|
|
|
|
"next", # Fetch more statuses
|
|
|
|
"save", # Save current timeline
|
2019-08-24 12:53:55 +02:00
|
|
|
]
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2023-03-15 09:55:50 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
tui: "app.TUI",
|
|
|
|
name: str,
|
|
|
|
statuses: List[Status],
|
|
|
|
focus: int = 0,
|
|
|
|
is_thread: bool = False
|
|
|
|
):
|
2023-03-15 09:43:54 +01:00
|
|
|
self.tui = tui
|
2019-08-28 15:32:57 +02:00
|
|
|
self.name = name
|
|
|
|
self.is_thread = is_thread
|
2019-08-24 11:20:31 +02:00
|
|
|
self.statuses = statuses
|
2019-08-28 15:32:57 +02:00
|
|
|
self.status_list = self.build_status_list(statuses, focus=focus)
|
2023-01-09 05:20:33 +01:00
|
|
|
|
2023-01-19 11:10:36 +01:00
|
|
|
try:
|
|
|
|
focused_status = statuses[focus]
|
2020-05-20 00:50:21 +02:00
|
|
|
except IndexError:
|
2023-01-19 11:10:36 +01:00
|
|
|
focused_status = None
|
|
|
|
|
|
|
|
self.status_details = StatusDetails(self, focused_status)
|
|
|
|
status_widget = self.wrap_status_details(self.status_details)
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
super().__init__([
|
2019-08-26 13:28:37 +02:00
|
|
|
("weight", 40, self.status_list),
|
2023-07-07 13:02:18 +02:00
|
|
|
("weight", 0, urwid.AttrWrap(urwid.SolidFill("│"), "columns_divider")),
|
2023-01-19 11:10:36 +01:00
|
|
|
("weight", 60, status_widget),
|
2019-08-30 12:28:03 +02:00
|
|
|
])
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2023-01-19 11:10:36 +01:00
|
|
|
def wrap_status_details(self, status_details: "StatusDetails") -> urwid.Widget:
|
2023-02-15 05:11:11 +01:00
|
|
|
"""Wrap StatusDetails widget with a scrollbar and footer."""
|
|
|
|
self.status_detail_scrollable = Scrollable(urwid.Padding(status_details, right=1))
|
2023-01-19 11:10:36 +01:00
|
|
|
return urwid.Padding(
|
|
|
|
urwid.Frame(
|
|
|
|
body=ScrollBar(
|
2023-02-15 05:11:11 +01:00
|
|
|
self.status_detail_scrollable,
|
2023-01-19 11:10:36 +01:00
|
|
|
thumb_char="\u2588",
|
|
|
|
trough_char="\u2591",
|
|
|
|
),
|
|
|
|
footer=self.get_option_text(status_details.status),
|
|
|
|
),
|
|
|
|
left=1
|
|
|
|
)
|
|
|
|
|
2019-08-28 15:32:57 +02:00
|
|
|
def build_status_list(self, statuses, focus):
|
2019-08-24 12:53:55 +02:00
|
|
|
items = [self.build_list_item(status) for status in statuses]
|
2019-08-24 11:20:31 +02:00
|
|
|
walker = urwid.SimpleFocusListWalker(items)
|
2019-08-28 15:32:57 +02:00
|
|
|
walker.set_focus(focus)
|
|
|
|
urwid.connect_signal(walker, "modified", self.modified)
|
2019-08-24 11:20:31 +02:00
|
|
|
return urwid.ListBox(walker)
|
|
|
|
|
2019-08-24 12:53:55 +02:00
|
|
|
def build_list_item(self, status):
|
2023-07-25 09:32:17 +02:00
|
|
|
item = StatusListItem(status, self.tui.args.relative_datetimes)
|
2019-08-30 12:28:03 +02:00
|
|
|
urwid.connect_signal(item, "click", lambda *args:
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_context_menu(status))
|
2019-08-24 12:53:55 +02:00
|
|
|
return urwid.AttrMap(item, None, focus_map={
|
2023-07-07 13:57:19 +02:00
|
|
|
"status_list_account": "status_list_selected",
|
|
|
|
"status_list_timestamp": "status_list_selected",
|
|
|
|
"highligh": "status_list_selected",
|
|
|
|
"dim": "status_list_selected",
|
2023-07-07 13:02:18 +02:00
|
|
|
None: "status_list_selected",
|
2019-08-24 12:53:55 +02:00
|
|
|
})
|
|
|
|
|
2023-01-19 11:10:36 +01:00
|
|
|
def get_option_text(self, status: Optional[Status]) -> Optional[urwid.Text]:
|
|
|
|
if not status:
|
|
|
|
return None
|
|
|
|
|
2023-02-15 16:14:41 +01:00
|
|
|
poll = status.original.data.get("poll")
|
2023-02-11 03:48:42 +01:00
|
|
|
|
2023-01-09 05:20:33 +01:00
|
|
|
options = [
|
2023-02-03 02:37:12 +01:00
|
|
|
"[A]ccount" if not status.is_mine else "",
|
2023-01-09 05:20:33 +01:00
|
|
|
"[B]oost",
|
|
|
|
"[D]elete" if status.is_mine else "",
|
|
|
|
"B[o]okmark",
|
|
|
|
"[F]avourite",
|
|
|
|
"[V]iew",
|
|
|
|
"[T]hread" if not self.is_thread else "",
|
2023-07-06 13:31:43 +02:00
|
|
|
"L[i]nks",
|
2023-01-09 05:20:33 +01:00
|
|
|
"[R]eply",
|
2023-02-11 03:48:42 +01:00
|
|
|
"[P]oll" if poll and not poll["expired"] else "",
|
2023-01-09 05:20:33 +01:00
|
|
|
"So[u]rce",
|
|
|
|
"[Z]oom",
|
2023-03-15 09:54:18 +01:00
|
|
|
"Tra[n]slate" if self.tui.can_translate else "",
|
2023-03-07 01:30:54 +01:00
|
|
|
"Cop[y]",
|
2023-07-07 13:02:18 +02:00
|
|
|
"Help([?])",
|
2023-01-09 05:20:33 +01:00
|
|
|
]
|
|
|
|
options = "\n" + " ".join(o for o in options if o)
|
2023-07-07 13:02:18 +02:00
|
|
|
options = highlight_keys(options, "shortcut_highlight", "shortcut")
|
2023-01-19 11:10:36 +01:00
|
|
|
return urwid.Text(options)
|
2023-01-09 05:20:33 +01:00
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
def get_focused_status(self):
|
2020-05-20 00:50:21 +02:00
|
|
|
try:
|
|
|
|
return self.statuses[self.status_list.body.focus]
|
|
|
|
except TypeError:
|
|
|
|
return None
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2019-08-28 15:32:57 +02:00
|
|
|
def get_focused_status_with_counts(self):
|
2019-08-29 10:43:56 +02:00
|
|
|
"""Returns a tuple of:
|
|
|
|
* focused status
|
|
|
|
* focused status' index in the status list
|
|
|
|
* length of the status list
|
|
|
|
"""
|
2019-08-28 15:32:57 +02:00
|
|
|
return (
|
|
|
|
self.get_focused_status(),
|
|
|
|
self.status_list.body.focus,
|
|
|
|
len(self.statuses),
|
|
|
|
)
|
|
|
|
|
|
|
|
def modified(self):
|
2019-08-24 11:20:31 +02:00
|
|
|
"""Called when the list focus switches to a new status"""
|
2019-08-28 15:32:57 +02:00
|
|
|
status, index, count = self.get_focused_status_with_counts()
|
2019-08-27 10:02:13 +02:00
|
|
|
self.draw_status_details(status)
|
2019-08-28 15:32:57 +02:00
|
|
|
self._emit("focus")
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2019-08-28 15:32:57 +02:00
|
|
|
def refresh_status_details(self):
|
|
|
|
"""Redraws the details of the focused status."""
|
|
|
|
status = self.get_focused_status()
|
2023-02-15 05:11:11 +01:00
|
|
|
pos = self.status_detail_scrollable.get_scrollpos()
|
2019-08-28 15:32:57 +02:00
|
|
|
self.draw_status_details(status)
|
2023-02-15 05:11:11 +01:00
|
|
|
self.status_detail_scrollable.set_scrollpos(pos)
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2019-08-27 10:02:13 +02:00
|
|
|
def draw_status_details(self, status):
|
2023-01-19 09:53:38 +01:00
|
|
|
self.status_details = StatusDetails(self, status)
|
2023-01-19 11:10:36 +01:00
|
|
|
widget = self.wrap_status_details(self.status_details)
|
|
|
|
self.contents[2] = widget, ("weight", 60, False)
|
2019-08-27 10:02:13 +02:00
|
|
|
|
2019-08-24 13:13:22 +02:00
|
|
|
def keypress(self, size, key):
|
2019-08-29 11:01:49 +02:00
|
|
|
status = self.get_focused_status()
|
2019-08-29 10:43:56 +02:00
|
|
|
command = self._command_map[key]
|
|
|
|
|
2020-05-20 00:50:21 +02:00
|
|
|
if not status:
|
|
|
|
return super().keypress(size, key)
|
|
|
|
|
2019-08-24 13:13:22 +02:00
|
|
|
# If down is pressed on last status in list emit a signal to load more.
|
|
|
|
# TODO: Consider pre-loading statuses earlier
|
2020-05-20 00:50:21 +02:00
|
|
|
if command in [urwid.CURSOR_DOWN, urwid.CURSOR_PAGE_DOWN] \
|
|
|
|
and self.status_list.body.focus:
|
2019-08-24 13:13:22 +02:00
|
|
|
index = self.status_list.body.focus + 1
|
|
|
|
count = len(self.statuses)
|
|
|
|
if index >= count:
|
|
|
|
self._emit("next")
|
|
|
|
|
2023-02-03 02:37:12 +01:00
|
|
|
if key in ("a", "A"):
|
2023-03-15 10:17:35 +01:00
|
|
|
account_id = status.original.data["account"]["id"]
|
|
|
|
self.tui.show_account(account_id)
|
2023-02-03 02:37:12 +01:00
|
|
|
return
|
|
|
|
|
2019-08-26 13:28:37 +02:00
|
|
|
if key in ("b", "B"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.async_toggle_reblog(self, status)
|
2019-08-26 13:28:37 +02:00
|
|
|
return
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
if key in ("c", "C"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_compose()
|
2019-08-27 10:02:13 +02:00
|
|
|
return
|
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
if key in ("d", "D"):
|
2023-03-15 10:17:35 +01:00
|
|
|
if status.is_mine:
|
|
|
|
self.tui.show_delete_confirmation(status)
|
2019-09-04 16:16:16 +02:00
|
|
|
return
|
|
|
|
|
2019-08-26 13:28:37 +02:00
|
|
|
if key in ("f", "F"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.async_toggle_favourite(self, status)
|
2019-08-26 13:28:37 +02:00
|
|
|
return
|
|
|
|
|
2019-08-29 11:47:44 +02:00
|
|
|
if key in ("m", "M"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_media(status)
|
2019-08-29 11:47:44 +02:00
|
|
|
return
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
if key in ("q", "Q"):
|
2019-08-28 15:32:57 +02:00
|
|
|
self._emit("close")
|
|
|
|
return
|
|
|
|
|
2019-09-06 15:12:18 +02:00
|
|
|
if key == "esc" and self.is_thread:
|
|
|
|
self._emit("close")
|
|
|
|
return
|
|
|
|
|
2019-08-29 11:01:49 +02:00
|
|
|
if key in ("r", "R"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_compose(status)
|
2019-08-29 11:01:49 +02:00
|
|
|
return
|
|
|
|
|
2019-08-29 14:01:26 +02:00
|
|
|
if key in ("s", "S"):
|
2019-09-22 12:13:40 +02:00
|
|
|
status.original.show_sensitive = True
|
2019-08-29 14:01:26 +02:00
|
|
|
self.refresh_status_details()
|
|
|
|
return
|
|
|
|
|
2022-12-27 11:46:29 +01:00
|
|
|
if key in ("o", "O"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.async_toggle_bookmark(self, status)
|
2022-12-27 11:39:50 +01:00
|
|
|
return
|
|
|
|
|
2023-07-06 13:31:43 +02:00
|
|
|
if key in ("i", "I"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_links(status)
|
2020-01-26 11:13:45 +01:00
|
|
|
return
|
|
|
|
|
2022-12-08 01:57:56 +01:00
|
|
|
if key in ("n", "N"):
|
2023-03-15 09:54:18 +01:00
|
|
|
if self.tui.can_translate:
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.async_translate(self, status)
|
2022-12-08 01:57:56 +01:00
|
|
|
return
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
if key in ("t", "T"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_thread(status)
|
2019-08-28 15:32:57 +02:00
|
|
|
return
|
|
|
|
|
2019-08-29 11:47:44 +02:00
|
|
|
if key in ("u", "U"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_status_source(status)
|
2019-08-29 11:47:44 +02:00
|
|
|
return
|
|
|
|
|
2019-08-24 13:13:22 +02:00
|
|
|
if key in ("v", "V"):
|
2019-09-22 12:13:40 +02:00
|
|
|
if status.original.url:
|
|
|
|
webbrowser.open(status.original.url)
|
2022-12-22 19:40:22 +01:00
|
|
|
# force a screen refresh; necessary with console browsers
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.clear_screen()
|
2019-08-29 10:43:56 +02:00
|
|
|
return
|
2019-08-24 13:13:22 +02:00
|
|
|
|
2023-02-11 03:48:42 +01:00
|
|
|
if key in ("e", "E"):
|
2020-01-26 21:29:08 +01:00
|
|
|
self._emit("save", status)
|
|
|
|
return
|
|
|
|
|
2021-10-10 01:29:46 +02:00
|
|
|
if key in ("z", "Z"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_status_zoom(self.status_details)
|
2021-10-10 01:29:46 +02:00
|
|
|
return
|
|
|
|
|
2023-02-11 03:48:42 +01:00
|
|
|
if key in ("p", "P"):
|
2023-02-15 16:14:41 +01:00
|
|
|
poll = status.original.data.get("poll")
|
2023-02-11 03:48:42 +01:00
|
|
|
if poll and not poll["expired"]:
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.show_poll(status)
|
2023-02-11 03:48:42 +01:00
|
|
|
return
|
|
|
|
|
2023-03-07 01:30:54 +01:00
|
|
|
if key in ("y", "Y"):
|
2023-03-15 10:17:35 +01:00
|
|
|
self.tui.copy_status(status)
|
2023-03-07 01:30:54 +01:00
|
|
|
return
|
|
|
|
|
2019-08-24 13:13:22 +02:00
|
|
|
return super().keypress(size, key)
|
|
|
|
|
2019-08-27 12:57:18 +02:00
|
|
|
def append_status(self, status):
|
2019-08-26 13:28:37 +02:00
|
|
|
self.statuses.append(status)
|
|
|
|
self.status_list.body.append(self.build_list_item(status))
|
|
|
|
|
2019-08-27 10:02:13 +02: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 12:57:18 +02:00
|
|
|
def append_statuses(self, statuses):
|
2019-08-26 13:28:37 +02:00
|
|
|
for status in statuses:
|
2019-08-27 12:57:18 +02: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 13:28:37 +02:00
|
|
|
|
2019-08-28 17:04:45 +02:00
|
|
|
def focus_status(self, status):
|
|
|
|
index = self.get_status_index(status.id)
|
|
|
|
self.status_list.body.set_focus(index)
|
|
|
|
|
2019-08-26 13:28:37 +02:00
|
|
|
def update_status(self, status):
|
|
|
|
"""Overwrite status in list with the new instance and redraw."""
|
2019-08-27 12:57:18 +02:00
|
|
|
index = self.get_status_index(status.id)
|
|
|
|
assert self.statuses[index].id == status.id # Sanity check
|
2019-08-26 13:28:37 +02: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 13:13:22 +02:00
|
|
|
|
2019-08-26 13:28:37 +02:00
|
|
|
# Redraw status details if status is focused
|
|
|
|
if index == self.status_list.body.focus:
|
2019-08-27 10:02:13 +02:00
|
|
|
self.draw_status_details(status)
|
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
def remove_status(self, status):
|
|
|
|
index = self.get_status_index(status.id)
|
|
|
|
assert self.statuses[index].id == status.id # Sanity check
|
|
|
|
|
2022-12-27 12:31:55 +01:00
|
|
|
del self.statuses[index]
|
|
|
|
del self.status_list.body[index]
|
2019-09-04 16:16:16 +02:00
|
|
|
self.refresh_status_details()
|
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
class StatusDetails(urwid.Pile):
|
2023-01-19 09:53:38 +01:00
|
|
|
def __init__(self, timeline: Timeline, status: Optional[Status]):
|
2023-01-19 11:10:36 +01:00
|
|
|
self.status = status
|
2023-03-15 09:54:18 +01:00
|
|
|
self.followed_accounts = timeline.tui.followed_accounts
|
2023-01-19 11:10:36 +01:00
|
|
|
|
2020-05-20 00:50:21 +02:00
|
|
|
reblogged_by = status.author if status and status.reblog else None
|
|
|
|
widget_list = list(self.content_generator(status.original, reblogged_by)
|
|
|
|
if status else ())
|
2019-08-24 12:53:55 +02:00
|
|
|
return super().__init__(widget_list)
|
|
|
|
|
2019-09-22 12:13:40 +02:00
|
|
|
def content_generator(self, status, reblogged_by):
|
|
|
|
if reblogged_by:
|
2019-10-01 09:27:27 +02:00
|
|
|
text = "♺ {} boosted".format(reblogged_by.display_name or reblogged_by.username)
|
2023-07-07 13:57:19 +02:00
|
|
|
yield ("pack", urwid.Text(("dim", text)))
|
|
|
|
yield ("pack", urwid.AttrMap(urwid.Divider("-"), "dim"))
|
2019-08-24 13:43:41 +02:00
|
|
|
|
|
|
|
if status.author.display_name:
|
2023-07-08 08:49:50 +02:00
|
|
|
yield ("pack", urwid.Text(("bold", status.author.display_name)))
|
2019-08-25 10:00:48 +02:00
|
|
|
|
2023-07-08 08:49:50 +02:00
|
|
|
account_color = "highlight" if status.author.account in self.followed_accounts else "account"
|
2023-01-03 04:55:16 +01:00
|
|
|
yield ("pack", urwid.Text((account_color, status.author.account)))
|
2019-08-25 17:58:46 +02:00
|
|
|
yield ("pack", urwid.Divider())
|
2019-08-24 12:53:55 +02:00
|
|
|
|
2019-08-29 14:01:26 +02:00
|
|
|
if status.data["spoiler_text"]:
|
|
|
|
yield ("pack", urwid.Text(status.data["spoiler_text"]))
|
|
|
|
yield ("pack", urwid.Divider())
|
|
|
|
|
|
|
|
# Show content warning
|
|
|
|
if status.data["spoiler_text"] and not status.show_sensitive:
|
|
|
|
yield ("pack", urwid.Text(("content_warning", "Marked as sensitive. Press S to view.")))
|
|
|
|
else:
|
2023-03-04 22:04:13 +01:00
|
|
|
content = status.original.translation if status.original.show_translation else status.data["content"]
|
2023-11-06 18:14:21 +01:00
|
|
|
widgetlist = html_to_widgets(content)
|
2023-09-23 03:32:19 +02:00
|
|
|
|
|
|
|
for line in widgetlist:
|
|
|
|
yield (line)
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2022-12-27 10:53:58 +01:00
|
|
|
media = status.data["media_attachments"]
|
|
|
|
if media:
|
|
|
|
for m in media:
|
2023-07-07 13:57:19 +02:00
|
|
|
yield ("pack", urwid.AttrMap(urwid.Divider("-"), "dim"))
|
2022-12-27 10:53:58 +01:00
|
|
|
yield ("pack", urwid.Text([("bold", "Media attachment"), " (", m["type"], ")"]))
|
|
|
|
if m["description"]:
|
|
|
|
yield ("pack", urwid.Text(m["description"]))
|
2023-11-16 11:46:54 +01:00
|
|
|
yield ("pack", url_to_widget(m["url"]))
|
2022-12-27 10:53:58 +01:00
|
|
|
|
2023-02-15 16:14:41 +01:00
|
|
|
poll = status.original.data.get("poll")
|
2022-12-27 10:53:58 +01:00
|
|
|
if poll:
|
|
|
|
yield ("pack", urwid.Divider())
|
|
|
|
yield ("pack", self.build_linebox(self.poll_generator(poll)))
|
|
|
|
|
|
|
|
card = status.data.get("card")
|
|
|
|
if card:
|
|
|
|
yield ("pack", urwid.Divider())
|
|
|
|
yield ("pack", self.build_linebox(self.card_generator(card)))
|
2019-08-25 17:58:46 +02:00
|
|
|
|
2019-08-31 15:05:50 +02:00
|
|
|
application = status.data.get("application") or {}
|
|
|
|
application = application.get("name")
|
|
|
|
|
2023-07-07 13:57:19 +02:00
|
|
|
yield ("pack", urwid.AttrWrap(urwid.Divider("-"), "dim"))
|
2022-12-08 01:57:56 +01:00
|
|
|
|
2022-12-11 23:26:15 +01:00
|
|
|
translated_from = (
|
2023-03-04 22:04:13 +01:00
|
|
|
language_name(status.original.translated_from)
|
|
|
|
if status.original.show_translation and status.original.translated_from
|
2022-12-11 23:26:15 +01:00
|
|
|
else None
|
|
|
|
)
|
2022-12-08 01:57:56 +01:00
|
|
|
|
2022-12-22 00:30:29 +01:00
|
|
|
visibility_colors = {
|
2023-07-07 13:57:19 +02:00
|
|
|
"public": "visibility_public",
|
|
|
|
"unlisted": "visibility_unlisted",
|
|
|
|
"private": "visibility_private",
|
|
|
|
"direct": "visibility_direct"
|
2022-12-22 00:30:29 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 07:48:53 +01:00
|
|
|
visibility = status.visibility.title()
|
2023-07-07 13:57:19 +02:00
|
|
|
visibility_color = visibility_colors.get(status.visibility, "dim")
|
2022-12-28 07:48:53 +01:00
|
|
|
|
2019-08-27 15:08:40 +02:00
|
|
|
yield ("pack", urwid.Text([
|
2023-07-07 13:57:19 +02:00
|
|
|
("status_detail_timestamp", f"{status.created_at.strftime('%Y-%m-%d %H:%M')} "),
|
|
|
|
("status_detail_bookmarked" if status.bookmarked else "dim", "b "),
|
|
|
|
("dim", f"⤶ {status.data['replies_count']} "),
|
|
|
|
("highlight" if status.reblogged else "dim", f"♺ {status.data['reblogs_count']} "),
|
|
|
|
("highlight" if status.favourited else "dim", f"★ {status.data['favourites_count']}"),
|
2022-12-28 07:48:53 +01:00
|
|
|
(visibility_color, f" · {visibility}"),
|
2023-07-07 13:57:19 +02:00
|
|
|
("highlight", f" · Translated from {translated_from} " if translated_from else ""),
|
|
|
|
("dim", f" · {application}" if application else ""),
|
2019-08-27 15:08:40 +02:00
|
|
|
]))
|
|
|
|
|
2019-08-25 17:58:46 +02:00
|
|
|
# Push things to bottom
|
2023-01-09 05:20:33 +01:00
|
|
|
yield ("weight", 1, urwid.BoxAdapter(urwid.SolidFill(" "), 1))
|
2019-08-24 12:53:55 +02:00
|
|
|
|
2019-08-27 14:59:06 +02:00
|
|
|
def build_linebox(self, contents):
|
|
|
|
contents = urwid.Pile(list(contents))
|
|
|
|
contents = urwid.Padding(contents, left=1, right=1)
|
|
|
|
return urwid.LineBox(contents)
|
|
|
|
|
2019-08-24 12:53:55 +02:00
|
|
|
def card_generator(self, card):
|
2023-07-07 13:57:19 +02:00
|
|
|
yield urwid.Text(("card_title", card["title"].strip()))
|
2019-09-07 20:48:11 +02:00
|
|
|
if card.get("author_name"):
|
2023-07-07 13:57:19 +02:00
|
|
|
yield urwid.Text(["by ", ("card_author", card["author_name"].strip())])
|
2019-08-24 14:14:46 +02:00
|
|
|
yield urwid.Text("")
|
|
|
|
if card["description"]:
|
|
|
|
yield urwid.Text(card["description"].strip())
|
|
|
|
yield urwid.Text("")
|
2023-11-16 11:46:54 +01:00
|
|
|
yield url_to_widget(card["url"])
|
2019-08-24 12:53:55 +02:00
|
|
|
|
2019-08-27 14:34:51 +02:00
|
|
|
def poll_generator(self, poll):
|
2022-11-28 03:27:02 +01:00
|
|
|
for idx, option in enumerate(poll["options"]):
|
2019-08-27 14:34:51 +02:00
|
|
|
perc = (round(100 * option["votes_count"] / poll["votes_count"])
|
|
|
|
if poll["votes_count"] else 0)
|
2022-11-28 03:27:02 +01:00
|
|
|
|
|
|
|
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)
|
2019-08-31 15:06:17 +02:00
|
|
|
yield urwid.ProgressBar("", "poll_bar", perc)
|
2019-08-27 14:34:51 +02:00
|
|
|
|
2022-11-12 07:55:25 +01:00
|
|
|
status = "Poll · {} votes".format(poll["votes_count"])
|
|
|
|
|
2019-08-27 14:34:51 +02:00
|
|
|
if poll["expired"]:
|
2022-11-12 07:55:25 +01:00
|
|
|
status += " · Closed"
|
|
|
|
|
|
|
|
if poll["expires_at"]:
|
2019-08-27 14:34:51 +02:00
|
|
|
expires_at = parse_datetime(poll["expires_at"]).strftime("%Y-%m-%d %H:%M")
|
2022-11-12 07:55:25 +01:00
|
|
|
status += " · Closes on {}".format(expires_at)
|
2019-08-27 14:34:51 +02:00
|
|
|
|
2023-07-07 13:57:19 +02:00
|
|
|
yield urwid.Text(("dim", status))
|
2019-08-27 14:34:51 +02:00
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
class StatusListItem(SelectableColumns):
|
2023-07-25 09:32:17 +02:00
|
|
|
def __init__(self, status, relative_datetimes):
|
2023-02-12 09:41:04 +01:00
|
|
|
edited_at = status.data.get("edited_at")
|
2023-01-29 09:23:57 +01:00
|
|
|
|
|
|
|
# TODO: hacky implementation to avoid creating conflicts for existing
|
|
|
|
# pull reuqests, refactor when merged.
|
|
|
|
created_at = (
|
|
|
|
time_ago(status.created_at).ljust(3, " ")
|
2023-07-25 09:32:17 +02:00
|
|
|
if relative_datetimes
|
2023-01-29 09:23:57 +01:00
|
|
|
else status.created_at.strftime("%Y-%m-%d %H:%M")
|
|
|
|
)
|
|
|
|
|
2023-02-12 09:41:04 +01:00
|
|
|
edited_flag = "*" if edited_at else " "
|
2023-07-07 13:57:19 +02:00
|
|
|
favourited = ("highlight", "★") if status.original.favourited else " "
|
|
|
|
reblogged = ("highlight", "♺") if status.original.reblogged else " "
|
|
|
|
is_reblog = ("dim", "♺") if status.reblog else " "
|
|
|
|
is_reply = ("dim", "⤶") if status.original.in_reply_to else " "
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
return super().__init__([
|
2023-07-07 13:57:19 +02:00
|
|
|
("pack", SelectableText(("status_list_timestamp", created_at), wrap="clip")),
|
|
|
|
("pack", urwid.Text(("status_list_timestamp", edited_flag))),
|
2019-08-24 11:20:31 +02:00
|
|
|
("pack", urwid.Text(" ")),
|
|
|
|
("pack", urwid.Text(favourited)),
|
2019-08-24 12:53:55 +02:00
|
|
|
("pack", urwid.Text(" ")),
|
2019-08-24 11:20:31 +02:00
|
|
|
("pack", urwid.Text(reblogged)),
|
2019-08-27 14:55:02 +02:00
|
|
|
("pack", urwid.Text(" ")),
|
2023-07-07 13:57:19 +02:00
|
|
|
urwid.Text(("status_list_account", status.original.account), wrap="clip"),
|
2019-08-31 15:24:03 +02:00
|
|
|
("pack", urwid.Text(is_reply)),
|
2019-09-09 10:43:13 +02:00
|
|
|
("pack", urwid.Text(is_reblog)),
|
2019-08-31 15:24:03 +02:00
|
|
|
("pack", urwid.Text(" ")),
|
2019-08-24 11:20:31 +02:00
|
|
|
])
|