Skip to content

Commit

Permalink
views: Append topic links in message info view.
Browse files Browse the repository at this point in the history
The topic and message links is generalised with a member function.
It sets the desired dimensions and widgets (now member variables)
and handles topic links if present.

Tests amended.
Fixes #709.
  • Loading branch information
Ezio-Sarthak committed Jan 6, 2021
1 parent a3a0181 commit e639e97
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,8 @@ def __init__(self, controller: Any, msg: Message, title: str,
('Edit History', 'Press {} to view'.format(keys))
)
# Render the category using the existing table methods if links exist.
if topic_links:
msg_info.append(('Topic Links', []))
if message_links:
msg_info.append(('Message Links', []))
if time_mentions:
Expand All @@ -1167,32 +1169,39 @@ def __init__(self, controller: Any, msg: Message, title: str,
self.widgets = (self.make_table_with_categories(
msg_info, column_widths))

# slice_index = Number of labels before links + 1 newline
# + 1 'Links' category label.
self.slice_index = len(msg_info[0][1]) + 2
if topic_links:
self.set_links(topic_links)
if message_links:
message_link_widgets = []
message_link_width = 0
for index, link in enumerate(message_links):
text, link_index, _ = message_links[link]
caption = '{}: {}\n{}'.format(link_index, text, link)
message_link_width = max(
message_link_width,
len(max(caption.split('\n'), key=len))
)

display_attr = None if index % 2 else 'popup_contrast'
message_link_widgets.append(
MessageLinkButton(controller, caption, link, display_attr)
)

# slice_index = Number of labels before message links + 1 newline
# + 1 'Message Links' category label.
slice_index = len(msg_info[0][1]) + 2
self.widgets = (self.widgets[:slice_index] + message_link_widgets
+ self.widgets[slice_index:])
self.popup_width = max(self.popup_width, message_link_width)
if topic_links:
self.slice_index += (len(topic_links) + 2)
self.set_links(message_links)

super().__init__(self.controller, self.widgets, 'MSG_INFO',
self.popup_width, title)

def set_links(self,
links: 'OrderedDict[str, Tuple[str, int, bool]]') -> None:
link_widgets = []
link_width = 0
for index, link in enumerate(links):
text, link_index, _ = links[link]
caption = '{}: {}\n{}'.format(link_index, text, link)
link_width = max(
link_width,
len(max(caption.split('\n'), key=len))
)

display_attr = None if index % 2 else 'popup_contrast'
link_widgets.append(
MessageLinkButton(self.controller, caption, link, display_attr)
)
self.widgets = (self.widgets[:self.slice_index] + link_widgets
+ self.widgets[self.slice_index:])
self.popup_width = max(self.popup_width, link_width)

def keypress(self, size: urwid_Size, key: str) -> str:
if (is_command_key('EDIT_HISTORY', key)
and self.show_edit_history_label):
Expand Down

0 comments on commit e639e97

Please sign in to comment.