Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/ui_tools/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def test_soup2markup(self, content, expected_markup, mocker):
metadata = dict(
server_url=SERVER_URL,
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
bq_len=0,
)
Expand Down
67 changes: 67 additions & 0 deletions tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def mock_external_classes(self, mocker: MockerFixture, msg_box: MessageBox) -> N
message=self.message,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
title="Full Rendered Message",
)
Expand Down Expand Up @@ -443,6 +444,7 @@ def test_keypress_show_msg_info(
msg=self.message,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)

Expand All @@ -467,6 +469,7 @@ def mock_external_classes(self, mocker: MockerFixture, msg_box: MessageBox) -> N
message=self.message,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
title="Full Raw Message",
)
Expand Down Expand Up @@ -519,6 +522,7 @@ def test_keypress_show_msg_info(
msg=self.message,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)

Expand All @@ -542,6 +546,7 @@ def mock_external_classes(self, mocker: MockerFixture) -> None:
message=self.message,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
title="Edit History",
)
Expand Down Expand Up @@ -590,6 +595,7 @@ def test_keypress_show_msg_info(
msg=self.message,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)

Expand Down Expand Up @@ -868,6 +874,7 @@ def mock_external_classes(
OrderedDict(),
OrderedDict(),
list(),
list(),
)

def test_init(self, message_fixture: Message) -> None:
Expand All @@ -885,6 +892,7 @@ def test_pop_up_info_order(self, message_fixture: Message) -> None:
title="Message Information",
topic_links=topic_links,
message_links=message_links,
code_snippets=list(),
time_mentions=list(),
)
msg_links = msg_info_view.button_widgets
Expand Down Expand Up @@ -933,6 +941,7 @@ def test_keypress_edit_history(
title="Message Information",
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)
size = widget_size(msg_info_view)
Expand All @@ -944,6 +953,7 @@ def test_keypress_edit_history(
message=message_fixture,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)
else:
Expand All @@ -962,6 +972,7 @@ def test_keypress_full_rendered_message(
title="Message Information",
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)
size = widget_size(msg_info_view)
Expand All @@ -972,6 +983,7 @@ def test_keypress_full_rendered_message(
message=message_fixture,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)

Expand All @@ -988,6 +1000,7 @@ def test_keypress_full_raw_message(
title="Message Information",
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)
size = widget_size(msg_info_view)
Expand All @@ -998,6 +1011,7 @@ def test_keypress_full_raw_message(
message=message_fixture,
topic_links=OrderedDict(),
message_links=OrderedDict(),
code_snippets=list(),
time_mentions=list(),
)

Expand Down Expand Up @@ -1100,6 +1114,7 @@ def test_height_reactions(
OrderedDict(),
OrderedDict(),
list(),
list(),
)
# 12 = 7 labels + 2 blank lines + 1 'Reactions' (category)
# + 4 reactions (excluding 'Message Links').
Expand Down Expand Up @@ -1153,6 +1168,58 @@ def test_create_link_buttons(
assert link_w._wrapped_widget.attr_map == expected_attr_map
assert link_width == expected_link_width

@pytest.mark.parametrize(
[
"initial_code_snippet",
"expected_code",
"expected_attr_map",
"expected_focus_map",
"expected_code_width",
],
[
(
[
(
"Python",
[
("pygments:k", "def"),
("pygments:w", " "),
("pygments:nf", "main"),
("pygments:p", "()"),
("pygments:w", "\n "),
("pygments:nb", "print"),
("pygments:p", "("),
("pygments:s2", '"Hello"'),
("pygments:p", ")"),
("pygments:w", "\n"),
],
)
],
'1: Python\ndef main()\n print("Hello")...',
{None: "popup_contrast"},
{None: "selected"},
95,
)
],
ids=["with_code_snippet"],
)
def test_create_code_snippet_buttons(
self,
initial_code_snippet: List[Tuple[str, List[Tuple[str, str]]]],
expected_code: str,
expected_attr_map: Dict[None, str],
expected_focus_map: Dict[None, str],
expected_code_width: int,
) -> None:
[code_w], copy_code_width = self.msg_info_view.create_code_snippet_buttons(
self.controller, initial_code_snippet
)

assert code_w._wrapped_widget.original_widget.text == expected_code
assert code_w._wrapped_widget.focus_map == expected_focus_map
assert code_w._wrapped_widget.attr_map == expected_attr_map
assert copy_code_width == expected_code_width


class TestStreamInfoView:
@pytest.fixture(autouse=True)
Expand Down
8 changes: 8 additions & 0 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def show_msg_info(
msg: Message,
topic_links: Dict[str, Tuple[str, int, bool]],
message_links: Dict[str, Tuple[str, int, bool]],
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
time_mentions: List[Tuple[str, str]],
) -> None:
msg_info_view = MsgInfoView(
Expand All @@ -287,6 +288,7 @@ def show_msg_info(
"Message Information (up/down scrolls)",
topic_links,
message_links,
code_snippets,
time_mentions,
)
self.show_pop_up(msg_info_view, "area:msg")
Expand Down Expand Up @@ -340,6 +342,7 @@ def show_full_rendered_message(
message: Message,
topic_links: Dict[str, Tuple[str, int, bool]],
message_links: Dict[str, Tuple[str, int, bool]],
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
time_mentions: List[Tuple[str, str]],
) -> None:
self.show_pop_up(
Expand All @@ -348,6 +351,7 @@ def show_full_rendered_message(
message,
topic_links,
message_links,
code_snippets,
time_mentions,
"Full rendered message (up/down scrolls)",
),
Expand All @@ -359,6 +363,7 @@ def show_full_raw_message(
message: Message,
topic_links: Dict[str, Tuple[str, int, bool]],
message_links: Dict[str, Tuple[str, int, bool]],
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
time_mentions: List[Tuple[str, str]],
) -> None:
self.show_pop_up(
Expand All @@ -367,6 +372,7 @@ def show_full_raw_message(
message,
topic_links,
message_links,
code_snippets,
time_mentions,
"Full raw message (up/down scrolls)",
),
Expand All @@ -378,6 +384,7 @@ def show_edit_history(
message: Message,
topic_links: Dict[str, Tuple[str, int, bool]],
message_links: Dict[str, Tuple[str, int, bool]],
code_snippets: List[Tuple[str, List[Tuple[str, str]]]],
time_mentions: List[Tuple[str, str]],
) -> None:
self.show_pop_up(
Expand All @@ -386,6 +393,7 @@ def show_edit_history(
message,
topic_links,
message_links,
code_snippets,
time_mentions,
"Edit History (up/down scrolls)",
),
Expand Down
53 changes: 53 additions & 0 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,59 @@ def handle_narrow_link(self) -> None:
self.controller.exit_popup()


class CodeSnippetButton(urwid.Button):
def __init__(
self,
*,
controller: Any,
caption: str,
display_code: List[Tuple[str, str]],
copy_code: str,
display_attr: Optional[str],
) -> None:
self.controller = controller
self.model = self.controller.model
self.view = self.controller.view
self.caption = caption
self.display_code = display_code
self.copy_code = copy_code

super().__init__("")

self.update_widget(display_code, display_attr)
urwid.connect_signal(self, "click", self.copy_to_clipboard)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it work for clicks, but not for the keypress. See eg. the StreamButton.


def update_widget(
self, display_code: List[Tuple[str, str]], display_attr: Optional[str] = None
) -> None:
"""
Overrides the existing button widget for custom styling.
"""
# Set cursor position next to len(caption) to avoid the cursor.
icon = urwid.SelectableIcon(display_code, cursor_position=len(display_code) + 1)
self._w = urwid.AttrMap(icon, display_attr, focus_map="selected")
Comment on lines +678 to +680
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to be working for this button. I see a flashing cursor on the button while testing.


def copy_to_clipboard(self, *_: Any) -> None:
self.controller.copy_to_clipboard(self.copy_code, "code")

def get_code_from_snippet(
self, snippet_list: List[Tuple[str, str]]
) -> Tuple[List[Tuple[str, str]], str]:
"""
Returns two lines of code from the given snippet list
as to summarize the code and
returns code in text format to be copied.
"""
snippet = snippet_list[:]
no_of_lines = 0
code = "".join(snip[1] for snip in snippet)
for index, snip in enumerate(snippet):
no_of_lines += 1 if "\n" in snip[1] else 0
if no_of_lines == 2:
return snippet[:index] + [("pygments:w", "...")], code
return snippet, code


class EditModeButton(urwid.Button):
def __init__(self, *, controller: Any, width: int) -> None:
self.controller = controller
Expand Down
Loading