Skip to content

Commit 04a83f8

Browse files
committed
refactor: ui_tools/helper: Add new enum Search Status.
The existing empty_search boolean property of views with search boxes is replaced by an enum property search_status that supports the states - DEFAULT, FILTERED and EMPTY. This allows tracking whether the results are filtered or not as well, without introducing a separate boolean property for that. Updated tests.
1 parent 28d5f6a commit 04a83f8

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

tests/ui/test_ui_tools.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from zulipterminal.config.keys import keys_for_command, primary_key_for_command
99
from zulipterminal.config.symbols import STATUS_ACTIVE
10-
from zulipterminal.helper import powerset
10+
from zulipterminal.helper import SearchStatus, powerset
1111
from zulipterminal.ui_tools.views import (
1212
SIDE_PANELS_MOUSE_SCROLL_LINES,
1313
LeftColumnView,
@@ -565,6 +565,7 @@ def test_keypress_GO_BACK(self, mocker, stream_view, key, widget_size):
565565
mocker.patch.object(stream_view, "set_focus")
566566
mocker.patch(VIEWS + ".urwid.Frame.keypress")
567567
mocker.patch.object(stream_view.stream_search_box, "reset_search_text")
568+
stream_view.search_status = SearchStatus.FILTERED
568569
stream_view.streams_btn_list = ["FOO", "foo", "fan", "boo", "BOO"]
569570
stream_view.focus_index_before_search = 3
570571

@@ -731,6 +732,7 @@ def test_keypress_GO_BACK(self, mocker, topic_view, key, widget_size):
731732
mocker.patch(VIEWS + ".TopicsView.set_focus")
732733
mocker.patch(VIEWS + ".urwid.Frame.keypress")
733734
mocker.patch.object(topic_view.topic_search_box, "reset_search_text")
735+
topic_view.search_status = SearchStatus.FILTERED
734736
topic_view.topics_btn_list = ["FOO", "foo", "fan", "boo", "BOO"]
735737
topic_view.focus_index_before_search = 3
736738

@@ -1112,6 +1114,7 @@ def test_keypress_GO_BACK(self, right_col_view, mocker, key, widget_size):
11121114
mocker.patch(VIEWS + ".RightColumnView.set_focus")
11131115
mocker.patch(VIEWS + ".RightColumnView.set_body")
11141116
mocker.patch.object(right_col_view.user_search, "reset_search_text")
1117+
right_col_view.search_status = SearchStatus.FILTERED
11151118
right_col_view.users_btn_list = []
11161119

11171120
right_col_view.keypress(size, key)

tests/ui_tools/test_boxes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
STREAM_MARKER_WEB_PUBLIC,
2525
)
2626
from zulipterminal.config.ui_mappings import StreamAccessType
27-
from zulipterminal.helper import Index, MinimalUserData
27+
from zulipterminal.helper import Index, MinimalUserData, SearchStatus
2828
from zulipterminal.ui_tools.boxes import PanelSearchBox, WriteBox, _MessageEditState
2929
from zulipterminal.urwid_types import urwid_Size
3030

@@ -1831,8 +1831,8 @@ def test_keypress_ENTER(
18311831
size = widget_size(panel_search_box)
18321832
panel_search_box.panel_view.view.controller.is_in_editor_mode = lambda: True
18331833
panel_search_box.panel_view.log = log
1834-
empty_search = not log
1835-
panel_search_box.panel_view.empty_search = empty_search
1834+
search_status = SearchStatus.FILTERED if log else SearchStatus.EMPTY
1835+
panel_search_box.panel_view.search_status = search_status
18361836
panel_search_box.set_caption("")
18371837
panel_search_box.edit_text = "key words"
18381838

zulipterminal/helper.py

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
from collections import defaultdict
99
from contextlib import contextmanager
10+
from enum import Enum
1011
from functools import partial, wraps
1112
from itertools import chain, combinations
1213
from re import ASCII, MULTILINE, findall, match
@@ -49,6 +50,12 @@
4950
StreamAccessType = Literal["public", "private", "web-public"]
5051

5152

53+
class SearchStatus(Enum):
54+
DEFAULT = 0
55+
FILTERED = 1
56+
EMPTY = 2
57+
58+
5259
class StreamData(TypedDict):
5360
name: str
5461
id: int

zulipterminal/ui_tools/boxes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
)
3737
from zulipterminal.config.ui_mappings import STREAM_ACCESS_TYPE
3838
from zulipterminal.helper import (
39+
SearchStatus,
3940
asynch,
4041
format_string,
4142
match_emoji,
@@ -1011,7 +1012,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
10111012
# Don't call 'Esc' when inside a popup search-box.
10121013
if not self.panel_view.view.controller.is_any_popup_open():
10131014
self.panel_view.keypress(size, primary_key_for_command("GO_BACK"))
1014-
elif is_command_key("EXECUTE_SEARCH", key) and not self.panel_view.empty_search:
1015+
elif (
1016+
is_command_key("EXECUTE_SEARCH", key)
1017+
and self.panel_view.search_status != SearchStatus.EMPTY
1018+
):
10151019
self.panel_view.view.controller.exit_editor_mode()
10161020
self.set_caption([("filter_results", " Search Results "), " "])
10171021
self.panel_view.set_focus("body")

zulipterminal/ui_tools/views.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
)
3737
from zulipterminal.config.ui_sizes import LEFT_WIDTH
3838
from zulipterminal.helper import (
39+
SearchStatus,
3940
TidiedUserInfo,
4041
asynch,
4142
match_emoji,
@@ -335,7 +336,7 @@ def __init__(self, streams_btn_list: List[Any], view: Any) -> None:
335336
),
336337
)
337338
self.search_lock = threading.Lock()
338-
self.empty_search = False
339+
self.search_status = SearchStatus.DEFAULT
339340

340341
@asynch
341342
def update_streams(self, search_box: Any, new_text: str) -> None:
@@ -352,7 +353,11 @@ def update_streams(self, search_box: Any, new_text: str) -> None:
352353
)[0]
353354

354355
streams_display_num = len(streams_display)
355-
self.empty_search = streams_display_num == 0
356+
self.search_status = (
357+
SearchStatus.EMPTY
358+
if streams_display_num == 0
359+
else SearchStatus.FILTERED
360+
)
356361

357362
# Add a divider to separate pinned streams from the rest.
358363
pinned_stream_names = [
@@ -371,7 +376,7 @@ def update_streams(self, search_box: Any, new_text: str) -> None:
371376
streams_display.insert(first_unpinned_index, StreamsViewDivider())
372377

373378
self.log.clear()
374-
if not self.empty_search:
379+
if self.search_status == SearchStatus.FILTERED:
375380
self.log.extend(streams_display)
376381
else:
377382
self.log.extend([self.stream_search_box.search_error])
@@ -404,6 +409,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
404409
self.log.extend(self.streams_btn_list)
405410
self.set_focus("body")
406411
self.log.set_focus(self.focus_index_before_search)
412+
self.search_status = SearchStatus.DEFAULT
407413
self.view.controller.update_screen()
408414
return key
409415
return super().keypress(size, key)
@@ -436,7 +442,7 @@ def __init__(
436442
header=self.header_list,
437443
)
438444
self.search_lock = threading.Lock()
439-
self.empty_search = False
445+
self.search_status = SearchStatus.DEFAULT
440446

441447
def _focus_position_for_topic_name(self) -> int:
442448
saved_topic_state = self.view.saved_topic_in_stream_id(
@@ -461,10 +467,14 @@ def update_topics(self, search_box: Any, new_text: str) -> None:
461467
for topic in self.topics_btn_list.copy()
462468
if new_text in topic.topic_name.lower()
463469
]
464-
self.empty_search = len(topics_to_display) == 0
470+
self.search_status = (
471+
SearchStatus.EMPTY
472+
if len(topics_to_display) == 0
473+
else SearchStatus.FILTERED
474+
)
465475

466476
self.log.clear()
467-
if not self.empty_search:
477+
if self.search_status == SearchStatus.FILTERED:
468478
self.log.extend(topics_to_display)
469479
else:
470480
self.log.extend([self.topic_search_box.search_error])
@@ -524,6 +534,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
524534
self.log.extend(self.topics_btn_list)
525535
self.set_focus("body")
526536
self.log.set_focus(self.focus_index_before_search)
537+
self.search_status = SearchStatus.DEFAULT
527538
self.view.controller.update_screen()
528539
return key
529540
return super().keypress(size, key)
@@ -665,7 +676,7 @@ def __init__(self, view: Any) -> None:
665676

666677
self.allow_update_user_list = True
667678
self.search_lock = threading.Lock()
668-
self.empty_search = False
679+
self.search_status = SearchStatus.DEFAULT
669680
super().__init__(self.users_view(), header=search_box)
670681

671682
@asynch
@@ -706,10 +717,12 @@ def update_user_list(
706717
else:
707718
users_display = users
708719

709-
self.empty_search = len(users_display) == 0
720+
self.search_status = (
721+
SearchStatus.EMPTY if len(users_display) == 0 else SearchStatus.FILTERED
722+
)
710723

711724
# FIXME Update log directly?
712-
if not self.empty_search:
725+
if self.search_status != SearchStatus.EMPTY:
713726
self.body = self.users_view(users_display)
714727
else:
715728
self.body = UsersView(
@@ -765,6 +778,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
765778
self.body = UsersView(self.view.controller, self.users_btn_list)
766779
self.set_body(self.body)
767780
self.set_focus("body")
781+
self.search_status = SearchStatus.DEFAULT
768782
self.view.controller.update_screen()
769783
return key
770784
elif is_command_key("GO_LEFT", key):
@@ -2029,7 +2043,7 @@ def __init__(
20292043
search_box = urwid.Pile(
20302044
[self.emoji_search, urwid.Divider(SECTION_DIVIDER_LINE)]
20312045
)
2032-
self.empty_search = False
2046+
self.search_status = SearchStatus.DEFAULT
20332047
self.search_lock = threading.Lock()
20342048
super().__init__(
20352049
controller,
@@ -2075,10 +2089,14 @@ def update_emoji_list(
20752089
else:
20762090
self.emojis_display = self.emoji_buttons
20772091

2078-
self.empty_search = len(self.emojis_display) == 0
2092+
self.search_status = (
2093+
SearchStatus.EMPTY
2094+
if len(self.emojis_display) == 0
2095+
else SearchStatus.FILTERED
2096+
)
20792097

20802098
body_content = self.emojis_display
2081-
if self.empty_search:
2099+
if self.search_status == SearchStatus.EMPTY:
20822100
body_content = [self.emoji_search.search_error]
20832101

20842102
self.contents["body"] = (
@@ -2152,5 +2170,6 @@ def keypress(self, size: urwid_Size, key: str) -> str:
21522170
self.emoji_search.reset_search_text()
21532171
self.controller.exit_editor_mode()
21542172
self.controller.exit_popup()
2173+
self.search_status = SearchStatus.DEFAULT
21552174
return key
21562175
return super().keypress(size, key)

0 commit comments

Comments
 (0)