Skip to content

Commit 0bf31b7

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 5418195 commit 0bf31b7

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

tests/ui/test_ui_tools.py

Lines changed: 4 additions & 1 deletion
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_CLEAR_SEARCH(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_CLEAR_SEARCH(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_CLEAR_SEARCH(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

Lines changed: 3 additions & 3 deletions
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 (
2929
MAX_MESSAGE_LENGTH_CONFIRMATION_POPUP,
3030
PanelSearchBox,
@@ -1903,8 +1903,8 @@ def test_keypress_ENTER(
19031903
size = widget_size(panel_search_box)
19041904
panel_search_box.panel_view.view.controller.is_in_editor_mode = lambda: True
19051905
panel_search_box.panel_view.log = log
1906-
empty_search = not log
1907-
panel_search_box.panel_view.empty_search = empty_search
1906+
search_status = SearchStatus.FILTERED if log else SearchStatus.EMPTY
1907+
panel_search_box.panel_view.search_status = search_status
19081908
panel_search_box.set_caption("")
19091909
panel_search_box.edit_text = "key words"
19101910

zulipterminal/helper.py

Lines changed: 7 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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,
@@ -1043,7 +1044,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
10431044
# Don't call 'Esc' when inside a popup search-box.
10441045
if not self.panel_view.view.controller.is_any_popup_open():
10451046
self.panel_view.keypress(size, primary_key_for_command("CLEAR_SEARCH"))
1046-
elif is_command_key("EXECUTE_SEARCH", key) and not self.panel_view.empty_search:
1047+
elif (
1048+
is_command_key("EXECUTE_SEARCH", key)
1049+
and self.panel_view.search_status != SearchStatus.EMPTY
1050+
):
10471051
self.panel_view.view.controller.exit_editor_mode()
10481052
self.set_caption([("filter_results", " Search Results "), " "])
10491053
self.panel_view.set_focus("body")

zulipterminal/ui_tools/views.py

Lines changed: 31 additions & 12 deletions
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):
@@ -2027,7 +2041,7 @@ def __init__(
20272041
search_box = urwid.Pile(
20282042
[self.emoji_search, urwid.Divider(SECTION_DIVIDER_LINE)]
20292043
)
2030-
self.empty_search = False
2044+
self.search_status = SearchStatus.DEFAULT
20312045
self.search_lock = threading.Lock()
20322046
super().__init__(
20332047
controller,
@@ -2073,10 +2087,14 @@ def update_emoji_list(
20732087
else:
20742088
self.emojis_display = self.emoji_buttons
20752089

2076-
self.empty_search = len(self.emojis_display) == 0
2090+
self.search_status = (
2091+
SearchStatus.EMPTY
2092+
if len(self.emojis_display) == 0
2093+
else SearchStatus.FILTERED
2094+
)
20772095

20782096
body_content = self.emojis_display
2079-
if self.empty_search:
2097+
if self.search_status == SearchStatus.EMPTY:
20802098
body_content = [self.emoji_search.search_error]
20812099

20822100
self.contents["body"] = (
@@ -2150,5 +2168,6 @@ def keypress(self, size: urwid_Size, key: str) -> str:
21502168
self.emoji_search.reset_search_text()
21512169
self.controller.exit_editor_mode()
21522170
self.controller.exit_popup()
2171+
self.search_status = SearchStatus.DEFAULT
21532172
return key
21542173
return super().keypress(size, key)

0 commit comments

Comments
 (0)