Highlight followed tags
This commit is contained in:
parent
15d377e889
commit
88c444c411
|
@ -113,6 +113,7 @@ class TUI(urwid.Frame):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.loop.set_alarm_in(0, lambda *args: self.async_load_instance())
|
self.loop.set_alarm_in(0, lambda *args: self.async_load_instance())
|
||||||
|
self.loop.set_alarm_in(0, lambda *args: self.async_load_followed_tags())
|
||||||
self.loop.set_alarm_in(0, lambda *args: self.async_load_timeline(
|
self.loop.set_alarm_in(0, lambda *args: self.async_load_timeline(
|
||||||
is_initial=True, timeline_name="home"))
|
is_initial=True, timeline_name="home"))
|
||||||
self.loop.run()
|
self.loop.run()
|
||||||
|
@ -237,7 +238,7 @@ class TUI(urwid.Frame):
|
||||||
self.loop.set_alarm_in(5, lambda *args: self.footer.clear_message())
|
self.loop.set_alarm_in(5, lambda *args: self.footer.clear_message())
|
||||||
config.save_config(self.config)
|
config.save_config(self.config)
|
||||||
|
|
||||||
timeline = Timeline(name, statuses, self.can_translate)
|
timeline = Timeline(name, statuses, self.can_translate, self.followed_tags)
|
||||||
|
|
||||||
self.connect_default_timeline_signals(timeline)
|
self.connect_default_timeline_signals(timeline)
|
||||||
urwid.connect_signal(timeline, "next", _next)
|
urwid.connect_signal(timeline, "next", _next)
|
||||||
|
@ -266,8 +267,9 @@ class TUI(urwid.Frame):
|
||||||
statuses = ancestors + [status] + descendants
|
statuses = ancestors + [status] + descendants
|
||||||
focus = len(ancestors)
|
focus = len(ancestors)
|
||||||
|
|
||||||
timeline = Timeline("thread", statuses, self.can_translate, focus,
|
timeline = Timeline("thread", statuses, self.can_translate,
|
||||||
is_thread=True)
|
self.followed_tags, focus, is_thread=True)
|
||||||
|
|
||||||
self.connect_default_timeline_signals(timeline)
|
self.connect_default_timeline_signals(timeline)
|
||||||
urwid.connect_signal(timeline, "close", _close)
|
urwid.connect_signal(timeline, "close", _close)
|
||||||
|
|
||||||
|
@ -334,6 +336,28 @@ class TUI(urwid.Frame):
|
||||||
|
|
||||||
return self.run_in_thread(_load_instance, done_callback=_done)
|
return self.run_in_thread(_load_instance, done_callback=_done)
|
||||||
|
|
||||||
|
def async_load_followed_tags(self):
|
||||||
|
def _load_tag_list():
|
||||||
|
logger.info("Loading tags")
|
||||||
|
try:
|
||||||
|
return api.followed_tags(self.app, self.user)
|
||||||
|
except:
|
||||||
|
# not supported by all Mastodon servers so fail silently if necessary
|
||||||
|
return []
|
||||||
|
|
||||||
|
def _done_tag_list(tags):
|
||||||
|
if len(tags) > 0:
|
||||||
|
self.followed_tags = [t["name"] for t in tags]
|
||||||
|
else:
|
||||||
|
self.followed_tags = []
|
||||||
|
logger.info("Loaded tags. Followed tags = {}".format(self.followed_tags))
|
||||||
|
|
||||||
|
self.run_in_thread(
|
||||||
|
_load_tag_list, done_callback=_done_tag_list
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def refresh_footer(self, timeline):
|
def refresh_footer(self, timeline):
|
||||||
"""Show status details in footer."""
|
"""Show status details in footer."""
|
||||||
status, index, count = timeline.get_focused_status_with_counts()
|
status, index, count = timeline.get_focused_status_with_counts()
|
||||||
|
|
|
@ -19,6 +19,7 @@ PALETTE = [
|
||||||
|
|
||||||
# Functional
|
# Functional
|
||||||
('hashtag', 'light cyan,bold', ''),
|
('hashtag', 'light cyan,bold', ''),
|
||||||
|
('followed_hashtag', 'yellow,bold', ''),
|
||||||
('link', ',italics', ''),
|
('link', ',italics', ''),
|
||||||
('link_focused', ',italics', 'dark magenta'),
|
('link_focused', ',italics', 'dark magenta'),
|
||||||
|
|
||||||
|
|
|
@ -36,16 +36,17 @@ class Timeline(urwid.Columns):
|
||||||
"clear-screen", # Clear the screen (used internally)
|
"clear-screen", # Clear the screen (used internally)
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, name, statuses, can_translate, focus=0, is_thread=False):
|
def __init__(self, name, statuses, can_translate, followed_tags=[], focus=0, is_thread=False):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.is_thread = is_thread
|
self.is_thread = is_thread
|
||||||
self.statuses = statuses
|
self.statuses = statuses
|
||||||
self.can_translate = can_translate
|
self.can_translate = can_translate
|
||||||
self.status_list = self.build_status_list(statuses, focus=focus)
|
self.status_list = self.build_status_list(statuses, focus=focus)
|
||||||
|
self.followed_tags = followed_tags
|
||||||
try:
|
try:
|
||||||
self.status_details = StatusDetails(statuses[focus], is_thread, can_translate)
|
self.status_details = StatusDetails(statuses[focus], is_thread, can_translate, followed_tags)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
self.status_details = StatusDetails(None, is_thread, can_translate)
|
self.status_details = StatusDetails(None, is_thread, can_translate, followed_tags)
|
||||||
|
|
||||||
super().__init__([
|
super().__init__([
|
||||||
("weight", 40, self.status_list),
|
("weight", 40, self.status_list),
|
||||||
|
@ -103,7 +104,7 @@ class Timeline(urwid.Columns):
|
||||||
self.draw_status_details(status)
|
self.draw_status_details(status)
|
||||||
|
|
||||||
def draw_status_details(self, status):
|
def draw_status_details(self, status):
|
||||||
self.status_details = StatusDetails(status, self.is_thread, self.can_translate)
|
self.status_details = StatusDetails(status, self.is_thread, self.can_translate, self.followed_tags)
|
||||||
self.contents[2] = urwid.Padding(self.status_details, left=1), ("weight", 60, False)
|
self.contents[2] = urwid.Padding(self.status_details, left=1), ("weight", 60, False)
|
||||||
|
|
||||||
def keypress(self, size, key):
|
def keypress(self, size, key):
|
||||||
|
@ -245,7 +246,7 @@ class Timeline(urwid.Columns):
|
||||||
|
|
||||||
|
|
||||||
class StatusDetails(urwid.Pile):
|
class StatusDetails(urwid.Pile):
|
||||||
def __init__(self, status, in_thread, can_translate=False):
|
def __init__(self, status, in_thread, can_translate=False, followed_tags=[]):
|
||||||
"""
|
"""
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
@ -257,6 +258,7 @@ class StatusDetails(urwid.Pile):
|
||||||
"""
|
"""
|
||||||
self.in_thread = in_thread
|
self.in_thread = in_thread
|
||||||
self.can_translate = can_translate
|
self.can_translate = can_translate
|
||||||
|
self.followed_tags = followed_tags
|
||||||
reblogged_by = status.author if status and status.reblog else None
|
reblogged_by = status.author if status and status.reblog else None
|
||||||
widget_list = list(self.content_generator(status.original, reblogged_by)
|
widget_list = list(self.content_generator(status.original, reblogged_by)
|
||||||
if status else ())
|
if status else ())
|
||||||
|
@ -284,7 +286,7 @@ class StatusDetails(urwid.Pile):
|
||||||
else:
|
else:
|
||||||
content = status.translation if status.show_translation else status.data["content"]
|
content = status.translation if status.show_translation else status.data["content"]
|
||||||
for line in format_content(content):
|
for line in format_content(content):
|
||||||
yield ("pack", urwid.Text(highlight_hashtags(line)))
|
yield ("pack", urwid.Text(highlight_hashtags(line, self.followed_tags)))
|
||||||
|
|
||||||
media = status.data["media_attachments"]
|
media = status.data["media_attachments"]
|
||||||
if media:
|
if media:
|
||||||
|
|
|
@ -51,11 +51,18 @@ def highlight_keys(text, high_attr, low_attr=""):
|
||||||
return list(_gen())
|
return list(_gen())
|
||||||
|
|
||||||
|
|
||||||
def highlight_hashtags(line, attr="hashtag"):
|
def highlight_hashtags(line, followed_tags, attr="hashtag",\
|
||||||
return [
|
followed_attr="followed_hashtag"):
|
||||||
(attr, p) if p.startswith("#") else p
|
hline = []
|
||||||
for p in re.split(HASHTAG_PATTERN, line)
|
for p in re.split(HASHTAG_PATTERN, line):
|
||||||
]
|
if p.startswith("#"):
|
||||||
|
if p[1:].lower() in (t.lower() for t in followed_tags):
|
||||||
|
hline.append((followed_attr,p))
|
||||||
|
else:
|
||||||
|
hline.append((attr,p))
|
||||||
|
else:
|
||||||
|
hline.append(p)
|
||||||
|
return hline
|
||||||
|
|
||||||
|
|
||||||
def show_media(paths):
|
def show_media(paths):
|
||||||
|
|
Loading…
Reference in New Issue