Commit af41834a authored by Lysander Trischler's avatar Lysander Trischler
Browse files

Refactor conversation rendering into mixin for reuse

parent b16a7f2c
......@@ -21,30 +21,18 @@ manager = twtxtmanager.TwtxtManager(config)
manager.load_twts()
renderer = twtxtrenderer.Renderer(config)
class TwtsListBox(widgets.VimListBox):
def __init__(self, manager, cmdline):
self._manager = manager
self._cmdline = cmdline
self._twts = []
self._search = None
self._search_downwards = True
items = []
def render(twt, level=0, indentation="", more_siblings=False):
widget, next_indentation = self.format_twt(twt, level, indentation, more_siblings)
items.append(widget)
self._twts.append(twt)
for i, reply in enumerate(twt.replies):
render(reply, level=level + 1, indentation=next_indentation, more_siblings=i < len(twt.replies) - 1)
for twt in reversed(manager.conversation_tree):
render(twt)
class TwtFormatter:
super().__init__(urwid.SimpleFocusListWalker(items))
def render_conversation(self, manager, twt, into_widgets, into_twts, level=0, indentation="", more_siblings=False):
widget, next_indentation = self.format_twt(manager, twt, level, indentation, more_siblings)
into_widgets.append(widget)
into_twts.append(twt)
for i, reply in enumerate(twt.replies):
self.render_conversation(manager, reply, into_widgets, into_twts, level=level + 1,
indentation=next_indentation, more_siblings=i < len(twt.replies) - 1)
def format_twt(self, twt, level, indentation, more_siblings):
def format_twt(self, manager, twt, level, indentation, more_siblings):
created_at = "%s (%s)" % (twt.created_at.astimezone().strftime('%Y-%m-%d %H:%M'),
twt.relative_datetime \
.replace("seconds", "s") \
......@@ -75,9 +63,9 @@ class TwtsListBox(widgets.VimListBox):
if token.nick:
nick = token.nick
url = token.url
following = self._manager.is_following(nick, url)
following = manager.is_following(nick, url)
elif token.url:
nick = self._manager.resolve_nick_by_url_from_cache(token.url)
nick = manager.resolve_nick_by_url_from_cache(token.url)
url = token.url
following = nick is not None
else:
......@@ -149,13 +137,28 @@ class TwtsListBox(widgets.VimListBox):
return (widgets.SelectableAttrMap(urwid.Columns(filter(lambda x: x, [
(28, urwid.Text(created_at)),
(self._manager.max_author_nick_width, urwid.Text(nick, align="right")),
(manager.max_author_nick_width, urwid.Text(nick, align="right")),
text]), dividechars=2),
renderer.read_normal_map if twt.read else renderer.unread_normal_map,
renderer.read_focus_map if twt.read else renderer.unread_focus_map),
next_indentation)
class TwtsListBox(widgets.VimListBox, TwtFormatter):
def __init__(self, manager, cmdline):
self._manager = manager
self._cmdline = cmdline
self._twts = []
self._search = None
self._search_downwards = True
widgets = []
for twt in reversed(manager.conversation_tree):
self.render_conversation(manager, twt, widgets, self._twts)
super().__init__(urwid.SimpleFocusListWalker(widgets))
def keypress(self, size, key):
if key == "r":
self.toggle_read()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment