Skip to content

Commit

Permalink
refactor: views: Get message links via create_link_buttons().
Browse files Browse the repository at this point in the history
This commit adds a static method to create link buttons in message
information popup.

Tests added.
  • Loading branch information
Ezio-Sarthak committed Apr 8, 2021
1 parent c597740 commit 8b0c634
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
30 changes: 30 additions & 0 deletions tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,36 @@ def test_height_reactions(self, message_fixture, to_vary_in_each_message):
expected_height = 9
assert self.msg_info_view.height == expected_height

@pytest.mark.parametrize([
'initial_link',
'expected_text',
'expected_attr_map',
'expected_focus_map',
'expected_link_width'
], [(
OrderedDict([('https://bar.com', ('Foo', 1, True))]),
'1: Foo\nhttps://bar.com',
{None: 'popup_contrast'},
{None: 'selected'},
15,
)],
)
def test_create_link_buttons(self, initial_link, expected_text,
expected_attr_map, expected_focus_map,
expected_link_width):
[link_w], link_width = self.msg_info_view.create_link_buttons(
self.controller, initial_link,
)

assert [link_w.link] == list(initial_link)
assert (link_w._wrapped_widget.original_widget.text
== expected_text)
assert (link_w._wrapped_widget.focus_map
== expected_focus_map)
assert (link_w._wrapped_widget.attr_map
== expected_attr_map)
assert link_width == expected_link_width

def test_keypress_navigation(self, mocker, widget_size,
navigation_key_expected_key_pair):
key, expected_key = navigation_key_expected_key_pair
Expand Down
47 changes: 33 additions & 14 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,31 +1272,50 @@ def __init__(self, controller: Any, msg: Message, title: str,
len(title))
widgets = self.make_table_with_categories(msg_info, column_widths)

if message_links:
message_link_widgets = []
message_link_width = 0
for index, link in enumerate(message_links):
text, link_index, _ = message_links[link]
caption = f"{link_index}: {text}\n{link}"
message_link_width = max(
message_link_width,
len(max(caption.split('\n'), key=len))
)
# To keep track of buttons (e.g., button links) and to facilitate
# computing their slice indexes
button_widgets = [] # type: List[Any]

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

# slice_index = Number of labels before message links + 1 newline
# + 1 'Message Links' category label.
slice_index = len(msg_info[0][1]) + 2
slice_index += sum([len(w) + 2 for w in button_widgets])
button_widgets.append(message_links)

widgets = (widgets[:slice_index] + message_link_widgets
+ widgets[slice_index:])
popup_width = max(popup_width, message_link_width)

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

@staticmethod
def create_link_buttons(
controller: Any,
links: 'OrderedDict[str, Tuple[str, int, bool]]'
) -> Tuple[List[MessageLinkButton], int]:
link_widgets = []
link_width = 0

for index, link in enumerate(links):
text, link_index, _ = links[link]
caption = f"{link_index}: {text}\n{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(controller, caption, link, display_attr)
)

return link_widgets, 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 8b0c634

Please sign in to comment.