From 8b0c634cc11f99977583956cb8cbf009b0d26078 Mon Sep 17 00:00:00 2001 From: Ezio-Sarthak Date: Tue, 12 Jan 2021 18:15:52 +0530 Subject: [PATCH] refactor: views: Get message links via create_link_buttons(). This commit adds a static method to create link buttons in message information popup. Tests added. --- tests/ui_tools/test_popups.py | 30 +++++++++++++++++++++ zulipterminal/ui_tools/views.py | 47 +++++++++++++++++++++++---------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/tests/ui_tools/test_popups.py b/tests/ui_tools/test_popups.py index a8d95e1c12..3ccb69264e 100644 --- a/tests/ui_tools/test_popups.py +++ b/tests/ui_tools/test_popups.py @@ -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 diff --git a/zulipterminal/ui_tools/views.py b/zulipterminal/ui_tools/views.py index 53f9598d87..54d9911ab5 100644 --- a/zulipterminal/ui_tools/views.py +++ b/zulipterminal/ui_tools/views.py @@ -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):