jump to reply (#105)

* Draft: adding lock to message update and using indexes for msgs

* Fix black formatting

* Rename msg_ids->msg_idx to better represent what variable contains

* Fix black formatting

* Check if msg_id in not_found

* Use lock in udpate_handler

* Add shortcut to jump to replied msg

* Show correct error msg when trying to jump to reply msg which does not replies

* Remove lock
This commit is contained in:
Nameless 2020-07-03 10:17:30 +08:00 committed by GitHub
parent eabe39d3c4
commit 2a0c726792
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View file

@ -54,6 +54,9 @@ def bind(
return fun(self)
for key in keys:
assert (
key not in binding
), f"Key {key} already binded to {binding[key]}"
binding[key] = fun if repeat_factor else _no_repeat_factor
return wrapper
@ -129,6 +132,20 @@ class Controller:
def back(self) -> str:
return "BACK"
@bind(msg_handler, ["m"])
def jump_to_reply_msg(self) -> None:
chat_id = self.model.chats.id_by_index(self.model.current_chat)
if not chat_id:
return
msg = MsgProxy(self.model.current_msg)
if not msg.reply_msg_id:
return self.present_error("This msg does not reply")
if not self.model.msgs.jump_to_msg_by_id(chat_id, msg.reply_msg_id):
return self.present_error(
"Can't jump to reply msg: it's not preloaded or deleted"
)
return self.render_msgs()
@bind(msg_handler, ["p"])
def forward_msgs(self) -> None:
"""Paste yanked msgs"""

View file

@ -292,6 +292,12 @@ class MsgModel:
self.not_found: Set[int] = set()
self.msg_idx: Dict[int, Dict[int, int]] = defaultdict(dict)
def jump_to_msg_by_id(self, chat_id: int, msg_id: int) -> bool:
if index := self.msg_idx[chat_id].get(msg_id):
self.current_msgs[chat_id] = index
return True
return False
def next_msg(self, chat_id: int, step: int = 1) -> bool:
current_msg = self.current_msgs[chat_id]
if current_msg == 0: