Skip to content

Commit b1012d9

Browse files
committed
bugfix: view: Fix Zulip crash during search.
This commit fixes a bug which causes Zulip to crash when searching Streams or Topics. This happens because when a search result is empty, log is empty and because an empty ListWalker returns None, it leads to a Type error when setting focus. This is fixed by adding a boolean `in_search_mode` which does not call `get_focus` untill the search is over. Slight reordering was done in `__init__` to fit the boolean in an ideal place w.r.t. readability. Incidentally, the same commit also corrects what the code for `focus_index_before_search` was intended to do. Fixes zulip#923 and Fixes zulip#975
1 parent f9d0982 commit b1012d9

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

zulipterminal/ui_tools/views.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,17 @@ def __init__(self) -> None:
293293
class StreamsView(urwid.Frame):
294294
def __init__(self, streams_btn_list: List[Any], view: Any) -> None:
295295
self.view = view
296-
self.log = urwid.SimpleFocusListWalker(streams_btn_list)
296+
# Create Stream List Box
297297
self.streams_btn_list = streams_btn_list
298-
self.focus_index_before_search = 0
298+
self.log = urwid.SimpleFocusListWalker(streams_btn_list)
299299
list_box = urwid.ListBox(self.log)
300+
# Create Stream Search Box
300301
self.stream_search_box = PanelSearchBox(self,
301302
'SEARCH_STREAMS',
302303
self.update_streams)
304+
self.focus_index_before_search = 0
305+
self.in_search_mode = False
306+
# Create Stream View Frame
303307
super().__init__(list_box, header=urwid.LineBox(
304308
self.stream_search_box, tlcorner='─', tline='', lline='',
305309
trcorner='─', blcorner='─', rline='',
@@ -356,6 +360,7 @@ def mouse_event(self, size: urwid_Size, event: str, button: int, col: int,
356360

357361
def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
358362
if is_command_key('SEARCH_STREAMS', key):
363+
self.in_search_mode = True
359364
self.set_focus('header')
360365
return key
361366
elif is_command_key('GO_BACK', key):
@@ -365,27 +370,33 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
365370
self.set_focus('body')
366371
self.log.set_focus(self.focus_index_before_search)
367372
self.view.controller.update_screen()
373+
self.in_search_mode = False
368374
return key
369375
return_value = super().keypress(size, key)
370-
_, self.focus_index_before_search = self.log.get_focus()
376+
if not self.in_search_mode:
377+
_, self.focus_index_before_search = self.log.get_focus()
371378
return return_value
372379

373380

374381
class TopicsView(urwid.Frame):
375382
def __init__(self, topics_btn_list: List[Any], view: Any,
376383
stream_button: Any) -> None:
377384
self.view = view
378-
self.log = urwid.SimpleFocusListWalker(topics_btn_list)
379-
self.topics_btn_list = topics_btn_list
380385
self.stream_button = stream_button
381-
self.focus_index_before_search = 0
386+
# Create Topic List Box
387+
self.topics_btn_list = topics_btn_list
388+
self.log = urwid.SimpleFocusListWalker(topics_btn_list)
382389
self.list_box = urwid.ListBox(self.log)
390+
# Create Topic Search Box
383391
self.topic_search_box = PanelSearchBox(self,
384392
'SEARCH_TOPICS',
385393
self.update_topics)
394+
self.focus_index_before_search = 0
395+
self.in_search_mode = False
386396
self.header_list = urwid.Pile([self.stream_button,
387397
urwid.Divider('─'),
388398
self.topic_search_box])
399+
# Create Topic View Frame
389400
super().__init__(self.list_box, header=urwid.LineBox(
390401
self.header_list, tlcorner='─', tline='', lline='',
391402
trcorner='─', blcorner='─', rline='',
@@ -452,6 +463,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
452463
self.view.show_left_panel(visible=False)
453464
self.view.body.focus_col = 1
454465
if is_command_key('SEARCH_TOPICS', key):
466+
self.in_search_mode = True
455467
self.set_focus('header')
456468
self.header_list.set_focus(2)
457469
return key
@@ -462,9 +474,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
462474
self.set_focus('body')
463475
self.log.set_focus(self.focus_index_before_search)
464476
self.view.controller.update_screen()
477+
self.in_search_mode = False
465478
return key
466479
return_value = super().keypress(size, key)
467-
_, self.focus_index_before_search = self.log.get_focus()
480+
if not self.in_search_mode:
481+
_, self.focus_index_before_search = self.log.get_focus()
468482
return return_value
469483

470484

0 commit comments

Comments
 (0)