Skip to content

[WIP] Refactor Narrow structure #1375

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ def index_messages(messages: List[Message], model: Any, index: Index) -> Index:
},
}
"""
narrow = model.narrow
narrow = model.get_narrow()
narrow_length = model.get_narrow_length()
for msg in messages:
if "edit_history" in msg:
index["edited_messages"].add(msg["id"])
Expand All @@ -409,7 +410,7 @@ def index_messages(messages: List[Message], model: Any, index: Index) -> Index:
index["search"].add(msg["id"])
continue

if len(narrow) == 1:
if narrow_length == 1:
if narrow[0][1] == "starred" and "starred" in msg["flags"]:
index["starred_msg_ids"].add(msg["id"])

Expand All @@ -436,7 +437,7 @@ def index_messages(messages: List[Message], model: Any, index: Index) -> Index:

if (
msg["type"] == "stream"
and len(narrow) == 2
and narrow_length == 2
and narrow[1][1] == msg["subject"]
):
topics_in_stream = index["topic_msg_ids"][msg["stream_id"]]
Expand Down Expand Up @@ -648,7 +649,7 @@ def display_error_if_present(response: Dict[str, Any], controller: Any) -> None:
def check_narrow_and_notify(
outer_narrow: List[Any], inner_narrow: List[Any], controller: Any
) -> None:
current_narrow = controller.model.narrow
current_narrow = controller.model.get_narrow()

if (
current_narrow != []
Expand Down
6 changes: 6 additions & 0 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ def unset_search_narrow(self) -> None:
if self.is_search_narrow():
self.narrow = [item for item in self.narrow if item[0] != "search"]

def get_narrow(self) -> List[Any]:
return self.narrow

def get_narrow_length(self) -> int:
return len(self.narrow)

def get_message_ids_in_current_narrow(self) -> Set[int]:
narrow = self.narrow
index = self.index
Expand Down
16 changes: 10 additions & 6 deletions zulipterminal/ui_tools/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:

def need_recipient_header(self) -> bool:
# Prevent redundant information in recipient bar
if len(self.model.narrow) == 1 and self.model.narrow[0][0] == "pm_with":
narrow = self.model.get_narrow()
narrow_length = self.model.get_narrow_length()
if narrow_length == 1 and narrow[0][0] == "pm_with":
return False
if len(self.model.narrow) == 2 and self.model.narrow[1][0] == "topic":
if narrow_length == 2 and narrow[1][0] == "topic":
return False

last_msg = self.last_message
Expand Down Expand Up @@ -193,7 +195,7 @@ def top_header_bar(self, message_view: Any) -> Any:
return message_view.private_header()

def top_search_bar(self) -> Any:
curr_narrow = self.model.narrow
curr_narrow = self.model.get_narrow()
is_search_narrow = self.model.is_search_narrow()
if is_search_narrow:
curr_narrow = [
Expand Down Expand Up @@ -890,6 +892,8 @@ def mouse_event(
return super().mouse_event(size, event, button, col, row, focus)

def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
narrow = self.model.get_narrow()
narrow_length = self.model.get_narrow_length()
if is_command_key("REPLY_MESSAGE", key):
if self.message["type"] == "private":
self.model.controller.view.write_box.private_box_view(
Expand All @@ -902,7 +906,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
stream_id=self.stream_id,
)
elif is_command_key("STREAM_MESSAGE", key):
if len(self.model.narrow) != 0 and self.model.narrow[0][0] == "stream":
if narrow_length != 0 and narrow[0][0] == "stream":
self.model.controller.view.write_box.stream_box_view(
caption=self.message["display_recipient"],
stream_id=self.stream_id,
Expand All @@ -923,7 +927,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
elif is_command_key("TOGGLE_NARROW", key):
self.model.unset_search_narrow()
if self.message["type"] == "private":
if len(self.model.narrow) == 1 and self.model.narrow[0][0] == "pm_with":
if narrow_length == 1 and narrow[0][0] == "pm_with":
self.model.controller.narrow_to_all_pm(
contextual_message_id=self.message["id"],
)
Expand All @@ -933,7 +937,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
contextual_message_id=self.message["id"],
)
elif self.message["type"] == "stream":
if len(self.model.narrow) > 1: # in a topic
if narrow_length > 1: # in a topic
self.model.controller.narrow_to_stream(
stream_name=self.stream_name,
contextual_message_id=self.message["id"],
Expand Down
4 changes: 2 additions & 2 deletions zulipterminal/ui_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_msg_box_list(
"""
MessageBox for every message displayed is created here.
"""
if not model.narrow and messages is None:
if not model.get_narrow() and messages is None:
messages = list(model.index["all_msg_ids"])
if messages is not None:
message_list = [model.index["messages"][id] for id in messages]
Expand Down Expand Up @@ -62,7 +62,7 @@ def is_muted(msg: Message, model: Any) -> bool:
if msg["type"] == "private": # noqa: SIM114
return False
# In a topic narrow
elif len(model.narrow) == 2:
elif model.get_narrow_length() == 2:
return False
elif model.is_muted_stream(msg["stream_id"]): # noqa: SIM114
return True
Expand Down