Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Empty Narrows #1543

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
58 changes: 56 additions & 2 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ def test_stream_muting_confirmation_popup(
"search_within_topic_narrow",
],
)
@pytest.mark.parametrize("msg_ids", [({200, 300, 400}), (set()), ({100})])
def test_search_message(
@pytest.mark.parametrize("msg_ids", [({200, 300, 400}), ({100})])
def test_search_message__hits(
self,
initial_narrow: List[Any],
final_narrow: List[Any],
Expand Down Expand Up @@ -550,6 +550,60 @@ def set_msg_ids(*args: Any, **kwargs: Any) -> None:
create_msg.assert_called_once_with(controller.model, msg_ids)
assert controller.model.index == dict(index_search_messages, search=msg_ids)

@pytest.mark.parametrize(
"initial_narrow, final_narrow",
[
([], [["search", "FOO"]]),
([["search", "BOO"]], [["search", "FOO"]]),
([["stream", "PTEST"]], [["stream", "PTEST"], ["search", "FOO"]]),
(
[["pm-with", "[email protected]"], ["search", "BOO"]],
[["pm-with", "[email protected]"], ["search", "FOO"]],
),
(
[["stream", "PTEST"], ["topic", "RDS"]],
[["stream", "PTEST"], ["topic", "RDS"], ["search", "FOO"]],
),
],
ids=[
"Default_all_msg_search",
"redo_default_search",
"search_within_stream",
"pm_search_again",
"search_within_topic_narrow",
],
)
def test_search_message__no_hits(
self,
initial_narrow: List[Any],
final_narrow: List[Any],
controller: Controller,
mocker: MockerFixture,
index_search_messages: Index,
msg_ids: Set[int] = set(),
) -> None:
get_message = mocker.patch(MODEL + ".get_messages")
create_msg = mocker.patch(MODULE + ".create_msg_box_list")
mocker.patch(MODEL + ".get_message_ids_in_current_narrow", return_value=msg_ids)
controller.model.index = index_search_messages # Any initial search index
controller.view.message_view = mocker.patch("urwid.ListBox")
controller.model.narrow = initial_narrow

def set_msg_ids(*args: Any, **kwargs: Any) -> None:
controller.model.index["search"].update(msg_ids)

get_message.side_effect = set_msg_ids
assert controller.model.index["search"] == {500}

controller.search_messages("FOO")

assert controller.model.narrow == final_narrow
get_message.assert_called_once_with(
num_after=0, num_before=30, anchor=10000000000
)
create_msg.assert_not_called()
assert controller.model.index == dict(index_search_messages, search=msg_ids)

@pytest.mark.parametrize(
"screen_size, expected_popup_size",
[
Expand Down
5 changes: 4 additions & 1 deletion zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,20 +504,23 @@ def show_media_confirmation_popup(
self, question, callback, location="center"
)

def search_messages(self, text: str) -> None:
def search_messages(self, text: str) -> bool:
# Search for a text in messages
self.model.index["search"].clear()
self.model.set_search_narrow(text)

self.model.get_messages(num_after=0, num_before=30, anchor=10000000000)
msg_id_list = self.model.get_message_ids_in_current_narrow()
if len(msg_id_list) == 0:
return False

w_list = create_msg_box_list(self.model, msg_id_list)
self.view.message_view.log.clear()
self.view.message_view.log.extend(w_list)
focus_position = 0
if 0 <= focus_position < len(w_list):
self.view.message_view.set_focus(focus_position)
return True

def save_draft_confirmation_popup(self, draft: Composition) -> None:
question = urwid.Text(
Expand Down
8 changes: 5 additions & 3 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,9 +1032,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
return key

elif is_command_key("EXECUTE_SEARCH", key):
self.controller.exit_editor_mode()
self.controller.search_messages(self.text_box.edit_text)
self.controller.view.middle_column.set_focus("body")
if self.controller.search_messages(self.text_box.edit_text):
self.controller.exit_editor_mode()
self.controller.view.middle_column.set_focus("body")
else:
self.controller.report_error(["No results found."])
Comment on lines -1035 to +1039
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if we want to take this approach rather than just have the empty result like the rest of this branch is pursuing, but it's an interesting extra change - I assume this is extra to the original version?

return key

key = super().keypress(size, key)
Expand Down
Loading